diff options
author | HampusM <hampus@hampusmat.com> | 2024-06-22 14:00:27 +0200 |
---|---|---|
committer | HampusM <hampus@hampusmat.com> | 2024-06-22 14:00:27 +0200 |
commit | a0dce2164cf348416ea15c63c5baa52afa66508f (patch) | |
tree | 9cc86afee8ddb615cf2738636bc67f408f4fbafc /engine | |
parent | edb5eb812bb6a3e7ed26526fab24b110be3828d4 (diff) |
feat(engine): add ability to change polygon mode per object
Diffstat (limited to 'engine')
-rw-r--r-- | engine/src/draw_flags.rs | 49 | ||||
-rw-r--r-- | engine/src/lib.rs | 1 | ||||
-rw-r--r-- | engine/src/opengl/mod.rs | 49 | ||||
-rw-r--r-- | engine/src/renderer/opengl.rs | 22 |
4 files changed, 120 insertions, 1 deletions
diff --git a/engine/src/draw_flags.rs b/engine/src/draw_flags.rs new file mode 100644 index 0000000..c8c11c9 --- /dev/null +++ b/engine/src/draw_flags.rs @@ -0,0 +1,49 @@ +use ecs::Component; + +use crate::util::builder; + +builder! { +/// Flags for how a object should be drawn. +#[builder(name = Builder, derives = (Debug, Default, Clone))] +#[derive(Debug, Default, Clone, Component)] +#[non_exhaustive] +pub struct DrawFlags +{ + pub polygon_mode_config: PolygonModeConfig, +} +} + +impl DrawFlags +{ + pub fn builder() -> Builder + { + Builder::default() + } +} + +#[derive(Debug, Default, Clone)] +pub struct PolygonModeConfig +{ + pub face: PolygonModeFace, + pub mode: PolygonMode, +} + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum PolygonMode +{ + Point, + Line, + + #[default] + Fill, +} + +#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub enum PolygonModeFace +{ + Front, + Back, + + #[default] + FrontAndBack, +} diff --git a/engine/src/lib.rs b/engine/src/lib.rs index f9daac3..bd954be 100644 --- a/engine/src/lib.rs +++ b/engine/src/lib.rs @@ -24,6 +24,7 @@ mod util; pub mod camera; pub mod data_types; pub mod delta_time; +pub mod draw_flags; pub mod event; pub mod file_format; pub mod input; diff --git a/engine/src/opengl/mod.rs b/engine/src/opengl/mod.rs index 40f0c58..0b1bb8a 100644 --- a/engine/src/opengl/mod.rs +++ b/engine/src/opengl/mod.rs @@ -33,6 +33,13 @@ pub fn clear_buffers(mask: BufferClearMask) } } +pub fn set_polygon_mode(face: impl Into<PolygonModeFace>, mode: impl Into<PolygonMode>) +{ + unsafe { + gl::PolygonMode(face.into() as u32, mode.into() as u32); + } +} + pub fn enable(capacity: Capability) { unsafe { @@ -56,3 +63,45 @@ pub enum Capability DepthTest = gl::DEPTH_TEST, MultiSample = gl::MULTISAMPLE, } + +#[derive(Debug)] +#[repr(u32)] +pub enum PolygonMode +{ + Point = gl::POINT, + Line = gl::LINE, + Fill = gl::FILL, +} + +impl From<crate::draw_flags::PolygonMode> for PolygonMode +{ + fn from(mode: crate::draw_flags::PolygonMode) -> Self + { + match mode { + crate::draw_flags::PolygonMode::Point => Self::Point, + crate::draw_flags::PolygonMode::Fill => Self::Fill, + crate::draw_flags::PolygonMode::Line => Self::Line, + } + } +} + +#[derive(Debug)] +#[repr(u32)] +pub enum PolygonModeFace +{ + Front = gl::FRONT, + Back = gl::BACK, + FrontAndBack = gl::FRONT_AND_BACK, +} + +impl From<crate::draw_flags::PolygonModeFace> for PolygonModeFace +{ + fn from(face: crate::draw_flags::PolygonModeFace) -> Self + { + match face { + crate::draw_flags::PolygonModeFace::Front => Self::Front, + crate::draw_flags::PolygonModeFace::Back => Self::Back, + crate::draw_flags::PolygonModeFace::FrontAndBack => Self::FrontAndBack, + } + } +} diff --git a/engine/src/renderer/opengl.rs b/engine/src/renderer/opengl.rs index 35f4938..108c504 100644 --- a/engine/src/renderer/opengl.rs +++ b/engine/src/renderer/opengl.rs @@ -14,6 +14,7 @@ use ecs::{Component, Query}; use crate::camera::Camera; use crate::color::Color; use crate::data_types::dimens::Dimens; +use crate::draw_flags::{DrawFlags, PolygonModeConfig}; use crate::event::{Present as PresentEvent, Start as StartEvent}; use crate::lighting::{DirectionalLight, GlobalLight, PointLight}; use crate::material::{Flags as MaterialFlags, Material}; @@ -102,6 +103,7 @@ fn render( Option<MaterialFlags>, Option<Position>, Option<Scale>, + Option<DrawFlags>, )>, point_light_query: Query<(PointLight,)>, directional_lights: Query<(DirectionalLight,)>, @@ -137,7 +139,9 @@ fn render( clear_buffers(BufferClearMask::COLOR | BufferClearMask::DEPTH); - for (mesh, shader_program, material, material_flags, position, scale) in &query { + for (mesh, shader_program, material, material_flags, position, scale, draw_flags) in + &query + { let material_flags = material_flags .map(|material_flags| material_flags.clone()) .unwrap_or_default(); @@ -187,7 +191,23 @@ fn render( shader_program.activate(); + if let Some(draw_flags) = &draw_flags { + crate::opengl::set_polygon_mode( + draw_flags.polygon_mode_config.face, + draw_flags.polygon_mode_config.mode, + ) + } + draw_mesh(&mesh); + + if draw_flags.is_some() { + let default_polygon_mode_config = PolygonModeConfig::default(); + + crate::opengl::set_polygon_mode( + default_polygon_mode_config.face, + default_polygon_mode_config.mode, + ) + } } } |