2014-07-23 09:20:33 +08:00
|
|
|
/****************************************************************************
|
2016-08-05 09:42:15 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
2014-08-07 15:53:20 +08:00
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
http://www.cocos2d-x.org
|
2014-08-07 15:53:20 +08:00
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
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:
|
2014-08-07 15:53:20 +08:00
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
|
|
all copies or substantial portions of the Software.
|
2014-08-07 15:53:20 +08:00
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2014-08-07 14:15:49 +08:00
|
|
|
#include "renderer/CCVertexIndexBuffer.h"
|
|
|
|
#include "base/CCEventType.h"
|
|
|
|
#include "base/CCEventListenerCustom.h"
|
2014-08-26 18:19:28 +08:00
|
|
|
#include "base/CCEventDispatcher.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "base/CCDirector.h"
|
2014-07-23 09:20:33 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-08-07 14:15:49 +08:00
|
|
|
bool VertexBuffer::_enableShadowCopy = true;
|
|
|
|
#else
|
|
|
|
bool VertexBuffer::_enableShadowCopy = false;
|
|
|
|
#endif
|
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-08-07 14:15:49 +08:00
|
|
|
bool IndexBuffer::_enableShadowCopy = true;
|
|
|
|
#else
|
|
|
|
bool IndexBuffer::_enableShadowCopy = false;
|
|
|
|
#endif
|
|
|
|
|
2015-01-12 14:59:12 +08:00
|
|
|
VertexBuffer* VertexBuffer::create(int sizePerVertex, int vertexNumber, GLenum usage/* = GL_STATIC_DRAW*/)
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
|
|
|
auto result = new (std::nothrow) VertexBuffer();
|
2015-01-12 14:59:12 +08:00
|
|
|
if(result && result->init(sizePerVertex, vertexNumber, usage))
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
|
|
|
result->autorelease();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(result);
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
VertexBuffer::VertexBuffer()
|
2014-10-30 22:25:46 +08:00
|
|
|
: _recreateVBOEventListener(nullptr)
|
|
|
|
, _vbo(0)
|
2014-07-23 09:20:33 +08:00
|
|
|
, _sizePerVertex(0)
|
2014-10-30 22:25:46 +08:00
|
|
|
, _vertexNumber(0)
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
2014-08-07 14:15:49 +08:00
|
|
|
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-08-15 14:05:17 +08:00
|
|
|
auto callBack = [this](EventCustom* event)
|
|
|
|
{
|
|
|
|
this->recreateVBO();
|
|
|
|
};
|
|
|
|
|
|
|
|
_recreateVBOEventListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_RENDERER_RECREATED, callBack);
|
|
|
|
|
2014-08-07 14:15:49 +08:00
|
|
|
#endif
|
2014-07-23 09:20:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexBuffer::~VertexBuffer()
|
|
|
|
{
|
|
|
|
if(glIsBuffer(_vbo))
|
|
|
|
{
|
|
|
|
glDeleteBuffers(1, &_vbo);
|
|
|
|
_vbo = 0;
|
|
|
|
}
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-08-15 14:05:17 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_recreateVBOEventListener);
|
|
|
|
#endif
|
2014-07-23 09:20:33 +08:00
|
|
|
}
|
|
|
|
|
2015-01-12 14:59:12 +08:00
|
|
|
bool VertexBuffer::init(int sizePerVertex, int vertexNumber, GLenum usage/* = GL_STATIC_DRAW*/)
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
|
|
|
if(0 == sizePerVertex || 0 == vertexNumber)
|
|
|
|
return false;
|
|
|
|
_sizePerVertex = sizePerVertex;
|
|
|
|
_vertexNumber = vertexNumber;
|
2015-01-12 14:59:12 +08:00
|
|
|
_usage = usage;
|
2014-07-23 09:20:33 +08:00
|
|
|
|
2014-08-07 14:15:49 +08:00
|
|
|
if(isShadowCopyEnabled())
|
|
|
|
{
|
|
|
|
_shadowCopy.resize(sizePerVertex * _vertexNumber);
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
glGenBuffers(1, &_vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
2015-01-12 14:59:12 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, getSize(), nullptr, _usage);
|
2014-07-23 09:20:33 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2014-08-07 14:15:49 +08:00
|
|
|
if(isShadowCopyEnabled())
|
|
|
|
{
|
|
|
|
memcpy(&_shadowCopy[begin * _sizePerVertex], verts, count * _sizePerVertex);
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, begin * _sizePerVertex, count * _sizePerVertex, verts);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2014-08-07 14:15:49 +08:00
|
|
|
GLuint VertexBuffer::getVBO() const
|
|
|
|
{
|
|
|
|
return _vbo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void VertexBuffer::recreateVBO() const
|
|
|
|
{
|
|
|
|
CCLOG("come to foreground of VertexBuffer");
|
|
|
|
glGenBuffers(1, &_vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
|
|
|
const void* buffer = nullptr;
|
|
|
|
if(isShadowCopyEnabled())
|
|
|
|
{
|
|
|
|
buffer = &_shadowCopy[0];
|
|
|
|
}
|
2014-08-15 14:05:17 +08:00
|
|
|
CCLOG("recreate IndexBuffer with size %d %d", getSizePerVertex(), _vertexNumber);
|
2015-01-12 14:59:12 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, _sizePerVertex * _vertexNumber, buffer, _usage);
|
2014-08-07 14:15:49 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
if(!glIsBuffer(_vbo))
|
|
|
|
{
|
|
|
|
CCLOGERROR("recreate VertexBuffer Error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
int VertexBuffer::getSize() const
|
|
|
|
{
|
|
|
|
return _sizePerVertex * _vertexNumber;
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:59:12 +08:00
|
|
|
IndexBuffer* IndexBuffer::create(IndexType type, int number, GLenum usage/* = GL_STATIC_DRAW*/)
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
|
|
|
auto result = new (std::nothrow) IndexBuffer();
|
2015-01-12 14:59:12 +08:00
|
|
|
if(result && result->init(type, number, usage))
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
|
|
|
result->autorelease();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
CC_SAFE_DELETE(result);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
IndexBuffer::IndexBuffer()
|
|
|
|
: _vbo(0)
|
|
|
|
, _type(IndexType::INDEX_TYPE_SHORT_16)
|
|
|
|
, _indexNumber(0)
|
2014-08-15 14:05:17 +08:00
|
|
|
, _recreateVBOEventListener(nullptr)
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-08-15 14:05:17 +08:00
|
|
|
auto callBack = [this](EventCustom* event)
|
|
|
|
{
|
|
|
|
this->recreateVBO();
|
|
|
|
};
|
|
|
|
|
|
|
|
_recreateVBOEventListener = Director::getInstance()->getEventDispatcher()->addCustomEventListener(EVENT_RENDERER_RECREATED, callBack);
|
2014-08-07 14:15:49 +08:00
|
|
|
#endif
|
2014-07-23 09:20:33 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
IndexBuffer::~IndexBuffer()
|
|
|
|
{
|
|
|
|
if(glIsBuffer(_vbo))
|
|
|
|
{
|
|
|
|
glDeleteBuffers(1, &_vbo);
|
|
|
|
_vbo = 0;
|
|
|
|
}
|
2015-05-09 00:19:13 +08:00
|
|
|
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID || CC_TARGET_PLATFORM == CC_PLATFORM_WINRT)
|
2014-08-15 14:05:17 +08:00
|
|
|
Director::getInstance()->getEventDispatcher()->removeEventListener(_recreateVBOEventListener);
|
|
|
|
#endif
|
2014-07-23 09:20:33 +08:00
|
|
|
}
|
|
|
|
|
2015-01-12 14:59:12 +08:00
|
|
|
bool IndexBuffer::init(IndexBuffer::IndexType type, int number, GLenum usage/* = GL_STATIC_DRAW*/)
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
|
|
|
if(number <=0 ) return false;
|
|
|
|
|
|
|
|
_type = type;
|
|
|
|
_indexNumber = number;
|
2015-01-12 14:59:12 +08:00
|
|
|
_usage = usage;
|
2014-07-23 09:20:33 +08:00
|
|
|
|
|
|
|
glGenBuffers(1, &_vbo);
|
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _vbo);
|
2015-01-12 14:59:12 +08:00
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, getSize(), nullptr, _usage);
|
2014-07-23 09:20:33 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
|
|
|
|
|
2014-08-07 14:15:49 +08:00
|
|
|
if(isShadowCopyEnabled())
|
|
|
|
{
|
|
|
|
_shadowCopy.resize(getSize());
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
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);
|
|
|
|
|
2014-08-07 14:15:49 +08:00
|
|
|
if(isShadowCopyEnabled())
|
|
|
|
{
|
|
|
|
memcpy(&_shadowCopy[begin * getSizePerIndex()], indices, count * getSizePerIndex());
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int IndexBuffer::getSize() const
|
|
|
|
{
|
|
|
|
return getSizePerIndex() * _indexNumber;
|
|
|
|
}
|
|
|
|
|
2014-08-07 14:15:49 +08:00
|
|
|
GLuint IndexBuffer::getVBO() const
|
|
|
|
{
|
|
|
|
return _vbo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void IndexBuffer::recreateVBO() const
|
|
|
|
{
|
|
|
|
CCLOG("come to foreground of IndexBuffer");
|
|
|
|
glGenBuffers(1, &_vbo);
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _vbo);
|
|
|
|
const void* buffer = nullptr;
|
|
|
|
if(isShadowCopyEnabled())
|
|
|
|
{
|
|
|
|
buffer = &_shadowCopy[0];
|
|
|
|
}
|
2014-08-15 14:05:17 +08:00
|
|
|
CCLOG("recreate IndexBuffer with size %d %d ", getSizePerIndex(), _indexNumber);
|
2015-01-12 14:59:12 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, getSize(), buffer, _usage);
|
2014-08-07 14:15:49 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, 0);
|
|
|
|
if(!glIsBuffer(_vbo))
|
|
|
|
{
|
|
|
|
CCLOGERROR("recreate IndexBuffer Error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
NS_CC_END
|