Skip to content

Commit

Permalink
feat: moved unsigned int types into feature flags
Browse files Browse the repository at this point in the history
  • Loading branch information
ElhamAryanpur committed Sep 10, 2024
1 parent 14d969a commit fd5f495
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 64 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

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

8 changes: 6 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "blue_engine"
version = "0.5.19"
version = "0.5.20"
authors = ["Elham Aryanpur <[email protected]>"]
edition = "2021"
description = "General-Purpose, Easy-to-use, Fast, and Portable graphics engine"
Expand All @@ -15,11 +15,15 @@ exclude = ["/examples"]
name = "blue_engine"

[features]
default = ["debug"]
default = ["debug", "u16"]
debug = ["dep:env_logger"]
android = ["dep:log", "dep:android_logger"]
android_native_activity = ["winit/android-native-activity"]
android_game_activity = ["winit/android-game-activity"]
# using u16 for indices and others
u16 = []
# using u32 for indices and others
u32 = []

[dependencies]
image = { version = "0.25.2" }
Expand Down
8 changes: 4 additions & 4 deletions examples/shapes/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,22 @@ pub fn square(name: impl StringBuffer, engine: &mut Engine) -> eyre::Result<()>
Vertex {
position: [1.0, 1.0, 0.0],
uv: [1.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, -1.0, 0.0],
uv: [1.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, -1.0, 0.0],
uv: [0.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, 1.0, 0.0],
uv: [0.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
],
vec![2, 1, 0, 2, 0, 3],
Expand Down
4 changes: 2 additions & 2 deletions src/definition.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
Pipeline, PipelineData, ShaderSettings, Shaders, StringBuffer, TextureData, TextureMode,
Textures, UniformBuffers, Vertex, VertexBuffers,
},
InstanceRaw,
InstanceRaw, UnsignedIntType,
};

impl crate::header::Renderer {
Expand Down Expand Up @@ -303,7 +303,7 @@ impl crate::header::Renderer {
pub fn build_vertex_buffer(
&mut self,
vertices: &Vec<Vertex>,
indices: &Vec<u32>,
indices: &Vec<UnsignedIntType>,
) -> eyre::Result<VertexBuffers> {
let vertex_buffer = self
.device
Expand Down
8 changes: 7 additions & 1 deletion src/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,12 @@ pub use uniform_buffer::*;

use downcast::{downcast, Any};

/// The uint type used for indices and more
#[cfg(feature = "u16")]
pub type UnsignedIntType = u16;
#[cfg(feature = "u32")]
pub type UnsignedIntType = u32;

macro_rules! impl_deref {
($struct:ty,$type:ty) => {
impl std::ops::Deref for $struct {
Expand Down Expand Up @@ -97,7 +103,7 @@ pub struct Object {
/// A list of Vertex
pub vertices: Vec<Vertex>,
/// A list of indices that dictates the order that vertices appear
pub indices: Vec<u32>,
pub indices: Vec<UnsignedIntType>,
/// Describes how to uniform buffer is structures
pub uniform_layout: wgpu::BindGroupLayout,
/// Pipeline holds all the data that is sent to GPU, including shaders and textures
Expand Down
6 changes: 3 additions & 3 deletions src/objects.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::header::{
};
use crate::uniform_type::{Array4, Matrix};
use crate::utils::default_resources::{DEFAULT_MATRIX_4, DEFAULT_SHADER, DEFAULT_TEXTURE};
use crate::{ObjectStorage, StringBuffer};
use crate::{ObjectStorage, StringBuffer, UnsignedIntType};

impl Renderer {
/// Creates a new object
Expand All @@ -27,7 +27,7 @@ impl Renderer {
&mut self,
name: impl StringBuffer,
vertices: Vec<Vertex>,
indices: Vec<u32>,
indices: Vec<UnsignedIntType>,
settings: ObjectSettings,
) -> eyre::Result<Object> {
let vertex_buffer = self.build_vertex_buffer(&vertices, &indices)?;
Expand Down Expand Up @@ -117,7 +117,7 @@ impl ObjectStorage {
&mut self,
name: impl StringBuffer,
vertices: Vec<Vertex>,
indices: Vec<u32>,
indices: Vec<UnsignedIntType>,
settings: ObjectSettings,
renderer: &mut Renderer,
) -> eyre::Result<()> {
Expand Down
81 changes: 41 additions & 40 deletions src/primitive_shapes/three_dimensions.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{ObjectSettings, ObjectStorage, Renderer, StringBuffer, Vertex};
use crate::{ObjectSettings, ObjectStorage, Renderer, StringBuffer, UnsignedIntType, Vertex};
use std::f32::consts::PI;

/// Creates a 3D cube
pub fn cube(
Expand All @@ -13,127 +14,127 @@ pub fn cube(
Vertex {
position: [-1.0, -1.0, 1.0],
uv: [0.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, -1.0, 1.0],
uv: [1.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, 1.0, 1.0],
uv: [1.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, 1.0, 1.0],
uv: [0.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
// Back Face
Vertex {
position: [-1.0, 1.0, -1.0],
uv: [1.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, 1.0, -1.0],
uv: [0.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, -1.0, -1.0],
uv: [0.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, -1.0, -1.0],
uv: [1.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
// Right face
Vertex {
position: [1.0, -1.0, -1.0],
uv: [1.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, 1.0, -1.0],
uv: [1.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, 1.0, 1.0],
uv: [0.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, -1.0, 1.0],
uv: [0.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
// Left Face
Vertex {
position: [-1.0, -1.0, 1.0],
uv: [1.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, 1.0, 1.0],
uv: [1.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, 1.0, -1.0],
uv: [0.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, -1.0, -1.0],
uv: [0.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
// Top Face
Vertex {
position: [1.0, 1.0, -1.0],
uv: [1.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, 1.0, -1.0],
uv: [0.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, 1.0, 1.0],
uv: [0.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, 1.0, 1.0],
uv: [1.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
// Bottom Face
Vertex {
position: [1.0, -1.0, 1.0],
uv: [1.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, -1.0, 1.0],
uv: [0.0, 0.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [-1.0, -1.0, -1.0],
uv: [0.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
Vertex {
position: [1.0, -1.0, -1.0],
uv: [1.0, 1.0],
normal: [0f32, 0f32, 0f32],
normal: [0.0, 0.0, 0.0],
},
],
vec![
Expand All @@ -155,7 +156,7 @@ pub fn cube(
///
/// ```
/// details = (stacks, sectors, radius)
/// example = (18, 36, 1f32)
/// example = (18, 36, 1.0)
/// ```
pub fn uv_sphere(
name: impl StringBuffer,
Expand All @@ -166,21 +167,21 @@ pub fn uv_sphere(
let sectors = details.1 as f32;
let stacks = details.0 as f32;
let length_inv = 1. / details.2;
let sector_step = 2. * std::f32::consts::PI / sectors;
let stack_step = std::f32::consts::PI / stacks;
let sector_step = 2. * PI / sectors;
let stack_step = PI / stacks;

let mut vertices: Vec<Vertex> = Vec::with_capacity(details.0 * details.1);
let mut indices: Vec<u32> = Vec::with_capacity(details.0 * details.1 * 2 * 3);
let mut indices: Vec<UnsignedIntType> = Vec::with_capacity(details.0 * details.1 * 2 * 3);

for i in 0..details.0 + 1 {
let stack_angle = std::f32::consts::PI / 2. - (i as f32) * stack_step;
let xy = details.2 * stack_angle.cos();
let z = details.2 * stack_angle.sin();
let stack_angle = PI / 2.0 - (i as f32) * stack_step;
let xy: f32 = details.2 * stack_angle.cos();
let z: f32 = details.2 * stack_angle.sin();

for j in 0..details.1 + 1 {
let sector_angle = (j as f32) * sector_step;
let x = xy * sector_angle.cos();
let y = xy * sector_angle.sin();
let x: f32 = xy * sector_angle.cos();
let y: f32 = xy * sector_angle.sin();

vertices.push(Vertex {
position: [x, y, z],
Expand All @@ -194,14 +195,14 @@ pub fn uv_sphere(
let mut k2 = k1 + details.1 + 1;
for _j in 0..details.1 {
if i != 0 {
indices.push(k1 as u32);
indices.push(k2 as u32);
indices.push((k1 + 1) as u32);
indices.push(k1 as UnsignedIntType);
indices.push(k2 as UnsignedIntType);
indices.push((k1 + 1) as UnsignedIntType);
}
if i != details.0 - 1 {
indices.push((k1 + 1) as u32);
indices.push(k2 as u32);
indices.push((k2 + 1) as u32);
indices.push((k1 + 1) as UnsignedIntType);
indices.push(k2 as UnsignedIntType);
indices.push((k2 + 1) as UnsignedIntType);
}
k1 += 1;
k2 += 1;
Expand Down
Loading

0 comments on commit fd5f495

Please sign in to comment.