xoreos  0.0.5
vertexbuffer.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #ifndef GRAPHICS_VERTEXBUFFER_H
26 #define GRAPHICS_VERTEXBUFFER_H
27 
28 #include <vector>
29 
30 #include "src/graphics/types.h"
31 
32 namespace Graphics {
33 
36  VPOSITION = 0,
40 };
41 
43 struct VertexAttrib {
44  GLuint index;
45  GLint size;
46  GLenum type;
47  GLsizei stride;
48  const GLvoid *pointer;
49 
51  VertexAttrib(GLuint i, GLint s, GLenum t, GLsizei st = 0, const GLvoid *p = 0) :
52  index(i), size(s), type(t), stride(st), pointer(p) { }
53 
54  GLvoid *getData();
55  const GLvoid *getData() const;
56 
57  // Render methods
58  void enable() const;
59  void disable() const;
60 };
61 
63 typedef std::vector<VertexAttrib> VertexDecl;
64 
65 class IndexBuffer;
66 
68 class VertexBuffer {
69 public:
70  VertexBuffer();
71 
72  VertexBuffer(const VertexBuffer &other);
73 
74  ~VertexBuffer();
75 
76  VertexBuffer &operator=(const VertexBuffer &other);
77 
79  void setSize(uint32 vertCount, uint32 vertSize);
80 
82  void setVertexDecl(const VertexDecl &decl);
83 
89  void setVertexDeclLinear(uint32 vertCount, VertexDecl &decl);
95  void setVertexDeclInterleave(uint32 vertCount, VertexDecl &decl);
96 
98  GLvoid *getData();
99 
101  const GLvoid *getData() const;
102 
104  GLvoid *getData(size_t vertexDecl);
105 
107  const GLvoid *getData(size_t vertexDecl) const;
108 
110  const VertexDecl &getVertexDecl() const;
111 
113  uint32 getCount() const;
114 
116  uint32 getSize() const;
117 
119  void initGL(GLuint hint = GL_STATIC_DRAW);
120 
122  void updateGL();
123 
125  void updateGLBound() const;
126 
128  void destroyGL();
129 
130  GLuint getVBO() const;
131 
132  // Render method
133 
135  void draw(GLenum mode, const IndexBuffer &indexBuffer) const;
136 
137 private:
142 
143  GLuint _vbo;
144  GLuint _hint;
145 
146  static uint32 getTypeSize(GLenum type);
147 };
148 
149 } // End of namespace Graphics
150 
151 #endif // GRAPHICS_VERTEXBUFFER_H
Vertex texture coordinates, VTCOORDi = VTCOORD + i.
Definition: vertexbuffer.h:39
uint32 getCount() const
Get vertex count.
Vertex position.
Definition: vertexbuffer.h:36
uint32 _size
Size of a buffer element in bytes (vertex attributes size sum).
Definition: vertexbuffer.h:140
GLuint index
Index of the vertex attribute (see VertexAttribIdEnum).
Definition: vertexbuffer.h:44
GLuint _vbo
Vertex Buffer Object.
Definition: vertexbuffer.h:143
GLenum type
Data type of each attribute component in the array.
Definition: vertexbuffer.h:46
Vertex color.
Definition: vertexbuffer.h:38
void updateGLBound() const
Update an existing GL buffer object, assuming it is already bound.
void setVertexDeclInterleave(uint32 vertCount, VertexDecl &decl)
Set the interleaved vertex declaration for this buffer.
Basic graphics types.
void setVertexDecl(const VertexDecl &decl)
Set vertex declaration for this buffer.
const GLvoid * pointer
Offset of the first component of the first generic vertex attribute.
Definition: vertexbuffer.h:48
std::vector< VertexAttrib > VertexDecl
Vertex data layout.
Definition: vertexbuffer.h:63
const VertexDecl & getVertexDecl() const
Access vertex declaration.
static uint32 getTypeSize(GLenum type)
Vertex normal.
Definition: vertexbuffer.h:37
void destroyGL()
Clear (destroy) GL resources associated with the buffer.
VertexAttribIdEnum
Vertex attribute data index enum, hardcoded for now.
Definition: vertexbuffer.h:35
void setVertexDeclLinear(uint32 vertCount, VertexDecl &decl)
Set the linear vertex declaration for this buffer.
uint32 getSize() const
Get vertex element size in bytes.
byte * _data
Buffer data.
Definition: vertexbuffer.h:141
Buffer containing vertex data.
Definition: vertexbuffer.h:68
VertexAttrib(GLuint i, GLint s, GLenum t, GLsizei st=0, const GLvoid *p=0)
Definition: vertexbuffer.h:51
GLuint _hint
GL hint for static or dynamic data.
Definition: vertexbuffer.h:144
VertexDecl _decl
Vertex declaration.
Definition: vertexbuffer.h:138
GLint size
Number of components per vertex attribute, must be 1, 2, 3, 4.
Definition: vertexbuffer.h:45
VertexBuffer & operator=(const VertexBuffer &other)
uint32_t uint32
Definition: types.h:204
GLvoid * getData()
Access buffer data.
uint32 _count
Number of elements in buffer.
Definition: vertexbuffer.h:139
void draw(GLenum mode, const IndexBuffer &indexBuffer) const
Draw this IndexBuffer/VertexBuffer combination.
Buffer containing indices data.
Definition: indexbuffer.h:33
void initGL(GLuint hint=GL_STATIC_DRAW)
Initialise internal buffer object for GL handling.
GLsizei stride
Byte offset between consecutive vertex attributes.
Definition: vertexbuffer.h:47
void updateGL()
Update existing GL buffer object.
Generic vertex attribute data.
Definition: vertexbuffer.h:43
uint8 byte
Definition: types.h:209
void setSize(uint32 vertCount, uint32 vertSize)
Change buffer size.