clean up primitive code

This commit is contained in:
Huabing.Xu 2014-08-05 17:49:35 +08:00
parent 01b69c9e4f
commit b1954ef516
5 changed files with 17 additions and 119 deletions

View File

@ -422,7 +422,7 @@ void TMXLayer::updatePrimitives()
auto primitiveIter= _primitives.find(iter.first); auto primitiveIter= _primitives.find(iter.first);
if(primitiveIter == _primitives.end()) 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->setCount(iter.second * 6);
primitive->setStart(start * 6); primitive->setStart(start * 6);

View File

@ -29,7 +29,7 @@
NS_CC_BEGIN 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(); auto result = new (std::nothrow) Primitive();
if( result && result->init(verts, indices, type)) if( result && result->init(verts, indices, type))
@ -55,7 +55,7 @@ IndexBuffer* Primitive::getIndexData()
Primitive::Primitive() Primitive::Primitive()
: _verts(nullptr) : _verts(nullptr)
, _indices(nullptr) , _indices(nullptr)
, _type(PrimitiveType::POINTS) , _type(GL_POINTS)
{ {
} }
@ -65,7 +65,7 @@ Primitive::~Primitive()
CC_SAFE_RELEASE_NULL(_indices); 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( nullptr == verts ) return false;
if(verts != _verts) if(verts != _verts)
@ -92,46 +92,15 @@ void Primitive::draw()
if(_verts && _indices) if(_verts && _indices)
{ {
_verts->use(); _verts->use();
switch (_type) { if(_indices!= nullptr)
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());
GLenum type = (_indices->getType() == IndexBuffer::IndexType::INDEX_TYPE_SHORT_16) ? GL_UNSIGNED_SHORT : GL_UNSIGNED_INT; glDrawElements((GLenum)_type, _count, type, (GLvoid*)(_start * _indices->getSizePerIndex()));
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indices->getVBO()); }
glDrawElements(GL_POINTS, _count, type, (GLvoid*)(_start * _indices->getSizePerIndex())); else
} {
else glDrawArrays((GLenum)_type, _count, _start);
{
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;
} }
} }
} }

View File

@ -31,23 +31,17 @@
#include "renderer/CCVertexIndexData.h" #include "renderer/CCVertexIndexData.h"
NS_CC_BEGIN NS_CC_BEGIN
enum class PrimitiveType
{
POINTS,
LINES,
TRIANGLES
};
class Primitive : public Ref class Primitive : public Ref
{ {
public: public:
static Primitive* create(VertexData* verts, IndexBuffer* indices, PrimitiveType type); static Primitive* create(VertexData* verts, IndexBuffer* indices, int type);
VertexData* getVertexData(); VertexData* getVertexData();
IndexBuffer* getIndexData(); IndexBuffer* getIndexData();
PrimitiveType getType() const { return _type; } int getType() const { return _type; }
//called by rendering framework //called by rendering framework
void draw(); void draw();
@ -61,14 +55,14 @@ protected:
Primitive(); Primitive();
virtual ~Primitive(); virtual ~Primitive();
bool init(VertexData* verts, IndexBuffer* indices, PrimitiveType type); bool init(VertexData* verts, IndexBuffer* indices, int type);
protected: protected:
VertexData* _verts; VertexData* _verts;
IndexBuffer* _indices; IndexBuffer* _indices;
int _start; int _start;
int _count; int _count;
PrimitiveType _type; int _type;
}; };
NS_CC_END NS_CC_END

View File

@ -113,43 +113,6 @@ VertexData::~VertexData()
_vertexStreams.clear(); _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() void VertexData::use()
{ {
uint32_t flags; uint32_t flags;

View File

@ -37,30 +37,6 @@ NS_CC_BEGIN
class VertexBuffer; 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 struct VertexStreamAttribute
{ {
VertexStreamAttribute() VertexStreamAttribute()
@ -110,10 +86,6 @@ protected:
}; };
std::map<int, BufferAttribute> _vertexStreams; std::map<int, BufferAttribute> _vertexStreams;
protected:
static GLint getGLSize(VertexType type);
static GLenum getGLType(VertexType type);
static GLint getGLSemanticBinding(VertexSemantic semantic);
}; };
NS_CC_END NS_CC_END