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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
//! The Mesh (CPU & GPU) module for CGL

#![allow(non_camel_case_types)]
use libc::{c_void, c_int, size_t, c_char, c_float};

use crate::math::{IVector4, Vector4, Vector3, Matrix4x4};

/// The internal handle used by CGL
#[repr(C)]
pub(crate) struct CGL_mesh_gpu {
    _private: c_void
}

/// The Mesh Vertex Structure
#[repr(C)] #[derive(Copy, Clone)]
pub struct MeshVertex {
    pub position: Vector4,
    pub normal: Vector4,
    pub texture_coordinates: Vector4,
    pub bone_weights: Vector4,
    pub bone_ids: IVector4
}

/// The Mesh Structure
#[repr(C)] #[derive(Debug)]
pub struct MeshCPU_C {
    pub(crate) index_count: usize,
    pub(crate) index_count_used: usize,
    pub(crate) indices: *mut u32,
    pub(crate) vertex_count: usize,
    pub(crate) vertex_count_used: usize,
    pub(crate) vertices: *mut MeshVertex
}

pub struct MeshCPU {
    pub(crate) handle: *mut MeshCPU_C,
    pub(crate) has_been_destroyed: bool
}

pub struct MeshGPU {
    pub(crate) handle: *mut CGL_mesh_gpu,
    pub(crate) has_been_destroyed: bool
}


extern {
    fn CGL_mesh_cpu_create(vertex_count: size_t, index_count: size_t) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_recalculate_normals(mesh: *mut MeshCPU_C) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_load_obj(path: *const c_char) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_triangle(a: Vector3, b: Vector3, c: Vector3) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_plane(front: Vector3, right: Vector3, resolution: c_int, scale: c_float) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_quad(a: Vector3, b: Vector3, c: Vector3, d: Vector3) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_cube(use_3d_tex_coords: c_int) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_sphere(res_u: c_int, res_v: c_int) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_create_cylinder(start: Vector3, end: Vector3, radius0: c_float, radius1: c_float, resolution: c_int) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_add_mesh(mesh: *mut MeshCPU_C, mesh_other: *mut MeshCPU_C) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_add_cube(mesh: *mut MeshCPU_C, use_3d_tex_coords: c_int) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_add_triangle(mesh: *mut MeshCPU_C, a: Vector3, b: Vector3, c: Vector3) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_add_quad(mesh: *mut MeshCPU_C, a: Vector3, b: Vector3, c: Vector3, d: Vector3) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_add_sphere(mesh: *mut MeshCPU_C, res_u: c_int, res_v: c_int) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_add_cylinder(mesh: *mut MeshCPU_C, start: Vector3, end: Vector3, radius0: f32, radius1: f32, resolution: c_int) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_offset_vertices(mesh: *mut MeshCPU_C, offset: Vector3) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_scale_vertices(mesh: *mut MeshCPU_C, scale: c_float) -> *mut MeshCPU_C;
    // fn CGL_mesh_cpu_rotate_vertices(mesh: *mut MeshCPU_C, CGL_quat rotation) -> *mut MeshCPU_C; // for future
    fn CGL_mesh_cpu_transform_vertices(mesh: *mut MeshCPU_C, transform: &Matrix4x4) -> *mut MeshCPU_C;
    fn CGL_mesh_cpu_destroy(mesh: *mut MeshCPU_C) -> c_void;

    fn CGL_mesh_gpu_create() -> *mut CGL_mesh_gpu; // create mesh (gpu)
    fn CGL_mesh_gpu_destroy(mesh: *mut CGL_mesh_gpu); // destroy mesh (gpu)
    fn CGL_mesh_gpu_render(mesh: *mut CGL_mesh_gpu); // render mesh (gpu)
    fn CGL_mesh_gpu_render_instanced(mesh: *mut CGL_mesh_gpu, count: u32); // render mesh instanced (gpu)
    fn CGL_mesh_gpu_set_user_data(mesh: *mut CGL_mesh_gpu, user_data: *mut std::os::raw::c_void); // set mesh user data
    fn CGL_mesh_gpu_get_user_data(mesh: *mut CGL_mesh_gpu) -> *mut std::os::raw::c_void; // get mesh user data
    fn CGL_mesh_gpu_upload(mesh: *mut CGL_mesh_gpu, mesh_cpu: *mut MeshCPU_C, static_draw: c_int); // upload mesh from (cpu) to (gpu)
}

impl MeshVertex {
    /// Creates a new `MeshVertex` object with default values.
    /// 
    /// # Examples
    /// 
    /// ```
    /// let vertex = cgl_rs::graphics::MeshVertex::new();
    /// ```
    pub fn new() -> MeshVertex {
        MeshVertex {
            position: Vector4::new(0.0, 0.0, 0.0, 0.0),
            normal: Vector4::new(0.0, 0.0, 0.0, 0.0),
            texture_coordinates: Vector4::new(0.0, 0.0, 0.0, 0.0),
            bone_weights: Vector4::new(0.0, 0.0, 0.0, 0.0),
            bone_ids: IVector4::new(0, 0, 0, 0)
        }
    }
}

impl MeshCPU {

    fn from_handle(handle: *mut MeshCPU_C) -> Result<MeshCPU, &'static str> {
        if handle.is_null() {
            Err("Failed to create MeshCPU_C")
        } else {
            Ok(MeshCPU {
                handle: handle,
                has_been_destroyed: false
            })
        }
    }

    /// Create a new MeshCPU object with the given vertex and index count
    /// 
    /// # Arguments
    /// 
    /// * `vertex_count` - The number of vertices in the mesh
    /// * `index_count` - The number of indices in the mesh
    /// 
    /// # Returns
    /// 
    /// * `Result<MeshCPU, &'static str>` - The new MeshCPU object, or an error message
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshCPU::new(3, 3).unwrap();
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn new(vertex_count: usize, index_count: usize) -> Result<MeshCPU, &'static str> {
        MeshCPU::from_handle(unsafe { CGL_mesh_cpu_create(vertex_count, index_count) })
    }

    /// Loads a mesh from an OBJ file
    /// 
    /// # Arguments
    /// 
    /// * `path` - The path to the OBJ file
    /// 
    /// # Returns
    /// 
    /// * `Result<MeshCPU, &'static str>` - The loaded MeshCPU object, or an error message
    /// 
    /// # Example
    /// 
    /// ```no_run
    /// cgl_rs::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshCPU::load_obj("path/to/mesh.obj").unwrap();
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn load_obj(path: &str) -> Result<MeshCPU, &'static str> {
        let c_path = std::ffi::CString::new(path).unwrap();
        MeshCPU::from_handle(unsafe { CGL_mesh_cpu_load_obj(c_path.as_ptr()) })
    }

    /// Creates a new MeshCPU object with a single triangle
    /// 
    /// # Arguments
    /// 
    /// * `a` - The first vertex of the triangle
    /// * `b` - The second vertex of the triangle
    /// * `c` - The third vertex of the triangle
    /// 
    /// # Returns
    /// 
    /// * `Result<MeshCPU, &'static str>` - The new MeshCPU object, or an error message
    /// 
    /// # Example
    /// 
    /// ```
    /// use cgl_rs::math::*;
    /// cgl_rs::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshCPU::triangle(
    ///         Vector3::new(0.0, 0.0, 0.0),
    ///         Vector3::new(1.0, 0.0, 0.0),
    ///         Vector3::new(0.0, 1.0, 0.0)
    ///     ).unwrap();
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn triangle(a: Vector3, b: Vector3, c: Vector3) -> Result<MeshCPU, &'static str> {
        MeshCPU::from_handle(unsafe { CGL_mesh_cpu_triangle(a, b, c) })
    }

    /// Creates a new MeshCPU object representing a plane with the given front and right vectors, resolution, and scale.
    ///
    /// # Arguments
    ///
    /// * `front` - The front vector of the plane
    /// * `right` - The right vector of the plane
    /// * `resolution` - The number of subdivisions along each axis of the plane
    /// * `scale` - The scale of the plane
    ///
    /// # Returns
    ///
    /// * `Result<MeshCPU, &'static str>` - The new MeshCPU object, or an error message
    ///
    /// # Example
    ///
    /// ```
    /// use cgl_rs::math::*;
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::plane(
    ///       Vector3::new(0.0, 0.0, 1.0),
    ///       Vector3::new(1.0, 0.0, 0.0),
    ///       10,
    ///       1.0
    ///    ).unwrap();
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn plane(front: Vector3, right: Vector3, resolution: i32, scale: f32) -> Result<MeshCPU, &'static str> {
        MeshCPU::from_handle(unsafe { CGL_mesh_cpu_plane(front, right, resolution, scale) })
    }

    /// Creates a new MeshCPU object representing a quad with the given vertices.
    ///
    /// # Arguments
    ///
    /// * `a` - The first vertex of the quad
    /// * `b` - The second vertex of the quad
    /// * `c` - The third vertex of the quad
    /// * `d` - The fourth vertex of the quad
    ///
    /// # Returns
    ///
    /// * `Result<MeshCPU, &'static str>` - The new MeshCPU object, or an error message
    ///
    /// # Example
    ///
    /// ```
    /// use cgl_rs::math::*;
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::quad(
    ///       Vector3::new(-1.0, -1.0, 0.0),
    ///       Vector3::new(1.0, -1.0, 0.0),
    ///       Vector3::new(1.0, 1.0, 0.0),
    ///       Vector3::new(-1.0, 1.0, 0.0)
    ///    ).unwrap();
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn quad(a: Vector3, b: Vector3, c: Vector3, d: Vector3) -> Result<MeshCPU, &'static str> {
        MeshCPU::from_handle(unsafe { CGL_mesh_cpu_quad(a, b, c, d) })
    }

    /// Creates a new MeshCPU object representing a cube.
    ///
    /// # Arguments
    ///
    /// * `use_3d_tex_coords` - Whether to use 3D texture coordinates
    ///
    /// # Returns
    ///
    /// * `Result<MeshCPU, &'static str>` - The new MeshCPU object, or an error message
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::cube(true).unwrap();
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn cube(use_3d_tex_coords: bool) -> Result<MeshCPU, &'static str> {
        MeshCPU::from_handle(unsafe { CGL_mesh_cpu_cube(use_3d_tex_coords as i32) })
    }

    /// Creates a new MeshCPU object representing a sphere.
    ///
    /// # Arguments
    ///
    /// * `res_u` - The number of subdivisions along the u-axis of the sphere
    /// * `res_v` - The number of subdivisions along the v-axis of the sphere
    ///
    /// # Returns
    ///
    /// * `Result<MeshCPU, &'static str>` - The new MeshCPU object, or an error message
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::sphere(10, 10).unwrap();
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn sphere(res_u: i32, res_v: i32) -> Result<MeshCPU, &'static str> {
        MeshCPU::from_handle(unsafe { CGL_mesh_cpu_sphere(res_u, res_v) })
    }

    /// Creates a new MeshCPU object representing a cylinder.
    ///
    /// # Arguments
    ///
    /// * `start` - The start position of the cylinder
    /// * `end` - The end position of the cylinder
    /// * `radius0` - The radius of the cylinder at the start position
    /// * `radius1` - The radius of the cylinder at the end position
    /// * `resolution` - The number of subdivisions around the cylinder
    ///
    /// # Returns
    ///
    /// * `Result<MeshCPU, &'static str>` - The new MeshCPU object, or an error message
    ///
    /// # Example
    ///
    /// ```
    /// use cgl_rs::math::*;
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::cylinder(
    ///       Vector3::new(0.0, 0.0, 0.0),
    ///       Vector3::new(0.0, 1.0, 0.0),
    ///       1.0,
    ///       1.0,
    ///       10
    ///    ).unwrap();
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn cylinder(start: Vector3, end: Vector3, radius0: f32, radius1: f32, resolution: i32) -> Result<MeshCPU, &'static str> {
        MeshCPU::from_handle(unsafe { CGL_mesh_cpu_create_cylinder(start, end, radius0, radius1, resolution) })
    }

    /// Adds the vertices and indices of another MeshCPU object to this MeshCPU object.
    ///
    /// # Arguments
    ///
    /// * `other` - The MeshCPU object to add to this MeshCPU object
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///    let mesh1 = cgl_rs::graphics::MeshCPU::new(100, 100).unwrap();
    ///    let mesh2 = cgl_rs::graphics::MeshCPU::sphere(3, 3).unwrap();
    ///    mesh1.add_mesh(&mesh2);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn add_mesh(&self, other: &MeshCPU) {
        unsafe {
            CGL_mesh_cpu_add_mesh(self.handle, other.handle);
        }
    }

    /// Adds the vertices and indices of a cube to this MeshCPU object.
    ///
    /// # Arguments
    ///
    /// * `use_3d_tex_coords` - Whether or not to use 3D texture coordinates
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::new(100, 100).unwrap();
    ///    mesh.add_cube(true);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn add_cube(&self, use_3d_tex_coords: bool) {
        unsafe {
            CGL_mesh_cpu_add_cube(self.handle, use_3d_tex_coords as i32);
        }
    }

    /// Adds the vertices and indices of a sphere to this MeshCPU object.
    ///
    /// # Arguments
    ///
    /// * `res_u` - The number of subdivisions along the u-axis of the sphere
    /// * `res_v` - The number of subdivisions along the v-axis of the sphere
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::new(100, 100).unwrap();
    ///    mesh.add_sphere(2, 2);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn add_sphere(&self, res_u: i32, res_v: i32) {
        unsafe {
            CGL_mesh_cpu_add_sphere(self.handle, res_u, res_v);
        }
    }

    /// Adds a triangle to this MeshCPU object.
    ///
    /// # Arguments
    ///
    /// * `a` - The first vertex of the triangle
    /// * `b` - The second vertex of the triangle
    /// * `c` - The third vertex of the triangle
    ///
    /// # Example
    ///
    /// ```
    /// use cgl_rs::math::*;
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::new(100, 100).unwrap();
    ///    mesh.add_triangle(Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0), Vector3::new(0.0, 1.0, 0.0));
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn add_triangle(&self, a: Vector3, b: Vector3, c: Vector3) {
        unsafe {
            CGL_mesh_cpu_add_triangle(self.handle, a, b, c);
        }
    }

    /// Adds a quad to this MeshCPU object.
    ///
    /// # Arguments
    ///
    /// * `a` - The first vertex of the quad
    /// * `b` - The second vertex of the quad
    /// * `c` - The third vertex of the quad
    /// * `d` - The fourth vertex of the quad
    ///
    /// # Example
    ///
    /// ```
    /// use cgl_rs::math::*;
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::new(100, 100).unwrap();
    ///    mesh.add_quad(Vector3::new(0.0, 0.0, 0.0), Vector3::new(1.0, 0.0, 0.0), Vector3::new(1.0, 1.0, 0.0), Vector3::new(0.0, 1.0, 0.0));
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn add_quad(&self, a: Vector3, b: Vector3, c: Vector3, d: Vector3) {
        unsafe {
            CGL_mesh_cpu_add_quad(self.handle, a, b, c, d);
        }
    }

    // Adds a cylinder to this MeshCPU object.
    ///
    /// # Arguments
    ///
    /// * `start` - The starting point of the cylinder
    /// * `end` - The ending point of the cylinder
    /// * `radius0` - The radius of the cylinder at the starting point
    /// * `radius1` - The radius of the cylinder at the ending point
    /// * `resolution` - The number of subdivisions around the circumference of the cylinder
    ///
    /// # Example
    ///
    /// ```
    /// use cgl_rs::math::*;
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::new(100, 100).unwrap();
    ///    mesh.add_cylinder(Vector3::new(0.0, 0.0, 0.0), Vector3::new(0.0, 1.0, 0.0), 0.5, 0.5, 10);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn add_cylinder(&self, start: Vector3, end: Vector3, radius0: f32, radius1: f32, resolution: i32) {
        unsafe {
            CGL_mesh_cpu_add_cylinder(self.handle, start, end, radius0, radius1, resolution);
        }
    }

    // Offsets the vertices of this MeshCPU object by a given vector.
    ///
    /// # Arguments
    ///
    /// * `offset` - The vector by which to offset the vertices
    ///
    /// # Example
    ///
    /// ```
    /// use cgl_rs::math::*;
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::new(100, 100).unwrap();
    ///    mesh.offset_vertices(Vector3::new(1.0, 0.0, 0.0));
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn offset_vertices(&self, offset: Vector3) {
        unsafe {
            CGL_mesh_cpu_offset_vertices(self.handle, offset);
        }
    }

    // Scales the vertices of this MeshCPU object by a given factor.
    ///
    /// # Arguments
    ///
    /// * `scale` - The factor by which to scale the vertices
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::new(100, 100).unwrap();
    ///    mesh.scale_vertices(2.0);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn scale_vertices(&self, scale: f32) {
        unsafe {
            CGL_mesh_cpu_scale_vertices(self.handle, scale);
        }
    }

    // Transforms the vertices of this MeshCPU object by a given transformation matrix.
    ///
    /// # Arguments
    ///
    /// * `transform` - The transformation matrix by which to transform the vertices
    ///
    /// # Example
    ///
    /// ```
    /// use cgl_rs::math::*;
    /// cgl_rs::init();
    /// {
    ///    let mesh = cgl_rs::graphics::MeshCPU::new(100, 100).unwrap();
    ///    let transform = Matrix4x4::identity();
    ///    mesh.transform_vertices(&transform);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn transform_vertices(&self, transform: &Matrix4x4) {
        unsafe {
            CGL_mesh_cpu_transform_vertices(self.handle, transform);
        }
    }

    /// Destroy the MeshCPU object
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// {
    ///    let mut mesh = cgl_rs::graphics::MeshCPU::new(3, 3).unwrap();
    ///    mesh.destroy(); // or just let the mesh go out of scope
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn destroy(&mut self) {
        if !self.has_been_destroyed {
            unsafe {
                CGL_mesh_cpu_destroy(self.handle);
            }
            self.has_been_destroyed = true;
        }
    }

    /// Recalculates the normals of the mesh
    /// 
    /// # Example
    /// 
    /// ```
    /// cgl_rs::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshCPU::new(3, 3).unwrap();
    ///     mesh.recalculate_normals();
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn recalculate_normals(&self) {
        unsafe {
            CGL_mesh_cpu_recalculate_normals(self.handle);
        }
    }


    // Get the vertex at the specified index.
    ///
    /// # Arguments
    ///
    /// * `index` - The index of the vertex to retrieve
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshCPU::new(3, 3).unwrap();
    ///     let vertex = mesh.get_vertex(0);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_vertex(&self, index: usize) -> &MeshVertex {
        unsafe {
            let vertex_array = self.handle.as_ref().unwrap().vertices;
            vertex_array.offset(index as isize).as_ref().unwrap()
        }
    }

    // Set the vertex at the specified index.
    ///
    /// # Arguments
    ///
    /// * `index` - The index of the vertex to set
    /// * `vertex` - The vertex to set
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshCPU::new(3, 3).unwrap();
    ///     let vertex = cgl_rs::graphics::MeshVertex::new();
    ///     mesh.set_vertex(0, &vertex);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_vertex(&self, index: usize, vertex: &MeshVertex) {
        unsafe {
            let vertex_array = self.handle.as_ref().unwrap().vertices;
            let vertex_ptr = vertex_array.offset(index as isize);
            *vertex_ptr = *vertex;
        }
    }

    // Get the index at the specified index.
    ///
    /// # Arguments
    ///
    /// * `index` - The index of the index to retrieve
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshCPU::new(3, 3).unwrap();
    ///     let index = mesh.get_index(0);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn get_index(&self, index: usize) -> u32 {
        unsafe {
            let index_array = self.handle.as_ref().unwrap().indices;
            *index_array.offset(index as isize)
        }
    }

    // Set the index at the specified index.
    ///
    /// # Arguments
    ///
    /// * `index` - The index of the index to set
    /// * `value` - The value to set
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshCPU::new(3, 3).unwrap();
    ///     mesh.set_index(0, 1);
    /// }
    /// cgl_rs::shutdown();
    /// ```
    pub fn set_index(&self, index: usize, value: u32) {
        unsafe {
            let index_array = self.handle.as_ref().unwrap().indices;
            let index_ptr = index_array.offset(index as isize);
            *index_ptr = value;
        }
    }


}

impl Drop for MeshCPU {
    fn drop(&mut self) {
        self.destroy();
    }
}

impl Clone for MeshCPU {
    fn clone(&self) -> Self {
        MeshCPU {
            handle: self.handle.clone(),
            has_been_destroyed: true
        }
    }
}


impl MeshGPU {

    /// Creates a new MeshGPU instance.
    ///
    /// # Returns
    ///
    /// Returns a `Result` containing a `MeshGPU` instance if successful, or a `&'static str` error message if unsuccessful.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::window::Window::new("Test Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshGPU::new().unwrap();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn new() -> Result<MeshGPU, &'static str> {
        let handle = unsafe {
            CGL_mesh_gpu_create()
        };
        if handle.is_null() {
            Err("Failed to create MeshGPU")
        } else {
            Ok(MeshGPU {
                handle: handle,
                has_been_destroyed: false
            })
        }
    }


    /// Renders the mesh using the GPU.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::window::Window::new("Test Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshGPU::new().unwrap();
    ///     mesh.render();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn render(&self) {
        unsafe {
            CGL_mesh_gpu_render(self.handle);
        }
    }

    /// Renders the mesh using the GPU with instancing.
    ///
    /// * `instance_count` - The number of instances to render.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::window::Window::new("Test Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mesh = cgl_rs::graphics::MeshGPU::new().unwrap();
    ///     mesh.render_instanced(10);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn render_instanced(&self, instance_count: u32) {
        unsafe {
            CGL_mesh_gpu_render_instanced(self.handle, instance_count);
        }
    }

    /// Uploads the mesh data to the GPU.
    ///
    /// * `mesh_cpu` - The mesh data to upload.
    /// * `static_draw` - Whether the mesh data is static or dynamic.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::window::Window::new("Test Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut mesh_cpu = cgl_rs::graphics::MeshCPU::new(3, 3).unwrap();
    ///     let mut mesh_gpu = cgl_rs::graphics::MeshGPU::new().unwrap();
    ///     mesh_gpu.upload(&mesh_cpu, true);
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn upload(&mut self, mesh_cpu: &MeshCPU, static_draw: bool) {
        unsafe {
            CGL_mesh_gpu_upload(self.handle, mesh_cpu.handle, static_draw as i32);
        }
    }

    /// Destroys the mesh GPU handle if it has not already been destroyed.
    ///
    /// # Example
    ///
    /// ```
    /// cgl_rs::init();
    /// let mut window = cgl_rs::window::Window::new("Test Window", 800, 600).unwrap();
    /// cgl_rs::graphics::init();
    /// {
    ///     let mut mesh = cgl_rs::graphics::MeshGPU::new().unwrap();
    ///     mesh.destroy();
    /// }
    /// cgl_rs::graphics::shutdown();
    /// window.destroy();
    /// cgl_rs::shutdown();
    /// ```
    pub fn destroy(&mut self) {
        if !self.has_been_destroyed {
            unsafe {
                CGL_mesh_gpu_destroy(self.handle);
            }
            self.has_been_destroyed = true;
        }
    }

}

impl Drop for MeshGPU {
    fn drop(&mut self) {
        self.destroy();
    }
}

impl Clone for MeshGPU {
    fn clone(&self) -> Self {
        MeshGPU {
            handle: self.handle.clone(),
            has_been_destroyed: true
        }
    }
}