add param for usage in class VertexBuffer and IndexBuffer

This commit is contained in:
Huabing.Xu 2015-01-12 14:59:12 +08:00
parent 6984e7097f
commit 2b79ff1d87
2 changed files with 18 additions and 14 deletions

View File

@ -42,10 +42,10 @@ bool IndexBuffer::_enableShadowCopy = true;
bool IndexBuffer::_enableShadowCopy = false; bool IndexBuffer::_enableShadowCopy = false;
#endif #endif
VertexBuffer* VertexBuffer::create(int sizePerVertex, int vertexNumber) VertexBuffer* VertexBuffer::create(int sizePerVertex, int vertexNumber, GLenum usage/* = GL_STATIC_DRAW*/)
{ {
auto result = new (std::nothrow) VertexBuffer(); auto result = new (std::nothrow) VertexBuffer();
if(result && result->init(sizePerVertex, vertexNumber)) if(result && result->init(sizePerVertex, vertexNumber, usage))
{ {
result->autorelease(); result->autorelease();
return result; return result;
@ -85,12 +85,13 @@ VertexBuffer::~VertexBuffer()
#endif #endif
} }
bool VertexBuffer::init(int sizePerVertex, int vertexNumber) bool VertexBuffer::init(int sizePerVertex, int vertexNumber, GLenum usage/* = GL_STATIC_DRAW*/)
{ {
if(0 == sizePerVertex || 0 == vertexNumber) if(0 == sizePerVertex || 0 == vertexNumber)
return false; return false;
_sizePerVertex = sizePerVertex; _sizePerVertex = sizePerVertex;
_vertexNumber = vertexNumber; _vertexNumber = vertexNumber;
_usage = usage;
if(isShadowCopyEnabled()) if(isShadowCopyEnabled())
{ {
@ -99,7 +100,7 @@ bool VertexBuffer::init(int sizePerVertex, int vertexNumber)
glGenBuffers(1, &_vbo); glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo); glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, getSize(), nullptr, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, getSize(), nullptr, _usage);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
return true; return true;
} }
@ -158,7 +159,7 @@ void VertexBuffer::recreateVBO() const
buffer = &_shadowCopy[0]; buffer = &_shadowCopy[0];
} }
CCLOG("recreate IndexBuffer with size %d %d", getSizePerVertex(), _vertexNumber); CCLOG("recreate IndexBuffer with size %d %d", getSizePerVertex(), _vertexNumber);
glBufferData(GL_ARRAY_BUFFER, _sizePerVertex * _vertexNumber, buffer, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, _sizePerVertex * _vertexNumber, buffer, _usage);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
if(!glIsBuffer(_vbo)) if(!glIsBuffer(_vbo))
{ {
@ -171,10 +172,10 @@ int VertexBuffer::getSize() const
return _sizePerVertex * _vertexNumber; return _sizePerVertex * _vertexNumber;
} }
IndexBuffer* IndexBuffer::create(IndexType type, int number) IndexBuffer* IndexBuffer::create(IndexType type, int number, GLenum usage/* = GL_STATIC_DRAW*/)
{ {
auto result = new (std::nothrow) IndexBuffer(); auto result = new (std::nothrow) IndexBuffer();
if(result && result->init(type, number)) if(result && result->init(type, number, usage))
{ {
result->autorelease(); result->autorelease();
return result; return result;
@ -211,16 +212,17 @@ IndexBuffer::~IndexBuffer()
#endif #endif
} }
bool IndexBuffer::init(IndexBuffer::IndexType type, int number) bool IndexBuffer::init(IndexBuffer::IndexType type, int number, GLenum usage/* = GL_STATIC_DRAW*/)
{ {
if(number <=0 ) return false; if(number <=0 ) return false;
_type = type; _type = type;
_indexNumber = number; _indexNumber = number;
_usage = usage;
glGenBuffers(1, &_vbo); glGenBuffers(1, &_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, getSize(), nullptr, GL_STATIC_DRAW); glBufferData(GL_ELEMENT_ARRAY_BUFFER, getSize(), nullptr, _usage);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
if(isShadowCopyEnabled()) if(isShadowCopyEnabled())
@ -295,7 +297,7 @@ void IndexBuffer::recreateVBO() const
buffer = &_shadowCopy[0]; buffer = &_shadowCopy[0];
} }
CCLOG("recreate IndexBuffer with size %d %d ", getSizePerIndex(), _indexNumber); CCLOG("recreate IndexBuffer with size %d %d ", getSizePerIndex(), _indexNumber);
glBufferData(GL_ARRAY_BUFFER, getSize(), buffer, GL_STATIC_DRAW); glBufferData(GL_ARRAY_BUFFER, getSize(), buffer, _usage);
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
if(!glIsBuffer(_vbo)) if(!glIsBuffer(_vbo))
{ {

View File

@ -36,7 +36,7 @@ class EventListenerCustom;
class CC_DLL VertexBuffer : public Ref class CC_DLL VertexBuffer : public Ref
{ {
public: public:
static VertexBuffer* create(int sizePerVertex, int vertexNumber); static VertexBuffer* create(int sizePerVertex, int vertexNumber, GLenum usage = GL_STATIC_DRAW);
int getSizePerVertex() const; int getSizePerVertex() const;
int getVertexNumber() const; int getVertexNumber() const;
@ -50,7 +50,7 @@ protected:
VertexBuffer(); VertexBuffer();
virtual ~VertexBuffer(); virtual ~VertexBuffer();
bool init(int sizePerVertex, int vertexNumber); bool init(int sizePerVertex, int vertexNumber, GLenum usage = GL_STATIC_DRAW);
protected: protected:
//event listener for foreground //event listener for foreground
void recreateVBO() const; void recreateVBO() const;
@ -61,6 +61,7 @@ protected:
int _vertexNumber; int _vertexNumber;
//buffer used for shadow copy //buffer used for shadow copy
std::vector<unsigned char> _shadowCopy; std::vector<unsigned char> _shadowCopy;
GLenum _usage;
protected: protected:
static bool _enableShadowCopy; static bool _enableShadowCopy;
public: public:
@ -78,7 +79,7 @@ public:
}; };
public: public:
static IndexBuffer* create(IndexType type, int number); static IndexBuffer* create(IndexType type, int number, GLenum usage = GL_STATIC_DRAW);
IndexType getType() const; IndexType getType() const;
int getSizePerIndex() const; int getSizePerIndex() const;
@ -93,7 +94,7 @@ protected:
IndexBuffer(); IndexBuffer();
virtual ~IndexBuffer(); virtual ~IndexBuffer();
bool init(IndexType type, int number); bool init(IndexType type, int number, GLenum usage = GL_STATIC_DRAW);
protected: protected:
mutable GLuint _vbo; mutable GLuint _vbo;
@ -106,6 +107,7 @@ protected:
EventListenerCustom* _recreateVBOEventListener; EventListenerCustom* _recreateVBOEventListener;
//buffer used for shadow copy //buffer used for shadow copy
std::vector<unsigned char> _shadowCopy; std::vector<unsigned char> _shadowCopy;
GLenum _usage;
protected: protected:
static bool _enableShadowCopy; static bool _enableShadowCopy;
public: public: