axmol/cocos/renderer/CCVertexIndexBuffer.h

255 lines
7.0 KiB
C
Raw Normal View History

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
class EventListenerCustom;
/**
VertexBuffer is an abstraction of low level openGL Vertex Buffer Object.
It is used to save an array of vertices.
*/
2014-08-12 11:56:21 +08:00
class CC_DLL VertexBuffer : public Ref
2014-07-23 09:20:33 +08:00
{
public:
/**
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.
*/
static VertexBuffer* create(int sizePerVertex, int vertexNumber, GLenum usage = GL_STATIC_DRAW);
/**Get the size in bytes of one vertex.*/
2014-07-23 09:20:33 +08:00
int getSizePerVertex() const;
/**Get the number of vertices.*/
2014-07-23 09:20:33 +08:00
int getVertexNumber() const;
/**
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.
*/
2014-07-23 09:20:33 +08:00
bool updateVertices(const void* verts, int count, int begin);
/**
Get the size of the vertex array in bytes, equals getSizePerVertex() * getVertexNumber().
*/
2014-07-23 09:20:33 +08:00
int getSize() const;
/**
Get the internal openGL handle.
*/
GLuint getVBO() const;
2014-08-01 14:46:35 +08:00
2014-07-23 09:20:33 +08:00
protected:
/**
Constructor.
*/
2014-07-23 09:20:33 +08:00
VertexBuffer();
/**
Destructor.
*/
2014-07-23 09:20:33 +08:00
virtual ~VertexBuffer();
/**
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.
*/
bool init(int sizePerVertex, int vertexNumber, GLenum usage = GL_STATIC_DRAW);
2014-07-23 09:20:33 +08:00
protected:
/**
Event handler for foreground.
*/
void recreateVBO() const;
/**
Event listener for foreground.
*/
EventListenerCustom* _recreateVBOEventListener;
protected:
/**
Internal handle for openGL.
*/
mutable GLuint _vbo;
/**
Size in bytes for one vertex.
*/
2014-07-23 09:20:33 +08:00
int _sizePerVertex;
/**
Number of vertices.
*/
2014-07-23 09:20:33 +08:00
int _vertexNumber;
/**
Buffer used for shadow copy.
*/
std::vector<unsigned char> _shadowCopy;
/**
Hint for optimisation in GL.
*/
GLenum _usage;
protected:
/**
Static member to indicate that use _shadowCopy or not.
*/
static bool _enableShadowCopy;
public:
/**
2015-03-18 19:32:10 +08:00
Static getter for shadowCopy.
*/
static bool isShadowCopyEnabled() { return _enableShadowCopy; }
2015-03-18 19:32:10 +08:00
/**
Static setter for shadowCopy.
*/
static void enableShadowCopy(bool enabled) { _enableShadowCopy = enabled; }
2014-07-23 09:20:33 +08:00
};
/**
IndexBuffer is an abstraction of low level openGL Buffer Object.
It used to save an array of indices.
*/
2014-08-12 11:56:21 +08:00
class CC_DLL IndexBuffer : public Ref
2014-07-23 09:20:33 +08:00
{
public:
/**
Enum for the type of index, short indices and int indices could be used.
*/
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:
/**
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.
*/
static IndexBuffer* create(IndexType type, int number, GLenum usage = GL_STATIC_DRAW);
/**
Getter for type of indices.
*/
2014-07-23 09:20:33 +08:00
IndexType getType() const;
/**
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;
/**
Get the number of indices.
*/
2014-07-23 09:20:33 +08:00
int getIndexNumber() const;
/**
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.
*/
2014-07-23 09:20:33 +08:00
bool updateIndices(const void* indices, int count, int begin);
/**
Get the size in bytes of the array of indices.
*/
2014-07-23 09:20:33 +08:00
int getSize() const;
/**
Get the openGL handle for index buffer.
*/
GLuint getVBO() const;
2014-07-23 09:20:33 +08:00
protected:
/**
Constructor.
*/
2014-07-23 09:20:33 +08:00
IndexBuffer();
/**
Destructor.
*/
2014-07-23 09:20:33 +08:00
virtual ~IndexBuffer();
/**
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.
*/
bool init(IndexType type, int number, GLenum usage = GL_STATIC_DRAW);
2014-07-23 09:20:33 +08:00
protected:
/**
Handle for openGL.
*/
mutable GLuint _vbo;
/**
Type for index.
*/
2014-07-23 09:20:33 +08:00
IndexType _type;
/**
Number of indices.
*/
2014-07-23 09:20:33 +08:00
int _indexNumber;
protected:
/**
Event handler for foreground.
*/
void recreateVBO() const;
/**
Event listener for foreground.
*/
EventListenerCustom* _recreateVBOEventListener;
/**
Buffer used for shadow copy.
*/
std::vector<unsigned char> _shadowCopy;
/**
Hint for optimisation in GL.
*/
GLenum _usage;
protected:
/**
Static member to indicate that use _shadowCopy or not.
*/
static bool _enableShadowCopy;
public:
/**
2015-03-18 19:32:10 +08:00
Static getter for shadowCopy.
*/
static bool isShadowCopyEnabled() { return _enableShadowCopy; }
2015-03-18 19:32:10 +08:00
/**
Static setter for shadowCopy.
*/
static void enableShadowCopy(bool enabled) { _enableShadowCopy = enabled; }
2014-07-23 09:20:33 +08:00
};
NS_CC_END
#endif /* __CC_VERTEX_INDEX_BUFFER_H__*/