refactor fast tmx rendering

This commit is contained in:
Huabing.Xu 2014-06-19 18:05:21 +08:00
parent d89c2176c3
commit e03c412e71
2 changed files with 47 additions and 73 deletions

View File

@ -117,9 +117,6 @@ TMXLayer2::TMXLayer2()
,_verticesToDraw(0) ,_verticesToDraw(0)
,_vertexZvalue(0) ,_vertexZvalue(0)
,_useAutomaticVertexZ(false) ,_useAutomaticVertexZ(false)
,_quads(nullptr)
,_indices(nullptr)
,_numQuads(0)
,_dirty(false) ,_dirty(false)
{} {}
@ -128,8 +125,6 @@ TMXLayer2::~TMXLayer2()
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);
CC_SAFE_FREE(_quads);
CC_SAFE_FREE(_indices);
} }
void TMXLayer2::draw(Renderer *renderer, const Mat4& transform, uint32_t flags) void TMXLayer2::draw(Renderer *renderer, const Mat4& transform, uint32_t flags)
@ -160,23 +155,12 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated)
inv.inverse(); inv.inverse();
rect = RectApplyTransform(rect, inv); rect = RectApplyTransform(rect, inv);
if (Configuration::getInstance()->supportsShareableVAO()) _verticesToDraw = updateTiles(rect);
{
V3F_T2F_Quad* quads = (V3F_T2F_Quad*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
GLushort* indices = (GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
_verticesToDraw = updateTiles(rect, quads, indices);
glUnmapBuffer(GL_ARRAY_BUFFER);
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
}
else
{
_verticesToDraw = updateTiles(rect, nullptr, nullptr);
if (_quads != nullptr && _indices != nullptr && _verticesToDraw > 0) if (_quads.size() > 0 && _indices.size() > 0 && _verticesToDraw > 0)
{ {
glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _numQuads , _quads, GL_DYNAMIC_DRAW); glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _quads.size() , &_quads[0], GL_DYNAMIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _numQuads * 6 , _indices, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _quads.size() * 6 , &_indices[0], GL_STATIC_DRAW);
}
} }
// don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices // don't draw more than 65535 vertices since we are using GL_UNSIGNED_SHORT for indices
@ -208,15 +192,14 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated)
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
} }
int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort *indices) int TMXLayer2::updateTiles(const Rect& culledRect)
{ {
int tilesUsed = 0; int tilesUsed = 0;
Rect visibleTiles = culledRect; Rect visibleTiles = culledRect;
Size mapTileSize = CC_SIZE_PIXELS_TO_POINTS(_mapTileSize); Size mapTileSize = CC_SIZE_PIXELS_TO_POINTS(_mapTileSize);
Size tileSize = CC_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize); Size tileSize = CC_SIZE_PIXELS_TO_POINTS(_tileSet->_tileSize);
Mat4 nodeToTileTransform = _tileToNodeTransform; Mat4 nodeToTileTransform = _tileToNodeTransform.getInversed();
nodeToTileTransform.inverse();
//transform to tile //transform to tile
visibleTiles = RectApplyTransform(visibleTiles, nodeToTileTransform); visibleTiles = RectApplyTransform(visibleTiles, nodeToTileTransform);
// tile coordinate is upside-down, so we need to make the tile coordinate use top-left for the start point. // tile coordinate is upside-down, so we need to make the tile coordinate use top-left for the start point.
@ -228,8 +211,6 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort
visibleTiles.origin.x = floor(visibleTiles.origin.x); visibleTiles.origin.x = floor(visibleTiles.origin.x);
visibleTiles.origin.y = floor(visibleTiles.origin.y); visibleTiles.origin.y = floor(visibleTiles.origin.y);
V3F_T2F_Quad* quadsTmp = quads;
GLushort* indicesTmp = indices;
// for the bigger tiles. // for the bigger tiles.
int tilesOverX = 0; int tilesOverX = 0;
@ -256,20 +237,15 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort
} }
// doesn't support VBO // doesn't support VBO
if (quadsTmp == nullptr)
{
int quadsNeed = std::min(static_cast<int>((visibleTiles.size.width + tilesOverX) * (visibleTiles.size.height + tilesOverY)), MAX_QUADS_COUNT); int quadsNeed = std::min(static_cast<int>((visibleTiles.size.width + tilesOverX) * (visibleTiles.size.height + tilesOverY)), MAX_QUADS_COUNT);
if (_numQuads < quadsNeed) if (_quads.size() < quadsNeed)
{ {
_numQuads = quadsNeed; _quads.resize(quadsNeed);
_quads = (V3F_T2F_Quad*)realloc(_quads, _numQuads * sizeof(V3F_T2F_Quad)); _indices.resize(quadsNeed * 6);
_indices = (GLushort*)realloc(_indices, _numQuads * 6 * sizeof(GLushort));
}
quadsTmp = _quads;
indicesTmp = _indices;
} }
V3F_T2F_Quad* quadsTmp = &_quads[0];
GLushort* indicesTmp = &_indices[0];
Size texSize = _tileSet->_imageSize; Size texSize = _tileSet->_imageSize;
for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y) for (int y = visibleTiles.origin.y - tilesOverY; y < visibleTiles.origin.y + visibleTiles.size.height + tilesOverY; ++y)

View File

@ -187,7 +187,7 @@ public:
protected: protected:
bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo); bool initWithTilesetInfo(TMXTilesetInfo *tilesetInfo, TMXLayerInfo *layerInfo, TMXMapInfo *mapInfo);
int updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort *indices); int updateTiles(const Rect& culledRect);
void setupVBO(); void setupVBO();
Point calculateLayerOffset(const Point& offset); Point calculateLayerOffset(const Point& offset);
@ -242,11 +242,9 @@ protected:
/** tile coordinate to node coordinate transform */ /** tile coordinate to node coordinate transform */
Mat4 _tileToNodeTransform; Mat4 _tileToNodeTransform;
/** quads to be rendered */ /** quads to be rendered */
V3F_T2F_Quad* _quads; std::vector<V3F_T2F_Quad> _quads;
/** number of quads */
int _numQuads;
/** indices */ /** indices */
GLushort* _indices; std::vector<GLushort> _indices;
bool _dirty; bool _dirty;
}; };