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)
@ -154,29 +149,18 @@ void TMXLayer2::onDraw(const Mat4 &transform, bool transformUpdated)
{ {
Size s = Director::getInstance()->getWinSize(); Size s = Director::getInstance()->getWinSize();
auto rect = Rect(0, 0, s.width, s.height); auto rect = Rect(0, 0, s.width, s.height);
Mat4 inv = transform; Mat4 inv = transform;
inv.inverse(); inv.inverse();
rect = RectApplyTransform(rect, inv); rect = RectApplyTransform(rect, inv);
if (Configuration::getInstance()->supportsShareableVAO()) _verticesToDraw = updateTiles(rect);
if (_quads.size() > 0 && _indices.size() > 0 && _verticesToDraw > 0)
{ {
V3F_T2F_Quad* quads = (V3F_T2F_Quad*)glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY); glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _quads.size() , &_quads[0], GL_DYNAMIC_DRAW);
GLushort* indices = (GLushort *)glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _quads.size() * 6 , &_indices[0], GL_STATIC_DRAW);
_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)
{
glBufferData(GL_ARRAY_BUFFER, sizeof(_quads[0]) * _numQuads , _quads, GL_DYNAMIC_DRAW);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(_indices[0]) * _numQuads * 6 , _indices, 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.
@ -227,9 +210,7 @@ int TMXLayer2::updateTiles(const Rect& culledRect, V3F_T2F_Quad *quads, GLushort
visibleTiles.size.height = ceil(visibleTiles.origin.y + visibleTiles.size.height) - floor(visibleTiles.origin.y); visibleTiles.size.height = ceil(visibleTiles.origin.y + visibleTiles.size.height) - floor(visibleTiles.origin.y);
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);
if (_quads.size() < quadsNeed)
{ {
int quadsNeed = std::min(static_cast<int>((visibleTiles.size.width + tilesOverX) * (visibleTiles.size.height + tilesOverY)), MAX_QUADS_COUNT); _quads.resize(quadsNeed);
if (_numQuads < quadsNeed) _indices.resize(quadsNeed * 6);
{
_numQuads = quadsNeed;
_quads = (V3F_T2F_Quad*)realloc(_quads, _numQuads * sizeof(V3F_T2F_Quad));
_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)
@ -456,13 +432,13 @@ void TMXLayer2::setupTiles()
Mat4 TMXLayer2::tileToNodeTransform() Mat4 TMXLayer2::tileToNodeTransform()
{ {
float w = _mapTileSize.width / CC_CONTENT_SCALE_FACTOR(); float w = _mapTileSize.width / CC_CONTENT_SCALE_FACTOR();
float h = _mapTileSize.height / CC_CONTENT_SCALE_FACTOR(); float h = _mapTileSize.height / CC_CONTENT_SCALE_FACTOR();
float offY = (_layerSize.height - 1) * h; float offY = (_layerSize.height - 1) * h;
switch(_layerOrientation) switch(_layerOrientation)
{ {
case TMXOrientationOrtho2: case TMXOrientationOrtho2:
{ {
_tileToNodeTransform = Mat4 _tileToNodeTransform = Mat4
( (
@ -474,7 +450,7 @@ Mat4 TMXLayer2::tileToNodeTransform()
return _tileToNodeTransform; return _tileToNodeTransform;
} }
case TMXOrientationIso2: case TMXOrientationIso2:
{ {
float offX = (_layerSize.width - 1) * w / 2; float offX = (_layerSize.width - 1) * w / 2;
_tileToNodeTransform = Mat4 _tileToNodeTransform = Mat4
@ -484,8 +460,8 @@ Mat4 TMXLayer2::tileToNodeTransform()
0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f,
0.0f, 0.0f, 0.0f, 1.0f 0.0f, 0.0f, 0.0f, 1.0f
); );
return _tileToNodeTransform; return _tileToNodeTransform;
} }
case TMXOrientationHex2: case TMXOrientationHex2:
{ {
_tileToNodeTransform = Mat4::IDENTITY; _tileToNodeTransform = Mat4::IDENTITY;
@ -496,22 +472,22 @@ Mat4 TMXLayer2::tileToNodeTransform()
_tileToNodeTransform = Mat4::IDENTITY; _tileToNodeTransform = Mat4::IDENTITY;
return _tileToNodeTransform; return _tileToNodeTransform;
} }
} }
} }
// removing / getting tiles // removing / getting tiles
Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate) Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate)
{ {
CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position"); CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position");
CCASSERT( _tiles, "TMXLayer: the tiles map has been released"); CCASSERT( _tiles, "TMXLayer: the tiles map has been released");
Sprite *tile = nullptr; Sprite *tile = nullptr;
int gid = this->getTileGIDAt(tileCoordinate); int gid = this->getTileGIDAt(tileCoordinate);
// if GID == 0, then no tile is present // if GID == 0, then no tile is present
if( gid ) { if( gid ) {
int index = tileCoordinate.x + tileCoordinate.y * _layerSize.width; int index = tileCoordinate.x + tileCoordinate.y * _layerSize.width;
auto it = _spriteContainer.find(index); auto it = _spriteContainer.find(index);
if (it != _spriteContainer.end()) if (it != _spriteContainer.end())
@ -521,7 +497,7 @@ Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate)
else else
{ {
// tile not created yet. create it // tile not created yet. create it
Rect rect = _tileSet->getRectForGID(gid); Rect rect = _tileSet->getRectForGID(gid);
rect = CC_RECT_PIXELS_TO_POINTS(rect); rect = CC_RECT_PIXELS_TO_POINTS(rect);
tile = Sprite::createWithTexture(_texture, rect); tile = Sprite::createWithTexture(_texture, rect);
@ -537,8 +513,8 @@ Sprite* TMXLayer2::getTileAt(const Point& tileCoordinate)
// tile is converted to sprite. // tile is converted to sprite.
setTileForGID(index, 0); setTileForGID(index, 0);
} }
} }
return tile; return tile;
} }
int TMXLayer2::getTileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags/* = nullptr*/) int TMXLayer2::getTileGIDAt(const Point& tileCoordinate, TMXTileFlags* flags/* = nullptr*/)
@ -606,24 +582,24 @@ int TMXLayer2::getVertexZForPos(const Point& pos)
void TMXLayer2::removeTileAt(const Point& tileCoordinate) void TMXLayer2::removeTileAt(const Point& tileCoordinate)
{ {
CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position"); CCASSERT( tileCoordinate.x < _layerSize.width && tileCoordinate.y < _layerSize.height && tileCoordinate.x >=0 && tileCoordinate.y >=0, "TMXLayer: invalid position");
int gid = this->getTileGIDAt(tileCoordinate); int gid = this->getTileGIDAt(tileCoordinate);
if( gid ) { if( gid ) {
int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width; int z = tileCoordinate.x + tileCoordinate.y * _layerSize.width;
// remove tile from GID map // remove tile from GID map
setTileForGID(z, 0); setTileForGID(z, 0);
// remove it from sprites // remove it from sprites
auto it = _spriteContainer.find(z); auto it = _spriteContainer.find(z);
if (it != _spriteContainer.end()) if (it != _spriteContainer.end())
{ {
this->removeChild(it->second.first); this->removeChild(it->second.first);
} }
} }
} }
void TMXLayer2::setTileForGID(int index, int gid) void TMXLayer2::setTileForGID(int index, int gid)

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;
}; };