From b1954ef51676cc9be112d814b2886d78ccb7dd8b Mon Sep 17 00:00:00 2001 From: "Huabing.Xu" Date: Tue, 5 Aug 2014 17:49:35 +0800 Subject: [PATCH] clean up primitive code --- cocos/2d/CCFastTMXLayer.cpp | 2 +- cocos/renderer/CCPrimitive.cpp | 55 ++++++---------------------- cocos/renderer/CCPrimitive.h | 14 ++----- cocos/renderer/CCVertexIndexData.cpp | 37 ------------------- cocos/renderer/CCVertexIndexData.h | 28 -------------- 5 files changed, 17 insertions(+), 119 deletions(-) diff --git a/cocos/2d/CCFastTMXLayer.cpp b/cocos/2d/CCFastTMXLayer.cpp index 344b859b7c..600c1f9e53 100644 --- a/cocos/2d/CCFastTMXLayer.cpp +++ b/cocos/2d/CCFastTMXLayer.cpp @@ -422,7 +422,7 @@ void TMXLayer::updatePrimitives() auto primitiveIter= _primitives.find(iter.first); if(primitiveIter == _primitives.end()) { - auto primitive = Primitive::create(_vData, _indexBuffer, PrimitiveType::TRIANGLES); + auto primitive = Primitive::create(_vData, _indexBuffer, GL_TRIANGLES); primitive->setCount(iter.second * 6); primitive->setStart(start * 6); diff --git a/cocos/renderer/CCPrimitive.cpp b/cocos/renderer/CCPrimitive.cpp index 861aeb5e04..7013c30321 100644 --- a/cocos/renderer/CCPrimitive.cpp +++ b/cocos/renderer/CCPrimitive.cpp @@ -29,7 +29,7 @@ NS_CC_BEGIN -Primitive* Primitive::create(VertexData* verts, IndexBuffer* indices, PrimitiveType type) +Primitive* Primitive::create(VertexData* verts, IndexBuffer* indices, int type) { auto result = new (std::nothrow) Primitive(); if( result && result->init(verts, indices, type)) @@ -55,7 +55,7 @@ IndexBuffer* Primitive::getIndexData() Primitive::Primitive() : _verts(nullptr) , _indices(nullptr) -, _type(PrimitiveType::POINTS) +, _type(GL_POINTS) { } @@ -65,7 +65,7 @@ Primitive::~Primitive() CC_SAFE_RELEASE_NULL(_indices); } -bool Primitive::init(VertexData* verts, IndexBuffer* indices, PrimitiveType type) +bool Primitive::init(VertexData* verts, IndexBuffer* indices, int type) { if( nullptr == verts ) return false; if(verts != _verts) @@ -92,46 +92,15 @@ void Primitive::draw() if(_verts && _indices) { _verts->use(); - switch (_type) { - case PrimitiveType::POINTS: - if(_indices!= nullptr) - { - GLenum type = (_indices->getType() == IndexBuffer::IndexType::INDEX_TYPE_SHORT_16) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT; - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indices->getVBO()); - glDrawElements(GL_POINTS, _count, type, (GLvoid*)(_start * _indices->getSizePerIndex())); - } - else - { - glDrawArrays(GL_POINTS, _count, _start); - } - break; - case PrimitiveType::LINES: - if(_indices!= nullptr) - { - GLenum type = (_indices->getType() == IndexBuffer::IndexType::INDEX_TYPE_SHORT_16) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT; - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indices->getVBO()); - glDrawElements(GL_LINES, _count, type, (GLvoid*)(_start * _indices->getSizePerIndex())); - } - else - { - glDrawArrays(GL_LINES, _count, _start); - } - break; - case PrimitiveType::TRIANGLES: - if(_indices!= nullptr) - { - GLenum type = (_indices->getType() == IndexBuffer::IndexType::INDEX_TYPE_SHORT_16) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT; - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indices->getVBO()); - glDrawElements(GL_TRIANGLES, _count, type, (GLvoid*)(_start * _indices->getSizePerIndex())); - } - else - { - glDrawArrays(GL_TRIANGLES, _count, _start); - } - break; - default: - CC_ASSERT(0); - break; + if(_indices!= nullptr) + { + GLenum type = (_indices->getType() == IndexBuffer::IndexType::INDEX_TYPE_SHORT_16) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT; + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indices->getVBO()); + glDrawElements((GLenum)_type, _count, type, (GLvoid*)(_start * _indices->getSizePerIndex())); + } + else + { + glDrawArrays((GLenum)_type, _count, _start); } } } diff --git a/cocos/renderer/CCPrimitive.h b/cocos/renderer/CCPrimitive.h index 541098314e..766816fa06 100644 --- a/cocos/renderer/CCPrimitive.h +++ b/cocos/renderer/CCPrimitive.h @@ -31,23 +31,17 @@ #include "renderer/CCVertexIndexData.h" NS_CC_BEGIN -enum class PrimitiveType -{ - POINTS, - LINES, - TRIANGLES -}; class Primitive : public Ref { public: - static Primitive* create(VertexData* verts, IndexBuffer* indices, PrimitiveType type); + static Primitive* create(VertexData* verts, IndexBuffer* indices, int type); VertexData* getVertexData(); IndexBuffer* getIndexData(); - PrimitiveType getType() const { return _type; } + int getType() const { return _type; } //called by rendering framework void draw(); @@ -61,14 +55,14 @@ protected: Primitive(); virtual ~Primitive(); - bool init(VertexData* verts, IndexBuffer* indices, PrimitiveType type); + bool init(VertexData* verts, IndexBuffer* indices, int type); protected: VertexData* _verts; IndexBuffer* _indices; int _start; int _count; - PrimitiveType _type; + int _type; }; NS_CC_END diff --git a/cocos/renderer/CCVertexIndexData.cpp b/cocos/renderer/CCVertexIndexData.cpp index 198287511e..5459bfb9bc 100644 --- a/cocos/renderer/CCVertexIndexData.cpp +++ b/cocos/renderer/CCVertexIndexData.cpp @@ -113,43 +113,6 @@ VertexData::~VertexData() _vertexStreams.clear(); } -GLint VertexData::getGLSize(VertexType type) -{ - if(VertexType::FLOAT1 == type) - { - return 1; - } - else if(VertexType::FLOAT2 == type) - { - return 2; - } - else if(VertexType::FLOAT3 == type) - { - return 3; - } - else - { - return 4; - } -} - -GLenum VertexData::getGLType(VertexType type) -{ - if(VertexType::BYTE4 == type) - { - return GL_UNSIGNED_BYTE; - } - else - { - return GL_FLOAT; - } -} - -GLint VertexData::getGLSemanticBinding(VertexSemantic semantic) -{ - return GLint(semantic); -} - void VertexData::use() { uint32_t flags; diff --git a/cocos/renderer/CCVertexIndexData.h b/cocos/renderer/CCVertexIndexData.h index 01b3068d29..4b286b0a84 100644 --- a/cocos/renderer/CCVertexIndexData.h +++ b/cocos/renderer/CCVertexIndexData.h @@ -37,30 +37,6 @@ NS_CC_BEGIN class VertexBuffer; -enum VertexSemantic -{ - UNKNOWN = -1, - POSITION, - COLOR, - NORMAL, - BLEND_WEIGHT, - BLEND_INDEX, - TEXTURECOORD0, - TEXTURECOORD1, - TEXTURECOORD2, - TEXTURECOORD3 -}; - -enum class VertexType -{ - UNKNOWN, - FLOAT1, - FLOAT2, - FLOAT3, - FLOAT4, - BYTE4 -}; - struct VertexStreamAttribute { VertexStreamAttribute() @@ -110,10 +86,6 @@ protected: }; std::map _vertexStreams; -protected: - static GLint getGLSize(VertexType type); - static GLenum getGLType(VertexType type); - static GLint getGLSemanticBinding(VertexSemantic semantic); }; NS_CC_END