Debian Codegraph @

plugins/dm.gui/gui/RenderableCharacterBatch.cpp

raw
1 #include "RenderableCharacterBatch.h"
2
3 #include "render.h"
4 #include "debugging/gl.h"
5
6 #define BUFFER_OFFSET(i) ((char *)NULL + (i))
7
8 namespace gui
9 {
10
11 RenderableCharacterBatch::RenderableCharacterBatch()
12 {
13 #ifdef RENDERABLE_CHARACTER_BATCH_USE_VBO
14 // Allocate a vertex buffer object
15 glGenBuffersARB(1, &_vboData);
16
17 debug::assertNoGlErrors();
18 #endif
19 }
20
21 RenderableCharacterBatch::~RenderableCharacterBatch()
22 {
23 #ifdef RENDERABLE_CHARACTER_BATCH_USE_VBO
24 glDeleteBuffersARB(1, &_vboData);
25 #endif
26 }
27
28 void RenderableCharacterBatch::addGlyph(const TextChar& ch)
29 {
30 _verts.push_back(ch.coords[0]);
31 _verts.push_back(ch.coords[1]);
32 _verts.push_back(ch.coords[2]);
33 _verts.push_back(ch.coords[3]);
34 }
35
36 void RenderableCharacterBatch::compile()
37 {
38 #ifdef RENDERABLE_CHARACTER_BATCH_USE_VBO
39 // Space needed for geometry
40 std::size_t dataSize = sizeof(Vertex2D) * _verts.size();
41
42 // Initialise the vertex buffer
43 glBindBuffer(GL_ARRAY_BUFFER, _vboData);
44
45 // Allocate space for vertices
46 glBufferData(GL_ARRAY_BUFFER, dataSize, NULL, GL_STATIC_DRAW);
47
48 debug::assertNoGlErrors();
49
50 // Upload the data
51 glBufferSubData(GL_ARRAY_BUFFER, 0, dataSize, &_verts.front());
52
53 debug::assertNoGlErrors();
54
55 glBindBuffer(GL_ARRAY_BUFFER, 0);
56
57 debug::assertNoGlErrors();
58 #endif
59 }
60
61 void RenderableCharacterBatch::render() const
62 {
63 #ifdef RENDERABLE_CHARACTER_BATCH_USE_VBO
64 // Bind the VBO buffer and submit the draw call
65 glBindBuffer(GL_ARRAY_BUFFER, _vboData);
66
67 glClientActiveTexture(GL_TEXTURE0);
68 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
69 glTexCoordPointer(2, GL_DOUBLE, sizeof(Vertex2D), BUFFER_OFFSET(sizeof(double)*2));
70 glVertexPointer(2, GL_DOUBLE, sizeof(Vertex2D), BUFFER_OFFSET(0));
71
72 glDrawArrays(GL_QUADS, 0, static_cast<GLsizei>(_verts.size()));
73
74 debug::assertNoGlErrors();
75
76 glBindBuffer(GL_ARRAY_BUFFER, 0);
77 #else
78 // Regular array draw call
79 glVertexPointer(2, GL_DOUBLE, sizeof(Vertex2D), &(_verts.front().vertex));
80 glEnableClientState(GL_TEXTURE_COORD_ARRAY);
81 glTexCoordPointer(2, GL_DOUBLE, sizeof(Vertex2D), &(_verts.front().texcoord));
82
83 glDrawArrays(GL_QUADS, 0, static_cast<GLsizei>(_verts.size()));
84
85 glDisableClientState(GL_TEXTURE_COORD_ARRAY);
86
87 debug::assertNoGlErrors();
88 #endif
89 }
90
91 } // namespace