diff options
author | HampusM <hampus@hampusmat.com> | 2025-05-10 13:10:46 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2025-05-10 13:42:34 +0200 |
commit | 6b2feeab0b845d05cdf76328d44b7b70a5e50c18 (patch) | |
tree | c214ad5e89a0a289d32094fb934caa6325b6cb4e /engine/src/renderer | |
parent | c1db67dbeaeeb447b8b4361ae6bb750785ed04bd (diff) |
refactor(engine): make GL renderer have it's own Vertex struct
Diffstat (limited to 'engine/src/renderer')
-rw-r--r-- | engine/src/renderer/opengl.rs | 12 | ||||
-rw-r--r-- | engine/src/renderer/opengl/vertex.rs | 64 |
2 files changed, 74 insertions, 2 deletions
diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs index 5666061..43ec16c 100644 --- a/engine/src/renderer/opengl.rs +++ b/engine/src/renderer/opengl.rs @@ -62,14 +62,16 @@ use crate::opengl::{ ContextFlags, }; use crate::projection::{ClipVolume, Projection}; +use crate::renderer::opengl::vertex::{AttributeComponentType, Vertex}; use crate::renderer::RENDER_PHASE; use crate::texture::{Id as TextureId, Texture}; use crate::transform::{Position, Scale}; use crate::util::{defer, Defer, RefOrValue}; use crate::vector::{Vec2, Vec3}; -use crate::vertex::{AttributeComponentType, Vertex}; use crate::window::Window; +mod vertex; + type RenderableEntity<'a> = ( &'a Mesh, &'a Material, @@ -381,7 +383,13 @@ impl GlObjects let mut vertex_arr = VertexArray::new(); let mut vertex_buffer = Buffer::new(); - vertex_buffer.store(mesh.vertices(), BufferUsage::Static); + vertex_buffer.store_mapped(mesh.vertices(), BufferUsage::Static, |vertex| { + Vertex { + pos: vertex.pos, + texture_coords: vertex.texture_coords, + normal: vertex.normal, + } + }); vertex_arr.bind_vertex_buffer(0, &vertex_buffer, 0); diff --git a/engine/src/renderer/opengl/vertex.rs b/engine/src/renderer/opengl/vertex.rs new file mode 100644 index 0000000..499b94b --- /dev/null +++ b/engine/src/renderer/opengl/vertex.rs @@ -0,0 +1,64 @@ +use crate::vector::{Vec2, Vec3}; + +#[derive(Debug, Clone)] +#[repr(C)] +pub struct Vertex +{ + pub pos: Vec3<f32>, + pub texture_coords: Vec2<f32>, + pub normal: Vec3<f32>, +} + +impl Vertex +{ + pub fn attrs() -> &'static [Attribute] + { + #[allow(clippy::cast_possible_truncation)] + &[ + Attribute { + index: 0, + component_type: AttributeComponentType::Float, + component_cnt: AttributeComponentCnt::Three, + component_size: size_of::<f32>() as u32, + }, + Attribute { + index: 1, + component_type: AttributeComponentType::Float, + component_cnt: AttributeComponentCnt::Two, + component_size: size_of::<f32>() as u32, + }, + Attribute { + index: 2, + component_type: AttributeComponentType::Float, + component_cnt: AttributeComponentCnt::Three, + component_size: size_of::<f32>() as u32, + }, + ] + } +} + +#[derive(Debug)] +pub struct Attribute +{ + pub index: u32, + pub component_type: AttributeComponentType, + pub component_cnt: AttributeComponentCnt, + pub component_size: u32, +} + +#[derive(Debug)] +pub enum AttributeComponentType +{ + Float, +} + +#[derive(Debug, Clone, Copy)] +#[repr(u32)] +#[allow(dead_code)] +pub enum AttributeComponentCnt +{ + One = 1, + Two = 2, + Three = 3, + Four = 4, +} |