From 697e29e5ad4a09110180736294b8a1330a13eb6e Mon Sep 17 00:00:00 2001 From: HampusM Date: Fri, 1 Mar 2024 19:34:09 +0100 Subject: refactor(engine): create texture OpenGL objects when needed --- engine/src/renderer/mod.rs | 36 +++++++++++++++++++++++++++++++++--- 1 file changed, 33 insertions(+), 3 deletions(-) (limited to 'engine/src/renderer') diff --git a/engine/src/renderer/mod.rs b/engine/src/renderer/mod.rs index 8977ac9..6c04323 100644 --- a/engine/src/renderer/mod.rs +++ b/engine/src/renderer/mod.rs @@ -24,11 +24,16 @@ use crate::opengl::shader::{ Program as GlShaderProgram, Shader as GlShader, }; -use crate::opengl::texture::{set_active_texture_unit, TextureUnit}; +use crate::opengl::texture::{ + set_active_texture_unit, + Texture as GlTexture, + TextureUnit, +}; use crate::opengl::vertex_array::{PrimitiveKind, VertexArray}; use crate::opengl::{clear_buffers, enable, BufferClearMask, Capability}; use crate::projection::new_perspective; use crate::shader::Program as ShaderProgram; +use crate::texture::{Id as TextureId, Texture}; use crate::vector::{Vec2, Vec3}; use crate::vertex::Vertex; @@ -37,6 +42,7 @@ pub struct Renderer { camera: CameraT, shader_programs: HashMap, + textures: HashMap, } impl Renderer @@ -73,6 +79,7 @@ where Ok(Self { camera, shader_programs: HashMap::new(), + textures: HashMap::new(), }) } @@ -111,7 +118,7 @@ where } pub fn render<'obj>( - &self, + &mut self, objects: impl IntoIterator, light_source: Option<&LightSource>, window_size: &WindowSize, @@ -143,12 +150,17 @@ where ); for (texture_id, texture) in obj.textures() { + let gl_texture = self + .textures + .entry(*texture_id) + .or_insert_with(|| create_gl_texture(texture)); + let texture_unit = TextureUnit::from_texture_id(*texture_id) .ok_or(Error::TextureIdIsInvalidTextureUnit)?; set_active_texture_unit(texture_unit); - texture.inner().bind(|_| {}); + gl_texture.bind(|_| {}); } Self::draw_object(obj); @@ -219,6 +231,24 @@ where } } +fn create_gl_texture(texture: &Texture) -> GlTexture +{ + let gl_texture = GlTexture::new(); + + gl_texture.bind(|texture_curr_bound| { + GlTexture::generate( + &texture_curr_bound, + &texture.dimensions(), + texture.image().as_bytes(), + texture.pixel_data_format(), + ); + }); + + gl_texture.apply_properties(texture.properties()); + + gl_texture +} + #[derive(Debug)] pub struct Renderable { -- cgit v1.2.3-18-g5258