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
//! A module for constructing polygonal quad shapes.

extern crate cgmath;
extern crate glium;

use self::cgmath::*;
use crate::errors::ShapeCreationError;
use crate::vertex::Vertex;

/// A polygonal quad.
///
/// This object is constructed using a `QuadBuilder` object.
pub struct Quad {
    vertices: glium::vertex::VertexBufferAny,
}

/// Allows a `Quad` object to be passed as a source of vertices.
impl<'a> From<&'a Quad> for glium::vertex::VerticesSource<'a> {
    fn from(quad: &'a Quad) -> glium::vertex::VerticesSource<'a> {
        (&quad.vertices).into()
    }
}

/// Allows a `Quad` object to be passed as a source of indices.
impl<'a> Into<glium::index::IndicesSource<'a>> for &'a Quad {
    fn into(self) -> glium::index::IndicesSource<'a> {
        glium::index::IndicesSource::NoIndices {
            primitives: glium::index::PrimitiveType::TriangleStrip,
        }
    }
}

/// Responsible for building and returning a `Quad` object.
///
/// By default, the resultant polygon will be a regular quad of length 2
/// on each side, with its centre at the origin, and aligned to face the
/// negative Z-axis. The default position, size, and alignment can be
/// overridden using the transformation methods on this object. These
/// defaults are chosen such that the default quad can be used directly
/// as geometry for screen-aligned effects.
///
/// The resultant geometry is constructed to suit OpenGL defaults - assuming
/// a right-handed coordinate system, front-facing polygons are defined in
/// counter-clock-wise order. Vertex normals point in the direction of their
/// respective face (such that the shape appears faceted when lit). Vertex
/// texture coordinates define a planar-projection on the face.
pub struct QuadBuilder {
    matrix: cgmath::Matrix4<f32>,
}

impl Default for QuadBuilder {
    fn default() -> QuadBuilder {
        QuadBuilder {
            matrix: cgmath::Matrix4::<f32>::identity(),
        }
    }
}

impl QuadBuilder {
    /// Create a new `QuadBuilder` object.
    pub fn new() -> QuadBuilder {
        Default::default()
    }

    /// Apply a scaling transformation to the shape.
    ///
    /// The `scale`, `translate`, and `rotate` functions accumulate, and are
    /// not commutative. The transformation functions are intended to provide
    /// flexibility in model-space. For per-instance world-space transformations,
    /// one should prefer to share as few shapes as possible across multiple
    /// instances, and instead rely on uniform constants in the shader and/or
    /// instanced drawing.
    pub fn scale(mut self, x: f32, y: f32, z: f32) -> Self {
        self.matrix = cgmath::Matrix4::from_nonuniform_scale(x, y, z) * self.matrix;
        self
    }

    /// Apply a translation transformation to the shape.
    ///
    /// The `scale`, `translate`, and `rotate` functions accumulate, and are
    /// not commutative. The transformation functions are intended to provide
    /// flexibility in model-space. For per-instance world-space transformations,
    /// one should prefer to share as few shapes as possible across multiple
    /// instances, and instead rely on uniform constants in the shader and/or
    /// instanced drawing.
    pub fn translate(mut self, x: f32, y: f32, z: f32) -> Self {
        self.matrix = cgmath::Matrix4::from_translation([x, y, z].into()) * self.matrix;
        self
    }

    /// Apply a rotation transformation to the shape about the x-axis.
    ///
    /// The `scale`, `translate`, and `rotate` functions accumulate, and are
    /// not commutative. The transformation functions are intended to provide
    /// flexibility in model-space. For per-instance world-space transformations,
    /// one should prefer to share as few shapes as possible across multiple
    /// instances, and instead rely on uniform constants in the shader and/or
    /// instanced drawing.
    pub fn rotate_x(mut self, radians: f32) -> Self {
        self.matrix = cgmath::Matrix4::<f32>::from(cgmath::Matrix3::<f32>::from_angle_x(
            cgmath::Rad::<f32>(radians),
        )) * self.matrix;
        self
    }

    /// Apply a rotation transformation to the shape about the y-axis.
    ///
    /// The `scale`, `translate`, and `rotate` functions accumulate, and are
    /// not commutative. The transformation functions are intended to provide
    /// flexibility in model-space. For per-instance world-space transformations,
    /// one should prefer to share as few shapes as possible across multiple
    /// instances, and instead rely on uniform constants in the shader and/or
    /// instanced drawing.
    pub fn rotate_y(mut self, radians: f32) -> Self {
        self.matrix = cgmath::Matrix4::<f32>::from(cgmath::Matrix3::<f32>::from_angle_y(
            cgmath::Rad::<f32>(radians),
        )) * self.matrix;
        self
    }

    /// Apply a rotation transformation to the shape about the z-axis.
    ///
    /// The `scale`, `translate`, and `rotate` functions accumulate, and are
    /// not commutative. The transformation functions are intended to provide
    /// flexibility in model-space. For per-instance world-space transformations,
    /// one should prefer to share as few shapes as possible across multiple
    /// instances, and instead rely on uniform constants in the shader and/or
    /// instanced drawing.
    pub fn rotate_z(mut self, radians: f32) -> Self {
        self.matrix = cgmath::Matrix4::<f32>::from(cgmath::Matrix3::<f32>::from_angle_z(
            cgmath::Rad::<f32>(radians),
        )) * self.matrix;
        self
    }

    /// Build a new `Quad` object.
    pub fn build<F>(self, display: &F) -> Result<Quad, ShapeCreationError>
    where
        F: glium::backend::Facade,
    {
        let vertices =
            glium::vertex::VertexBuffer::<Vertex>::new(display, &self.build_vertices()?)?;

        Ok(Quad {
            vertices: glium::vertex::VertexBufferAny::from(vertices),
        })
    }

    /// Build the Quad vertices and return them in a vector.
    ///
    /// Useful if you wish to do other things with the vertices besides constructing
    /// a `Quad` object (e.g. unit testing, further processing, etc).
    pub fn build_vertices(&self) -> Result<Vec<Vertex>, ShapeCreationError> {
        // Compute the normal transformation matrix.
        let normal_matrix = Matrix3::<f32>::from_cols(
            self.matrix.x.truncate(),
            self.matrix.y.truncate(),
            self.matrix.z.truncate(),
        )
        .invert()
        .unwrap_or_else(Matrix3::<f32>::identity)
        .transpose();

        // Build the vertices.
        let verts_per_quad = 4;
        let mut vertices = Vec::<Vertex>::with_capacity(verts_per_quad);
        for vert in 0..verts_per_quad {
            let (u, v) = ((vert / 2) as f32, (vert % 2) as f32);
            let position = Vector4::<f32>::new((u * 2.0) - 1.0, (v * 2.0) - 1.0, 0.0, 1.0);
            let normal = Vector3::<f32>::new(0.0, 0.0, -1.0);
            vertices.push(Vertex {
                position: Point3::<f32>::from_homogeneous(self.matrix * position).into(),
                normal: (normal_matrix * normal).normalize().into(),
                texcoord: [u, v],
            });
        }
        Ok(vertices)
    }
}

#[test]
pub fn ensure_default_quad_has_edge_lengths_of_two() {
    use std::f32;
    let vertices = QuadBuilder::new()
        .build_vertices()
        .expect("Failed to build vertices");
    let mut min = Vector3::<f32>::new(f32::MAX, f32::MAX, f32::MAX);
    let mut max = -min;
    for vertex in &vertices {
        let pos = Vector3::<f32>::from(vertex.position);
        min.x = f32::min(min.x, pos.x);
        min.y = f32::min(min.y, pos.y);
        min.z = f32::min(min.z, pos.z);
        max.x = f32::max(max.x, pos.x);
        max.y = f32::max(max.y, pos.y);
        max.z = f32::max(max.z, pos.z);
    }
    assert_eq!(min, Vector3::new(-1.0, -1.0, 0.0));
    assert_eq!(max, Vector3::new(1.0, 1.0, 0.0));
}

#[test]
pub fn ensure_default_quad_has_centroid_at_origin() {
    let vertices = QuadBuilder::new()
        .build_vertices()
        .expect("Failed to build vertices");
    let mut sum = Vector3::<f32>::zero();
    for vertex in &vertices {
        sum += Vector3::<f32>::from(vertex.position);
    }
    assert_eq!(sum, Vector3::<f32>::zero());
}

#[test]
pub fn ensure_default_quad_is_planar() {
    let vertices = QuadBuilder::new()
        .build_vertices()
        .expect("Failed to build vertices");
    let tri0 = [
        Vector3::<f32>::from(vertices[0].position),
        Vector3::<f32>::from(vertices[1].position),
        Vector3::<f32>::from(vertices[2].position),
    ];

    let tri1 = [
        Vector3::<f32>::from(vertices[2].position),
        Vector3::<f32>::from(vertices[1].position),
        Vector3::<f32>::from(vertices[3].position),
    ];

    let n0 = (tri0[1] - tri0[0]).cross(tri0[2] - tri0[0]).normalize();
    let n1 = (tri1[1] - tri1[0]).cross(tri1[2] - tri1[0]).normalize();
    assert_ulps_eq!(n0, n1, epsilon = 0.0001);
}

#[test]
pub fn ensure_default_quad_has_ccw_triangles() {
    let vertices = QuadBuilder::new()
        .build_vertices()
        .expect("Failed to build vertices");
    let tris = [[0, 1, 2], [2, 1, 3]];
    for tri in tris.iter() {
        let v0 = Vector3::<f32>::from(vertices[tri[0]].position);
        let v1 = Vector3::<f32>::from(vertices[tri[1]].position);
        let v2 = Vector3::<f32>::from(vertices[tri[2]].position);
        let eyepos = v0 + Vector3::<f32>::from(vertices[tri[0]].normal);
        let e0 = v1 - v0;
        let e1 = v2 - v0;
        let n = e0.cross(e1);
        assert!(n.dot(v0 - eyepos) <= 0.0);
        assert!(n.dot(v1 - eyepos) <= 0.0);
        assert!(n.dot(v2 - eyepos) <= 0.0);
    }
}

#[test]
pub fn ensure_default_quad_has_face_aligned_normals() {
    let vertices = QuadBuilder::new()
        .build_vertices()
        .expect("Failed to build vertices");
    let tri0 = [
        Vector3::<f32>::from(vertices[0].position),
        Vector3::<f32>::from(vertices[1].position),
        Vector3::<f32>::from(vertices[2].position),
    ];
    let fnormal = (tri0[1] - tri0[0]).cross(tri0[2] - tri0[0]).normalize();
    for vertex in vertices.iter() {
        let vnormal = Vector3::<f32>::from(vertex.normal);
        assert_eq!(vnormal, fnormal);
    }
}

#[test]
pub fn ensure_quad_uvs_are_in_correct_range() {
    use std::f32;
    let vertices = QuadBuilder::new()
        .build_vertices()
        .expect("Failed to build vertices");
    let mut min = Vector2::<f32>::new(f32::MAX, f32::MAX);
    let mut max = -min;
    for vertex in &vertices {
        min.x = f32::min(min.x, vertex.texcoord[0]);
        min.y = f32::min(min.y, vertex.texcoord[1]);
        max.x = f32::max(max.x, vertex.texcoord[0]);
        max.y = f32::max(max.y, vertex.texcoord[1]);
    }
    assert!(min == Vector2::<f32>::zero());
    assert!(max == Vector2::<f32>::from_value(1.0));
}