From bbf11f5d06d1898110cf0b156247f25c7746d2df Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Mon, 4 Aug 2014 16:08:54 +0800 Subject: [PATCH] refactor vertex data using vertex data in fast_tmx --- cocos/2d/CCFastTMXLayer.cpp | 39 +++++++++++++++++++--------- cocos/2d/CCFastTMXLayer.h | 2 ++ cocos/renderer/CCVertexIndexData.cpp | 15 +++++------ cocos/renderer/CCVertexIndexData.h | 29 +++++++++++++++------ 4 files changed, 57 insertions(+), 28 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index 187d599aee..1399729f1b 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -121,7 +121,7 @@ TMXLayer::TMXLayer() , _useAutomaticVertexZ(false) , _dirty(true) , _quadsDirty(true) -, _vertexBuffer(nullptr) +, _vData(nullptr) , _indexBuffer(nullptr) { } @@ -131,7 +131,7 @@ TMXLayer::~TMXLayer() CC_SAFE_RELEASE(_tileSet); CC_SAFE_RELEASE(_texture); CC_SAFE_DELETE_ARRAY(_tiles); - CC_SAFE_RELEASE(_vertexBuffer); + CC_SAFE_RELEASE(_vData); CC_SAFE_RELEASE(_indexBuffer); } @@ -177,14 +177,11 @@ void TMXLayer::onDraw(int offset, int count) getGLProgramState()->apply(_modelViewTransform); GL::bindVAO(0); - glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer->getVBO()); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer->getVBO()); + _vData->use(); - GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)0); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof(V3F_C4B_T2F, colors)); - glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof(V3F_C4B_T2F, texCoords)); + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer->getVBO()); glDrawElements(GL_TRIANGLES, (GLsizei)count * 6, GL_UNSIGNED_INT, (GLvoid*)(offset * 6 * sizeof(int))); + glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, count * 4); @@ -286,12 +283,30 @@ void TMXLayer::updateTiles(const Rect& culledRect) void TMXLayer::updateVertexBuffer() { GL::bindVAO(0); - if(nullptr == _vertexBuffer) + VertexBuffer *vertexBuffer(nullptr); + if(nullptr == _vData) { - _vertexBuffer = VertexBuffer::create(sizeof(V3F_C4B_T2F), (int)_totalQuads.size() * 4); - CC_SAFE_RETAIN(_vertexBuffer); +// glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer->getVBO()); +// GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); +// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)0); +// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof(V3F_C4B_T2F, colors)); +// glVertexAttribPointer(GLProgram::VERTEX_ATTRIB_TEX_COORD, 2, GL_FLOAT, GL_FALSE, sizeof(V3F_C4B_T2F), (GLvoid*)offsetof(V3F_C4B_T2F, texCoords)); +// glDrawElements(GL_TRIANGLES, (GLsizei)count * 6, GL_UNSIGNED_INT, (GLvoid*)(offset * 6 * sizeof(int))); + vertexBuffer = VertexBuffer::create(sizeof(V3F_C4B_T2F), (int)_totalQuads.size() * 4); + _vData = VertexData::create(); + _vData->setStream(vertexBuffer, VertexStreamAttribute(0, GLProgram::VERTEX_ATTRIB_POSITION, GL_FLOAT, 3)); + _vData->setStream(vertexBuffer, VertexStreamAttribute(offsetof(V3F_C4B_T2F, colors), GLProgram::VERTEX_ATTRIB_COLOR, GL_UNSIGNED_BYTE, 4, true)); + _vData->setStream(vertexBuffer, VertexStreamAttribute(offsetof(V3F_C4B_T2F, texCoords), GLProgram::VERTEX_ATTRIB_TEX_COORD, GL_FLOAT, 2)); + CC_SAFE_RETAIN(_vData); + } + else + { + vertexBuffer = _vData->getStreamBuffer(GLProgram::VERTEX_ATTRIB_POSITION); + } + if(vertexBuffer) + { + vertexBuffer->updateVertices((void*)&_totalQuads[0], (int)_totalQuads.size() * 4, 0); } - _vertexBuffer->updateVertices((void*)&_totalQuads[0], (int)_totalQuads.size() * 4, 0); } diff --git a/cocos/2d/CCFastTMXLayer.h b/cocos/2d/CCFastTMXLayer.h index 639178d0e9..3e7c5c05da 100644 --- a/cocos/2d/CCFastTMXLayer.h +++ b/cocos/2d/CCFastTMXLayer.h @@ -252,6 +252,8 @@ protected: VertexBuffer* _vertexBuffer; + VertexData* _vData; + IndexBuffer* _indexBuffer; public: diff --git a/cocos/renderer/CCVertexIndexData.cpp b/cocos/renderer/CCVertexIndexData.cpp index 14a169b6fe..fe978071b4 100644 --- a/cocos/renderer/CCVertexIndexData.cpp +++ b/cocos/renderer/CCVertexIndexData.cpp @@ -68,7 +68,7 @@ bool VertexData::setStream(VertexBuffer* buffer, const VertexStreamAttribute& st return true; } -void VertexData::removeStream(VertexSemantic semantic) +void VertexData::removeStream(int semantic) { auto iter = _vertexStreams.find(semantic); if(iter != _vertexStreams.end()) @@ -78,21 +78,21 @@ void VertexData::removeStream(VertexSemantic semantic) } } -const VertexStreamAttribute* VertexData::getStreamAttribute(VertexSemantic semantic) const +const VertexStreamAttribute* VertexData::getStreamAttribute(int semantic) const { auto iter = _vertexStreams.find(semantic); if(iter == _vertexStreams.end()) return nullptr; else return &iter->second._stream; } -VertexStreamAttribute* VertexData::getStreamAttribute(VertexSemantic semantic) +VertexStreamAttribute* VertexData::getStreamAttribute(int semantic) { auto iter = _vertexStreams.find(semantic); if(iter == _vertexStreams.end()) return nullptr; else return &iter->second._stream; } -VertexBuffer* VertexData::getStreamBuffer(VertexSemantic semantic) const +VertexBuffer* VertexData::getStreamBuffer(int semantic) const { auto iter = _vertexStreams.find(semantic); if(iter == _vertexStreams.end()) return nullptr; @@ -154,11 +154,10 @@ void VertexData::use() { for(auto& element : _vertexStreams) { - glEnableVertexAttribArray(getGLSemanticBinding(element.second._stream._semantic)); + glEnableVertexAttribArray((GLint)element.second._stream._semantic); glBindBuffer(GL_ARRAY_BUFFER, element.second._buffer->getVBO()); - glVertexAttribPointer(getGLSemanticBinding(element.second._stream._semantic),getGLSize(element.second._stream._type), - getGLType(element.second._stream._type),false, element.second._buffer->getSizePerVertex(), (GLvoid*)element.second._stream._offset); - + glVertexAttribPointer(GLint(element.second._stream._semantic),element.second._stream._size, + element.second._stream._type,element.second._stream._normalize, element.second._buffer->getSizePerVertex(), (GLvoid*)element.second._stream._offset); } } diff --git a/cocos/renderer/CCVertexIndexData.h b/cocos/renderer/CCVertexIndexData.h index c4e15afb83..5eb9405647 100644 --- a/cocos/renderer/CCVertexIndexData.h +++ b/cocos/renderer/CCVertexIndexData.h @@ -64,12 +64,25 @@ enum class VertexType struct VertexStreamAttribute { VertexStreamAttribute() - : _offset(0),_semantic(VertexSemantic::UNKNOWN),_type(VertexType::UNKNOWN) + : _offset(0),_semantic(0),_type(0),_size(0), _normalize(false) { } + + VertexStreamAttribute(int offset, int semantic, int type, int size) + : _offset(offset),_semantic(semantic),_type(type),_size(size), _normalize(false) + { + } + + VertexStreamAttribute(int offset, int semantic, int type, int size, bool normalize) + : _offset(offset),_semantic(semantic),_type(type),_size(size), _normalize(normalize) + { + } + + bool _normalize; int _offset; - VertexSemantic _semantic; - VertexType _type; + int _semantic; + int _type; + int _size; }; class VertexData : public Ref @@ -79,11 +92,11 @@ public: size_t getVertexStreamCount() const; bool setStream(VertexBuffer* buffer, const VertexStreamAttribute& stream); - void removeStream(VertexSemantic semantic); - const VertexStreamAttribute* getStreamAttribute(VertexSemantic semantic) const; - VertexStreamAttribute* getStreamAttribute(VertexSemantic semantic); + void removeStream(int semantic); + const VertexStreamAttribute* getStreamAttribute(int semantic) const; + VertexStreamAttribute* getStreamAttribute(int semantic); - VertexBuffer* getStreamBuffer(VertexSemantic semantic) const; + VertexBuffer* getStreamBuffer(int semantic) const; void use(); protected: @@ -96,7 +109,7 @@ protected: VertexStreamAttribute _stream; }; - std::map _vertexStreams; + std::map _vertexStreams; protected: static GLint getGLSize(VertexType type); static GLenum getGLType(VertexType type);