-
Notifications
You must be signed in to change notification settings - Fork 1
/
tessellator.cpp
706 lines (589 loc) · 25.7 KB
/
tessellator.cpp
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
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#include <tess/tessellator.h>
#include <tess/polygon_tessellator.h>
namespace tess
{
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
// rectangular-base pyramid
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
triangle_mesh tessellate_box(const vec3& extents)
{
const auto ext = vec2(extents.x, extents.y);
return tessellate_pyramid(ext, ext, extents.z);
}
triangle_mesh tessellate_pyramid(const vec2& top_extents, const vec2& bottom_extents, float height, const vec2& offset /*= {0.0f, 0.0f}*/)
{
// vertices are computed as follows
// 7+------+6
// /| /| y
// / | / | |
// / 3+---/--+2 |
// 4+------+5 / *---x
// | / | / /
// |/ |/ z
// 0+------+1
//
// v0(--+) v4(-++)
// v1(+-+) v5(+++)
// v2(+--) v6(++-)
// v3(---) v7(-+-)
const auto half_offset = offset * 0.5f;
const auto v0 = vec3(-0.5f * top_extents.x + half_offset.x, -0.5f * top_extents.y + half_offset.y, 0.5f * height);
const auto v1 = vec3( 0.5f * top_extents.x + half_offset.x, -0.5f * top_extents.y + half_offset.y, 0.5f * height);
const auto v2 = vec3( 0.5f * bottom_extents.x - half_offset.x, -0.5f * bottom_extents.y - half_offset.y, -0.5f * height);
const auto v3 = vec3(-0.5f * bottom_extents.x - half_offset.x, -0.5f * bottom_extents.y - half_offset.y, -0.5f * height);
const auto v4 = vec3(-0.5f * top_extents.x + half_offset.x, 0.5f * top_extents.y + half_offset.y, 0.5f * height);
const auto v5 = vec3( 0.5f * top_extents.x + half_offset.x, 0.5f * top_extents.y + half_offset.y, 0.5f * height);
const auto v6 = vec3( 0.5f * bottom_extents.x - half_offset.x, 0.5f * bottom_extents.y - half_offset.y, -0.5f * height);
const auto v7 = vec3(-0.5f * bottom_extents.x - half_offset.x, 0.5f * bottom_extents.y - half_offset.y, -0.5f * height);
const auto nxp = (v2-v1).cross(v5-v1).normalized();
const auto nxn = (v4-v0).cross(v3-v0).normalized();
const auto nyp = (v6-v5).cross(v4-v5).normalized();
const auto nyn = (v0-v1).cross(v2-v1).normalized();
triangle_mesh mesh;
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// vertices
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// +x
mesh.vertices.push_back({v2, nxp});
mesh.vertices.push_back({v6, nxp});
mesh.vertices.push_back({v5, nxp});
mesh.vertices.push_back({v1, nxp});
// +z
mesh.vertices.push_back({v1, vec3::UNIT_Z});
mesh.vertices.push_back({v5, vec3::UNIT_Z});
mesh.vertices.push_back({v4, vec3::UNIT_Z});
mesh.vertices.push_back({v0, vec3::UNIT_Z});
// +y
mesh.vertices.push_back({v5, nyp});
mesh.vertices.push_back({v6, nyp});
mesh.vertices.push_back({v7, nyp});
mesh.vertices.push_back({v4, nyp});
// -x
mesh.vertices.push_back({v0, nxn});
mesh.vertices.push_back({v4, nxn});
mesh.vertices.push_back({v7, nxn});
mesh.vertices.push_back({v3, nxn});
// -z
mesh.vertices.push_back({v3, -vec3::UNIT_Z});
mesh.vertices.push_back({v7, -vec3::UNIT_Z});
mesh.vertices.push_back({v6, -vec3::UNIT_Z});
mesh.vertices.push_back({v2, -vec3::UNIT_Z});
// -y
mesh.vertices.push_back({v0, nyn});
mesh.vertices.push_back({v3, nyn});
mesh.vertices.push_back({v2, nyn});
mesh.vertices.push_back({v1, nyn});
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// elements
// -----------------------------------------------------------------------------------------------------------------------------------------------------
for(unsigned int i = 0; i < 6; ++i)
{
mesh.elements.push_back(0+i*4);
mesh.elements.push_back(1+i*4);
mesh.elements.push_back(2+i*4);
mesh.elements.push_back(0+i*4);
mesh.elements.push_back(2+i*4);
mesh.elements.push_back(3+i*4);
}
return mesh;
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
// truncated cone
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
triangle_mesh tessellate_cylinder(float radius, float height,
int segment_count /*= 16*/, bool with_caps /*= true*/)
{
return tessellate_cone_slope_offset(radius, radius, height, vec2::ZERO, vec2::ZERO, vec2::ZERO, segment_count, with_caps);
}
triangle_mesh tessellate_cylinder_offset(float radius, float height, const vec2& offset,
int segment_count /*= 16*/, bool with_caps /*= true*/)
{
return tessellate_cone_slope_offset(radius, radius, height, vec2::ZERO, vec2::ZERO, offset, segment_count, with_caps);
}
triangle_mesh tessellate_cylinder_slope(float radius, float height, const vec2& top_slope_angles, const vec2& bottom_slope_angles,
int segment_count /*= 16*/, const bool with_caps /*= true*/)
{
return tessellate_cone_slope_offset(radius, radius, height, top_slope_angles, bottom_slope_angles, vec2::ZERO, segment_count, with_caps);
}
triangle_mesh tessellate_cylinder_slope_offset(float radius, float height, const vec2& top_slope_angles, const vec2& bottom_slope_angles, const vec2& offset,
int segment_count /*= 16*/, bool with_caps /*= true*/)
{
return tessellate_cone_slope_offset(radius, radius, height, top_slope_angles, bottom_slope_angles, offset, segment_count, with_caps);
}
triangle_mesh tessellate_cone(float top_radius, float bottom_radius, float height,
int segment_count /*= 16*/, bool with_caps /*= true*/)
{
return tessellate_cone_slope_offset(top_radius, bottom_radius, height, vec2::ZERO, vec2::ZERO, vec2::ZERO, segment_count, with_caps);
}
triangle_mesh tessellate_cone_offset(float top_radius, float bottom_radius, float height, const vec2& offset,
int segment_count /*= 16*/, bool with_caps /*= true*/)
{
return tessellate_cone_slope_offset(top_radius, bottom_radius, height, vec2::ZERO, vec2::ZERO, offset, segment_count, with_caps);
}
triangle_mesh tessellate_cone_slope(float top_radius, float bottom_radius, float height, const vec2& top_slope_angles, const vec2& bottom_slope_angles,
int segment_count /*= 16*/, const bool with_caps /*= true*/)
{
return tessellate_cone_slope_offset(top_radius, bottom_radius, height, top_slope_angles, bottom_slope_angles, vec2::ZERO, segment_count, with_caps);
}
triangle_mesh tessellate_cone_slope_offset(float top_radius, float bottom_radius, float height, const vec2& top_slope_angles, const vec2& bottom_slope_angles,
const vec2& offset, int segment_count /*= 16*/, bool with_caps /*= true*/)
{
vector<vec3> top_positions;
vector<vec3> bottom_positions;
top_positions.reserve(segment_count);
bottom_positions.reserve(segment_count);
const auto top_scale = math::equal(top_radius, 0.0f)? 1e-6f : top_radius;
const auto bottom_scale = math::equal(bottom_radius, 0.0f)? 1e-6f : bottom_radius;
const auto top_offset = vec3(offset.x*0.5f, offset.y*0.5f, height * 0.5f);
const auto bottom_offset = vec3(-offset.x*0.5f, -offset.y*0.5f, -height * 0.5f);
auto delta_angle = math::TWO_PI / static_cast<float>(segment_count);
auto angle = 0.0f;
const auto top_slope_quat = quat(vec3(top_slope_angles.x, top_slope_angles.y, 0));
const auto bottom_slope_quat = quat(vec3(bottom_slope_angles.x, bottom_slope_angles.y, 0));
for(int i = 0; i < segment_count; ++i)
{
auto x = math::cos(angle);
auto y = math::sin(angle);
// top position
auto top_pos = vec3(x*top_scale, y*top_scale, 0.0f);
top_pos = top_slope_quat.mul(top_pos);
top_pos += top_offset;
top_positions.push_back(top_pos);
// bottom position
auto bottom_pos = vec3(x*bottom_scale, y*bottom_scale, 0.0f);
bottom_pos = bottom_slope_quat.mul(bottom_pos);
bottom_pos += bottom_offset;
bottom_positions.push_back(bottom_pos);
angle += delta_angle;
}
triangle_mesh mesh;
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// vertices
// -----------------------------------------------------------------------------------------------------------------------------------------------------
int top_cap_start = 0;
int bottom_cap_start = 0;
if(with_caps)
{
// top cap
top_cap_start = mesh.vertices.size();
for(int i = 0; i < segment_count; ++i)
{
mesh.vertices.push_back({top_positions[i], top_slope_quat.mul(vec3::UNIT_Z)});
}
// bottom cap
bottom_cap_start = mesh.vertices.size();
for(int i = 0; i < segment_count; ++i)
{
mesh.vertices.push_back({bottom_positions[i], bottom_slope_quat.mul(-vec3::UNIT_Z)});
}
}
// body
int body_start = mesh.vertices.size();
for(int i = 0; i < segment_count; ++i)
{
const auto& b = bottom_positions[i];
const auto& b1 = bottom_positions[(i+1) % segment_count];
const auto& b0 = bottom_positions[(i-1) < 0? segment_count-1 : i-1];
const auto& t = top_positions[i];
const auto& t1 = top_positions[(i+1) % segment_count];
const auto& t0 = top_positions[(i-1) < 0? segment_count-1 : i-1];
const auto bn = (b-t).cross(b1-b0).normalized();
const auto tn = (b-t).cross(t1-t0).normalized();
mesh.vertices.push_back({b, bn});
mesh.vertices.push_back({t, tn});
}
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// elements
// -----------------------------------------------------------------------------------------------------------------------------------------------------
if(with_caps)
{
int cap_count = (segment_count - 2) / 2;
// top cap
for(int i = 0; i < cap_count; ++i)
{
mesh.elements.push_back(top_cap_start + i + 1);
mesh.elements.push_back(top_cap_start + segment_count - (i+1));
mesh.elements.push_back(top_cap_start + i);
mesh.elements.push_back(top_cap_start + segment_count - (i+2));
mesh.elements.push_back(top_cap_start + segment_count - (i+1));
mesh.elements.push_back(top_cap_start + i + 1);
}
// bottom cap
for(int i = 0; i < cap_count; ++i)
{
mesh.elements.push_back(bottom_cap_start + i);
mesh.elements.push_back(bottom_cap_start + segment_count - (i+1));
mesh.elements.push_back(bottom_cap_start + i + 1);
mesh.elements.push_back(bottom_cap_start + i + 1);
mesh.elements.push_back(bottom_cap_start + segment_count - (i+1));
mesh.elements.push_back(bottom_cap_start + segment_count - (i+2));
}
}
// body
int body_limit = segment_count * 2;
for(int i = 0; i < segment_count; ++i)
{
auto curr = i*2;
mesh.elements.push_back(body_start + (curr + 2) % body_limit);
mesh.elements.push_back(body_start + curr + 1);
mesh.elements.push_back(body_start + curr + 0);
mesh.elements.push_back(body_start + (curr + 2) % body_limit);
mesh.elements.push_back(body_start + (curr + 3) % body_limit);
mesh.elements.push_back(body_start + curr + 1);
}
return mesh;
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
// truncated toroid
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
triangle_mesh tessellate_circular_torus(float in_radius, float out_radius, float sweep_angle,
int segment_count /*= 16*/, int sweep_count /*= 8*/, bool with_caps /*= false*/)
{
vector<vec3> positions;
vector<vec3> section_centers;
auto xy_angle = 0.0f;
const auto xy_delta_angle = math::TWO_PI / static_cast<float>(segment_count);
auto sweep = 0.0f;
const auto sweep_delta_angle = sweep_angle / static_cast<float>(sweep_count);
for(int i = 0; i < sweep_count+1; ++i)
{
section_centers.push_back(vec3(out_radius * math::cos(sweep), out_radius * math::sin(sweep), 0.0f));
for(int j = 0; j < segment_count; ++j)
{
float section = out_radius + in_radius * math::sin(xy_angle);
positions.push_back({section * math::cos(sweep), section * math::sin(sweep), in_radius * math::cos(xy_angle)});
xy_angle += xy_delta_angle;
}
sweep += sweep_delta_angle;
}
triangle_mesh mesh;
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// vertices
// -----------------------------------------------------------------------------------------------------------------------------------------------------
int first_cap_start = 0;
int second_cap_start = 0;
if(with_caps)
{
// first cap
first_cap_start = mesh.vertices.size();
for(int i = 0; i < segment_count; ++i)
{
mesh.vertices.push_back({positions[i], -vec3::UNIT_Y});
}
// second cap
second_cap_start = mesh.vertices.size();
const auto& last = positions[positions.size()-1];
const auto& last_minus_1 = positions[positions.size()-2];
const auto& last_minus_2 = positions[positions.size()-3];
const auto cap_normal = (last - last_minus_1).cross(last_minus_2 - last_minus_1).normalized();
for(int i = 0; i < segment_count; ++i)
{
mesh.vertices.push_back({positions[positions.size()-segment_count+i], cap_normal});
}
}
// body
int body_start = mesh.vertices.size();
for(int i = 0; i < sweep_count+1; ++i)
{
for(int j = 0; j < segment_count; ++j)
{
const auto& v = positions[i*segment_count+j];
const auto n = (v - section_centers[i]).normalized();
mesh.vertices.push_back({v, n});
}
}
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// elements
// -----------------------------------------------------------------------------------------------------------------------------------------------------
if(with_caps)
{
int cap_count = (segment_count - 2) / 2;
// first cap
for(int i = 0; i < cap_count; ++i)
{
mesh.elements.push_back(first_cap_start + i);
mesh.elements.push_back(first_cap_start + segment_count - (i+1));
mesh.elements.push_back(first_cap_start + i + 1);
mesh.elements.push_back(first_cap_start + i + 1);
mesh.elements.push_back(first_cap_start + segment_count - (i+1));
mesh.elements.push_back(first_cap_start + segment_count - (i+2));
}
// second cap
for(int i = 0; i < cap_count; ++i)
{
mesh.elements.push_back(second_cap_start + i + 1);
mesh.elements.push_back(second_cap_start + segment_count - (i+1));
mesh.elements.push_back(second_cap_start + i);
mesh.elements.push_back(second_cap_start + segment_count - (i+2));
mesh.elements.push_back(second_cap_start + segment_count - (i+1));
mesh.elements.push_back(second_cap_start + i + 1);
}
}
// body
for(int i = 0; i < sweep_count; ++i)
{
for(int j = 0; j < segment_count; ++j)
{
int curr_start = i*segment_count;
int next_start = (i+1)*segment_count;
mesh.elements.push_back(body_start+curr_start+(j+1) % segment_count);
mesh.elements.push_back(body_start+next_start+j);
mesh.elements.push_back(body_start+curr_start+j);
mesh.elements.push_back(body_start+next_start+(j+1) % segment_count);
mesh.elements.push_back(body_start+next_start+j);
mesh.elements.push_back(body_start+curr_start+(j+1) % segment_count);
}
}
return mesh;
}
triangle_mesh tessellate_rectangular_torus(float in_radius, float out_radius, float in_height, float sweep_angle,
int sweep_count /*= 8*/, bool with_caps /*= false*/)
{
vector<vec3> positions;
auto sweep = 0.0f;
const auto sweep_delta_angle = sweep_angle / static_cast<float>(sweep_count);
const auto hh = in_height * 0.5f;
for(int i = 0; i < sweep_count+1; ++i)
{
const auto inner_scale = out_radius - in_radius;
const auto outer_scale = out_radius + in_radius;
// lower inner corner
positions.push_back({inner_scale * math::cos(sweep), inner_scale * math::sin(sweep), -hh});
// lower outer corner
positions.push_back({outer_scale * math::cos(sweep), outer_scale * math::sin(sweep), -hh});
// upper outer corner
positions.push_back({outer_scale * math::cos(sweep), outer_scale * math::sin(sweep), hh});
// upper inner corner
positions.push_back({inner_scale * math::cos(sweep), inner_scale * math::sin(sweep), hh});
sweep += sweep_delta_angle;
}
triangle_mesh mesh;
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// vertices
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// normals will be computed later for each triangle
int first_cap_start = 0;
int second_cap_start = 0;
if(with_caps)
{
// first cap
first_cap_start = mesh.vertices.size();
for(int i = 0; i < 4; ++i)
{
mesh.vertices.push_back({positions[i], {}});
}
// second cap
second_cap_start = mesh.vertices.size();
for(int i = 0; i < 4; ++i)
{
mesh.vertices.push_back({positions[positions.size()-4+i], {}});
}
}
// body
// add entire curved surfaces one after the other to make element indexing easier
int body_start = mesh.vertices.size();
for(int k = 0; k < 4; ++k)
{
for(int i = 0; i < sweep_count+1; ++i)
{
const auto base = i*4;
const auto curr = base + k;
const auto next = base + (k+1) % 4;
mesh.vertices.push_back({positions[curr], {}});
mesh.vertices.push_back({positions[next], {}});
}
}
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// elements
// -----------------------------------------------------------------------------------------------------------------------------------------------------
if(with_caps)
{
// first cap
mesh.elements.push_back(first_cap_start + 1);
mesh.elements.push_back(first_cap_start + 4 - 1);
mesh.elements.push_back(first_cap_start + 0);
mesh.elements.push_back(first_cap_start + 4 - 2);
mesh.elements.push_back(first_cap_start + 4 - 1);
mesh.elements.push_back(first_cap_start + 1);
// second cap
mesh.elements.push_back(second_cap_start + 0);
mesh.elements.push_back(second_cap_start + 4 - 1);
mesh.elements.push_back(second_cap_start + 1);
mesh.elements.push_back(second_cap_start + 1);
mesh.elements.push_back(second_cap_start + 4 - 1);
mesh.elements.push_back(second_cap_start + 4 - 2);
}
// body
for(int k = 0; k < 4; ++k)
{
for(int i = 0; i < sweep_count; ++i)
{
const auto base = 2*k*(sweep_count+1)+i*2;
const auto curr = body_start + base;
const auto next_curr = body_start + base + 2;
mesh.elements.push_back(next_curr);
mesh.elements.push_back(curr+1);
mesh.elements.push_back(curr);
mesh.elements.push_back(next_curr);
mesh.elements.push_back(next_curr+1);
mesh.elements.push_back(curr+1);
}
}
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// normals
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// flat surfaces: all vertices of the same triangle get the same normal
for(decltype(mesh.elements)::size_type i = 0; i < mesh.elements.size(); i+=3)
{
auto& v0 = mesh.vertices[mesh.elements[i+0]];
auto& v1 = mesh.vertices[mesh.elements[i+1]];
auto& v2 = mesh.vertices[mesh.elements[i+2]];
const auto n = (v1.position - v0.position).cross(v2.position - v0.position).normalized();
v0.normal = n;
v1.normal = n;
v2.normal = n;
}
return mesh;
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
// ellipsoid
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
triangle_mesh tessellate_dish(float radius, float height, int horizontal_count /*= 16*/, int vertical_count /*= 8*/, bool with_cap /*= false*/)
{
return tessellate_ellipsoid(vec3(radius, radius, height), horizontal_count, vertical_count, math::HALF_PI, with_cap);
}
triangle_mesh tessellate_sphere(float radius,
int horizontal_count /*= 16*/, int vertical_count /*= 16*/)
{
return tessellate_ellipsoid(vec3(radius, radius, radius), horizontal_count, vertical_count);
}
triangle_mesh tessellate_ellipsoid(const vec3& radii, int horizontal_count /*= 16*/, int vertical_count /*= 16*/,
float max_vertical_angle /*= math::PI*/, bool bottom_cap /*= true*/)
{
triangle_mesh mesh;
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// vertices
// -----------------------------------------------------------------------------------------------------------------------------------------------------
const auto delta_horizontal_angle = math::TWO_PI / static_cast<float>(horizontal_count);
const auto delta_vertical_angle = max_vertical_angle / static_cast<float>(vertical_count);
auto vertical_angle = delta_vertical_angle;
if(max_vertical_angle < math::PI)
{
++vertical_count;
}
// top vertex
mesh.vertices.push_back({vec3::UNIT_Z * radii.z, vec3::UNIT_Z});
// body
const auto inv_raddi_sqr = (radii * radii).reciprocal();
for(int v = 0; v < vertical_count-1; ++v)
{
float horizontal_angle = 0.0f;
for(int h = 0; h < horizontal_count; ++h)
{
vec3 pos;
pos.x = radii.x*math::sin(vertical_angle)*math::cos(horizontal_angle);
pos.y = radii.y*math::sin(vertical_angle)*math::sin(horizontal_angle);
pos.z = radii.z*math::cos(vertical_angle);
vec3 nrm = pos;
nrm *= inv_raddi_sqr;
mesh.vertices.push_back({pos, nrm.normalized()});
horizontal_angle += delta_horizontal_angle;
}
vertical_angle += delta_vertical_angle;
}
// bottom vertices
int bottom_cap_start = 0;
if(bottom_cap)
{
if(max_vertical_angle < math::PI)
{
bottom_cap_start = mesh.vertices.size();
for(int i = 0; i < horizontal_count; ++i)
{
mesh.vertices.push_back({mesh.vertices[bottom_cap_start-horizontal_count+i].position, -vec3::UNIT_Z});
}
}
else
{
mesh.vertices.push_back({-vec3::UNIT_Z * radii.z, -vec3::UNIT_Z});
}
}
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// elements
// -----------------------------------------------------------------------------------------------------------------------------------------------------
// top cap
int top_start = 1;
for(int i = 0; i < horizontal_count; ++i)
{
mesh.elements.push_back(0);
mesh.elements.push_back(top_start + i);
mesh.elements.push_back(top_start + (i+1) % horizontal_count);
}
// body
int body_start = top_start;
for(int j = 0; j < vertical_count-2; ++j)
{
for(int i = 0; i < horizontal_count; ++i)
{
mesh.elements.push_back(body_start + (i+1) % horizontal_count);
mesh.elements.push_back(body_start + i);
mesh.elements.push_back(body_start + i + horizontal_count);
mesh.elements.push_back(body_start + i + horizontal_count);
mesh.elements.push_back(body_start + (i+1) % horizontal_count + horizontal_count);
mesh.elements.push_back(body_start + (i+1) % horizontal_count);
}
body_start += horizontal_count;
}
// bottom cap
if(bottom_cap)
{
if(max_vertical_angle < math::PI)
{
int cap_count = (horizontal_count - 2) / 2;
for(int i = 0; i < cap_count; ++i)
{
mesh.elements.push_back(bottom_cap_start + i);
mesh.elements.push_back(bottom_cap_start + horizontal_count - (i+1));
mesh.elements.push_back(bottom_cap_start + i + 1);
mesh.elements.push_back(bottom_cap_start + i + 1);
mesh.elements.push_back(bottom_cap_start + horizontal_count - (i+1));
mesh.elements.push_back(bottom_cap_start + horizontal_count - (i+2));
}
}
else
{
int bottom_start = mesh.vertices.size() - 1 - horizontal_count;
for(int i = 0; i < horizontal_count; ++i)
{
mesh.elements.push_back(mesh.vertices.size()-1);
mesh.elements.push_back(bottom_start + (i+1) % horizontal_count);
mesh.elements.push_back(bottom_start + i);
}
}
}
return mesh;
}
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
// polygonal mesh
// ---------------------------------------------------------------------------------------------------------------------------------------------------------
static polygon_tessellator* s_polygon_tessellator = nullptr;
void tessellate_polygonal_begin()
{
if(s_polygon_tessellator != nullptr)
{
delete s_polygon_tessellator;
}
s_polygon_tessellator = new polygon_tessellator();
}
void tessellate_polygonal_add(const polygon& poly)
{
s_polygon_tessellator->add_polygon(poly);
}
triangle_mesh tessellate_polygonal_end()
{
auto ret = s_polygon_tessellator->end();
delete s_polygon_tessellator;
s_polygon_tessellator = nullptr;
return ret;
}
} // namespace tess