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
//! Mesh resource.

use std::iter::{once, Chain, Once};
use std::marker::PhantomData;

use amethyst_assets::Handle;

use amethyst_core::cgmath::{Deg, Matrix4, Point3, Transform, Vector3};
use gfx::Primitive;

use error::Result;
use types::{Factory, RawBuffer, Slice};
use vertex::{Attributes, VertexFormat};

/// Raw buffer with its attributes
#[derive(Clone, Debug)]
pub struct VertexBuffer {
    attrs: Attributes<'static>,
    raw: RawBuffer,
}

/// Vertex data that can be built into `VertexBuffer`
#[doc(hidden)]
pub trait VertexData {
    const ATTRIBUTES: Attributes<'static>;

    /// Get vertex count in buffer
    fn len(&self) -> usize;

    /// Build `VertexBuffer`
    fn build(&self, factory: &mut Factory) -> Result<VertexBuffer>;
}

/// Construct new vertex data from raw data and vertex format
pub fn vertex_data<D, V>(data: D) -> (D, PhantomData<V>)
where
    D: AsRef<[V]>,
    V: VertexFormat,
{
    (data, PhantomData)
}

impl<D, V> VertexData for (D, PhantomData<V>)
where
    D: AsRef<[V]>,
    V: VertexFormat,
{
    const ATTRIBUTES: Attributes<'static> = V::ATTRIBUTES;

    fn len(&self) -> usize {
        self.0.as_ref().len()
    }

    fn build(&self, factory: &mut Factory) -> Result<VertexBuffer> {
        use gfx::Factory;
        use gfx::buffer::Role;
        use gfx::memory::{cast_slice, Bind};

        let verts = self.0.as_ref();
        let slice = cast_slice(verts);
        let stride = slice.len() / verts.len();
        let role = Role::Vertex;
        let bind = Bind::empty();

        let vbuf = factory.create_buffer_immutable_raw(slice, stride, role, bind)?;
        Ok(VertexBuffer {
            attrs: V::ATTRIBUTES,
            raw: vbuf,
        })
    }
}

/// Set of vertex data
#[doc(hidden)]
pub trait VertexDataSet {
    /// Iterator for `VertexBuffer`s built
    type VertexBufferIter: Iterator<Item = VertexBuffer>;

    /// Get smalles vertex count across buffers
    fn len(&self) -> usize;

    /// Build `VertexBuffer`s
    fn build(&self, factory: &mut Factory) -> Result<Self::VertexBufferIter>;
}

impl<H> VertexDataSet for (H, ())
where
    H: VertexData,
{
    type VertexBufferIter = Once<VertexBuffer>;

    fn len(&self) -> usize {
        self.0.len()
    }

    fn build(&self, factory: &mut Factory) -> Result<Self::VertexBufferIter> {
        let (ref head, _) = *self;
        Ok(once(head.build(factory)?))
    }
}

impl<H, T> VertexDataSet for (H, T)
where
    H: VertexData,
    T: VertexDataSet,
{
    type VertexBufferIter = Chain<Once<VertexBuffer>, T::VertexBufferIter>;

    fn len(&self) -> usize {
        use std::cmp::min;
        min(self.0.len(), self.1.len())
    }

    fn build(&self, factory: &mut Factory) -> Result<Self::VertexBufferIter> {
        let (ref head, ref tail) = *self;
        Ok(once(head.build(factory)?).chain(tail.build(factory)?))
    }
}

/// A handle to a mesh.
pub type MeshHandle = Handle<Mesh>;

/// Represents a polygonal mesh.
#[derive(Clone, Debug)]
pub struct Mesh {
    slice: Slice,
    transform: Matrix4<f32>,
    vbufs: Vec<VertexBuffer>,
}

impl Mesh {
    /// Builds a new mesh from the given vertices.
    pub fn build<D, V>(verts: D) -> MeshBuilder<((D, PhantomData<V>), ())>
    where
        D: AsRef<[V]>,
        V: VertexFormat,
    {
        MeshBuilder::new(verts)
    }

    /// Returns the mesh's vertex buffer which matches requested attributes
    pub fn buffer(&self, attributes: Attributes) -> Option<&RawBuffer> {
        for vbuf in self.vbufs.iter() {
            let mut find = attributes.iter();
            let mut next = find.next();
            let mut i = 0;
            let mut j = 0;
            loop {
                let attrs = vbuf.attrs;
                match next {
                    Some(&attr) => {
                        if i == attrs.len() {
                            // try next vbuf
                            break;
                        } else if attrs[(i + j) % attrs.len()] == attr {
                            // match. search next attribute
                            next = find.next();
                            j = i;
                            i = 0;
                        } else {
                            // continue searching
                            i += 1;
                        }
                    }
                    None => {
                        // All atributes found
                        return Some(&vbuf.raw);
                    }
                }
            }
        }

        // None of the vertex buffers match requested attributes
        None
    }

    /// Returns associated `Slice`
    pub fn slice(&self) -> &Slice {
        &self.slice
    }

    /// Returns the transformation matrix of the mesh.
    ///
    /// This four-by-four matrix applies translation, rotation, and scaling to
    /// the mesh. It is often referred to in the computer graphics industry as
    /// the "model matrix".
    pub fn transform(&self) -> Matrix4<f32> {
        self.transform
    }
}

/// Builds new meshes.
#[derive(Clone, Debug, Deserialize, PartialEq, Serialize)]
pub struct MeshBuilder<T> {
    prim: Primitive,
    transform: Matrix4<f32>,
    vertices: T,
}

impl<D, V> MeshBuilder<((D, PhantomData<V>), ())>
where
    D: AsRef<[V]>,
    V: VertexFormat,
{
    /// Creates a new `MeshBuilder` with the given vertices.
    pub fn new(verts: D) -> Self {
        use amethyst_core::cgmath::SquareMatrix;
        assert!(check_attributes_are_sorted(V::ATTRIBUTES));
        MeshBuilder {
            prim: Primitive::TriangleList,
            transform: Matrix4::identity(),
            vertices: (vertex_data(verts), ()),
        }
    }
}

impl<T> MeshBuilder<T>
where
    T: VertexDataSet,
{
    /// Add another vertices to the `MeshBuilder`
    pub fn with_buffer<D, V>(self, verts: D) -> MeshBuilder<((D, PhantomData<V>), T)>
    where
        D: AsRef<[V]>,
        V: VertexFormat,
    {
        assert!(check_attributes_are_sorted(V::ATTRIBUTES));
        MeshBuilder {
            prim: self.prim,
            transform: self.transform,
            vertices: (vertex_data(verts), self.vertices),
        }
    }

    /// Sets the primitive type of the mesh.
    ///
    /// By default, meshes are constructed as triangle lists.
    pub fn with_prim_type(mut self, prim: Primitive) -> Self {
        self.prim = prim;
        self
    }

    /// Sets the position of the mesh in 3D space.
    pub fn with_position<P: Into<Point3<f32>>>(mut self, pos: P) -> Self {
        use amethyst_core::cgmath::EuclideanSpace;

        let trans = Matrix4::from_translation(pos.into().to_vec());
        self.transform.concat_self(&trans);
        self
    }

    /// Rotates the mesh a certain number of degrees around the given axis.
    pub fn with_rotation<Ax, An>(mut self, axis: Ax, angle: An) -> Self
    where
        Ax: Into<Vector3<f32>>,
        An: Into<Deg<f32>>,
    {
        let rot = Matrix4::from_axis_angle(axis.into(), angle.into());
        self.transform.concat_self(&rot);
        self
    }

    /// Scales the mesh size according to the given value.
    pub fn with_scale(mut self, val: f32) -> Self {
        let scale = Matrix4::from_scale(val);
        self.transform.concat_self(&scale);
        self
    }

    /// Sets the transformation matrix of the mesh.
    ///
    /// This four-by-four matrix applies translation, rotation, and scaling to
    /// the mesh. It is often referred to in the computer graphics industry as
    /// the "model matrix".
    pub fn with_transform<M: Into<Matrix4<f32>>>(mut self, mat: M) -> Self {
        self.transform = mat.into();
        self
    }

    /// Builds and returns the new mesh.
    pub fn build(self, fac: &mut Factory) -> Result<Mesh> {
        use gfx::IndexBuffer;
        let count = self.vertices.len();

        let slice = Slice {
            start: 0,
            end: count as u32,
            base_vertex: 0,
            instances: None,
            buffer: IndexBuffer::Auto,
        };

        Ok(Mesh {
            slice: slice,
            transform: self.transform,
            vbufs: self.vertices.build(fac)?.collect(),
        })
    }
}

/// Check that attributes are sorted
fn check_attributes_are_sorted(attrs: Attributes) -> bool {
    let mut last = 0;
    for attr in attrs {
        if last > attr.1.offset {
            return false;
        }
        last = attr.1.offset;
    }
    true
}