tiny_game_framework/graphics/
renderer.rs

1use std::{collections::HashMap, ffi::CString};
2
3use gl::{types::GLuint, UseProgram};
4use glam::{vec3, Vec2, Vec3, Vec4};
5
6use crate::{cstr, load_texture, Camera, EventLoop, InstanceMesh, Light, Model, Particle, Shader, Texture, DEFAULT_SHADER, FULL_SHADER, INSTANCE_SHADER, LIGHT_SHADER, PARTICLE_SHADER};
7
8use super::Mesh;
9
10#[derive(PartialEq, Debug, Clone, Copy)]
11pub struct Vertex {
12    pub position: Vec3,
13    pub color: Vec4,
14    pub tex_coords: Vec2,
15    pub normal: Vec3,
16}
17
18impl Vertex {
19    pub fn new(position: Vec3, color: Vec4, tex_coords: Vec2, normal: Vec3) -> Self {
20        Self {
21            position,
22            color,
23            tex_coords,
24            normal,
25        }
26    }
27}
28
29pub struct Renderer {
30    pub models: HashMap<String, Model>,
31    pub meshes: HashMap<String, Mesh>,
32    pub instance_meshes: HashMap<String, InstanceMesh>,
33    pub lights: HashMap<String, Light>,
34    pub particles: HashMap<String, Particle>,
35
36    pub camera: Camera,
37
38    textures: HashMap<String, GLuint>,
39}
40
41impl Renderer {
42    pub fn new() -> Self {
43        let mut camera = Camera::new();
44        camera.set_projection(crate::ProjectionType::Orthographic);
45
46        Self {
47            models: HashMap::new(),
48            meshes: HashMap::new(),
49            instance_meshes: HashMap::new(),
50            lights: HashMap::new(),
51            particles: HashMap::new(),
52
53            camera,
54            textures: HashMap::new(),
55        }
56    }
57
58    pub fn add_texture(&mut self, texture_name: String, texture_path: String){
59        self.textures.insert(texture_name, unsafe { load_texture(&texture_path) });
60    }
61
62    pub fn get_texture(&self, texture_name: String) -> GLuint{
63        *self.textures.get(&texture_name).unwrap()
64    }
65
66    pub fn update(&mut self, el: &EventLoop) {
67        for particle in self.particles.values_mut() {
68            particle.update(&el);
69        }
70    }
71
72    pub unsafe fn draw(&self, el: &EventLoop) {
73        INSTANCE_SHADER.use_shader();
74        self.camera.send_uniforms(&INSTANCE_SHADER);
75        UseProgram(0);
76
77        PARTICLE_SHADER.use_shader();
78        self.camera.send_uniforms(&PARTICLE_SHADER);
79        UseProgram(0);
80
81        DEFAULT_SHADER.use_shader();
82        self.camera.send_uniforms(&DEFAULT_SHADER);
83        UseProgram(0);
84
85        LIGHT_SHADER.use_shader();
86        self.camera.send_uniforms(&LIGHT_SHADER);
87        self.send_light_uniforms(&LIGHT_SHADER);
88        UseProgram(0);
89
90        FULL_SHADER.use_shader();
91        self.camera.send_uniforms(&FULL_SHADER);
92        self.send_light_uniforms(&FULL_SHADER);
93        UseProgram(0);
94
95
96        for value in &self.instance_meshes {
97            value.1.draw(&el);
98        }
99        
100        for value in &self.meshes {
101            value.1.draw();
102        }
103
104        for model in &self.models {
105            model.1.draw();
106        }
107
108        for particle in &self.particles {
109            particle.1.draw();
110        }
111    }
112}