use custom command for fast_tmx rendering

This commit is contained in:
Huabing.Xu 2014-06-27 10:30:21 +08:00
parent 81aa9c2c74
commit a04b5fc5d5
2 changed files with 58 additions and 32 deletions

View File

@ -109,19 +109,20 @@ bool FastTMXLayer::initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo
} }
FastTMXLayer::FastTMXLayer() FastTMXLayer::FastTMXLayer()
:_layerName("") : _layerName("")
,_layerSize(Size::ZERO) , _layerSize(Size::ZERO)
,_mapTileSize(Size::ZERO) , _mapTileSize(Size::ZERO)
,_tiles(nullptr) , _tiles(nullptr)
,_tileSet(nullptr) , _tileSet(nullptr)
,_layerOrientation(FastTMXOrientationOrtho) , _layerOrientation(FastTMXOrientationOrtho)
,_texture(nullptr) ,_texture(nullptr)
,_verticesToDraw(0) , _verticesToDraw(0)
,_vertexZvalue(0) , _vertexZvalue(0)
,_useAutomaticVertexZ(false) , _useAutomaticVertexZ(false)
,_dirty(true) , _dirty(true)
, _quadsDirty(true) , _quadsDirty(true)
{ {
_buffersVBO[0] = _buffersVBO[1] = 0;
} }
FastTMXLayer::~FastTMXLayer() FastTMXLayer::~FastTMXLayer()
@ -129,6 +130,15 @@ FastTMXLayer::~FastTMXLayer()
CC_SAFE_RELEASE(_tileSet); CC_SAFE_RELEASE(_tileSet);
CC_SAFE_RELEASE(_texture); CC_SAFE_RELEASE(_texture);
CC_SAFE_DELETE_ARRAY(_tiles); CC_SAFE_DELETE_ARRAY(_tiles);
if(glIsBuffer(_buffersVBO[0]))
{
glDeleteBuffers(1, &_buffersVBO[0]);
}
if(glIsBuffer(_buffersVBO[1]))
{
glDeleteBuffers(1, &_buffersVBO[1]);
}
} }
bool sortQuadCommand(const V3F_C4B_T2F_Quad& a, const V3F_C4B_T2F_Quad& b) bool sortQuadCommand(const V3F_C4B_T2F_Quad& a, const V3F_C4B_T2F_Quad& b)
@ -166,7 +176,6 @@ void FastTMXLayer::draw(Renderer *renderer, const Mat4& transform, uint32_t flag
auto& cmd = _renderCommands[index++]; auto& cmd = _renderCommands[index++];
cmd.init(iter.first); cmd.init(iter.first);
printf("in draw function of FastTMXLayer %d\n", iter.second.size());
cmd.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, &iter.second); cmd.func = CC_CALLBACK_0(FastTMXLayer::onDraw, this, &iter.second);
renderer->addCommand(&cmd); renderer->addCommand(&cmd);
} }
@ -179,7 +188,7 @@ void FastTMXLayer::onDraw(const std::vector<int> *indices)
getGLProgramState()->apply(_modelViewTransform); getGLProgramState()->apply(_modelViewTransform);
glBindVertexArray(0); glBindVertexArray(0);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION); glEnableVertexAttribArray(GLProgram::VERTEX_ATTRIB_POSITION);
@ -188,7 +197,6 @@ void FastTMXLayer::onDraw(const std::vector<int> *indices)
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_totalQuads[0]); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), &_totalQuads[0]);
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, colors)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, colors));
glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, texCoords)); glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), ((char*)&_totalQuads[0]) + offsetof(V3F_C4B_T2F, texCoords));
printf("in onDraw function of FastTMXLayer %d\n", indices->size());
glDrawElements(GL_TRIANGLES, (GLsizei)indices->size(), GL_UNSIGNED_INT, &(*indices)[0]); glDrawElements(GL_TRIANGLES, (GLsizei)indices->size(), GL_UNSIGNED_INT, &(*indices)[0]);
} }
@ -283,26 +291,43 @@ int FastTMXLayer::updateTiles(const Rect& culledRect)
return tilesUsed * 6; return tilesUsed * 6;
} }
void FastTMXLayer::setupVBO() void FastTMXLayer::updateVertexBuffer()
{ {
glGenBuffers(2, &_buffersVBO[0]); glBindVertexArray(0);
if(!glIsBuffer(_buffersVBO[0]))
// 10922 = 65536/6 {
int total = std::min(static_cast<int>(_layerSize.width * _layerSize.height), MAX_QUADS_COUNT); glGenBuffers(1, &_buffersVBO[0]);
}
// Vertex + Tex Coords
glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]); glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
glBufferData(GL_ARRAY_BUFFER, total * sizeof(V3F_T2F_Quad), NULL, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(V3F_C4B_T2F_Quad) * _quads.size(), (GLvoid*)&_quads[0], GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
// Indices
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, total * 6 * sizeof(GLushort), NULL, GL_DYNAMIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
CHECK_GL_ERROR_DEBUG();
} }
//void FastTMXLayer::updateIndexBuffer()
//{
//}
//void FastTMXLayer::setupVBO()
//{
// glGenBuffers(2, &_buffersVBO[0]);
//
// // 10922 = 65536/6
// int total = std::min(static_cast<int>(_layerSize.width * _layerSize.height), MAX_QUADS_COUNT);
//
// // Vertex + Tex Coords
// glBindBuffer(GL_ARRAY_BUFFER, _buffersVBO[0]);
// glBufferData(GL_ARRAY_BUFFER, total * sizeof(V3F_T2F_Quad), NULL, GL_DYNAMIC_DRAW);
// glBindBuffer(GL_ARRAY_BUFFER, 0);
//
// // Indices
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _buffersVBO[1]);
// glBufferData(GL_ELEMENT_ARRAY_BUFFER, total * 6 * sizeof(GLushort), NULL, GL_DYNAMIC_DRAW);
// glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
//
// CHECK_GL_ERROR_DEBUG();
//}
// FastTMXLayer - setup Tiles // FastTMXLayer - setup Tiles
void FastTMXLayer::setupTiles() void FastTMXLayer::setupTiles()
{ {
@ -340,7 +365,6 @@ void FastTMXLayer::setupTiles()
_screenTileCount = _screenGridSize.width * _screenGridSize.height; _screenTileCount = _screenGridSize.width * _screenGridSize.height;
setupVBO();
} }
Mat4 FastTMXLayer::tileToNodeTransform() Mat4 FastTMXLayer::tileToNodeTransform()
@ -497,7 +521,7 @@ void FastTMXLayer::updateTotalQuads()
} }
} }
//TODO: update VBOs updateVertexBuffer();
_quadsDirty = false; _quadsDirty = false;
} }

View File

@ -189,7 +189,6 @@ protected:
bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
int updateTiles(const Rect& culledRect); int updateTiles(const Rect& culledRect);
void setupVBO();
Vec2 calculateLayerOffset(const Vec2& offset); Vec2 calculateLayerOffset(const Vec2& offset);
/* The layer recognizes some special properties, like cc_vertez */ /* The layer recognizes some special properties, like cc_vertez */
@ -209,6 +208,9 @@ protected:
void onDraw(const std::vector<int>* indices); void onDraw(const std::vector<int>* indices);
inline int getTileIndexByPos(int x, int y) const { return x + y * (int) _layerSize.width; } inline int getTileIndexByPos(int x, int y) const { return x + y * (int) _layerSize.width; }
void updateVertexBuffer();
//void updateIndexBuffer();
protected: protected:
//! name of the layer //! name of the layer
@ -232,7 +234,7 @@ protected:
/** container for sprite children. map<index, pair<sprite, gid> > */ /** container for sprite children. map<index, pair<sprite, gid> > */
std::map<int, std::pair<Sprite*, int> > _spriteContainer; std::map<int, std::pair<Sprite*, int> > _spriteContainer;
GLuint _buffersVBO[3]; //0: vertex, 1: tex coords, 2: indices GLuint _buffersVBO[2]; //0: vertex, 1: indices
Size _screenGridSize; Size _screenGridSize;
Rect _screenGridRect; Rect _screenGridRect;