2013-04-13 05:51:29 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013 Zynga 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 "CCGLBufferedNode.h"
|
|
|
|
|
2013-05-30 08:46:25 +08:00
|
|
|
CCGLBufferedNode::CCGLBufferedNode()
|
2013-04-13 05:51:29 +08:00
|
|
|
{
|
|
|
|
for(int i = 0; i < BUFFER_SLOTS; i++)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
_bufferObject[i] = 0;
|
|
|
|
_bufferSize[i] = 0;
|
|
|
|
_indexBufferObject[i] = 0;
|
|
|
|
_indexBufferSize[i] = 0;
|
2013-04-13 05:51:29 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-30 08:46:25 +08:00
|
|
|
CCGLBufferedNode::~CCGLBufferedNode()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < BUFFER_SLOTS; i++)
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_bufferSize[i])
|
2013-05-30 08:46:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(1, &(_bufferObject[i]));
|
2013-05-30 08:46:25 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_indexBufferSize[i])
|
2013-05-30 08:46:25 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(1, &(_indexBufferObject[i]));
|
2013-05-30 08:46:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-04-13 05:51:29 +08:00
|
|
|
void CCGLBufferedNode::setGLBufferData(void *buf, GLuint bufSize, int slot)
|
|
|
|
{
|
|
|
|
// WebGL doesn't support client-side arrays, so generate a buffer and load the data first.
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_bufferSize[slot] < bufSize)
|
2013-04-13 05:51:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_bufferObject[slot])
|
2013-04-13 05:51:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(1, &(_bufferObject[slot]));
|
2013-04-13 05:51:29 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenBuffers(1, &(_bufferObject[slot]));
|
|
|
|
_bufferSize[slot] = bufSize;
|
2013-04-13 05:51:29 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _bufferObject[slot]);
|
2013-04-13 05:51:29 +08:00
|
|
|
glBufferData(GL_ARRAY_BUFFER, bufSize, buf, GL_DYNAMIC_DRAW);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, _bufferObject[slot]);
|
2013-04-13 05:51:29 +08:00
|
|
|
glBufferSubData(GL_ARRAY_BUFFER, 0, bufSize, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void CCGLBufferedNode::setGLIndexData(void *buf, GLuint bufSize, int slot)
|
|
|
|
{
|
|
|
|
// WebGL doesn't support client-side arrays, so generate a buffer and load the data first.
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_indexBufferSize[slot] < bufSize)
|
2013-04-13 05:51:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
if(_indexBufferObject[slot])
|
2013-04-13 05:51:29 +08:00
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glDeleteBuffers(1, &(_indexBufferObject[slot]));
|
2013-04-13 05:51:29 +08:00
|
|
|
}
|
2013-06-15 14:03:30 +08:00
|
|
|
glGenBuffers(1, &(_indexBufferObject[slot]));
|
|
|
|
_indexBufferSize[slot] = bufSize;
|
2013-04-13 05:51:29 +08:00
|
|
|
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferObject[slot]);
|
2013-04-13 05:51:29 +08:00
|
|
|
glBufferData(GL_ELEMENT_ARRAY_BUFFER, bufSize, buf, GL_DYNAMIC_DRAW);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-06-15 14:03:30 +08:00
|
|
|
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _indexBufferObject[slot]);
|
2013-04-13 05:51:29 +08:00
|
|
|
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, bufSize, buf);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|