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
use std::ops::{Add, AddAssign, Sub, SubAssign, Mul, MulAssign};

use crate::{GlEnum, GlAttrib, GlUniform, GlslType, impl_glsl};

macro_rules! impl_vec {
    ($Name: ident, $($field: ident),+; $size: expr) => {
        #[repr(C)]
        #[derive(Default, Debug, Clone, Copy, PartialEq)]
        pub struct $Name<T> { $(pub $field: T),+ }

        impl<T> $Name<T> {
            pub fn new($($field: T),+) -> $Name<T> {
                $Name { $($field: $field),+ }
            }

            // pub fn from_array(data: [T; $size])-> $Name<T> where T: Default {
            //     let mut vec = $Name::default();
            //     let ptr = &vec;
            //     vec
            // }

            pub fn as_array(&self) -> [&T; $size] {
                [$(&self.$field),+]
            }

            // pub fn dot(&self, rhs: &Self) -> T {
            //     let sum: T = ($(&self.$field * rhs.$field),+);
            //     sum
            // }

            /* pub fn from_value(v: T) -> $Name<T> {
                $Name { $($field: v),+ }
            } */
            pub fn stride() -> usize { $size * std::mem::size_of::<T>() }
        }

        impl<T: Add> Add<$Name<T> > for $Name<T>
            where T: Add<Output = T>
        {
            type Output = $Name<T>;
            fn add(self, rhs: $Name<T>) -> Self::Output {
                $Name::new($(self.$field + rhs.$field),+)
            }
        }

        impl<T: Sub> Sub<$Name<T> > for $Name<T>
            where T: Sub<Output = T>
        {
            type Output = $Name<T>;
            fn sub(self, rhs: $Name<T>) -> Self::Output {
                $Name::new($(self.$field - rhs.$field),+)
            }
        }

        impl<T: AddAssign> AddAssign<$Name<T> > for $Name<T> {
            fn add_assign(&mut self, rhs: Self) {
                $(self.$field += rhs.$field);+
            }
        }

        impl<T: SubAssign> SubAssign<$Name<T> > for $Name<T> {
            fn sub_assign(&mut self, rhs: Self) {
                $(self.$field -= rhs.$field);+
            }
        }

        impl<T: GlEnum> GlEnum for $Name<T> {
            fn gl_enum() -> u32 { T::gl_enum() }

            fn to_enum(&self) -> u32 { T::gl_enum() }
        }

        impl<T: GlEnum> GlAttrib for $Name<T> {
            fn size() -> u32 { $size }
        }

        impl<T: GlUniform> GlUniform for $Name<T> {
            fn uniform(location: i32, val: &Self) {
                Self::uniformv(location, $size, val as *const Self);
            }
            
            fn uniformv(location: i32, count: i32, ptr: *const Self) {
                T::uniformv(location, count * $size, ptr as *const T);
            }

            fn send_uniform(&self, location: i32) {
                let ptr: *const T = &self.x;
                T::uniformv(location, $size, ptr);
            }
        }
    }
}

pub trait UniformType {
    fn get_type(&self) -> u32;
}

impl_vec!(Vector2, x, y; 2);
impl_vec!(Vector3, x, y, z; 3);
impl_vec!(Vector4, x, y, z, w; 4);

pub type BVec2 = Vector2<bool>;
pub type IVec2 = Vector2<i32>;
pub type UVec2 = Vector2<u32>;
pub type Vec2 = Vector2<f32>;

impl_glsl!(BVec2, "bvec2");
impl_glsl!(IVec2, "ivec2");
impl_glsl!(UVec2, "uvec2");
impl_glsl!(Vec2, "vec2");

pub type BVec3 = Vector3<bool>;
pub type IVec3 = Vector3<i32>;
pub type UVec3 = Vector3<u32>;
pub type Vec3 = Vector3<f32>;

impl_glsl!(BVec3, "bvec3");
impl_glsl!(IVec3, "ivec3");
impl_glsl!(UVec3, "uvec3");
impl_glsl!(Vec3, "vec3");

pub type BVec4 = Vector4<bool>;
pub type IVec4 = Vector4<i32>;
pub type UVec4 = Vector4<u32>;
pub type Vec4 = Vector4<f32>;

impl_glsl!(BVec4, "bvec4");
impl_glsl!(IVec4, "ivec4");
impl_glsl!(UVec4, "uvec4");
impl_glsl!(Vec4, "vec4");

impl Vec3 {
    pub fn zero() -> Self {
        Self { x: 0.0, y: 0.0, z: 0.0 }
    }

    pub fn up() -> Self { Self { x: 0.0, y: 1.0, z: 0.0 } }
    pub fn down() -> Self { Self { x: 0.0, y: -1.0, z: 0.0 } }
    pub fn front() -> Self { Self { x: 0.0, y: 0.0, z: -1.0 } }
    pub fn back() -> Self { Self { x: 0.0, y: 0.0, z: 1.0 } }

    pub fn from_array(data: [f32; 4]) -> Self {
        Vec3 { x: data[0], y: data[1], z: data[2] }
    }

    pub fn dot(&self, rhs: &Self) -> f32 {
        self.x*rhs.x + self.y*rhs.y + self.z*rhs.z
    }

    pub fn len(&self) -> f32 {
        self.x*self.x + self.y*self.y + self.z*self.z
    }

    pub fn cross(&self, other: &Self) -> Self {
        let x = self.y*other.z - self.z*other.y;
        let y = self.z*other.x - self.x*other.z;
        let z = self.x*other.y - self.y*other.x;
        Vec3::new(x, y, z)
    }

    pub fn normalize(&self) -> Self {
        let len = self.len().sqrt();
        Vec3::new(self.x / len, self.y / len, self.z / len)
    }

    pub fn scalar(&self, val: f32) -> Self {
        Vec3::new(self.x * val, self.y * val, self.z * val)
    }
}

impl Vec4 {
    pub fn zero() -> Vec4 {
        Self { x: 0.0, y: 0.0, z: 0.0, w: 0.0 }
    }

    pub fn from_array(data: [f32; 4]) -> Vec4 {
        Vec4 { x: data[0], y: data[1], z: data[2], w: data[3] }
    }

    pub fn dot(&self, rhs: &Self) -> f32 {
        self.x*rhs.x + self.y*rhs.y + self.z*rhs.z + self.w*rhs.w
    }

    pub fn len(&self) -> f32 {
        self.x*self.x + self.y*self.y + self.z*self.z + self.w*self.w
    }

    pub fn cross(&self, other: &Self) -> Self {
        let x = self.y*other.z - self.z*other.y;
        let y = self.z*other.x - self.x*other.z;
        let z = self.x*other.y - self.y*other.x;
        Vec4::new(x, y, z, 0.0)
    }

    pub fn normalize(&self) -> Self {
        let len = self.len().sqrt();
        Vec4::new(self.x / len, self.y / len, self.z / len, self.w / len)
    }

    pub fn apply_matrix(&mut self, matrix: Mat4) {
        let aux = self.clone();
        let m = matrix.transpose();
        self.x = aux.dot(&m.data[0]);
        self.y = aux.dot(&m.data[1]);
        self.z = aux.dot(&m.data[2]);
        self.w = aux.dot(&m.data[3]);
    }
}

#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq)]
pub struct Mat4 {
    pub data: [Vec4; 4],
}

impl Default for Mat4 {
    fn default() -> Self { Mat4::identity() }
}

impl Mat4 {
    pub fn new(data: [Vec4; 4]) -> Mat4 {
        Mat4 { data }
    }

    pub fn identity() -> Mat4 {
        let data: [Vec4; 4] = [
            Vec4::new(1.0, 0.0, 0.0, 0.0),
            Vec4::new(0.0, 1.0, 0.0, 0.0),
            Vec4::new(0.0, 0.0, 1.0, 0.0),
            Vec4::new(0.0, 0.0, 0.0, 1.0),
        ];
        Mat4 { data }
    }

    pub fn ortho(left: f32, right: f32, bottom: f32, top: f32, near: f32, far: f32) -> Mat4 {
        let mut data: [Vec4; 4] = [Vec4::zero(); 4];
        data[0].x = 2.0 / (right - left);
        data[0].y = 0.0;
        data[0].z = 0.0;
        data[0].w = 0.0;

        data[1].x = 0.0;
        data[1].y = 2.0 / (top - bottom);
        data[1].z = 0.0;
        data[1].w = 0.0;
        
        data[2].x = 0.0;
        data[2].y = 0.0;
        data[2].z = -2.0 / (far - near);
        data[2].w = 0.0;

        data[3].x = -(right + left) / (right - left);
        data[3].y = -(top + bottom) / (top - bottom);
        data[3].z = -(far + near) / (far - near);
        data[3].w = 1.0;
        Mat4 { data }
    }

    pub fn perspective(fov: f32, aspect: f32, near: f32, far: f32) -> Mat4 {
        let mut data: [Vec4; 4] = [Vec4::zero(); 4];
        let f = 1.0 / (fov / 2.0).tan();
        let inv_range = 1.0 / (near - far);

        data[0].x = f / aspect;
        data[0].y = 0.0;
        data[0].z = 0.0;
        data[0].w = 0.0;

        data[1].x = 0.0;
        data[1].y = f;
        data[1].z = 0.0;
        data[1].w = 0.0;
        
        data[2].x = 0.0;
        data[2].y = 0.0;
        data[2].z = (far + near) * inv_range;
        data[2].w = -1.0;

        data[3].x = 0.0;
        data[3].y = 0.0;
        data[3].z = 2.0 * far * near * inv_range;
        data[3].w = 0.0;
        Mat4 { data }
    }

    pub fn look_at(position: Vec3, target: Vec3, up: Vec3) -> Mat4 {
        let f = (target - position).normalize();
        let r = up.cross(&f).normalize();
        let u = f.cross(&r);
        
        let data: [Vec4; 4] = [
            Vec4::new(r.x, u.x, -f.x, 0.0),
            Vec4::new(r.y, u.y, -f.y, 0.0),
            Vec4::new(r.z, u.z, -f.z, 0.0),
            Vec4::new(-r.dot(&position), -u.dot(&position), f.dot(&position), 1.0),
        ];
        Mat4 { data }
    }

    pub fn from_array(data: [f32; 16]) -> Mat4 {
        let mut dt: [Vec4; 4] = [Vec4::zero(); 4];

        for i in 0..4 {
            let index = 4 * i;
            dt[i] = Vec4::new(data[index], data[index+1], data[index+2], data[index+3]);
        }
        Mat4 { data: dt }
    }

    pub fn translation_matrix(pos: Vec3) -> Mat4 {
        let mut mat = Mat4::identity();
        mat.data[3].x = pos.x;
        mat.data[3].y = pos.y;
        mat.data[3].z = pos.z;
        mat
    }

    pub fn scale_matrix(scale: Vec3) -> Mat4 {
        let mut mat = Mat4::identity();
        mat.data[0].x = scale.x;
        mat.data[1].y = scale.y;
        mat.data[2].z = scale.z;
        mat
    }

    pub fn rotation_matrix(axis: Vec3, angle: f32) -> Mat4 {
        let r = angle.to_radians();
        let mut m = Mat4::identity();
        m
    }

    pub fn rotation_matrix_z(angle: f32) -> Mat4 {
        let rad = angle.to_radians();
        let mut m = Mat4::identity();
        m.data[0].x = rad.cos();
        m.data[0].y = rad.sin();
        m.data[1].x = -rad.sin();
        m.data[1].y = rad.cos();
        m.data[2].z = 1.0;
        m.data[3].w = 1.0;
        m
    }

    pub fn transpose(&self) -> Mat4 {
        let mut data: [f32; 16] = [0.0; 16];

        let mut i = 0;
        for vec in self.data {
            data[i] = vec.x;
            data[4+i] = vec.y;
            data[8+i] = vec.z;
            data[12+i] = vec.w;
            i += 1;
        }
        Mat4::from_array(data)
    }

    pub fn translate(&mut self, v: Vec3) {
        let mat = Mat4::translation_matrix(v);
        *self *= mat;
    }

    pub fn scale(&mut self, scale: Vec3) {
        let mat = Mat4::scale_matrix(scale);
        *self *= mat;
    }

    pub fn rotate(&mut self, angle: f32) {
        let mat = Mat4::rotation_matrix_z(angle);
        *self *= mat;
    }
}

impl Mul for Mat4 {
    type Output = Mat4;
    fn mul(self, rhs: Self) -> Self::Output {
        let aux = rhs.transpose();
        let mut data: [Vec4; 4] = [Vec4::zero(); 4];
        let mut i = 0;
        for d in &mut data {
            d.x = self.data[i].dot(&aux.data[0]);
            d.y = self.data[i].dot(&aux.data[1]);
            d.z = self.data[i].dot(&aux.data[2]);
            d.w = self.data[i].dot(&aux.data[3]);
            i += 1;
        }

        Mat4 { data }
    }
}

impl MulAssign for Mat4 {
    fn mul_assign(&mut self, rhs: Self) {
        let aux = rhs.transpose();
        let mut data: [Vec4; 4] = [Vec4::zero(); 4];
        let mut i = 0;
        for d in &mut data {
            d.x = self.data[i].dot(&aux.data[0]);
            d.y = self.data[i].dot(&aux.data[1]);
            d.z = self.data[i].dot(&aux.data[2]);
            d.w = self.data[i].dot(&aux.data[3]);
            i += 1;
        }
        self.data = data;
    }
}

impl_glsl!(Mat4, "mat4");

impl GlUniform for Mat4 {
    fn uniform(location: i32, val: &Self) {
        Self::uniformv(location, 1, val);
    }
    
    fn uniformv(location: i32, count: i32, ptr: *const Self) {
        unsafe {
            gl::UniformMatrix4fv(location, count, gl::FALSE, ptr as *const f32);
        }
    }

    fn send_uniform(&self, location: i32) {
        Self::uniform(location, self);
    }
}