Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdraw_triangles_intersections.cpp
More file actions
379 lines (313 loc) · 14.4 KB
/
draw_triangles_intersections.cpp
File metadata and controls
379 lines (313 loc) · 14.4 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
#include <array>
#include <sm/vec>
#include <sm/vvec>
#include <sm/algo>
#include <mplot/Visual.h>
#include <mplot/VisualModel.h>
#include <mplot/SphereVisual.h>
#include <mplot/VectorVisual.h>
using namespace mplot;
//! This class creates the vertices for a single triangle. It also draws some spheres and arrows
//! for coordinates and normal vectors, for illustration.
template<int glver = mplot::gl::version_4_1>
class trivis : public VisualModel<glver>
{
public:
trivis (const sm::vec<float> _offset = {0,0,0}) : mplot::VisualModel<glver>::VisualModel (_offset) {}
static constexpr bool switch_tri_indices_order = false;
//! Compute a triangle from 3 coordinates
void computeTriangle()
{
// v is the face normal
sm::vec<float> u1 = coords[0] - coords[1];
sm::vec<float> u2 = coords[1] - coords[2];
this->normal = u1.cross(u2);
this->normal.renormalize();
// Push corner vertices, colours and normals
for (unsigned int i = 0; i < 3U; ++i) {
this->vertex_push (this->coords[i], this->vertexPositions);
this->vertex_push (this->colours[i], this->vertexColors);
this->vertex_push (this->normal, this->vertexNormals);
}
// This is just index 0, 1, 2
this->indices.push_back (this->idx);
if constexpr (switch_tri_indices_order) { // for demo/debug
this->indices.push_back (this->idx + 2);
this->indices.push_back (this->idx + 1);
} else {
this->indices.push_back (this->idx + 1);
this->indices.push_back (this->idx + 2);
}
this->idx += 3; // Move the index counter on for the next drawing
}
//! Initialize vertex buffer objects and vertex array object.
void initializeVertices() {
// Compute the triangle
this->computeTriangle();
// Show indices/coords JUST from the triangle
sm::vvec<unsigned int> indvv;
indvv.set_from (this->indices);
this->addLabel (std::string("Index draw order: ") + indvv.str(), {0.0f, -0.6f, 0.0f}, mplot::TextFeatures(0.16f));
this->addLabel (std::string("Vtx 0 ") + coords[0].str(), coords[0] + sm::vec<float>{-0.3f, -0.2f, 0.0f}, mplot::TextFeatures(0.1f));
if constexpr (switch_tri_indices_order) { // for demo/debug
this->addLabel (std::string("Vtx 2 ") + coords[1].str(), coords[1] + sm::vec<float>{-0.3f, -0.2f, 0.0f}, mplot::TextFeatures(0.1f));
this->addLabel (std::string("Vtx 1 ") + coords[2].str(), coords[2] + sm::vec<float>{-0.3f, 0.2f, 0.0f}, mplot::TextFeatures(0.1f));
} else {
this->addLabel (std::string("Vtx 1 ") + coords[1].str(), coords[1] + sm::vec<float>{-0.3f, -0.2f, 0.0f}, mplot::TextFeatures(0.1f));
this->addLabel (std::string("Vtx 2 ") + coords[2].str(), coords[2] + sm::vec<float>{-0.3f, 0.2f, 0.0f}, mplot::TextFeatures(0.1f));
}
this->addLabel ("Vertex normals: " + this->normal.str(), {0, -0.9, 0}, mplot::TextFeatures(0.16));
// Add illustrative stuff
for (unsigned int i = 0; i < 3U; ++i) {
this->computeSphereGeo (this->coords[i], this->colours[i], 0.05f, 2);
this->computeArrow (this->coords[i], this->coords[i] + this->normal, this->colours[i], 0.015f);
}
}
//! The positions of the vertices of the triangle
std::array<sm::vec<float>, 3> coords = {
sm::vec<float>{0.0f, 0.0f, 0.0f},
sm::vec<float>{2.0f, 0.0f, 0.0f},
sm::vec<float>{0.0f, 2.0f, 0.0f}
};
std::array<std::array<float, 3>, 3> colours = { mplot::colour::firebrick, mplot::colour::orchid1, mplot::colour::navy };
sm::vec<float, 3> normal = {0.0f};
};
//! Like trivis, but with one extra vertex and drawing two triangles.
template<int glver = mplot::gl::version_4_1>
class doubletrivis : public VisualModel<glver>
{
public:
doubletrivis (const sm::vec<float, 3> _offset = {0.0f}) : mplot::VisualModel<glver>::VisualModel (_offset) {}
//! Compute two triangles from 4 corners
void computeTriangles()
{
// v is the face normal
sm::vec<float> u1 = coords[0] - coords[1];
sm::vec<float> u2 = coords[1] - coords[2];
this->normal = u1.cross(u2);
this->normal.renormalize();
// Push corner vertices, colours and normals
for (unsigned int i = 0; i < 4U; ++i) {
this->vertex_push (this->coords[i], this->vertexPositions);
this->vertex_push (this->colours[i], this->vertexColors);
this->vertex_push (this->normal, this->vertexNormals);
}
// First triangle 0, 1, 2
this->indices.push_back (this->idx);
this->indices.push_back (this->idx + 1);
this->indices.push_back (this->idx + 2);
// Second triangle 1, 3, 2
this->indices.push_back (this->idx + 1);
this->indices.push_back (this->idx + 3);
this->indices.push_back (this->idx + 2);
this->idx += 4; // Increase by the *number of vertices that we added*
}
//! Initialize vertex buffer objects and vertex array object.
void initializeVertices() {
// Compute the triangle
this->computeTriangles();
// Show indices/coords JUST from the triangble
sm::vvec<unsigned int> indvv;
indvv.set_from (this->indices);
this->addLabel (std::string("Index draw order: ") + indvv.str(), {0, -0.6, 0}, mplot::TextFeatures(0.16));
this->addLabel (std::string("Vtx 0 ") + coords[0].str(), coords[0] + sm::vec<float>{-0.3f, -0.2f, 0.0f}, mplot::TextFeatures(0.1));
this->addLabel (std::string("Vtx 1 ") + coords[1].str(), coords[1] + sm::vec<float>{-0.3f, -0.2f, 0.0f}, mplot::TextFeatures(0.1));
this->addLabel (std::string("Vtx 2 ") + coords[2].str(), coords[2] + sm::vec<float>{-0.3f, 0.2f, 0.0f}, mplot::TextFeatures(0.1));
this->addLabel (std::string("Vtx 3 ") + coords[3].str(), coords[3] + sm::vec<float>{-0.3f, 0.2f, 0.0f}, mplot::TextFeatures(0.1));
// Add illustrative stuff
for (unsigned int i = 0; i < 4U; ++i) {
this->computeSphereGeo (this->coords[i], this->colours[i], 0.05f, 2);
this->computeArrow (this->coords[i], this->coords[i] + this->normal, this->colours[i], 0.015f);
}
}
//! The positions of the vertices of the triangle
std::array<sm::vec<float>, 4> coords = {
sm::vec<float>{0, 0, 0},
sm::vec<float>{2, 0, 0},
sm::vec<float>{0, 2, 0},
sm::vec<float>{2, 2, 0}
};
std::array<std::array<float, 3>, 4> colours = {
mplot::colour::firebrick,
mplot::colour::orchid1,
mplot::colour::navy,
mplot::colour::lightblue2
};
sm::vec<float> normal = {0.0f};
};
//! This class creates the vertices for two triangles where you can see the colour difference -
//! this has to be made with 6 vertices, four of which share two locations.
template<int glver = mplot::gl::version_4_1>
class twocolourtri : public VisualModel<glver>
{
public:
twocolourtri (const sm::vec<float> _offset = {0,0,0}) : mplot::VisualModel<glver>::VisualModel (_offset) {}
//! Compute a triangle from 3 arbitrary corners
void computeTriangles()
{
// v is the face normal
sm::vec<float> u1 = coords[0] - coords[1];
sm::vec<float> u2 = coords[1] - coords[2];
this->normals[0] = u1.cross(u2);
this->normals[0].renormalize();
u1 = coords[3] - coords[4];
u2 = coords[4] - coords[5];
this->normals[1] = u1.cross(u2);
this->normals[1].renormalize();
// Push corner vertices, colours and normals
for (unsigned int i = 0; i < 6U; ++i) {
this->vertex_push (this->coords[i], this->vertexPositions);
this->vertex_push (this->colours[i], this->vertexColors);
this->vertex_push (this->normals[i<3?0:1], this->vertexNormals);
}
// First triangle 0, 1, 2
this->indices.push_back (this->idx);
this->indices.push_back (this->idx + 1);
this->indices.push_back (this->idx + 2);
// Second triangle 1, 3, 2
this->indices.push_back (this->idx + 3);
this->indices.push_back (this->idx + 4);
this->indices.push_back (this->idx + 5);
this->idx += 6; // Increase by the *number of vertices that we added*
}
//! Initialize vertex buffer objects and vertex array object.
void initializeVertices() {
// Compute the triangle
this->computeTriangles();
// Show indices/coords JUST from the triangble
sm::vvec<unsigned int> indvv;
indvv.set_from (this->indices);
this->addLabel (std::string("Index draw order: ") + indvv.str(), {0, -0.6, 0}, mplot::TextFeatures(0.16));
this->addLabel (std::string("Vtx 0 ") + coords[0].str(), coords[0] + sm::vec<float>{-0.3, -0.2, 0}, mplot::TextFeatures(0.1));
this->addLabel (std::string("Vtx 1 & 3 ") + coords[1].str(), coords[1] + sm::vec<float>{-0.3, -0.2, 0}, mplot::TextFeatures(0.1));
this->addLabel (std::string("Vtx 2 & 5") + coords[2].str(), coords[2] + sm::vec<float>{-0.3, 0.2, 0}, mplot::TextFeatures(0.1));
this->addLabel (std::string("Vtx 4 ") + coords[4].str(), coords[4] + sm::vec<float>{-0.3, 0.2, 0}, mplot::TextFeatures(0.1));
// Add illustrative stuff
for (unsigned int i = 0; i < 6U; ++i) {
this->computeSphereGeo (this->coords[i], this->colours[i], 0.05f, 2);
this->computeArrow (this->coords[i], this->coords[i] + this->normals[i<3?0:1], this->colours[i], 0.015f);
}
}
//! The positions of the vertices of the triangle
std::array<sm::vec<float>, 6> coords = {
sm::vec<float>{0, 0, -0.2},
sm::vec<float>{2, 0, 0},
sm::vec<float>{0, 2, 0},
sm::vec<float>{2, 0, 0},
sm::vec<float>{2, 2, -0.2},
sm::vec<float>{0, 2, 0}
};
std::array<std::array<float, 3>, 6> colours = {
mplot::colour::orchid1,
mplot::colour::firebrick,
mplot::colour::firebrick,
mplot::colour::navy,
mplot::colour::orchid1,
mplot::colour::navy
};
std::array<sm::vec<float>, 2> normals = { sm::vec<float>{0.0f}, sm::vec<float>{0.0f} };
};
int main()
{
mplot::Visual v(1024, 768, "Drawing with triangles");
v.lightingEffects();
auto tv = std::make_unique<trivis<>>(sm::vec<float>{0.0f});
v.bindmodel (tv);
tv->finalize();
[[maybe_unused]] auto tvp = v.addVisualModel (tv);
// A coord from which we instantiate a ray
sm::vec<> start = { 2, 0, 5 };
sm::vec<> dirn = { 0, 0, -10 };
sm::vec<> start_fr2 = { 1, .9, 5 };
sm::vec<> dirn_fr2 = { 0.5, -0.7, -10 };
sm::vec<> start_bh = { 0, 0, -5 };
sm::vec<> dirn_bh = { 1.5, 1.5, 10 };
auto sv = std::make_unique<mplot::SphereVisual<>>(start, 0.1, mplot::colour::goldenrod3);
v.bindmodel (sv);
sv->finalize();
v.addVisualModel (sv);
sv = std::make_unique<mplot::SphereVisual<>>(start_bh, 0.1, mplot::colour::goldenrod3);
v.bindmodel (sv);
sv->finalize();
v.addVisualModel (sv);
sv = std::make_unique<mplot::SphereVisual<>>(start_fr2, 0.1, mplot::colour::goldenrod3);
v.bindmodel (sv);
sv->finalize();
v.addVisualModel (sv);
auto vvm = std::make_unique<mplot::VectorVisual<float, 3>>(start);
v.bindmodel (vvm);
vvm->thevec = dirn;
vvm->vgoes = VectorGoes::FromOrigin;
vvm->thickness = 0.02f;
vvm->arrowhead_prop = 0.1f;
vvm->fixed_colour = true;
vvm->single_colour = mplot::colour::crimson;
vvm->addLabel ("Ray", {-0.8, -0.5, 0}, mplot::TextFeatures(0.1f));
vvm->finalize();
[[maybe_unused]] auto vvmp = v.addVisualModel (vvm);
vvm = std::make_unique<mplot::VectorVisual<float, 3>>(start_bh);
v.bindmodel (vvm);
vvm->thevec = dirn_bh;
vvm->vgoes = VectorGoes::FromOrigin;
vvm->thickness = 0.02f;
vvm->arrowhead_prop = 0.1f;
vvm->fixed_colour = true;
vvm->single_colour = mplot::colour::crimson;
vvm->addLabel ("Ray from behind", {-0.8, -0.5, 0}, mplot::TextFeatures(0.1f));
vvm->finalize();
[[maybe_unused]] auto vvmp2 = v.addVisualModel (vvm);
vvm = std::make_unique<mplot::VectorVisual<float, 3>>(start_fr2);
v.bindmodel (vvm);
vvm->thevec = dirn_fr2;
vvm->vgoes = VectorGoes::FromOrigin;
vvm->thickness = 0.02f;
vvm->arrowhead_prop = 0.1f;
vvm->fixed_colour = true;
vvm->single_colour = mplot::colour::crimson;
vvm->addLabel ("Ray from front through middle", {-0.8, -0.5, 0}, mplot::TextFeatures(0.1f));
vvm->finalize();
[[maybe_unused]] auto vvmp3 = v.addVisualModel (vvm);
tvp->make_navmesh();
auto vm = tvp->getViewMatrix();
auto vmi = vm.inverse();
auto start_wr = (vmi * start).less_one_dim(); // wr to tvp
std::cout << "start_wr = " << start_wr << std::endl;
auto [hit, ti] = tvp->navmesh->find_triangle_crossing (start_wr, dirn, vm);
if (ti == std::numeric_limits<uint32_t>::max()) {
std::cout << "NO HIT\n";
} else {
std::cout << "Indices: " << ti << std::endl;
std::cout << "Contains hit " << hit << std::endl;
sv = std::make_unique<mplot::SphereVisual<>>(hit, 0.07, mplot::colour::springgreen2);
v.bindmodel (sv);
sv->finalize();
v.addVisualModel (sv);
}
auto start_wr_fr2 = (vmi * start_fr2).less_one_dim(); // wr to tvp
auto [hit_fr2, ti_fr2] = tvp->navmesh->find_triangle_crossing (start_wr_fr2, dirn_fr2, vm);
if (ti_fr2 == std::numeric_limits<uint32_t>::max()) {
std::cout << "NO HIT\n";
} else {
std::cout << "Indices: " << ti_fr2 << std::endl;
std::cout << "Contains hit " << hit_fr2 << std::endl;
sv = std::make_unique<mplot::SphereVisual<>>(hit_fr2, 0.07, mplot::colour::springgreen2);
v.bindmodel (sv);
sv->finalize();
v.addVisualModel (sv);
}
auto start_wr_bh = (vmi * start_bh).less_one_dim(); // wr to tvp
std::cout << "start_wr = " << start_wr << std::endl;
auto [hit_bh, ti_bh] = tvp->navmesh->find_triangle_crossing (start_wr_bh, dirn_bh, vm);
if (ti_bh == std::numeric_limits<uint32_t>::max()) {
std::cout << "NO HIT\n";
} else {
std::cout << "Indices: " << ti_bh << std::endl;
std::cout << "Contains hit " << hit_bh << std::endl;
sv = std::make_unique<mplot::SphereVisual<>>(hit_bh, 0.07, mplot::colour::springgreen2);
v.bindmodel (sv);
sv->finalize();
v.addVisualModel (sv);
}
v.keepOpen();
return 0;
}
You can’t perform that action at this time.
