-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDebugRenderer.cpp
More file actions
378 lines (300 loc) · 10.2 KB
/
DebugRenderer.cpp
File metadata and controls
378 lines (300 loc) · 10.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
#include "DebugRenderer.h"
namespace NS2
{
const float PI = 3.14159265359f;
const char* FRAGMENT_SRC = R"(#version 130
in vec4 fragmentColor;
in vec2 fragmentPosition;
out vec4 color;
void main() {
color = fragmentColor;
})";
const char* VERTEX_SRC = R"(#version 130
in vec2 vertexPosition;
in vec4 vertexColor;
out vec4 fragmentColor;
out vec2 fragmentPosition;
uniform mat4 cameraMatrix;
void main() {
gl_Position.xy = (cameraMatrix * vec4(vertexPosition, 0.0, 1.0)).xy;
gl_Position.z = 0.0;
gl_Position.w = 1.0;
fragmentColor = vertexColor;
fragmentPosition = vertexPosition;
})";
DebugRenderer::DebugRenderer()
{
}
DebugRenderer::~DebugRenderer()
{
dispose();
}
void DebugRenderer::init()
{
// Shader init
m_program.compileShadersFromSource(VERTEX_SRC, FRAGMENT_SRC);
m_program.addAttribute("vertexPosition");
m_program.addAttribute("vertexColor");
m_program.linkShaders();
// Set up buffers
glGenVertexArrays(1, &m_vao);
glGenBuffers(1, &m_vbo);
glGenBuffers(1, &m_ibo);
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
// Set up attributes
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
// this is the position attribute pointer
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, sizeof(DebugVertex), (void*)offsetof(DebugVertex, position));
// this is the color attribute pointer
glVertexAttribPointer(1, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(DebugVertex), (void*)offsetof(DebugVertex, color));
glBindVertexArray(0);
}
void DebugRenderer::end()
{
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, m_vertices.size() * sizeof(DebugVertex), nullptr, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ARRAY_BUFFER, 0, m_vertices.size() * sizeof(DebugVertex), m_vertices.data());
glBindBuffer(GL_ARRAY_BUFFER, 0);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, m_indices.size() * sizeof(GLuint), nullptr, GL_DYNAMIC_DRAW);
glBufferSubData(GL_ELEMENT_ARRAY_BUFFER, 0, m_indices.size() * sizeof(GLuint), m_indices.data());
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, 0);
m_numElements = m_indices.size();
m_indices.clear();
m_vertices.clear();
}
glm::vec2 rotatePoint(const glm::vec2& position, float angle)
{
glm::vec2 newPoint;
newPoint.x = position.x * cos(angle) - position.y * sin(angle);
newPoint.y = position.x * sin(angle) + position.y * cos(angle);
return newPoint;
}
void DebugRenderer::drawLine(const glm::vec2& a, const glm::vec2& b, const ColorRGBA8& color) {
int i = m_vertices.size();
m_vertices.resize(m_vertices.size() + 2);
m_vertices[i].position = a;
m_vertices[i].color = color;
m_vertices[i + 1].position = b;
m_vertices[i + 1].color = color;
m_indices.push_back(i);
m_indices.push_back(i + 1);
}
void DebugRenderer::drawPolygon(const std::vector<glm::vec2>& points, const ColorRGBA8& color)
{
// Set up vertexes
int start = m_vertices.size();
int size = points.size();
m_vertices.resize(m_vertices.size() + size);
for (int i = 0; i < size; i++)
{
m_vertices[start + i].position = points[i];
m_vertices[start + i].color = color;
}
// Set up indices
m_indices.reserve(m_indices.size() + size * 2);
for (int i = 0; i < size - 1; i++)
{
m_indices.push_back(start + i);
m_indices.push_back(start + i + 1);
}
m_indices.push_back(start + size - 1);
m_indices.push_back(start);
}
void DebugRenderer::drawPolygon(const std::vector<glm::vec2>& points, const ColorRGBA8& color, float angle)
{
// Set up vertexes
int start = m_vertices.size();
int size = points.size();
m_vertices.resize(m_vertices.size() + size);
for (int i = 0; i < size; i++)
{
m_vertices[start + i].position = rotatePoint(points[i], angle);
m_vertices[start + i].color = color;
}
// Set up indices
m_indices.reserve(m_indices.size() + size * 2);
for (int i = 0; i < size - 1; i++)
{
m_indices.push_back(start + i);
m_indices.push_back(start + i + 1);
}
m_indices.push_back(start + size - 1);
m_indices.push_back(start);
}
void DebugRenderer::drawBox(const glm::vec4& destRect, const ColorRGBA8& color)
{
int i = m_vertices.size();
m_vertices.resize(m_vertices.size() + 4);
m_vertices[i].position.x = destRect.x;
m_vertices[i].position.y = destRect.y + destRect.w;
m_vertices[i + 1].position.x = destRect.x;
m_vertices[i + 1].position.y = destRect.y;
m_vertices[i + 2].position.x = destRect.x + destRect.z;
m_vertices[i + 2].position.y = destRect.y;
m_vertices[i + 3].position.x = destRect.x + destRect.z;
m_vertices[i + 3].position.y = destRect.y + destRect.w;
for (int j = i; j < i + 4; j++)
{
m_vertices[j].color = color;
}
m_indices.reserve(m_indices.size() + 8);
m_indices.push_back(i);
m_indices.push_back(i + 1);
m_indices.push_back(i + 1);
m_indices.push_back(i + 2);
m_indices.push_back(i + 2);
m_indices.push_back(i + 3);
m_indices.push_back(i + 3);
m_indices.push_back(i);
}
void DebugRenderer::drawBox(const glm::vec2& position, const glm::vec2& dimensions, const ColorRGBA8& color)
{
int i = m_vertices.size();
m_vertices.resize(m_vertices.size() + 4);
m_vertices[i].position.x = position.x;
m_vertices[i].position.y = position.y + dimensions.y;
m_vertices[i + 1].position.x = position.x;
m_vertices[i + 1].position.y = position.y;
m_vertices[i + 2].position.x = position.x + dimensions.x;
m_vertices[i + 2].position.y = position.y;
m_vertices[i + 3].position.x = position.x + dimensions.x;
m_vertices[i + 3].position.y = position.y + dimensions.y;
for (int j = i; j < i + 4; j++)
{
m_vertices[j].color = color;
}
m_indices.reserve(m_indices.size() + 8);
m_indices.push_back(i);
m_indices.push_back(i + 1);
m_indices.push_back(i + 1);
m_indices.push_back(i + 2);
m_indices.push_back(i + 2);
m_indices.push_back(i + 3);
m_indices.push_back(i + 3);
m_indices.push_back(i);
}
void DebugRenderer::drawBox(const glm::vec4& destRect, const ColorRGBA8& color, float angle)
{
int i = m_vertices.size();
m_vertices.resize(m_vertices.size() + 4);
glm::vec2 halfDims(destRect.z / 2.0f, destRect.w / 2.0f);
// Get points centered at origin
glm::vec2 tl(-halfDims.x, halfDims.y); // top left point
glm::vec2 bl(-halfDims.x, -halfDims.y); // bottom left point
glm::vec2 br(halfDims.x, -halfDims.y); // bottom right point
glm::vec2 tr(halfDims.x, halfDims.y); // top right point
glm::vec2 positionOffset(destRect.x, destRect.y);
// Rotate the points
m_vertices[i].position = rotatePoint(tl, angle) + halfDims + positionOffset;
m_vertices[i + 1].position = rotatePoint(bl, angle) + halfDims + positionOffset;
m_vertices[i + 2].position = rotatePoint(br, angle) + halfDims + positionOffset;
m_vertices[i + 3].position = rotatePoint(tr, angle) + halfDims + positionOffset;
for (int j = i; j < i + 4; j++)
{
m_vertices[j].color = color;
}
m_indices.reserve(m_indices.size() + 8);
m_indices.push_back(i);
m_indices.push_back(i + 1);
m_indices.push_back(i + 1);
m_indices.push_back(i + 2);
m_indices.push_back(i + 2);
m_indices.push_back(i + 3);
m_indices.push_back(i + 3);
m_indices.push_back(i);
}
void DebugRenderer::drawBox(const glm::vec2& position, const glm::vec2& dimensions, const ColorRGBA8& color, float angle)
{
int i = m_vertices.size();
m_vertices.resize(m_vertices.size() + 4);
glm::vec2 halfDims(dimensions.x / 2.0f, dimensions.y / 2.0f);
// Get points centered at origin
glm::vec2 tl(-halfDims.x, halfDims.y); // top left point
glm::vec2 bl(-halfDims.x, -halfDims.y); // bottom left point
glm::vec2 br(halfDims.x, -halfDims.y); // bottom right point
glm::vec2 tr(halfDims.x, halfDims.y); // top right point
glm::vec2 positionOffset(position.x, position.y);
// Rotate the points
m_vertices[i].position = rotatePoint(tl, angle) + halfDims + positionOffset;
m_vertices[i + 1].position = rotatePoint(bl, angle) + halfDims + positionOffset;
m_vertices[i + 2].position = rotatePoint(br, angle) + halfDims + positionOffset;
m_vertices[i + 3].position = rotatePoint(tr, angle) + halfDims + positionOffset;
for (int j = i; j < i + 4; j++)
{
m_vertices[j].color = color;
}
m_indices.reserve(m_indices.size() + 8);
m_indices.push_back(i);
m_indices.push_back(i + 1);
m_indices.push_back(i + 1);
m_indices.push_back(i + 2);
m_indices.push_back(i + 2);
m_indices.push_back(i + 3);
m_indices.push_back(i + 3);
m_indices.push_back(i);
}
void DebugRenderer::drawBox(const glm::vec4& destRect, const ColorRGBA8& color, const glm::vec2& direction)
{
const glm::vec2 right(1.0f, 0.0f); // Origine direction
float angle = acos(glm::dot(right, direction));
if (direction.y < 0.0f)
angle = -angle;
drawBox(destRect, color, angle);
}
void DebugRenderer::drawCircle(const glm::vec2& center, const ColorRGBA8& color, float radius)
{
const int NUm_VERTICES = (radius * 4 > 100) ? 100 : (int)radius * 4;
// Set up vertexes
int start = m_vertices.size();
m_vertices.resize(m_vertices.size() + NUm_VERTICES);
for (int i = 0; i < NUm_VERTICES; i++)
{
float angle = ((float)i / NUm_VERTICES) * PI * 2.0f;
m_vertices[start + i].position.x = cos(angle) * radius + center.x;
m_vertices[start + i].position.y = sin(angle) * radius + center.y;
m_vertices[start + i].color = color;
}
// Set up indices
m_indices.reserve(m_indices.size() + NUm_VERTICES * 2);
for (int i = 0; i < NUm_VERTICES - 1; i++)
{
m_indices.push_back(start + i);
m_indices.push_back(start + i + 1);
}
m_indices.push_back(start + NUm_VERTICES - 1);
m_indices.push_back(start);
}
void DebugRenderer::render(const glm::mat4& projectionMatrix, float lineWith)
{
m_program.use();
{
GLint cmLocation = m_program.getUniformLocation("cameraMatrix");
glUniformMatrix4fv(cmLocation, 1, GL_FALSE, &(projectionMatrix[0][0]));
glLineWidth(lineWith);
glBindVertexArray(m_vao);
glDrawElements(GL_LINES, m_numElements, GL_UNSIGNED_INT, 0);
glBindVertexArray(0);
}
m_program.unuse();
}
void DebugRenderer::dispose()
{
if (m_vao)
{
glDeleteVertexArrays(1, &m_vao);
}
if (m_vbo)
{
glDeleteBuffers(1, &m_vbo);
}
if (m_ibo)
{
glDeleteBuffers(1, &m_ibo);
}
m_program.dispose();
}
}