-
-
Notifications
You must be signed in to change notification settings - Fork 46
Expand file tree
/
Copy pathgizmos.cpp
More file actions
428 lines (376 loc) · 14.7 KB
/
gizmos.cpp
File metadata and controls
428 lines (376 loc) · 14.7 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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#include "gizmos.h"
#include "components/camera.h"
#include "components/transform.h"
#include "resources/material.h"
#include "resources/mesh.h"
#include "commandbuffer.h"
#define OVERRIDE "texture0"
Mesh *Gizmos::s_wire = nullptr;
Mesh *Gizmos::s_solid = nullptr;
MaterialInstance *Gizmos::s_wireMaterial = nullptr;
MaterialInstance *Gizmos::s_solidMaterial = nullptr;
Material *Gizmos::s_spriteMaterial = nullptr;
struct SpriteBatches {
Mesh *mesh;
MaterialInstance *material;
};
static unordered_map<string, SpriteBatches> s_sprites;
/*!
\class Gizmos
\brief The Gizmos class provides functions to draw various graphical primitives for debugging purposes in a 3D space.
\inmodule Engine
\note Gizmos can be drawn only in Editor.
The Gizmos class provides a collection of static methods to draw various shapes and primitives for debugging in a 3D space.
Users can use these methods to visualize different elements during development and debugging.
*/
/*!
\internal
Initializes static member variables by loading necessary resources.
*/
void Gizmos::init() {
if(s_wireMaterial == nullptr) {
Material *m = Engine::loadResource<Material>(".embedded/gizmo.shader");
if(m) {
s_wireMaterial = m->createInstance();
}
}
if(s_solidMaterial == nullptr) {
Material *m = Engine::loadResource<Material>(".embedded/solid.shader");
if(m) {
s_solidMaterial = m->createInstance();
}
}
if(s_spriteMaterial == nullptr) {
s_spriteMaterial = Engine::loadResource<Material>(".embedded/DefaultSprite.mtl");
}
if(s_wire == nullptr) {
s_wire = Engine::objectCreate<Mesh>("Gizmo Lines Batch");
s_wire->makeDynamic();
}
if(s_solid == nullptr) {
s_solid = Engine::objectCreate<Mesh>("Gizmo Solid Batch");
s_solid->makeDynamic();
}
}
/*!
Clears the content of wireframe, solid, and sprite batches.
*/
void Gizmos::clear() {
s_wire->clear();
s_solid->clear();
for(auto &it : s_sprites) {
it.second.mesh->clear();
}
}
/*!
\internal
Draws the sprite batch using the provided command buffer.
*/
void Gizmos::drawSpriteBatch(CommandBuffer *buffer) {
for(auto &it : s_sprites) {
if(!it.second.mesh->isEmpty()) {
buffer->drawMesh(Matrix4(), it.second.mesh, 0, CommandBuffer::TRANSLUCENT, it.second.material);
}
}
}
/*!
\internal
Draws the solid batch using the provided command buffer.
*/
void Gizmos::drawSolidBatch(CommandBuffer *buffer) {
if(!s_solid->isEmpty()) {
buffer->drawMesh(Matrix4(), s_solid, 0, CommandBuffer::TRANSLUCENT, s_solidMaterial);
}
}
/*!
\internal
Draws the wireframe batch using the provided command buffer.
*/
void Gizmos::drawWireBatch(CommandBuffer *buffer) {
if(!s_wire->isEmpty()) {
buffer->drawMesh(Matrix4(), s_wire, 0, CommandBuffer::TRANSLUCENT, s_wireMaterial);
}
}
/*!
Draws a solid box with specified \a center, \a size and \a color in the 3D space.
Parameter \a transform can be used to move, rotate and scale this box.
*/
void Gizmos::drawBox(const Vector3 ¢er, const Vector3 &size, const Vector4 &color, const Matrix4 &transform) {
Vector3 min(center - size * 0.5f);
Vector3 max(center + size * 0.5f);
Mesh mesh;
mesh.setVertices({
Vector3(min.x, min.y, min.z),
Vector3(max.x, min.y, min.z),
Vector3(max.x, min.y, max.z),
Vector3(min.x, min.y, max.z),
Vector3(min.x, max.y, min.z),
Vector3(max.x, max.y, min.z),
Vector3(max.x, max.y, max.z),
Vector3(min.x, max.y, max.z)
});
mesh.setIndices({0, 1, 2, 0, 2, 3, // bottom
4, 6, 5, 4, 7, 6, // top
0, 1, 5, 0, 5, 4, // front
3, 6, 2, 3, 7, 6, // back
0, 7, 3, 0, 4, 7, // left
1, 6, 2, 1, 5, 6, // right
});
mesh.setColors(Vector4Vector(mesh.vertices().size(), color));
s_solid->batchMesh(mesh, &transform);
}
/*!
Draws an billboard icon at the specified \a center with the given \a size, \a color, and \a transform.
Parameter \a name will be used to set a texture to render.
*/
void Gizmos::drawIcon(const Vector3 ¢er, const Vector2 &size, const string &name, const Vector4 &color, const Matrix4 &transform) {
Matrix4 model(center, Quaternion(), Vector3(size, size.x));
Matrix4 q = model * Matrix4(Camera::current()->transform()->quaternion().toMatrix());
Mesh mesh;
mesh.setIndices({0, 1, 2, 0, 2, 3});
mesh.setVertices({Vector3(-0.5f,-0.5f, 0.0f),
Vector3(-0.5f, 0.5f, 0.0f),
Vector3( 0.5f, 0.5f, 0.0f),
Vector3( 0.5f,-0.5f, 0.0f)});
mesh.setUv0({Vector2(0.0f, 0.0f),
Vector2(0.0f, 1.0f),
Vector2(1.0f, 1.0f),
Vector2(1.0f, 0.0f)});
mesh.setColors(Vector4Vector(4, color));
auto it = s_sprites.find(name);
if(it != s_sprites.end()) {
it->second.mesh->batchMesh(mesh, &q);
} else {
if(s_spriteMaterial) {
SpriteBatches batch;
batch.mesh = Engine::objectCreate<Mesh>(name);
batch.mesh->makeDynamic();
batch.mesh->batchMesh(mesh, &q);
batch.material = s_spriteMaterial->createInstance();
batch.material->setTexture(OVERRIDE, Engine::loadResource<Texture>(name));
s_sprites[name] = batch;
}
}
}
/*!
Draws a \a mesh with a specified \a color and \a transform.
*/
void Gizmos::drawMesh(Mesh &mesh, const Vector4 &color, const Matrix4 &transform) {
Mesh m;
m.setVertices(mesh.vertices());
m.setIndices(mesh.indices());
m.setNormals(mesh.normals());
m.setTangents(mesh.tangents());
m.setUv0(mesh.uv0());
m.setColors(Vector4Vector(m.vertices().size(), color));
s_solid->batchMesh(m, &transform);
}
/*!
Draws a solid sphere with specified \a center, \a radius and \a color in the 3D space.
Parameter \a transform can be used to move, rotate and scale this sphere.
*/
void Gizmos::drawSphere(const Vector3 ¢er, float radius, const Vector4 &color, const Matrix4 &transform) {
const uint32_t steps = 12;
float stackStep = PI / (float)steps;
float sectorStep = 2.0f * stackStep;
Vector3Vector vertices;
vertices.reserve((steps + 1) * (steps + 1));
IndexVector indices;
for(uint32_t i = 0; i <= steps; i++) {
float stackAngle = PI / 2 - i * stackStep;
float xz = radius * cosf(stackAngle);
float y = radius * sinf(stackAngle);
for(uint32_t j = 0; j <= steps; j++) {
float sectorAngle = j * sectorStep;
float x = xz * cosf(sectorAngle);
float z = xz * sinf(sectorAngle);
vertices.push_back(Vector3(x, y, z));
}
}
for(uint32_t i = 0; i < steps; i++) {
uint32_t k1 = i * (steps + 1);
uint32_t k2 = k1 + steps + 1;
for(uint32_t j = 0; j < steps; j++, k1++, k2++) {
indices.push_back(k1);
indices.push_back(k2);
indices.push_back(k1 + 1);
indices.push_back(k1 + 1);
indices.push_back(k2);
indices.push_back(k2 + 1);
}
}
Mesh mesh;
mesh.setVertices(vertices);
mesh.setIndices(indices);
mesh.setColors(Vector4Vector(mesh.vertices().size(), color));
s_solid->batchMesh(mesh, &transform);
}
/*!
Draws a solid arc in the 3D space with the specified \a center, \a radius and \a color in the 3D space.
Parameters \a start and \a angle allows to specify angles to draw a sector in degrees.
Parameter \a transform can be used to move, rotate and scale this arc.
*/
void Gizmos::drawSolidArc(const Vector3 ¢er, float radius, float start, float angle, const Vector4 &color, const Matrix4 &transform) {
Mesh mesh;
mesh.setVertices(Mathf::pointsArc(Quaternion(), radius, start, angle, 180, true));
size_t size = mesh.vertices().size();
IndexVector indices;
indices.resize((size - 1) * 3);
for(int i = 0; i < size - 1; i++) {
indices[i * 3] = 0;
indices[i * 3 + 1] = i;
indices[i * 3 + 2] = i+1;
}
mesh.setIndices(indices);
mesh.setColors(Vector4Vector(size, color));
Matrix4 t(transform);
t[12] += center.x;
t[13] += center.y;
t[14] += center.z;
s_solid->batchMesh(mesh, &t);
}
/*!
Draws lines connecting specified \a points and \a color in 3D space.
Parameter \a indices specifies relations between points.
Parameter \a transform can be used to move, rotate and scale this structure.
*/
void Gizmos::drawLines(const Vector3Vector &points, const IndexVector &indices, const Vector4 &color, const Matrix4 &transform) {
Mesh mesh;
mesh.setVertices(points);
mesh.setIndices(indices);
mesh.setColors(Vector4Vector(points.size(), color));
s_wire->batchMesh(mesh, &transform);
}
/*!
Draws a wire arc in the 3D space with the specified \a center, \a radius and \a color in the 3D space.
Parameters \a start and \a angle allows to specify angles to draw a sector in degrees.
Parameter \a transform can be used to move, rotate and scale this arc.
*/
void Gizmos::drawArc(const Vector3 ¢er, float radius, float start, float angle, const Vector4 &color, const Matrix4 &transform) {
Mesh mesh;
mesh.setVertices(Mathf::pointsArc(Quaternion(), radius, start, angle, 180));
size_t size = mesh.vertices().size();
IndexVector indices;
indices.resize((size - 1) * 2);
for(int i = 0; i < size - 1; i++) {
indices[i * 2] = i;
indices[i * 2 + 1] = i+1;
}
mesh.setIndices(indices);
mesh.setColors(Vector4Vector(size, color));
Matrix4 t(transform);
t[12] += center.x;
t[13] += center.y;
t[14] += center.z;
s_wire->batchMesh(mesh, &t);
}
/*!
Draws a wire circle in the 3D space with the specified \a center, \a radius and \a color in the 3D space.
Parameter \a transform can be used to move, rotate and scale this circle.
*/
void Gizmos::drawCircle(const Vector3 ¢er, float radius, const Vector4 &color, const Matrix4 &transform) {
drawArc(center, radius, 0, 360, color, transform);
}
/*!
Draws a wire rectangle in the 3D space with the specified \a center, \a size and \a color in the 3D space.
Parameter \a transform can be used to move, rotate and scale this rectangle.
*/
void Gizmos::drawRectangle(const Vector3 ¢er, const Vector2 &size, const Vector4 &color, const Matrix4 &transform) {
Vector2 min(Vector2(center.x, center.y) - size * 0.5f);
Vector2 max(Vector2(center.x, center.y) + size * 0.5f);
Mesh mesh;
mesh.setVertices({
Vector3(min.x, min.y, center.z),
Vector3(max.x, min.y, center.z),
Vector3(max.x, max.y, center.z),
Vector3(min.x, max.y, center.z)
});
mesh.setIndices({
0, 1, 1, 2, 2, 3, 3, 0
});
mesh.setColors(Vector4Vector(mesh.vertices().size(), color));
s_wire->batchMesh(mesh, &transform);
}
/*!
Draws a wire box in the 3D space with the specified \a center, \a size and \a color in the 3D space.
Parameter \a transform can be used to move, rotate and scale this box.
*/
void Gizmos::drawWireBox(const Vector3 ¢er, const Vector3 &size, const Vector4 &color, const Matrix4 &transform) {
Vector3 min(center - size * 0.5f);
Vector3 max(center + size * 0.5f);
Mesh mesh;
mesh.setVertices({
Vector3(min.x, min.y, min.z),
Vector3(max.x, min.y, min.z),
Vector3(max.x, min.y, max.z),
Vector3(min.x, min.y, max.z),
Vector3(min.x, max.y, min.z),
Vector3(max.x, max.y, min.z),
Vector3(max.x, max.y, max.z),
Vector3(min.x, max.y, max.z)
});
mesh.setIndices({0, 1, 1, 2, 2, 3, 3, 0,
4, 5, 5, 6, 6, 7, 7, 4,
0, 4, 1, 5, 2, 6, 3, 7});
mesh.setColors(Vector4Vector(mesh.vertices().size(), color));
s_wire->batchMesh(mesh, &transform);
}
/*!
Draws a wireframe version of the specified \a mesh and \a color in 3D space.
Parameter \a transform can be used to move, rotate and scale this mesh.
*/
void Gizmos::drawWireMesh(Mesh &mesh, const Vector4 &color, const Matrix4 &transform) {
Mesh m;
m.setVertices(mesh.vertices());
m.setIndices(mesh.indices());
m.setColors(Vector4Vector(m.vertices().size(), color));
s_wire->batchMesh(m, &transform);
}
/*!
Draws a wire sphere in the 3D space with the specified \a center, \a radius and \a color in the 3D space.
Parameter \a transform can be used to move, rotate and scale this sphere.
*/
void Gizmos::drawWireSphere(const Vector3 ¢er, float radius, const Vector4 &color, const Matrix4 &transform) {
drawCircle(center, radius, color, transform);
Matrix4 t = transform * Matrix4(Quaternion(Vector3(1, 0, 0), 90).toMatrix());
drawCircle(center, radius, color, t);
t = transform * Matrix4(Quaternion(Vector3(0, 0, 1), 90).toMatrix());
drawCircle(center, radius, color, t);
}
/*!
Draws a wire capsule in the 3D space with the specified \a center, \a radius, \a height and \a color in the 3D space.
Parameter \a transform can be used to move, rotate and scale this capsule.
*/
void Gizmos::drawWireCapsule(const Vector3 ¢er, float radius, float height, const Vector4 &color, const Matrix4 &transform) {
float half = height * 0.5f - radius;
{
Vector3 cap(0, half, 0);
Matrix4 t;
t = transform * Matrix4(cap, Quaternion(), Vector3(1.0f));
drawCircle(Vector3(), radius, color, t);
t = transform * Matrix4(cap, Quaternion(Vector3(-90, 0, 0)), Vector3(1.0f));
drawArc(Vector3(), radius, 0, 180, color, t);
t = transform * Matrix4(cap, Quaternion(Vector3(-90, 90, 0)), Vector3(1.0f));
drawArc(Vector3(), radius, 0, 180, color, t);
}
{
Vector3 cap(0,-half, 0);
Matrix4 t(transform);
t = transform * Matrix4(cap, Quaternion(), Vector3(1.0f));
drawCircle(Vector3(), radius, color, t);
t = transform * Matrix4(cap, Quaternion(Vector3(90, 0, 0)), Vector3(1.0f));
drawArc(Vector3(), radius, 0, 180, color, t);
t = transform * Matrix4(cap, Quaternion(Vector3(90, 90, 0)), Vector3(1.0f));
drawArc(Vector3(), radius, 0, 180, color, t);
}
Vector3Vector points = { Vector3( radius, half, 0),
Vector3( radius,-half, 0),
Vector3(-radius, half, 0),
Vector3(-radius,-half, 0),
Vector3( 0, half, radius),
Vector3( 0,-half, radius),
Vector3( 0, half,-radius),
Vector3( 0,-half,-radius)};
IndexVector indices = {0, 1, 2, 3, 4, 5, 6, 7};
drawLines(points, indices, color, transform);
}