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
use std::{ffi::CString, rc::Rc};

use crate::{
    ffi,
    math::{BoundingBox, Vector3},
    texture::{Image, Texture2D},
};

pub use crate::ffi::MaterialMapIndex;

/// Mesh, vertex data and vao/vbo
#[derive(Debug)]
pub struct Mesh {
    pub(crate) raw: ffi::Mesh,
}

impl Mesh {
    /// Upload mesh vertex data in GPU and provide VAO/VBO ids
    #[inline]
    pub fn upload(&mut self, dynamic: bool) {
        unsafe { ffi::UploadMesh(&mut self.raw as *mut _, dynamic) }
    }

    /// Update mesh vertex data in GPU for a specific buffer index
    #[inline]
    pub fn update_buffer(&self, index: u32, data: &[u8], offset: u32) {
        unsafe {
            ffi::UpdateMeshBuffer(
                self.raw.clone(),
                index as _,
                data.as_ptr() as *const _,
                data.len() as _,
                offset as _,
            )
        }
    }

    /// Export mesh data to file, returns true on success
    #[inline]
    pub fn export(&self, file_name: &str) -> bool {
        let file_name = CString::new(file_name).unwrap();

        unsafe { ffi::ExportMesh(self.raw.clone(), file_name.as_ptr()) }
    }

    /// Compute mesh bounding box limits
    #[inline]
    pub fn get_bounding_box(&self) -> BoundingBox {
        unsafe { ffi::GetMeshBoundingBox(self.raw.clone()).into() }
    }

    /// Compute mesh tangents
    #[inline]
    pub fn generate_tangents(&mut self) {
        unsafe { ffi::GenMeshTangents(&mut self.raw as *mut _) }
    }

    /// Generate polygonal mesh
    #[inline]
    pub fn generate_polygon(sides: u32, radius: f32) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshPoly(sides as _, radius) },
        }
    }

    /// Generate plane mesh (with subdivisions)
    #[inline]
    pub fn generate_plane(width: f32, length: f32, res_x: u32, res_z: u32) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshPlane(width, length, res_x as _, res_z as _) },
        }
    }

    /// Generate cuboid mesh
    #[inline]
    pub fn generate_cube(width: f32, height: f32, length: f32) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshCube(width, height, length) },
        }
    }

    /// Generate sphere mesh (standard sphere)
    #[inline]
    pub fn generate_sphere(radius: f32, rings: u32, slices: u32) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshSphere(radius, rings as _, slices as _) },
        }
    }

    /// Generate half-sphere mesh (no bottom cap)
    #[inline]
    pub fn generate_hemisphere(radius: f32, rings: u32, slices: u32) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshHemiSphere(radius, rings as _, slices as _) },
        }
    }

    /// Generate cylinder mesh
    #[inline]
    pub fn generate_cylinder(radius: f32, height: f32, slices: u32) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshCylinder(radius, height, slices as _) },
        }
    }

    /// Generate cone/pyramid mesh
    #[inline]
    pub fn generate_cone(radius: f32, height: f32, slices: u32) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshCone(radius, height, slices as _) },
        }
    }

    /// Generate torus mesh
    #[inline]
    pub fn generate_torus(radius: f32, size: f32, rad_seg: u32, sides: u32) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshTorus(radius, size, rad_seg as _, sides as _) },
        }
    }

    /// Generate trefoil knot mesh
    #[inline]
    pub fn generate_knot(radius: f32, size: f32, rad_seg: u32, sides: u32) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshKnot(radius, size, rad_seg as _, sides as _) },
        }
    }

    /// Generate heightmap mesh from image data
    #[inline]
    pub fn generate_heightmap(heightmap: &Image, size: Vector3) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshHeightmap(heightmap.raw.clone(), size.into()) },
        }
    }

    /// Generate cubes-based map mesh from image data
    #[inline]
    pub fn generate_cubicmap(cubicmap: &Image, cube_size: Vector3) -> Self {
        Self {
            raw: unsafe { ffi::GenMeshCubicmap(cubicmap.raw.clone(), cube_size.into()) },
        }
    }
}

impl Drop for Mesh {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::UnloadMesh(self.raw.clone()) }
    }
}

/// Model, meshes, materials and animation data
#[derive(Debug)]
pub struct Model {
    pub(crate) raw: ffi::Model,
}

impl Model {
    /// Load model from files (meshes and materials)
    #[inline]
    pub fn from_file(file_name: &str) -> Option<Self> {
        let file_name = CString::new(file_name).unwrap();

        let raw = unsafe { ffi::LoadModel(file_name.as_ptr()) };

        if unsafe { ffi::IsModelReady(raw.clone()) } {
            Some(Self { raw })
        } else {
            None
        }
    }

    /// Load model from generated mesh (default material)
    #[inline]
    pub fn from_mesh(mesh: Mesh) -> Self {
        let raw_mesh = mesh.raw.clone();
        // LoadModelFromMesh 'takes ownership' of the mesh
        std::mem::forget(mesh);

        Self {
            raw: unsafe { ffi::LoadModelFromMesh(raw_mesh) },
        }
    }

    /// Compute model bounding box limits (considers all meshes)
    #[inline]
    pub fn get_bounding_box(&self) -> BoundingBox {
        unsafe { ffi::GetModelBoundingBox(self.raw.clone()).into() }
    }

    /// Set material for a mesh
    #[inline]
    pub fn set_mesh_material(&mut self, mesh_id: u32, material_id: u32) {
        unsafe {
            ffi::SetModelMeshMaterial(&mut self.raw as *mut _, mesh_id as _, material_id as _)
        }
    }

    /// Update model animation pose
    #[inline]
    pub fn update_animation(&self, anim: &ModelAnimation, frame: u32) {
        unsafe { ffi::UpdateModelAnimation(self.raw.clone(), anim.raw.clone(), frame as _) }
    }

    /// Check model animation skeleton match
    #[inline]
    pub fn is_animation_valid(&self, anim: &ModelAnimation) -> bool {
        unsafe { ffi::IsModelAnimationValid(self.raw.clone(), anim.raw.clone()) }
    }
}

impl Drop for Model {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::UnloadModel(self.raw.clone()) }
    }
}

/// Material, includes shader and maps
#[derive(Debug)]
pub struct Material {
    pub(crate) raw: ffi::Material,
}

impl Material {
    /// Load materials from model file
    #[inline]
    pub fn from_file(file_name: &str) -> Vec<Self> {
        let file_name = CString::new(file_name).unwrap();
        let mut count: i32 = 0;

        let mats = unsafe { ffi::LoadMaterials(file_name.as_ptr(), &mut count as *mut _) };

        let mut vec = Vec::new();

        for i in 0..(count as usize) {
            let mat = unsafe { mats.add(i).read() };

            if unsafe { ffi::IsMaterialReady(mat.clone()) } {
                vec.push(Self { raw: mat });
            }
        }

        vec
    }

    /// Set texture for a material map type
    ///
    /// Returns `true` on success (if `texture` hasn't been cloned or all of them have been dropped, i.e. if its the underlying `Rc` has only 1 strong reference)
    #[inline]
    pub fn set_texture(&mut self, map_type: MaterialMapIndex, texture: Texture2D) -> bool {
        if Rc::strong_count(&texture.raw) == 1 {
            let raw = texture.raw.clone();
            drop(texture);

            unsafe {
                ffi::SetMaterialTexture(
                    &mut self.raw as *mut _,
                    map_type as _,
                    Rc::into_inner(raw).unwrap(),
                );
            }

            true
        } else {
            false
        }
    }
}

impl Default for Material {
    #[inline]
    fn default() -> Self {
        Self {
            raw: unsafe { ffi::LoadMaterialDefault() },
        }
    }
}

impl Drop for Material {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::UnloadMaterial(self.raw.clone()) }
    }
}

/// Model animation
#[derive(Debug)]
pub struct ModelAnimation {
    raw: ffi::ModelAnimation,
}

impl ModelAnimation {
    /// Load model animations from file
    #[inline]
    pub fn from_file(file_name: &str) -> Vec<Self> {
        let file_name = CString::new(file_name).unwrap();
        let mut count: u32 = 0;

        let anims = unsafe { ffi::LoadModelAnimations(file_name.as_ptr(), &mut count as *mut _) };

        let mut vec = Vec::new();

        for i in 0..(count as usize) {
            vec.push(ModelAnimation {
                raw: unsafe { anims.add(i).read() },
            })
        }

        unsafe {
            ffi::UnloadModelAnimations(anims, count);
        }

        vec
    }
}

impl Drop for ModelAnimation {
    #[inline]
    fn drop(&mut self) {
        unsafe { ffi::UnloadModelAnimation(self.raw.clone()) }
    }
}