2014-07-25 09:58:24 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 Chukong Technologies Inc.
|
2014-08-07 15:53:20 +08:00
|
|
|
|
2014-07-25 09:58:24 +08:00
|
|
|
http://www.cocos2d-x.org
|
2014-08-07 15:53:20 +08:00
|
|
|
|
2014-07-25 09:58:24 +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-25 09:58:24 +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-25 09:58:24 +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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include "renderer/CCVertexIndexData.h"
|
2014-08-26 18:19:28 +08:00
|
|
|
#include "renderer/ccGLStateCache.h"
|
2014-08-29 15:39:52 +08:00
|
|
|
#include "renderer/CCVertexIndexBuffer.h"
|
|
|
|
|
2014-07-25 09:58:24 +08:00
|
|
|
NS_CC_BEGIN
|
|
|
|
|
|
|
|
VertexData* VertexData::create()
|
|
|
|
{
|
2014-07-25 11:58:54 +08:00
|
|
|
VertexData* result = new (std::nothrow) VertexData();
|
|
|
|
if(result)
|
|
|
|
{
|
|
|
|
result->autorelease();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
CC_SAFE_DELETE(result);
|
2014-07-25 09:58:24 +08:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-25 11:58:54 +08:00
|
|
|
size_t VertexData::getVertexStreamCount() const
|
|
|
|
{
|
|
|
|
return _vertexStreams.size();
|
|
|
|
}
|
2014-07-25 09:58:24 +08:00
|
|
|
|
2014-08-01 14:46:35 +08:00
|
|
|
bool VertexData::setStream(VertexBuffer* buffer, const VertexStreamAttribute& stream)
|
2014-07-25 09:58:24 +08:00
|
|
|
{
|
2014-07-25 11:58:54 +08:00
|
|
|
if( buffer == nullptr ) return false;
|
2014-08-01 14:46:35 +08:00
|
|
|
auto iter = _vertexStreams.find(stream._semantic);
|
2014-07-25 11:58:54 +08:00
|
|
|
if(iter == _vertexStreams.end())
|
|
|
|
{
|
|
|
|
buffer->retain();
|
2014-08-01 14:46:35 +08:00
|
|
|
auto& bufferAttribute = _vertexStreams[stream._semantic];
|
2014-07-25 11:58:54 +08:00
|
|
|
bufferAttribute._buffer = buffer;
|
|
|
|
bufferAttribute._stream = stream;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
buffer->retain();
|
|
|
|
iter->second._buffer->release();
|
|
|
|
iter->second._stream = stream;
|
|
|
|
iter->second._buffer = buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
2014-07-25 09:58:24 +08:00
|
|
|
}
|
|
|
|
|
2014-08-04 16:08:54 +08:00
|
|
|
void VertexData::removeStream(int semantic)
|
2014-07-25 11:58:54 +08:00
|
|
|
{
|
2014-08-01 14:46:35 +08:00
|
|
|
auto iter = _vertexStreams.find(semantic);
|
2014-07-25 11:58:54 +08:00
|
|
|
if(iter != _vertexStreams.end())
|
|
|
|
{
|
|
|
|
iter->second._buffer->release();
|
|
|
|
_vertexStreams.erase(iter);
|
|
|
|
}
|
|
|
|
}
|
2014-07-25 09:58:24 +08:00
|
|
|
|
2014-08-04 16:08:54 +08:00
|
|
|
const VertexStreamAttribute* VertexData::getStreamAttribute(int semantic) const
|
2014-07-25 11:58:54 +08:00
|
|
|
{
|
2014-08-01 14:46:35 +08:00
|
|
|
auto iter = _vertexStreams.find(semantic);
|
2014-07-25 11:58:54 +08:00
|
|
|
if(iter == _vertexStreams.end()) return nullptr;
|
|
|
|
else return &iter->second._stream;
|
|
|
|
}
|
2014-07-25 09:58:24 +08:00
|
|
|
|
2014-08-04 16:08:54 +08:00
|
|
|
VertexStreamAttribute* VertexData::getStreamAttribute(int semantic)
|
2014-07-25 11:58:54 +08:00
|
|
|
{
|
2014-08-01 14:46:35 +08:00
|
|
|
auto iter = _vertexStreams.find(semantic);
|
2014-07-25 11:58:54 +08:00
|
|
|
if(iter == _vertexStreams.end()) return nullptr;
|
|
|
|
else return &iter->second._stream;
|
|
|
|
}
|
2014-07-25 09:58:24 +08:00
|
|
|
|
2014-08-04 16:08:54 +08:00
|
|
|
VertexBuffer* VertexData::getStreamBuffer(int semantic) const
|
2014-07-25 09:58:24 +08:00
|
|
|
{
|
2014-08-01 14:46:35 +08:00
|
|
|
auto iter = _vertexStreams.find(semantic);
|
2014-07-25 11:58:54 +08:00
|
|
|
if(iter == _vertexStreams.end()) return nullptr;
|
|
|
|
else return iter->second._buffer;
|
2014-07-25 09:58:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
VertexData::VertexData()
|
2014-07-25 11:58:54 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
}
|
2014-07-25 09:58:24 +08:00
|
|
|
|
|
|
|
VertexData::~VertexData()
|
2014-07-25 11:58:54 +08:00
|
|
|
{
|
|
|
|
for(auto& element : _vertexStreams)
|
|
|
|
{
|
|
|
|
element.second._buffer->release();
|
|
|
|
}
|
|
|
|
_vertexStreams.clear();
|
|
|
|
}
|
|
|
|
|
2014-08-01 14:46:35 +08:00
|
|
|
void VertexData::use()
|
|
|
|
{
|
2014-08-08 13:47:50 +08:00
|
|
|
uint32_t flags(0);
|
2014-08-01 14:46:35 +08:00
|
|
|
for(auto& element : _vertexStreams)
|
|
|
|
{
|
2014-08-05 15:08:01 +08:00
|
|
|
flags = flags | (1 << element.second._stream._semantic);
|
|
|
|
}
|
|
|
|
|
|
|
|
GL::enableVertexAttribs(flags);
|
2015-08-26 06:07:03 +08:00
|
|
|
|
|
|
|
int lastVBO = -1;
|
2014-08-05 15:08:01 +08:00
|
|
|
for(auto& element : _vertexStreams)
|
|
|
|
{
|
|
|
|
//glEnableVertexAttribArray((GLint)element.second._stream._semantic);
|
2015-08-26 06:07:03 +08:00
|
|
|
auto vertexStreamAttrib = element.second._stream;
|
|
|
|
auto vertexBuffer = element.second._buffer;
|
|
|
|
|
|
|
|
// don't call glBindBuffer() if not needed. Expensive operation.
|
|
|
|
int vbo = vertexBuffer->getVBO();
|
|
|
|
if (vbo != lastVBO) {
|
|
|
|
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer->getVBO());
|
|
|
|
lastVBO = vbo;
|
|
|
|
}
|
|
|
|
glVertexAttribPointer(GLint(vertexStreamAttrib._semantic),
|
|
|
|
vertexStreamAttrib._size,
|
|
|
|
vertexStreamAttrib._type,
|
|
|
|
vertexStreamAttrib._normalize,
|
|
|
|
vertexBuffer->getSizePerVertex(),
|
2015-08-27 01:16:50 +08:00
|
|
|
(GLvoid*)((long)vertexStreamAttrib._offset));
|
2014-08-01 14:46:35 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-07-25 09:58:24 +08:00
|
|
|
NS_CC_END
|