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
mod generated;
#[cfg(feature = "nalgebra")]
type Vec3 = nalgebra::Vector3<f32>;
#[cfg(feature = "nalgebra")]
type Vec2 = nalgebra::Vector2<f32>;
#[cfg(feature = "glam")]
type Vec3 = glam::Vec3;
#[cfg(feature = "glam")]
type Vec2 = glam::Vec2;
pub trait Geometry {
fn num_faces(&self) -> usize;
fn num_vertices_of_face(&self, face: usize) -> usize;
fn position(&self, face: usize, vert: usize) -> [f32; 3];
fn normal(&self, face: usize, vert: usize) -> [f32; 3];
fn tex_coord(&self, face: usize, vert: usize) -> [f32; 2];
fn set_tangent(
&mut self,
tangent: [f32; 3],
_bi_tangent: [f32; 3],
_f_mag_s: f32,
_f_mag_t: f32,
bi_tangent_preserves_orientation: bool,
face: usize,
vert: usize,
) {
let sign = if bi_tangent_preserves_orientation {
1.0
} else {
-1.0
};
self.set_tangent_encoded([tangent[0], tangent[1], tangent[2], sign], face, vert);
}
fn set_tangent_encoded(&mut self, _tangent: [f32; 4], _face: usize, _vert: usize) {}
}
pub fn generate_tangents<I: Geometry>(geometry: &mut I) -> bool {
unsafe { generated::genTangSpace(geometry, 180.0) }
}
fn get_position<I: Geometry>(geometry: &mut I, index: usize) -> Vec3 {
let (face, vert) = index_to_face_vert(index);
geometry.position(face, vert).into()
}
fn get_tex_coord<I: Geometry>(geometry: &mut I, index: usize) -> Vec3 {
let (face, vert) = index_to_face_vert(index);
let tex_coord: Vec2 = geometry.tex_coord(face, vert).into();
#[cfg(feature = "nalgebra")]
let val = tex_coord.insert_row(2, 1.0);
#[cfg(feature = "glam")]
let val = tex_coord.extend(1.0);
val
}
fn get_normal<I: Geometry>(geometry: &mut I, index: usize) -> Vec3 {
let (face, vert) = index_to_face_vert(index);
geometry.normal(face, vert).into()
}
fn index_to_face_vert(index: usize) -> (usize, usize) {
(index >> 2, index & 0x3)
}
fn face_vert_to_index(face: usize, vert: usize) -> usize {
face << 2 | vert & 0x3
}