2014-07-23 09:20:33 +08:00
|
|
|
/****************************************************************************
|
|
|
|
Copyright (c) 2013-2014 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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#ifndef __CC_VERTEX_INDEX_BUFFER_H__
|
|
|
|
#define __CC_VERTEX_INDEX_BUFFER_H__
|
|
|
|
|
2014-08-29 15:39:52 +08:00
|
|
|
#include <vector>
|
2014-07-23 09:20:33 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-09-10 07:50:02 +08:00
|
|
|
#include "platform/CCGL.h"
|
2014-07-23 09:20:33 +08:00
|
|
|
|
|
|
|
NS_CC_BEGIN
|
|
|
|
|
2014-08-15 14:05:17 +08:00
|
|
|
class EventListenerCustom;
|
|
|
|
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
|
|
|
VertexBuffer is an abstraction of low level openGL Vertex Buffer Object.
|
2015-03-18 17:40:10 +08:00
|
|
|
It is used to save an array of vertices.
|
2015-03-19 20:20:16 +08:00
|
|
|
*@js NA
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2014-08-12 11:56:21 +08:00
|
|
|
class CC_DLL VertexBuffer : public Ref
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
2015-03-18 17:40:10 +08:00
|
|
|
Create an instance of VertexBuffer.
|
|
|
|
@param sizePerVertex Size in bytes of one vertex.
|
|
|
|
@param vertexNumber The number of vertex.
|
|
|
|
@param usage A hint to indicate whether the vertexBuffer are updated frequently or not to let GL optimise it.
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2015-01-12 14:59:12 +08:00
|
|
|
static VertexBuffer* create(int sizePerVertex, int vertexNumber, GLenum usage = GL_STATIC_DRAW);
|
2015-03-18 17:40:10 +08:00
|
|
|
/**Get the size in bytes of one vertex.*/
|
2014-07-23 09:20:33 +08:00
|
|
|
int getSizePerVertex() const;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**Get the number of vertices.*/
|
2014-07-23 09:20:33 +08:00
|
|
|
int getVertexNumber() const;
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
2015-03-18 17:40:10 +08:00
|
|
|
Update all or part of vertice data, if the range specified exceeds the vertex buffer, it will be clipped.
|
|
|
|
@param verts The pointer of the vertex data.
|
|
|
|
@param count The number of vertices to update.
|
|
|
|
@param begin The first vertex to update.
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
bool updateVertices(const void* verts, int count, int begin);
|
|
|
|
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Get the size of the vertex array in bytes, equals getSizePerVertex() * getVertexNumber().
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
int getSize() const;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Get the internal openGL handle.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
GLuint getVBO() const;
|
2014-08-01 14:46:35 +08:00
|
|
|
|
2014-07-23 09:20:33 +08:00
|
|
|
protected:
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Constructor.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
VertexBuffer();
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Destructor.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
virtual ~VertexBuffer();
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
2015-03-18 17:40:10 +08:00
|
|
|
Init the storage of vertex buffer.
|
|
|
|
@param sizePerVertex Size in bytes of one vertex.
|
|
|
|
@param vertexNumber The number of vertex.
|
|
|
|
@param usage A hint to indicate whether the vertexBuffer are updated frequently or not to let GL optimise it.
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2015-01-12 14:59:12 +08:00
|
|
|
bool init(int sizePerVertex, int vertexNumber, GLenum usage = GL_STATIC_DRAW);
|
2014-07-23 09:20:33 +08:00
|
|
|
protected:
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Event handler for foreground.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
void recreateVBO() const;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Event listener for foreground.
|
|
|
|
*/
|
2014-08-15 14:05:17 +08:00
|
|
|
EventListenerCustom* _recreateVBOEventListener;
|
2014-08-07 14:15:49 +08:00
|
|
|
protected:
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Internal handle for openGL.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
mutable GLuint _vbo;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Size in bytes for one vertex.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
int _sizePerVertex;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Number of vertices.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
int _vertexNumber;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Buffer used for shadow copy.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
std::vector<unsigned char> _shadowCopy;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Hint for optimisation in GL.
|
|
|
|
*/
|
2015-01-12 14:59:12 +08:00
|
|
|
GLenum _usage;
|
2014-08-07 14:15:49 +08:00
|
|
|
protected:
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Static member to indicate that use _shadowCopy or not.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
static bool _enableShadowCopy;
|
|
|
|
public:
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
2015-03-18 19:32:10 +08:00
|
|
|
Static getter for shadowCopy.
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
static bool isShadowCopyEnabled() { return _enableShadowCopy; }
|
2015-03-18 19:32:10 +08:00
|
|
|
/**
|
|
|
|
Static setter for shadowCopy.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
static void enableShadowCopy(bool enabled) { _enableShadowCopy = enabled; }
|
2014-07-23 09:20:33 +08:00
|
|
|
};
|
|
|
|
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
|
|
|
IndexBuffer is an abstraction of low level openGL Buffer Object.
|
2015-03-18 17:40:10 +08:00
|
|
|
It used to save an array of indices.
|
2015-03-19 20:20:16 +08:00
|
|
|
@js NA
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2014-08-12 11:56:21 +08:00
|
|
|
class CC_DLL IndexBuffer : public Ref
|
2014-07-23 09:20:33 +08:00
|
|
|
{
|
|
|
|
public:
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
2015-03-18 17:40:10 +08:00
|
|
|
Enum for the type of index, short indices and int indices could be used.
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
enum class IndexType
|
|
|
|
{
|
2015-03-18 19:32:10 +08:00
|
|
|
/**Short index will be used.*/
|
2014-07-23 09:20:33 +08:00
|
|
|
INDEX_TYPE_SHORT_16,
|
2015-03-18 19:32:10 +08:00
|
|
|
/**Int index will be used.*/
|
2014-07-23 09:20:33 +08:00
|
|
|
INDEX_TYPE_UINT_32
|
|
|
|
};
|
|
|
|
|
|
|
|
public:
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
2015-03-18 17:40:10 +08:00
|
|
|
Create an instance of IndexBuffer.
|
|
|
|
@param type type of index.
|
|
|
|
@param number The number of indices.
|
|
|
|
@param usage A hint to indicate whether the vertexBuffer are updated frequently or not to let GL optimise it.
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2015-01-12 14:59:12 +08:00
|
|
|
static IndexBuffer* create(IndexType type, int number, GLenum usage = GL_STATIC_DRAW);
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Getter for type of indices.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
IndexType getType() const;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Get the size in bytes for one index, will be 2 for INDEX_TYPE_SHORT_16 and 4 for INDEX_TYPE_UINT_32.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
int getSizePerIndex() const;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Get the number of indices.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
int getIndexNumber() const;
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
2015-03-18 17:40:10 +08:00
|
|
|
Update all or part of indices data, if the range specified exceeds the vertex buffer, it will be clipped.
|
|
|
|
@param indices The pointer of the index data.
|
|
|
|
@param count The number of indices to update.
|
|
|
|
@param begin The start index to update.
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
bool updateIndices(const void* indices, int count, int begin);
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Get the size in bytes of the array of indices.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
int getSize() const;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Get the openGL handle for index buffer.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
GLuint getVBO() const;
|
2014-07-23 09:20:33 +08:00
|
|
|
|
|
|
|
protected:
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Constructor.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
IndexBuffer();
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Destructor.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
virtual ~IndexBuffer();
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
2015-03-18 17:40:10 +08:00
|
|
|
Init the storageof IndexBuffer.
|
|
|
|
@param type type of index.
|
|
|
|
@param number The number of indices.
|
|
|
|
@param usage A hint to indicate whether the vertexBuffer are updated frequently or not to let GL optimise it.
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2015-01-12 14:59:12 +08:00
|
|
|
bool init(IndexType type, int number, GLenum usage = GL_STATIC_DRAW);
|
2014-07-23 09:20:33 +08:00
|
|
|
|
|
|
|
protected:
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Handle for openGL.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
mutable GLuint _vbo;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Type for index.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
IndexType _type;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Number of indices.
|
|
|
|
*/
|
2014-07-23 09:20:33 +08:00
|
|
|
int _indexNumber;
|
2014-08-07 14:15:49 +08:00
|
|
|
|
|
|
|
protected:
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Event handler for foreground.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
void recreateVBO() const;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Event listener for foreground.
|
|
|
|
*/
|
2014-08-15 14:05:17 +08:00
|
|
|
EventListenerCustom* _recreateVBOEventListener;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Buffer used for shadow copy.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
std::vector<unsigned char> _shadowCopy;
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Hint for optimisation in GL.
|
|
|
|
*/
|
2015-01-12 14:59:12 +08:00
|
|
|
GLenum _usage;
|
2014-08-07 14:15:49 +08:00
|
|
|
protected:
|
2015-03-18 17:40:10 +08:00
|
|
|
/**
|
|
|
|
Static member to indicate that use _shadowCopy or not.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
static bool _enableShadowCopy;
|
|
|
|
public:
|
2015-03-18 17:18:25 +08:00
|
|
|
/**
|
2015-03-18 19:32:10 +08:00
|
|
|
Static getter for shadowCopy.
|
2015-03-18 17:18:25 +08:00
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
static bool isShadowCopyEnabled() { return _enableShadowCopy; }
|
2015-03-18 19:32:10 +08:00
|
|
|
/**
|
|
|
|
Static setter for shadowCopy.
|
|
|
|
*/
|
2014-08-07 14:15:49 +08:00
|
|
|
static void enableShadowCopy(bool enabled) { _enableShadowCopy = enabled; }
|
2014-07-23 09:20:33 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
NS_CC_END
|
|
|
|
|
|
|
|
#endif /* __CC_VERTEX_INDEX_BUFFER_H__*/
|