refactor vertex data

using vertex data in fast_tmx
This commit is contained in:
Huabing.Xu 2014-08-04 16:08:54 +08:00
parent a44f4c95e1
commit bbf11f5d06
4 changed files with 57 additions and 28 deletions

View File

@ -121,7 +121,7 @@ TMXLayer::TMXLayer()
, _useAutomaticVertexZ(false) , _useAutomaticVertexZ(false)
, _dirty(true) , _dirty(true)
, _quadsDirty(true) , _quadsDirty(true)
, _vertexBuffer(nullptr) , _vData(nullptr)
, _indexBuffer(nullptr) , _indexBuffer(nullptr)
{ {
} }
@ -131,7 +131,7 @@ TMXLayer::~TMXLayer()
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_RELEASE(_vertexBuffer); CC_SAFE_RELEASE(_vData);
CC_SAFE_RELEASE(_indexBuffer); CC_SAFE_RELEASE(_indexBuffer);
} }
@ -177,14 +177,11 @@ void TMXLayer::onDraw(int offset, int count)
getGLProgramState()->apply(_modelViewTransform); getGLProgramState()->apply(_modelViewTransform);
GL::bindVAO(0); GL::bindVAO(0);
glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer->getVBO()); _vData->use();
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer->getVBO());
GL::enableVertexAttribs(GL::VERTEX_ATTRIB_FLAG_POS_COLOR_TEX); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBuffer->getVBO());
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))); glDrawElements(GL_TRIANGLES, (GLsizei)count * 6, GL_UNSIGNED_INT, (GLvoid*)(offset * 6 * sizeof(int)));
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, count * 4); CC_INCREMENT_GL_DRAWN_BATCHES_AND_VERTICES(1, count * 4);
@ -286,12 +283,30 @@ void TMXLayer::updateTiles(const Rect& culledRect)
void TMXLayer::updateVertexBuffer() void TMXLayer::updateVertexBuffer()
{ {
GL::bindVAO(0); GL::bindVAO(0);
if(nullptr == _vertexBuffer) VertexBuffer *vertexBuffer(nullptr);
if(nullptr == _vData)
{ {
_vertexBuffer = VertexBuffer::create(sizeof(V3F_C4B_T2F), (int)_totalQuads.size() * 4); // glBindBuffer(GL_ARRAY_BUFFER, _vertexBuffer->getVBO());
CC_SAFE_RETAIN(_vertexBuffer); // 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);
} }

View File

@ -252,6 +252,8 @@ protected:
VertexBuffer* _vertexBuffer; VertexBuffer* _vertexBuffer;
VertexData* _vData;
IndexBuffer* _indexBuffer; IndexBuffer* _indexBuffer;
public: public:

View File

@ -68,7 +68,7 @@ bool VertexData::setStream(VertexBuffer* buffer, const VertexStreamAttribute& st
return true; return true;
} }
void VertexData::removeStream(VertexSemantic semantic) void VertexData::removeStream(int semantic)
{ {
auto iter = _vertexStreams.find(semantic); auto iter = _vertexStreams.find(semantic);
if(iter != _vertexStreams.end()) 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); auto iter = _vertexStreams.find(semantic);
if(iter == _vertexStreams.end()) return nullptr; if(iter == _vertexStreams.end()) return nullptr;
else return &iter->second._stream; else return &iter->second._stream;
} }
VertexStreamAttribute* VertexData::getStreamAttribute(VertexSemantic semantic) VertexStreamAttribute* VertexData::getStreamAttribute(int semantic)
{ {
auto iter = _vertexStreams.find(semantic); auto iter = _vertexStreams.find(semantic);
if(iter == _vertexStreams.end()) return nullptr; if(iter == _vertexStreams.end()) return nullptr;
else return &iter->second._stream; else return &iter->second._stream;
} }
VertexBuffer* VertexData::getStreamBuffer(VertexSemantic semantic) const VertexBuffer* VertexData::getStreamBuffer(int semantic) const
{ {
auto iter = _vertexStreams.find(semantic); auto iter = _vertexStreams.find(semantic);
if(iter == _vertexStreams.end()) return nullptr; if(iter == _vertexStreams.end()) return nullptr;
@ -154,11 +154,10 @@ void VertexData::use()
{ {
for(auto& element : _vertexStreams) for(auto& element : _vertexStreams)
{ {
glEnableVertexAttribArray(getGLSemanticBinding(element.second._stream._semantic)); glEnableVertexAttribArray((GLint)element.second._stream._semantic);
glBindBuffer(GL_ARRAY_BUFFER, element.second._buffer->getVBO()); glBindBuffer(GL_ARRAY_BUFFER, element.second._buffer->getVBO());
glVertexAttribPointer(getGLSemanticBinding(element.second._stream._semantic),getGLSize(element.second._stream._type), glVertexAttribPointer(GLint(element.second._stream._semantic),element.second._stream._size,
getGLType(element.second._stream._type),false, element.second._buffer->getSizePerVertex(), (GLvoid*)element.second._stream._offset); element.second._stream._type,element.second._stream._normalize, element.second._buffer->getSizePerVertex(), (GLvoid*)element.second._stream._offset);
} }
} }

View File

@ -64,12 +64,25 @@ enum class VertexType
struct VertexStreamAttribute struct VertexStreamAttribute
{ {
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; int _offset;
VertexSemantic _semantic; int _semantic;
VertexType _type; int _type;
int _size;
}; };
class VertexData : public Ref class VertexData : public Ref
@ -79,11 +92,11 @@ public:
size_t getVertexStreamCount() const; size_t getVertexStreamCount() const;
bool setStream(VertexBuffer* buffer, const VertexStreamAttribute& stream); bool setStream(VertexBuffer* buffer, const VertexStreamAttribute& stream);
void removeStream(VertexSemantic semantic); void removeStream(int semantic);
const VertexStreamAttribute* getStreamAttribute(VertexSemantic semantic) const; const VertexStreamAttribute* getStreamAttribute(int semantic) const;
VertexStreamAttribute* getStreamAttribute(VertexSemantic semantic); VertexStreamAttribute* getStreamAttribute(int semantic);
VertexBuffer* getStreamBuffer(VertexSemantic semantic) const; VertexBuffer* getStreamBuffer(int semantic) const;
void use(); void use();
protected: protected:
@ -96,7 +109,7 @@ protected:
VertexStreamAttribute _stream; VertexStreamAttribute _stream;
}; };
std::map<VertexSemantic, BufferAttribute> _vertexStreams; std::map<int, BufferAttribute> _vertexStreams;
protected: protected:
static GLint getGLSize(VertexType type); static GLint getGLSize(VertexType type);
static GLenum getGLType(VertexType type); static GLenum getGLType(VertexType type);