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
use std::marker::PhantomData;

use crate::collision::NewtonCollision;
use crate::ffi;
use crate::math::Mat4;
use crate::newton::Newton;

/// Newton mesh wrapper.
///
/// Meshes can be used to collect geometry information (vertex & indices) from
/// Collisions in order to be displayed by the application rendering engine.
pub struct Mesh<'a> {
    raw: *const ffi::NewtonMesh,
    owned: bool,
    _phantom: PhantomData<&'a ()>,
}

impl<'a> Drop for Mesh<'a> {
    fn drop(&mut self) {
        unsafe { ffi::NewtonMeshDestroy(self.raw) }
    }
}

impl<'a> Mesh<'a> {
    pub fn create(newton: &'a Newton) -> Self {
        unsafe {
            let raw = ffi::NewtonMeshCreate(newton.as_raw());
            Self::from_raw(raw, true)
        }
    }

    pub fn from_collision<C>(_: &'a Newton, col: &C) -> Self
        where C: NewtonCollision
    {
        unsafe {
            let raw = ffi::NewtonMeshCreateFromCollision(col.as_raw());
            Self::from_raw(raw, true)
        }
    }

    pub unsafe fn from_raw(raw: *const ffi::NewtonMesh, owned: bool) -> Self {
        Self { raw, owned, _phantom: PhantomData }
    }

    pub fn as_raw(&self) -> *const ffi::NewtonMesh {
        self.raw
    }

    pub fn into_raw(self) -> *const ffi::NewtonMesh {
        self.raw
    }
}