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
use crate::opengl::gl_unbind_array_buffer;
use crate::opengl::gl_draw_arrays;
use crate::opengl::gl_unbind_element_buffer;
use crate::opengl::gl_draw_elements;
pub use nalgebra_glm as glm;

pub mod sys;
pub mod opengl;
pub mod buffers;
pub mod graphics;
pub mod vertex;

use sys::*;
use buffers::*;
use graphics::*;
use vertex::*;
use opengl::{opengl, gl};

const CLIP_NEAR_DEFAULT: f32 = 0.1;
const CLIP_FAR_DEFAULT: f32 = 1000.;

// pub trait DrawSurface {

//     fn swap_buffers(&self);

//     fn clear(&self, color: &glm::Vec4) {
//         unsafe {
//             opengl().ClearColor(color.x, color.y, color.z, color.w);
//             opengl().Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
//         }
//     }

//     fn width(&self) -> f32;
//     fn height(&self) -> f32;
// }

pub trait Renderable {  
    fn draw(&self, renderer: &Renderer);
}

#[derive(Copy, Clone, Debug, Default)]
struct ProjectionInfo {
    width: f32, height: f32,
    fov_deg: f32, clip_near: f32, clip_far: f32
}

impl ProjectionInfo {
    pub fn aspect(&self) -> f32 { self.width / self.height }
    pub fn fov_rad(&self) -> f32 { sys::radians(self.fov_deg) }

    pub fn to_matrix(&self) -> glm::Mat4 {
        glm::perspective(self.aspect(), self.fov_rad(), self.clip_near, self.clip_far)
    }
}

pub struct Renderer {
    pub camera: FlyCamera,

    draw_vao: VAO,
    quad_buffer: VertexBuffer,
    instanced_mat_buffer: VertexBuffer,

    shader: Shader,
    instanced_shader: Shader,
    
    default_texture: Texture,
    projection: glm::Mat4,
    projection_info: ProjectionInfo,
}

// TODO :: Implement some type of builder pattern for renderer to pass flags on create,
//         until then we have no way of modifying renderer defaults outside of cumbersome set_* calls
// TODO :: Implement Instancing
impl Renderer {

    pub const DEFAULT_FOV: f32 = 45.;
    pub const U_PROJECTION: &'static str = "u_projection";
    pub const U_VIEW: &'static str = "u_view";
    pub const U_MODEL: &'static str = "u_model";

    pub fn new(width: u32, height: u32) -> Self {     
        unsafe {
            let gl = opengl();
            gl.Viewport(0, 0, width as i32, height as i32);
            gl.Enable(gl::DEPTH_TEST);
            gl.Enable(gl::BLEND);
            gl.BlendFunc(gl::SRC_ALPHA, gl::ONE_MINUS_SRC_ALPHA);
        };
        let projection_info = ProjectionInfo { 
            width: width as f32, height: height as f32, 
            fov_deg: Self::DEFAULT_FOV, clip_near: 0.1, clip_far: 100. 
        };

        let projection = projection_info.to_matrix();

        let camera = { 
            let mut c = FlyCamera::new();
            c.position.z = -3.;
            c.look_direction.z = 3.;
            c
        };

        let shader = Shader::default();
        let instanced_shader = Shader::default_instanced();
        let default_texture = Texture::new_blank();

        let quad_buffer = VertexBuffer::new(&sys::Quad::default_verts(), DrawUsage::Dynamic);
        let instanced_mat_buffer = VertexBuffer::zeroed::<glm::Mat4>(2, DrawUsage::Dynamic, DrawPrimitive::Triangles);

        let draw_vao = VAO::new();

        Renderer { 
            camera, draw_vao, quad_buffer,
            instanced_mat_buffer, shader, 
            instanced_shader,
            default_texture, projection,
            projection_info
        }
    }

    pub fn set_viewport(&mut self, rect: &Rectf) {
        self.projection_info.width = rect.w;
        self.projection_info.height = rect.h;
        self.projection = self.projection_info.to_matrix();
        unsafe { opengl().Viewport(rect.x as i32, rect.y as i32, rect.w as i32, rect.h as i32) }
    }

    // NOTE :: Maybe add a way to set near and far clip here as well?
    pub fn set_projection(&mut self, width: f32, height: f32, fov_deg: f32) {
        self.projection_info = ProjectionInfo {
            width, height, fov_deg, ..self.projection_info
        };
        self.projection = self.projection_info.to_matrix();
    }
    
    pub fn begin_draw_texture(rt: &RenderTexture) {
        FrameBuffer::apply(&rt.frame_buffer);
    }
    pub fn end_draw_texture() {
        FrameBuffer::unbind();
    }

    pub fn draw<T>(&self, renderable: &T) where T: Renderable {
        renderable.draw(self);
    }

    pub fn draw_mesh(&self, mesh: &Mesh) {
        let shader = match mesh.shader.as_ref() {
            Some(s) => s,
            None => {
                self.shader.set_uniform_matrix("u_projection", &self.projection);
                self.shader.set_uniform_matrix("u_view", &self.camera.view());
                self.shader.set_uniform_matrix("u_model", mesh.transform.model());
                &self.shader
            }
        };

        shader.apply();

        self.draw_vao.set_buffer_layout(&mesh.buffer);

        let texture = mesh.texture.as_ref().unwrap_or(&self.default_texture);
        texture.apply();

        if let Some(e) = mesh.indices.as_ref() {
            self.draw_elements(&e, mesh.buffer.draw_prim);
        } else {
            self.draw_arrays(0, mesh.buffer.vert_count(), mesh.buffer.draw_prim);
        }

    }

    pub fn draw_quad<'b, T>(&self, q: &Quad, texture: T) where T: Into<Option<&'b Texture>> {
        let texture = texture.into().unwrap_or(&self.default_texture);
        texture.apply();

        self.draw_vao.set_buffer_layout(&self.quad_buffer);

        self.quad_buffer.write(&q.verts, 0);

        self.draw_arrays(0, self.quad_buffer.vert_count(), DrawPrimitive::TriangleStrip);
    }

    pub fn draw_buffer<'b, T>(&self, buffer: &VertexBuffer, first_vertex: u32, texture: T) where T: Into<Option<&'b Texture>> {
        let texture = texture.into().unwrap_or(&self.default_texture);
        texture.apply();

        self.draw_vao.set_buffer_layout(&buffer);

        self.draw_arrays(first_vertex, buffer.vert_count(), buffer.draw_prim);
        // gl_unbind_array_buffer();
    }

    /** 
     * Draws given buffer without any kind of vertex transformation
    // */
    // pub fn draw_buffer_static<'b, T>(&self, buffer: &VertexBuffer, first_vertex: u32, texture: T) where T: Into<Option<&'b Texture>> {
    //     let texture = texture.into().unwrap_or(&self.default_texture);
    //     texture.apply();

    //     self.shader.set_uniform_matrix("u_projection", &glm::Mat4::identity());
    //     self.shader.set_uniform_matrix("u_view", &glm::Mat4::identity());
    //     self.shader.set_uniform_matrix("u_model", &glm::Mat4::identity());

    //     self.draw_vao.set_buffer_layout(&buffer);

    //     gl_draw_arrays(first_vertex, buffer.vert_count(), buffer.draw_prim);
    //     // gl_unbind_array_buffer();
    // }
    /** 
     * Draws given buffer without any kind of vertex transformation
    */
    pub fn draw_indexed_buffer<'b, T>(&self, buffer: &VertexBuffer, ebo: &ElementBuffer, texture: T) where T: Into<Option<&'b Texture>> {
        let texture = texture.into().unwrap_or(&self.default_texture);
        texture.apply();

        self.draw_vao.set_buffer_layout(&buffer);

        self.draw_elements(&ebo, buffer.draw_prim);
    }
    
    pub fn clear_black(&self) {
       self.clear(0., 0., 0., 1.);
    }

    pub fn use_default_shader<'b, T>(&self, xform: T) where T: Into<Option<&'b Transform>> {
        if let Some(xform) = xform.into() {
            self.shader.set_uniform_matrix("u_projection", &self.projection);
            self.shader.set_uniform_matrix("u_view", &self.camera.view());
            self.shader.set_uniform_matrix("u_model", xform.model());
        } else {
            self.shader.set_uniform_matrix("u_projection", &glm::Mat4::identity());
            self.shader.set_uniform_matrix("u_view", &glm::Mat4::identity());
            self.shader.set_uniform_matrix("u_model", &glm::Mat4::identity());
        }

        self.shader.apply(); // we dont need this apply() call... but why not for good measure lol

    }

    pub fn clear(&self, r: f32, g: f32, b: f32, a: f32) {
        unsafe {
            opengl().ClearColor(r, g, b, a);
            opengl().Clear(gl::COLOR_BUFFER_BIT | gl::DEPTH_BUFFER_BIT);
        }
    }

    pub fn projection(&self) -> &glm::Mat4 { &self.projection }
    pub fn view(&self) -> glm::Mat4 { self.camera.view() }
    
    /**
     * Binds renderer's internal VAO
    */
    pub fn set_current(&self) {
        self.draw_vao.apply();
    }
    
    fn draw_arrays(&self, start: u32, vert_count: u32, prim: DrawPrimitive) {
        self.draw_vao.apply();
        gl_draw_arrays(start, vert_count, prim);        
    }

    fn draw_elements(&self, ebo: &ElementBuffer, prim: DrawPrimitive) {
        self.draw_vao.apply();
        ebo.apply();
        gl_draw_elements(ebo.count, prim);
        ebo.unbind();
    }
}


pub struct FlyCamera {
    pub position: glm::Vec3,
    pub look_direction: glm::Vec3,
    pub rotation: glm::Vec3,

    pub speed: f32,
    pub sensitivity: f32,
}

impl FlyCamera {

    pub fn new() -> Self {
        FlyCamera::default()
    }

    pub fn view(&self) -> glm::Mat4 {
        glm::look_at(&self.position, &self.target(), &UP_VECTOR.into())
    }

    pub fn target(&self) -> glm::Vec3 {
        self.position + self.look_direction
    }

    pub fn move_forward(&mut self, delta_time: f32) {
        self.position += self.speed * self.look_direction * delta_time;
    }

    pub fn move_backward(&mut self, delta_time: f32) {
        self.position -= self.speed * self.look_direction * delta_time;
    }

    pub fn move_left(&mut self, delta_time: f32) {
        let v: glm::Vec3 = self.look_direction.cross(&UP_VECTOR.into());
        self.position -= glm::normalize(&v) * self.speed * delta_time;
    }

    pub fn move_right(&mut self, delta_time: f32) {
        let v = self.look_direction.cross(&UP_VECTOR.into());
        self.position += glm::normalize(&v) * self.speed * delta_time;
    }

    pub fn look_towards(&mut self, offset: &glm::Vec3) {
        let delta = offset * self.sensitivity;

        self.rotation += glm::vec3(delta.x, delta.y, 0.);
        self.rotation.y = sys::clamp_s(self.rotation.y, -89., 89.);
        let frontx = f32::cos(radians(self.rotation.x)) * f32::cos(radians(self.rotation.y));
        let fronty = f32::sin(radians(self.rotation.y));
        let frontz = f32::sin(radians(self.rotation.x)) * f32::cos(radians(self.rotation.y));
        
        self.look_direction = glm::normalize(&glm::vec3(frontx, fronty, frontz));
    }
}

impl Default for FlyCamera {
    
    fn default() -> Self { 
        let zvec = glm::vec3(0., 0., 0.);
        FlyCamera { 
            position: zvec,
            look_direction: zvec,
            rotation: zvec,
            speed: 0., sensitivity: 0.
        }
    }
}