diff options
author | HampusM <hampus@hampusmat.com> | 2024-03-01 19:34:09 +0100 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-03-01 19:34:09 +0100 |
commit | 697e29e5ad4a09110180736294b8a1330a13eb6e (patch) | |
tree | 520fac535b0f63bf6efea88a5264c1aca29c90c9 /engine/src/renderer/mod.rs | |
parent | 2db4158d0eaa05b9a7cc7b2e18567cdb38e45a20 (diff) |
refactor(engine): create texture OpenGL objects when needed
Diffstat (limited to 'engine/src/renderer/mod.rs')
-rw-r--r-- | engine/src/renderer/mod.rs | 36 |
1 files changed, 33 insertions, 3 deletions
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<CameraT> { camera: CameraT, shader_programs: HashMap<u64, GlShaderProgram>, + textures: HashMap<TextureId, GlTexture>, } impl<CameraT> Renderer<CameraT> @@ -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<Item = &'obj Object>, 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 { |