Skip to content

Commit

Permalink
feat: added scissor and clear color finally
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamAryanpur committed Feb 1, 2024
1 parent d8165f3 commit ee77156
Show file tree
Hide file tree
Showing 11 changed files with 104 additions and 71 deletions.
9 changes: 1 addition & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 2 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blue_engine"
version = "0.5.3"
version = "0.5.4"
authors = ["Elham Aryanpur <elhamaryanpur5@gmail.com>"]
edition = "2021"
description = "General-Purpose, Easy-to-use, Fast, and Portable graphics engine"
Expand All @@ -17,16 +17,14 @@ name = "blue_engine"
default = ["debug"]
debug = ["dep:env_logger"]
android = ["dep:log", "dep:android_logger"]
NON_FILL_POLYGON_MODE = []

[dependencies]
image = { version = "0.24" }
futures = "0.3"
winit = { version = "0.29.8", features = ["rwh_05"] }
winit_input_helper = "0.15"
wgpu = { version = "0.19" }
bytemuck = { version = "1.14", features = ["derive"] }
winit_input_helper = "0.15"
anyhow = "1.0"
color-eyre = "0.6"
downcast = "0.11"
nalgebra-glm = "0.18"
Expand Down
2 changes: 1 addition & 1 deletion examples/shapes/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use blue_engine::{
StringBuffer,
};

pub fn square(name: impl StringBuffer, engine: &mut Engine) -> anyhow::Result<()> {
pub fn square(name: impl StringBuffer, engine: &mut Engine) -> color_eyre::Result<()> {
engine.objects.new_object(
name,
vec![
Expand Down
10 changes: 5 additions & 5 deletions src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl crate::header::Renderer {
vertex_buffer: VertexBuffers,
texture: Textures,
uniform: Option<UniformBuffers>,
) -> Result<Pipeline, anyhow::Error> {
) -> color_eyre::Result<Pipeline> {
Ok(Pipeline {
shader: PipelineData::Data(shader),
vertex_buffer: PipelineData::Data(vertex_buffer),
Expand All @@ -39,7 +39,7 @@ impl crate::header::Renderer {
shader_source: String,
uniform_layout: Option<&BindGroupLayout>,
settings: ShaderSettings,
) -> Result<Shaders, anyhow::Error> {
) -> color_eyre::Result<Shaders> {
let shader = self
.device
.create_shader_module(wgpu::ShaderModuleDescriptor {
Expand Down Expand Up @@ -117,7 +117,7 @@ impl crate::header::Renderer {
texture_data: TextureData,
texture_mode: TextureMode,
//texture_format: TextureFormat,
) -> anyhow::Result<Textures> {
) -> color_eyre::Result<Textures> {
let mode: wgpu::AddressMode;
match texture_mode {
TextureMode::Clamp => mode = wgpu::AddressMode::Repeat,
Expand Down Expand Up @@ -266,7 +266,7 @@ impl crate::header::Renderer {
pub fn build_uniform_buffer(
&mut self,
uniforms: &Vec<wgpu::Buffer>,
) -> Result<(UniformBuffers, BindGroupLayout), anyhow::Error> {
) -> color_eyre::Result<(UniformBuffers, BindGroupLayout)> {
let mut buffer_entry = Vec::<wgpu::BindGroupEntry>::new();
let mut buffer_layout = Vec::<wgpu::BindGroupLayoutEntry>::new();

Expand Down Expand Up @@ -309,7 +309,7 @@ impl crate::header::Renderer {
&mut self,
verticies: &Vec<Vertex>,
indicies: &Vec<u16>,
) -> Result<VertexBuffers, anyhow::Error> {
) -> color_eyre::Result<VertexBuffers> {
let vertex_buffer = self
.device
.create_buffer_init(&wgpu::util::BufferInitDescriptor {
Expand Down
19 changes: 18 additions & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ pub struct Renderer {
pub default_data: Option<(crate::Textures, crate::Shaders, crate::UniformBuffers)>,
/// The camera used in the engine
pub camera: Option<crate::UniformBuffers>,
/// Background clear color
pub clear_color: wgpu::Color,
/// Scissor cut section of the screen to render to
/// (x, y, width, height)
pub scissor_rect: Option<(u32, u32, u32, u32)>,
}
unsafe impl Sync for Renderer {}
unsafe impl Send for Renderer {}
Expand All @@ -282,18 +287,30 @@ pub struct WindowDescriptor {
pub power_preference: crate::PowerPreference,
/// The backend to use for the draw
pub backends: crate::Backends,
/// The features to be enabled on a backend
pub features: crate::wgpu::Features,
}
impl std::default::Default for WindowDescriptor {
/// Will quickly create a window with default settings
fn default() -> Self {
let backends = crate::Backends::all();
Self {
width: 800,
height: 600,
title: "Blue Engine",
decorations: true,
resizable: true,
power_preference: crate::PowerPreference::LowPower,
backends: crate::Backends::all(),
backends,
features: if backends.contains(wgpu::Backends::VULKAN) {
wgpu::Features::POLYGON_MODE_LINE | wgpu::Features::POLYGON_MODE_POINT
} else if backends
.contains(wgpu::Backends::VULKAN | wgpu::Backends::METAL | wgpu::Backends::DX12)
{
wgpu::Features::POLYGON_MODE_LINE
} else {
wgpu::Features::empty()
},
}
}
}
Expand Down
42 changes: 24 additions & 18 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ impl Renderer {
verticies: Vec<Vertex>,
indicies: Vec<u16>,
settings: ObjectSettings,
) -> anyhow::Result<Object> {
) -> color_eyre::Result<Object> {
let vertex_buffer = self.build_vertex_buffer(&verticies, &indicies)?;

let uniform = self.build_uniform_buffer(&vec![
Expand Down Expand Up @@ -123,7 +123,7 @@ impl ObjectStorage {
indicies: Vec<u16>,
settings: ObjectSettings,
renderer: &mut Renderer,
) -> anyhow::Result<()> {
) -> color_eyre::Result<()> {
self.add_object(
name.clone(),
renderer.build_object(name.clone(), verticies, indicies, settings)?,
Expand All @@ -140,12 +140,12 @@ impl ObjectStorage {
}

/// Adds an object to the storage
pub fn add_object(&mut self, key: impl StringBuffer, object: Object) -> anyhow::Result<()> {
pub fn add_object(&mut self, key: impl StringBuffer, object: Object) -> color_eyre::Result<()> {
fn add_object_inner(
object_storage: &mut ObjectStorage,
key: String,
object: Object,
) -> anyhow::Result<()> {
) -> color_eyre::Result<()> {
object_storage.insert(key, object);

Ok(())
Expand Down Expand Up @@ -296,7 +296,13 @@ impl Object {
}

/// Changes the color of the object. If textures exist, the color of textures will change
pub fn set_color(&mut self, red: f32, green: f32, blue: f32, alpha: f32) -> anyhow::Result<()> {
pub fn set_color(
&mut self,
red: f32,
green: f32,
blue: f32,
alpha: f32,
) -> color_eyre::Result<()> {
self.color = Array4 {
data: [red, green, blue, alpha],
};
Expand All @@ -311,7 +317,7 @@ impl Object {
green: f32,
blue: f32,
alpha: f32,
) -> anyhow::Result<()> {
) -> color_eyre::Result<()> {
self.uniform_color = Array4 {
data: [red, green, blue, alpha],
};
Expand All @@ -323,14 +329,14 @@ impl Object {
/// Changes the render order of the Object.
///
/// Objects with higher number get rendered later and appear "on top" when occupying the same space
pub fn set_render_order(&mut self, render_order: usize) -> anyhow::Result<()> {
pub fn set_render_order(&mut self, render_order: usize) -> color_eyre::Result<()> {
self.render_order = render_order;

Ok(())
}

/// Replaces the object's texture with provided one
pub fn set_texture(&mut self, texture: Textures) -> anyhow::Result<()> {
pub fn set_texture(&mut self, texture: Textures) -> color_eyre::Result<()> {
self.pipeline.texture = PipelineData::Data(texture);
self.changed = true;

Expand Down Expand Up @@ -358,7 +364,7 @@ impl Object {
}

/// Update and apply changes done to an object
pub fn update(&mut self, renderer: &mut Renderer) -> anyhow::Result<()> {
pub fn update(&mut self, renderer: &mut Renderer) -> color_eyre::Result<()> {
self.update_vertex_buffer(renderer)?;
self.update_uniform_buffer(renderer)?;
self.update_shader(renderer)?;
Expand All @@ -371,7 +377,7 @@ impl Object {
pub fn update_and_return(
&mut self,
renderer: &mut Renderer,
) -> anyhow::Result<(crate::VertexBuffers, crate::UniformBuffers, crate::Shaders)> {
) -> color_eyre::Result<(crate::VertexBuffers, crate::UniformBuffers, crate::Shaders)> {
let vertex_buffer = self.update_vertex_buffer_and_return(renderer)?;
let unifrom_buffer = self.update_uniform_buffer_and_return(renderer)?;
let shader = self.update_shader_and_return(renderer)?;
Expand All @@ -380,7 +386,7 @@ impl Object {
}

/// Update and apply changes done to the vertex buffer
pub fn update_vertex_buffer(&mut self, renderer: &mut Renderer) -> anyhow::Result<()> {
pub fn update_vertex_buffer(&mut self, renderer: &mut Renderer) -> color_eyre::Result<()> {
let updated_buffer = renderer.build_vertex_buffer(&self.vertices, &self.indices)?;
self.pipeline.vertex_buffer = PipelineData::Data(updated_buffer);

Expand All @@ -391,7 +397,7 @@ impl Object {
pub fn update_vertex_buffer_and_return(
&mut self,
renderer: &mut Renderer,
) -> anyhow::Result<crate::VertexBuffers> {
) -> color_eyre::Result<crate::VertexBuffers> {
let updated_buffer = renderer.build_vertex_buffer(&self.vertices, &self.indices)?;
let updated_buffer_2 = renderer.build_vertex_buffer(&self.vertices, &self.indices)?;
self.pipeline.vertex_buffer = PipelineData::Data(updated_buffer);
Expand All @@ -400,7 +406,7 @@ impl Object {
}

/// Update and apply changes done to the shader
pub fn update_shader(&mut self, renderer: &mut Renderer) -> anyhow::Result<()> {
pub fn update_shader(&mut self, renderer: &mut Renderer) -> color_eyre::Result<()> {
let updated_shader = renderer.build_shader(
self.name.as_str(),
self.shader_builder.shader.clone(),
Expand All @@ -416,7 +422,7 @@ impl Object {
pub fn update_shader_and_return(
&mut self,
renderer: &mut Renderer,
) -> anyhow::Result<crate::Shaders> {
) -> color_eyre::Result<crate::Shaders> {
let updated_shader = renderer.build_shader(
self.name.as_str(),
self.shader_builder.shader.clone(),
Expand All @@ -435,7 +441,7 @@ impl Object {
}

/// Update and apply changes done to the uniform buffer
pub fn update_uniform_buffer(&mut self, renderer: &mut Renderer) -> anyhow::Result<()> {
pub fn update_uniform_buffer(&mut self, renderer: &mut Renderer) -> color_eyre::Result<()> {
self.uniform_buffers[0] = renderer.build_uniform_buffer_part(
"Transformation Matrix",
uniform_type::Matrix::from_im(
Expand All @@ -456,7 +462,7 @@ impl Object {
pub fn update_uniform_buffer_and_return(
&mut self,
renderer: &mut Renderer,
) -> anyhow::Result<crate::UniformBuffers> {
) -> color_eyre::Result<crate::UniformBuffers> {
self.uniform_buffers[0] = renderer.build_uniform_buffer_part(
"Transformation Matrix",
uniform_type::Matrix::from_im(
Expand All @@ -475,7 +481,7 @@ impl Object {
}

/// Updates the instance buffer
pub fn update_instance_buffer(&mut self, renderer: &mut Renderer) -> anyhow::Result<()> {
pub fn update_instance_buffer(&mut self, renderer: &mut Renderer) -> color_eyre::Result<()> {
let instance_data = self
.instances
.iter()
Expand All @@ -490,7 +496,7 @@ impl Object {
pub fn update_instance_buffer_and_return(
&mut self,
renderer: &mut Renderer,
) -> anyhow::Result<wgpu::Buffer> {
) -> color_eyre::Result<wgpu::Buffer> {
let instance_data = self
.instances
.iter()
Expand Down
4 changes: 2 additions & 2 deletions src/primitive_shapes/three_dimensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ pub fn cube(
name: impl StringBuffer,
renderer: &mut Renderer,
objects: &mut ObjectStorage,
) -> anyhow::Result<()> {
) -> color_eyre::Result<()> {
objects.new_object(
name.clone(),
vec![
Expand Down Expand Up @@ -162,7 +162,7 @@ pub fn uv_sphere(
details: (usize, usize, f32),
renderer: &mut Renderer,
objects: &mut ObjectStorage,
) -> anyhow::Result<()> {
) -> color_eyre::Result<()> {
let sectors = details.1 as f32;
let stacks = details.0 as f32;
let length_inv = 1. / details.2;
Expand Down
Loading

0 comments on commit ee77156

Please sign in to comment.