axmol/cocos/renderer/CCVertexIndexBuffer.cpp

248 lines
6.6 KiB
C++
Raw Normal View History

2014-07-23 09:20:33 +08:00
/****************************************************************************
Copyright (c) 2008-2010 Ricardo Quesada
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2011 Zynga Inc.
Copyright (c) 2013-2014 Chukong Technologies Inc.
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#include "CCVertexIndexBuffer.h"
NS_CC_BEGIN
VertexBuffer* VertexBuffer::create(int sizePerVertex, int vertexNumber)
{
auto result = new (std::nothrow) VertexBuffer();
if(result && result->init(sizePerVertex, vertexNumber))
{
result->autorelease();
return result;
}
CC_SAFE_DELETE(result);
return nullptr;
}
VertexBuffer::VertexBuffer()
: _vbo(0)
, _vertexNumber(0)
, _sizePerVertex(0)
{
}
VertexBuffer::~VertexBuffer()
{
if(glIsBuffer(_vbo))
{
glDeleteBuffers(1, &_vbo);
_vbo = 0;
}
}
bool VertexBuffer::init(int sizePerVertex, int vertexNumber)
{
if(0 == sizePerVertex || 0 == vertexNumber)
return false;
_sizePerVertex = sizePerVertex;
_vertexNumber = vertexNumber;
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, _sizePerVertex * _vertexNumber, nullptr, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return true;
}
int VertexBuffer::getSizePerVertex() const
{
return _sizePerVertex;
}
int VertexBuffer::getVertexNumber() const
{
return _vertexNumber;
}
bool VertexBuffer::updateVertices(const void* verts, int count, int begin)
{
if(count <= 0 || nullptr == verts) return false;
if(begin < 0)
{
CCLOGERROR("Update vertices with begin = %d, will set begin to 0", begin);
begin = 0;
}
if(count + begin > _vertexNumber)
{
CCLOGERROR("updated vertices exceed the max size of vertex buffer, will set count to _vertexNumber-begin");
count = _vertexNumber - begin;
}
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferSubData(GL_ARRAY_BUFFER, begin * _sizePerVertex, count * _sizePerVertex, verts);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return true;
}
bool VertexBuffer::getVertices(void* verts, int count, int begin) const
{
if(count <= 0 || nullptr == verts) return false;
if(begin < 0)
{
CCLOGERROR("get vertices with begin = %d, will set begin to 0", begin);
begin = 0;
}
if(count + begin > _vertexNumber)
{
CCLOGERROR("get vertices exceed the max size of vertex buffer, will set count to _vertexNumber-begin");
count = _vertexNumber - begin;
}
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
GLvoid* data = glMapBuffer(GL_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(verts, ((char*)data) + begin * _sizePerVertex, count * _sizePerVertex);
glUnmapBuffer(GL_ARRAY_BUFFER);
glBindBuffer(GL_ARRAY_BUFFER, 0);
return true;
}
int VertexBuffer::getSize() const
{
return _sizePerVertex * _vertexNumber;
}
IndexBuffer* IndexBuffer::create(IndexType type, int number)
{
auto result = new (std::nothrow) IndexBuffer();
if(result && result->init(type, number))
{
result->autorelease();
return result;
}
CC_SAFE_DELETE(result);
return nullptr;
}
IndexBuffer::IndexBuffer()
: _vbo(0)
, _type(IndexType::INDEX_TYPE_SHORT_16)
, _indexNumber(0)
{
}
IndexBuffer::~IndexBuffer()
{
if(glIsBuffer(_vbo))
{
glDeleteBuffers(1, &_vbo);
_vbo = 0;
}
}
bool IndexBuffer::init(IndexBuffer::IndexType type, int number)
{
if(number <=0 ) return false;
_type = type;
_indexNumber = number;
glGenBuffers(1, &_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, getSizePerIndex() * _indexNumber, nullptr, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return true;
}
IndexBuffer::IndexType IndexBuffer::getType() const
{
return _type;
}
int IndexBuffer::getSizePerIndex() const
{
return IndexType::INDEX_TYPE_SHORT_16 == _type ? 2 : 4;
}
int IndexBuffer::getIndexNumber() const
{
return _indexNumber;
}
bool IndexBuffer::updateIndices(const void* indices, int count, int begin)
{
if(count <= 0 || nullptr == indices) return false;
if(begin < 0)
{
CCLOGERROR("Update indices with begin = %d, will set begin to 0", begin);
begin = 0;
}
if(count + begin > _indexNumber)
{
CCLOGERROR("updated indices exceed the max size of vertex buffer, will set count to _indexNumber-begin");
count = _indexNumber - begin;
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, begin * getSizePerIndex(), count * getSizePerIndex(), indices);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return true;
}
bool IndexBuffer::getIndices(void* indices, int count, int begin)
{
if(count <= 0 || nullptr == indices) return false;
if(begin < 0)
{
CCLOGERROR("get indices with begin = %d, will set begin to 0", begin);
begin = 0;
}
if(count + begin > _indexNumber)
{
CCLOGERROR("get indices exceed the max size of vertex buffer, will set count to _indexNumber-begin");
count = _indexNumber - begin;
}
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo);
GLvoid* data = glMapBuffer(GL_ELEMENT_ARRAY_BUFFER, GL_WRITE_ONLY);
memcpy(indices, ((char*)data) + begin * getSizePerIndex(), count * getSizePerIndex());
glUnmapBuffer(GL_ELEMENT_ARRAY_BUFFER);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
return true;
}
int IndexBuffer::getSize() const
{
return getSizePerIndex() * _indexNumber;
}
NS_CC_END