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
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
//! This module generates meshes
use std::{
collections::HashMap,
f32::consts::TAU,
fs::File,
io::{BufReader, Read},
};
use lin_alg::f32::Vec3;
use crate::{
graphics::UP_VEC,
types::{Mesh, Vertex},
};
/// Rotate a 2d vector counter-clockwise a given angle.
fn rotate_vec_2d(vec: [f32; 2], θ: f32) -> [f32; 2] {
// Self-contained 2d rotation matrix (col-maj)
let (sin_θ, cos_θ) = θ.sin_cos();
let mat = [cos_θ, sin_θ, -sin_θ, cos_θ];
[
vec[0] * mat[0] + vec[1] * mat[2],
vec[0] * mat[1] + vec[1] * mat[3],
]
}
impl Mesh {
// /// Create a triangular face, with no volume. Only visible from one side.
// /// Useful for building a grid surface like terrain, or a surface plot.
// pub fn new_tri_face(a: [f32; 3], b: [f32; 3], c: [f32; 3]) -> Self {
// let norm = Vec3::new(0., 0., -1.);
// let vertices = vec![
// Vertex::new(a, norm),
// Vertex::new(b, norm),
// Vertex::new(c, norm),
// ];
// let indices = vec![0, 1, 2];
// Self {
// vertices,
// indices,
// material: 0,
// }
// }
/// Create a grid surface of triangles.
/// Useful for building a grid surface like terrain, or a surface plot.
/// `grid`'s outer vec is rows; inner vec is col-associated values within that
/// row. The grid is evenly-spaced.
/// todo: You should draw both sides.
/// Create a sided surface. Useful as terrain, or as a 2-sided plot.
/// Note that the grid is viewed here as x, z, with values in y direction, to be compatible
/// with the convention of Z-up used elsewhere.
///
/// Points are (x, y, z), with Z being the vertical component. Their indices correspond to how they're
/// tied together in a mesh.
// pub fn new_surface(grid: &Vec<Vec<f32>>, start: f32, step: f32, two_sided: bool) -> Self {
pub fn new_surface(points: &Vec<Vec<Vec3>>, two_sided: bool) -> Self {
let mut vertices = Vec::new();
let mut indices = Vec::new();
// let mut x = start;
let mut this_vert_i = 0;
// Note: Y is
for (i, rows) in points.into_iter().enumerate() {
for (j, point) in rows.into_iter().enumerate() {
let x = point.x;
let y = point.z; // Swap y and z coords.
let z = point.y;
// for (i, row) in posits.iter().enumerate() {
// let mut z = start;
// for (j, y_posit) in row.into_iter().enumerate() {
vertices.push(Vertex::new([x, y, z], Vec3::new_zero()));
// To understand how we set up the triangles (index buffer),
// it's best to draw it out.
// Upper triangle: This exists for every vertex except
// the bottom and right edges.
// (grid.length is num_rows)
if i != points.len() - 1 && j != rows.len() - 1 {
indices.append(&mut vec![
this_vert_i,
this_vert_i + points.len(),
this_vert_i + 1,
]);
}
// Lower triangle: This exists for every vertex except
// the top and left edges.
if i != 0 && j != 0 {
indices.append(&mut vec![
this_vert_i,
this_vert_i - points.len(),
this_vert_i - 1,
]);
}
// z += step;
this_vert_i += 1;
// }
}
// x += step;
}
// Now that we've populated our vertices, update their normals.
for i in 0..indices.len() / 3 {
let tri_start_i = i * 3;
// Find the vertices that make up each triangle.
let vert0 = vertices[indices[tri_start_i]];
let vert1 = vertices[indices[tri_start_i + 1]];
let vert2 = vertices[indices[tri_start_i + 2]];
// Convert from arrays to Vec3.
let v0 = Vec3::new(vert0.position[0], vert0.position[1], vert0.position[2]);
let v1 = Vec3::new(vert1.position[0], vert1.position[1], vert1.position[2]);
let v2 = Vec3::new(vert2.position[0], vert2.position[1], vert2.position[2]);
let norm = (v2 - v0).to_normalized().cross((v1 - v0).to_normalized());
// todo: DRY on this indexing.
vertices[indices[tri_start_i]].normal = norm;
vertices[indices[tri_start_i + 1]].normal = norm;
vertices[indices[tri_start_i + 1]].normal = norm;
}
// If dual-sided, We need to replicate vertices, since the normal will be opposite.
// Then, update the index buffer with these new vertices, using the opposite triangle order.
if two_sided {
let orig_vert_len = vertices.len();
let mut vertices_other_side = Vec::new();
for vertex in &vertices {
let mut new_vertex = vertex.clone();
new_vertex.normal *= -1.;
vertices_other_side.push(new_vertex);
}
vertices.append(&mut vertices_other_side);
let mut new_indices = Vec::new();
for i in 0..indices.len() / 3 {
let tri_start_i = i * 3;
// Opposite direction of first-side indices.
new_indices.push(indices[tri_start_i] + orig_vert_len);
new_indices.push(indices[tri_start_i + 2] + orig_vert_len);
new_indices.push(indices[tri_start_i + 1] + orig_vert_len);
}
indices.append(&mut new_indices);
}
Self {
vertices,
indices,
material: 0,
}
}
/// Creates an isosphere, from subdividing an icosahedron. 2-3 subdivisions works well for most uses.
pub fn new_sphere(radius: f32, mut subdivisions: u32) -> Self {
if subdivisions > 4 {
println!(
"Warning: Sphere subdivisions > 4 is not allowed due to extreme performance cost. Setting to 4."
);
subdivisions = 4;
}
let mut vertices = Vec::new();
let mut indices = Vec::new();
// Create the initial icosahedron.
// 't' is the golden ratio.
let t = (1.0 + 5.0_f32.sqrt()) / 2.0;
let mut initial_points = vec![
Vec3::new(-1.0, t, 0.0),
Vec3::new(1.0, t, 0.0),
Vec3::new(-1.0, -t, 0.0),
Vec3::new(1.0, -t, 0.0),
Vec3::new(0.0, -1.0, t),
Vec3::new(0.0, 1.0, t),
Vec3::new(0.0, -1.0, -t),
Vec3::new(0.0, 1.0, -t),
Vec3::new(t, 0.0, -1.0),
Vec3::new(t, 0.0, 1.0),
Vec3::new(-t, 0.0, -1.0),
Vec3::new(-t, 0.0, 1.0),
];
// Normalize each vertex to lie on the sphere and scale by the radius.
for point in &mut initial_points {
*point = point.to_normalized() * radius;
vertices.push(Vertex::new(point.to_arr(), point.to_normalized()));
}
// The 20 faces of the icosahedron defined as triangles (each index refers to a vertex).
let mut faces = vec![
[0, 11, 5],
[0, 5, 1],
[0, 1, 7],
[0, 7, 10],
[0, 10, 11],
[1, 5, 9],
[5, 11, 4],
[11, 10, 2],
[10, 7, 6],
[7, 1, 8],
[3, 9, 4],
[3, 4, 2],
[3, 2, 6],
[3, 6, 8],
[3, 8, 9],
[4, 9, 5],
[2, 4, 11],
[6, 2, 10],
[8, 6, 7],
[9, 8, 1],
];
// Cache to avoid duplicating vertices at the midpoints.
let mut middle_point_cache = HashMap::<(usize, usize), usize>::new();
// Helper closure to get the midpoint between two vertices.
// The midpoint is computed, normalized to the sphere and added to the vertex list.
let mut get_middle_point = |a: usize, b: usize| -> usize {
let key = if a < b { (a, b) } else { (b, a) };
if let Some(&index) = middle_point_cache.get(&key) {
return index;
}
let point_a = Vec3::from_slice(&vertices[a].position).unwrap();
let point_b = Vec3::from_slice(&vertices[b].position).unwrap();
let middle = (point_a + point_b) * 0.5;
let normalized = middle.to_normalized() * radius;
let index = vertices.len();
vertices.push(Vertex::new(normalized.to_arr(), normalized.to_normalized()));
middle_point_cache.insert(key, index);
index
};
// Subdivide each triangular face.
// For each subdivision iteration, every triangle is split into 4 smaller ones.
for _ in 0..subdivisions {
let mut new_faces = Vec::new();
for tri in &faces {
let a = tri[0];
let b = tri[1];
let c = tri[2];
let ab = get_middle_point(a, b);
let bc = get_middle_point(b, c);
let ca = get_middle_point(c, a);
new_faces.push([a, ca, ab]);
new_faces.push([b, ab, bc]);
new_faces.push([c, bc, ca]);
new_faces.push([ab, ca, bc]);
}
faces = new_faces;
}
// Flatten the faces into the indices vector.
for face in &mut faces {
if subdivisions % 2 == 0 {
let first = face[0];
face[0] = face[1];
face[1] = first;
}
indices.extend_from_slice(face);
}
Self {
vertices,
indices,
material: 0,
}
}
/// Create a UV sphere mesh. A higher number of latitudes and longitudes results in a
/// a smoother sphere.
pub fn new_sphere_uv(radius: f32, num_lats: usize, num_lons: usize) -> Self {
let mut vertices = Vec::new();
// We use faces to construct indices (triangles)
let mut faces = Vec::new();
let mut indices = Vec::new();
// In radians
let lat_size = TAU / (2. * num_lats as f32);
let lon_size = TAU / num_lons as f32;
let mut current_i = 0;
// Bottom vertex and faces
vertices.push(Vertex::new([0., -radius, 0.], Vec3::new(0., -1., 0.)));
current_i += 1;
// Faces connected to the bottom vertex.
for k in 0..num_lons {
if k == num_lons - 1 {
indices.append(&mut vec![0, k + 2 - num_lons, k + 1]);
} else {
indices.append(&mut vec![0, k + 2, k + 1]);
}
}
// Don't include the top or bottom (0, TAU/2) angles in lats.
for i in 1..num_lats {
let θ = i as f32 * lat_size;
for j in 0..num_lons {
let φ = j as f32 * lon_size;
// https://en.wikipedia.org/wiki/Spherical_coordinate_system
let x = radius * φ.cos() * θ.sin();
let y = radius * φ.sin() * θ.sin();
let z = radius * θ.cos();
vertices.push(Vertex::new([x, y, z], Vec3::new(x, y, z).to_normalized()));
if i < num_lats - 1 {
// In CCW order
if j == num_lons - 1 {
faces.push([
current_i,
current_i + 1 - num_lons,
current_i + 1,
current_i + num_lons,
]);
} else {
faces.push([
current_i,
current_i + 1,
current_i + num_lons + 1,
current_i + num_lons,
]);
}
}
current_i += 1;
}
}
// Top vertex and faces
vertices.push(Vertex::new([0., radius, 0.], Vec3::new(0., 1., 0.)));
// Faces connected to the bottom vertex.
let top_ring_start_i = current_i - num_lons;
// todo: There's a rougue internal triangle on both the top and bottom caps, but it
// todo does'nt appear to be visible from the outside. Possibly related: The caps look wrong.
for k in 0..num_lons {
if k == num_lons - 1 {
indices.append(&mut vec![current_i, top_ring_start_i + k, top_ring_start_i]);
} else {
indices.append(&mut vec![
current_i,
top_ring_start_i + k,
top_ring_start_i + k + 1,
]);
}
}
for f in faces {
indices.append(&mut vec![f[0], f[1], f[2], f[0], f[2], f[3]]);
}
Self {
vertices,
indices,
material: 0,
}
}
// todo: Fill this in.
// todo: DRY with cylinder
pub fn new_ring(len: f32, radius_inner: f32, radius_outer: f32, num_sides: usize) -> Self {
let angle_between_vertices = TAU / num_sides as f32;
let mut circle_vertices_inner = Vec::new();
for i in 0..num_sides {
circle_vertices_inner.push(rotate_vec_2d(
[radius_inner, 0.],
i as f32 * angle_between_vertices,
));
}
// todo DRY
let mut circle_vertices_outer = Vec::new();
for i in 0..num_sides {
circle_vertices_outer.push(rotate_vec_2d(
[radius_outer, 0.],
i as f32 * angle_between_vertices,
));
}
let half_len = len * 0.5;
let mod_ = 2 * num_sides;
let mut vertices = Vec::new();
let mut indices = Vec::new();
let mut i_vertex = 0;
// Set up the sides
for vert in &circle_vertices_inner {
// The number of faces is the number of angles - 1.
// Triangle 1: This top, this bottom, next top.
indices.append(&mut vec![i_vertex, i_vertex + 1, (i_vertex + 2) % mod_]);
// Triangle 2: This bottom, next bottom, next top.
indices.append(&mut vec![
// Note: Order swapped from outer.
i_vertex + 1,
(i_vertex + 2) % mod_,
(i_vertex + 3) % mod_,
]);
// On edge face, top
vertices.push(Vertex::new(
[vert[0], half_len, vert[1]],
Vec3::new(vert[0], 0., vert[1]).to_normalized(),
));
i_vertex += 1;
// On edge face, bottom
vertices.push(Vertex::new(
[vert[0], -half_len, vert[1]],
Vec3::new(vert[0], 0., vert[1]).to_normalized(),
));
i_vertex += 1;
}
// todo DRY!
for vert in &circle_vertices_outer {
// The number of faces is the number of angles - 1.
// Triangle 1: This top, this bottom, next top.
indices.append(&mut vec![i_vertex, i_vertex + 1, (i_vertex + 2) % mod_]);
// Triangle 2: This bottom, next bottom, next top.
indices.append(&mut vec![
i_vertex + 1,
(i_vertex + 3) % mod_,
(i_vertex + 2) % mod_,
]);
// On edge face, top
vertices.push(Vertex::new(
[vert[0], half_len, vert[1]],
Vec3::new(vert[0], 0., vert[1]).to_normalized(),
));
i_vertex += 1;
// On edge face, bottom
vertices.push(Vertex::new(
[vert[0], -half_len, vert[1]],
Vec3::new(vert[0], 0., vert[1]).to_normalized(),
));
i_vertex += 1;
}
let top_anchor = i_vertex;
let bottom_anchor = i_vertex + 1;
// for (j, vert) in circle_vertices_inner.iter().enumerate() {
// // We need num_sides - 2 triangles using this anchor-vertex algorithm.
// if j != 0 && j != num_sides - 1 {
// indices.append(&mut vec![top_anchor, i_vertex, i_vertex + 2]);
// // We need CCW triangles for both, so reverse order on the bottom face.
// indices.append(&mut vec![bottom_anchor, i_vertex + 3, i_vertex + 1]);
// }
//
// // Top face
// vertices.push(Vertex::new([vert[0], half_len, vert[1]], UP_VEC));
// i_vertex += 1;
//
// // Bottom face
// vertices.push(Vertex::new([vert[0], -half_len, vert[1]], -UP_VEC));
// i_vertex += 1;
// }
Self {
vertices,
indices,
material: 0,
}
}
/// Create a box (rectangular prism) mesh.
pub fn new_box(len_x: f32, len_y: f32, len_z: f32) -> Self {
let x = len_x / 2.;
let y = len_y / 2.;
let z = len_z / 2.;
// Aft face
let abl = [-x, -y, -z];
let abr = [x, -y, -z];
let atr = [x, y, -z];
let atl = [-x, y, -z];
// Forward face
let fbl = [-x, -y, z];
let fbr = [x, -y, z];
let ftr = [x, y, z];
let ftl = [-x, y, z];
// Normal vectors
let aft = Vec3::new(0., 0., -1.);
let fwd = Vec3::new(0., 0., 1.);
let l = Vec3::new(-1., 0., 0.);
let r = Vec3::new(1., 0., 0.);
let t = Vec3::new(0., 1., 0.);
let b = Vec3::new(0., -1., 0.);
let vertices = vec![
// Aft
Vertex::new(abl, aft),
Vertex::new(abr, aft),
Vertex::new(atr, aft),
Vertex::new(atl, aft),
// Fwd
Vertex::new(fbl, fwd),
Vertex::new(ftl, fwd),
Vertex::new(ftr, fwd),
Vertex::new(fbr, fwd),
// Left
Vertex::new(fbl, l),
Vertex::new(abl, l),
Vertex::new(atl, l),
Vertex::new(ftl, l),
// Right
Vertex::new(abr, r),
Vertex::new(fbr, r),
Vertex::new(ftr, r),
Vertex::new(atr, r),
// Top
Vertex::new(atl, t),
Vertex::new(atr, t),
Vertex::new(ftr, t),
Vertex::new(ftl, t),
// Bottom
Vertex::new(abl, b),
Vertex::new(fbl, b),
Vertex::new(fbr, b),
Vertex::new(abr, b),
];
let faces = [
[0, 1, 2, 3],
[4, 5, 6, 7],
[8, 9, 10, 11],
[12, 13, 14, 15],
[16, 17, 18, 19],
[20, 21, 22, 23],
];
let mut indices = Vec::new();
for face in &faces {
indices.append(&mut vec![
face[0], face[1], face[2], face[0], face[2], face[3],
]);
}
Self {
vertices,
indices,
material: 0,
}
}
/// Create a tetrahedron mesh
pub fn new_tetrahedron(side_len: f32) -> Self {
let c = side_len / 2.;
let v_0 = [c, c, c];
let v_1 = [c, -c, -c];
let v_2 = [-c, c, -c];
let v_3 = [-c, -c, c];
// Note: For tetrahedrons, the normals are the corners of the cube we
// didn't use for vertices.
let n_0 = Vec3::new(1., 1., -1.).to_normalized();
let n_1 = Vec3::new(1., -1., 1.).to_normalized();
let n_2 = Vec3::new(-1., 1., 1.).to_normalized();
let n_3 = Vec3::new(-1., -1., -1.).to_normalized();
let vertices = vec![
// Face 0
Vertex::new(v_0, n_0),
Vertex::new(v_2, n_0),
Vertex::new(v_1, n_0),
// Face 1
Vertex::new(v_0, n_1),
Vertex::new(v_1, n_1),
Vertex::new(v_3, n_1),
// Face 2
Vertex::new(v_0, n_2),
Vertex::new(v_3, n_2),
Vertex::new(v_2, n_2),
// Face 3
Vertex::new(v_1, n_3),
Vertex::new(v_2, n_3),
Vertex::new(v_3, n_3),
];
// These indices define faces by triangles. (each 3 represent a triangle, starting at index 0.
// Indices are arranged CCW, from front of face
// Note that because we're using "hard" lighting on faces, we can't repeat any vertices, since
// they each have a different normal.
#[rustfmt::skip]
// let indices: &[u32] = &[
let indices = vec![
0, 1, 2,
3, 4, 5,
6, 7, 8,
9, 10, 11,
];
Self {
vertices,
indices,
// vertex_buffer: Vec<usize>,
// index_buffer: Vec<usize>,
// num_elements: u32,
material: 0,
}
}
/// Create a cylinder
pub fn new_cylinder(len: f32, radius: f32, num_sides: usize) -> Self {
let angle_between_vertices = TAU / num_sides as f32;
let mut circle_vertices = Vec::new();
for i in 0..num_sides {
circle_vertices.push(rotate_vec_2d(
[radius, 0.],
i as f32 * angle_between_vertices,
));
}
let half_len = len * 0.5;
let mod_ = 2 * num_sides;
let mut vertices = Vec::new();
let mut indices = Vec::new();
let mut i_vertex = 0;
// Set up the sides
for vert in &circle_vertices {
// The number of faces is the number of angles - 1.
// Triangle 1: This top, this bottom, next top.
indices.append(&mut vec![i_vertex, i_vertex + 1, (i_vertex + 2) % mod_]);
// Triangle 2: This bottom, next bottom, next top.
indices.append(&mut vec![
i_vertex + 1,
(i_vertex + 3) % mod_,
(i_vertex + 2) % mod_,
]);
// On edge face, top
vertices.push(Vertex::new(
[vert[0], half_len, vert[1]],
Vec3::new(vert[0], 0., vert[1]).to_normalized(),
));
i_vertex += 1;
// On edge face, bottom
vertices.push(Vertex::new(
[vert[0], -half_len, vert[1]],
Vec3::new(vert[0], 0., vert[1]).to_normalized(),
));
i_vertex += 1;
}
let top_anchor = i_vertex;
let bottom_anchor = i_vertex + 1;
for (j, vert) in circle_vertices.iter().enumerate() {
// We need num_sides - 2 triangles using this anchor-vertex algorithm.
if j != 0 && j != num_sides - 1 {
indices.append(&mut vec![top_anchor, i_vertex, i_vertex + 2]);
// We need CCW triangles for both, so reverse order on the bottom face.
indices.append(&mut vec![bottom_anchor, i_vertex + 3, i_vertex + 1]);
}
// Top face
vertices.push(Vertex::new([vert[0], half_len, vert[1]], UP_VEC));
i_vertex += 1;
// Bottom face
vertices.push(Vertex::new([vert[0], -half_len, vert[1]], -UP_VEC));
i_vertex += 1;
}
Self {
vertices,
indices,
material: 0,
}
}
pub fn new_pyramid(len: f32, radius: f32, num_sides: usize) -> Self {
// Note: DRY with cylinder; this is similar, but one of the faces consists of a single, central point.
let angle_between_vertices = TAU / num_sides as f32;
let mut circle_vertices = Vec::new();
for i in 0..num_sides {
circle_vertices.push(rotate_vec_2d(
[radius, 0.],
i as f32 * angle_between_vertices,
));
}
let half_len = len * 0.5;
let mut vertices = Vec::new();
let mut indices = Vec::new();
let tip = 0;
let mut i_vertex = 0;
// A central point; the tip of the arrow.
vertices.push(Vertex::new([0., half_len, 0.], UP_VEC));
i_vertex += 1;
indices.push(tip);
// Set up the sides.
for (j, vert) in circle_vertices.iter().enumerate() {
// The number of faces is the number of angles - 1.
let next = if j == num_sides - 1 {
i_vertex + 1 - num_sides // Wrap to the first.
} else {
i_vertex + 1
};
// Triangle: This edge, next edge, central;
indices.append(&mut vec![i_vertex, next, tip]);
vertices.push(Vertex::new(
[vert[0], -half_len, vert[1]],
Vec3::new(vert[0], len, vert[1]).to_normalized(),
));
i_vertex += 1;
}
let bottom_anchor = i_vertex; // An arbitrary point shared by all bottom triangles.
// Set up the bottom face.
for (j, vert) in circle_vertices.iter().enumerate() {
// We need num_sides - 2 triangles using this anchor-vertex algorithm.
if j != 0 && j != num_sides - 1 {
indices.append(&mut vec![bottom_anchor, i_vertex + 1, i_vertex]);
}
vertices.push(Vertex::new([vert[0], -half_len, vert[1]], -UP_VEC));
i_vertex += 1;
}
Self {
vertices,
indices,
material: 0,
}
}
/// Create an arrow, oriented up.
pub fn new_arrow(len: f32, radius: f32, num_sides: usize) -> Self {
let tip_offset = len / 2.;
let cylinder = Self::new_cylinder(len, radius, num_sides);
let tip = Self::new_pyramid(len * 0.5, radius * 3., num_sides);
let mut vertices = cylinder.vertices.clone();
let mut indices = cylinder.indices.clone();
for vertex in tip.vertices {
vertices.push(Vertex {
position: [
vertex.position[0],
vertex.position[1] + tip_offset,
vertex.position[2],
],
..vertex
});
}
let ci2 = cylinder.indices.clone();
let tip_start_index = ci2.iter().max().unwrap() + 1;
for index in tip.indices {
indices.push(index + tip_start_index);
}
Self {
vertices,
indices,
material: 0,
}
}
/// Load a mesh from obj data.
/// [File type description](https://en.wikipedia.org/wiki/Wavefront_.obj_file)
/// [Example](https://github.com/gfx-rs/wgpu/blob/master/wgpu/examples/skybox/main.rs)
pub fn from_obj(obj_data: &[u8]) -> Self {
let data = obj::ObjData::load_buf(&obj_data[..]).unwrap();
let mut vertices = Vec::new();
for object in data.objects {
for group in object.groups {
vertices.clear();
for poly in group.polys {
for end_index in 2..poly.0.len() {
for &index in &[0, end_index - 1, end_index] {
let obj::IndexTuple(position_id, _texture_id, normal_id) =
poly.0[index];
let n = data.normal[normal_id.unwrap()];
vertices.push(Vertex::new(
data.position[position_id],
Vec3::new(n[0], n[1], n[2]),
));
}
}
}
}
}
// todo: Is this right?
let indices = (0..vertices.len()).collect();
Self {
vertices,
indices,
material: 0,
}
}
/// Load a mesh from a obj file.
/// [File type description](https://en.wikipedia.org/wiki/Wavefront_.obj_file)
/// [Example](https://github.com/gfx-rs/wgpu/blob/master/wgpu/examples/skybox/main.rs)
pub fn from_obj_file(filename: &str) -> Self {
// todo: Add a way to load from an obj bytestream etc instead of just file API.
let f = File::open(filename).unwrap();
let mut reader = BufReader::new(f);
let mut file_buf = Vec::new();
reader.read_to_end(&mut file_buf).unwrap();
Self::from_obj(&file_buf)
}
}