grafix_toolbox/kit/opengl/geom/
mesh.rs1use super::*;
2use GL::spec::*;
3
4pub trait AnyMesh {
5 fn Draw(&self);
6 fn to_trait(self) -> Box<dyn AnyMesh>;
7}
8impl<I: IdxType, C: AttrType, T: AttrType, N: AttrType> AnyMesh for Mesh<I, C, T, N> {
9 fn Draw(&self) {
10 self.vao.Bind().Draw(self.draw);
11 }
12 fn to_trait(self) -> Box<dyn AnyMesh> {
13 Box(self)
14 }
15}
16pub struct Mesh<I, C, T, N> {
17 pub vao: Vao<I>,
18 pub buff: (IdxArr<I>, AttrArr<C>, Option<AttrArr<T>>, AttrArr<N>),
19 pub draw: (u32, GLenum),
20}
21impl<I: IdxType, C: AttrType, T: AttrType, N: AttrType> Mesh<I, C, T, N> {
22 pub fn new(args: impl MeshArgs<I, C, T, N>) -> Self {
23 let (idx, xyz, uv, norm, mode) = args.get();
24
25 let draw = (u32(idx.len()), mode);
26
27 let idx = IdxArr::new(idx);
28 let xyz = AttrArr::new(xyz);
29 let norm = AttrArr::new(norm);
30
31 let mut vao = Vao::new();
32 vao.BindIdxs(&idx);
33
34 let uv = uv.map(|uv| {
35 let uv = AttrArr::new(uv);
36 vao.AttribFmt(&uv, (1, 2));
37 uv
38 });
39 vao.AttribFmt(&xyz, (0, 3));
40 vao.AttribFmt(&norm, (2, 3));
41 let buff = (idx, xyz, uv, norm);
42
43 Self { vao, buff, draw }
44 }
45}
46
47type MArgs<'s, I, C, T, N> = (&'s [I], &'s [C], Option<&'s [T]>, &'s [N], GLenum);
48pub trait MeshArgs<I, C, T, N> {
49 fn get(&self) -> MArgs<'_, I, C, T, N>;
50}
51impl<I: IdxType, C: AttrType, T: AttrType, N: AttrType, SI: AsRef<[I]>, SC: AsRef<[C]>, ST: AsRef<[T]>, SN: AsRef<[N]>> MeshArgs<I, C, T, N> for (SI, SC, ST, SN, GLenum) {
52 fn get(&self) -> MArgs<'_, I, C, T, N> {
53 (self.0.as_ref(), self.1.as_ref(), Some(self.2.as_ref()), self.3.as_ref(), self.4)
54 }
55}
56impl<I: IdxType, C: AttrType, N: AttrType, SI: AsRef<[I]>, SC: AsRef<[C]>, SN: AsRef<[N]>> MeshArgs<I, C, f16, N> for (SI, SC, SN, GLenum) {
57 fn get(&self) -> MArgs<'_, I, C, f16, N> {
58 (self.0.as_ref(), self.1.as_ref(), None, self.2.as_ref(), self.3)
59 }
60}