This repository has been archived by the owner on May 18, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adjust spinning cube example to use WindowHandler
This example uses the window handler logic to render. For now it is used to develop the missing parts in order to render the cube. * add Mesh and Cube structs
- Loading branch information
Showing
8 changed files
with
275 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,32 @@ | ||
use renderer::{Renderer, ShaderLayout, ShaderStage, VertexBufferDescriptor, VertexFormat, VertexStateDescriptor}; | ||
use std::collections::HashMap; | ||
|
||
fn main() { | ||
let vertex_source = include_str!("shaders/vertex.glsl"); | ||
let fragment_source = include_str!("shaders/fragment.glsl"); | ||
use renderer::{Renderer, Shader, ShaderStage, WindowHandler, WindowSettings}; | ||
|
||
pub struct Example { | ||
shaders: HashMap<ShaderStage, Shader>, | ||
} | ||
|
||
impl WindowHandler for Example { | ||
fn load(_window: &renderer::Window, _resources: &resources::Resources, renderer: &mut Renderer) -> Self where Self: Sized { | ||
let vertex_shader = renderer.create_shader(include_str!("shaders/vertex.glsl"), ShaderStage::Vertex); | ||
let fragment_shader = renderer.create_shader(include_str!("shaders/fragment.glsl"), ShaderStage::Fragment); | ||
|
||
let renderer = futures::executor::block_on(Renderer::new()); | ||
let _vertex_shader = renderer.create_shader(vertex_source, ShaderStage::Vertex); | ||
let fragment_shader = renderer.create_shader(fragment_source, ShaderStage::Fragment); | ||
let mut shaders = HashMap::new(); | ||
shaders.insert(ShaderStage::Vertex, vertex_shader); | ||
shaders.insert(ShaderStage::Fragment, fragment_shader); | ||
|
||
// check what is parsed | ||
let shader_layout = ShaderLayout::from_shader(&fragment_shader); | ||
dbg!(shader_layout); | ||
Example { | ||
shaders, | ||
} | ||
} | ||
|
||
let mut vertex_stage = VertexStateDescriptor::new(); | ||
vertex_stage.add(VertexBufferDescriptor::new(vec![VertexFormat::Float3])); | ||
fn render(&mut self, window: &mut renderer::Window, renderer: &mut Renderer) { | ||
let mut render_pass = renderer.begin_render_pass(); | ||
render_pass.begin(&mut window.surface, &window.depth_buffer, &self.shaders); | ||
render_pass.finish(); | ||
} | ||
} | ||
|
||
fn main() { | ||
Example::run(WindowSettings::default()); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,234 @@ | ||
use std::borrow::Cow; | ||
|
||
use crate::PrimitiveTopology; | ||
|
||
#[derive(Debug)] | ||
pub enum VertexAttributeValues { | ||
Float(Vec<f32>), | ||
Float2(Vec<[f32; 2]>), | ||
Float3(Vec<[f32; 3]>), | ||
Float4(Vec<[f32; 4]>), | ||
} | ||
|
||
impl VertexAttributeValues { | ||
pub fn len(&self) -> usize { | ||
match *self { | ||
VertexAttributeValues::Float(ref values) => values.len(), | ||
VertexAttributeValues::Float2(ref values) => values.len(), | ||
VertexAttributeValues::Float3(ref values) => values.len(), | ||
VertexAttributeValues::Float4(ref values) => values.len(), | ||
} | ||
} | ||
|
||
pub fn is_empty(&self) -> bool { | ||
self.len() == 0 | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub struct VertexAttribute { | ||
/// The name of the vertex attribute | ||
pub name: Cow<'static, str>, | ||
/// The list of vertex attribute values | ||
pub values: VertexAttributeValues, | ||
} | ||
|
||
impl VertexAttribute { | ||
/// Sets the positions for the mesh | ||
pub fn positions(positions: Vec<[f32; 3]>) -> Self { | ||
VertexAttribute { | ||
name: "positions".into(), | ||
values: VertexAttributeValues::Float3(positions), | ||
} | ||
} | ||
|
||
/// Sets the normals for the mesh | ||
pub fn normals(normals: Vec<[f32; 3]>) -> Self { | ||
VertexAttribute { | ||
name: "normals".into(), | ||
values: VertexAttributeValues::Float3(normals), | ||
} | ||
} | ||
|
||
/// Sets the tex coords / uv coords for all vertices | ||
pub fn texcoords(texcoords: Vec<[f32; 2]>) -> Self { | ||
VertexAttribute { | ||
name: "texcoords".into(), | ||
values: VertexAttributeValues::Float2(texcoords), | ||
} | ||
} | ||
} | ||
|
||
#[derive(Debug)] | ||
pub enum Indices { | ||
/// u16 based indices list | ||
U16(Vec<u16>), | ||
/// u32 based indices list | ||
U32(Vec<u32>), | ||
} | ||
|
||
/// A Mesh struct that contains vertices, normals, tex coords. | ||
#[derive(Debug)] | ||
pub struct Mesh { | ||
/// Type of primitive topology | ||
pub topology: PrimitiveTopology, | ||
/// The list of vertex attributes | ||
pub attributes: Vec<VertexAttribute>, | ||
/// The list of optional indices referencing vertex data | ||
pub indices: Option<Indices>, | ||
} | ||
|
||
impl Mesh { | ||
/// Creates a new Mesh instance | ||
pub fn new(topology: PrimitiveTopology) -> Self { | ||
Mesh { | ||
topology, | ||
attributes: Vec::new(), | ||
indices: None, | ||
} | ||
} | ||
} | ||
|
||
pub mod shape { | ||
use crate::{Indices, Mesh, PrimitiveTopology, VertexAttribute}; | ||
|
||
pub struct Cube { | ||
/// Half the side length of the cube | ||
pub size: f32, | ||
} | ||
|
||
impl Default for Cube { | ||
fn default() -> Self { | ||
Cube { size: 1.0, } | ||
} | ||
} | ||
|
||
impl From<Cube> for Mesh { | ||
fn from(cube: Cube) -> Self { | ||
let half = cube.size / 2.0; | ||
let vertices = vec![ | ||
[-half, -half, half], | ||
[ half, -half, half], | ||
[ half, half, half], | ||
[-half, half, half], | ||
|
||
[-half, -half, -half], | ||
[-half, half, -half], | ||
[ half, half, -half], | ||
[ half, -half, -half], | ||
|
||
[-half, half, -half], | ||
[-half, half, half], | ||
[ half, half, half], | ||
[ half, half, -half], | ||
|
||
[-half, -half, -half], | ||
[ half, -half, -half], | ||
[ half, -half, half], | ||
[-half, -half, half], | ||
|
||
[ half, -half, -half], | ||
[ half, half, -half], | ||
[ half, half, half], | ||
[ half, -half, half], | ||
|
||
[-half, -half, -half], | ||
[-half, -half, half], | ||
[-half, half, half], | ||
[-half, half, -half], | ||
]; | ||
|
||
let normals = vec![ | ||
[0.0, 0.0, 1.0], | ||
[0.0, 0.0, 1.0], | ||
[0.0, 0.0, 1.0], | ||
[0.0, 0.0, 1.0], | ||
|
||
[0.0, 0.0, -1.0], | ||
[0.0, 0.0, -1.0], | ||
[0.0, 0.0, -1.0], | ||
[0.0, 0.0, -1.0], | ||
|
||
[0.0, 1.0, 0.0], | ||
[0.0, 1.0, 0.0], | ||
[0.0, 1.0, 0.0], | ||
[0.0, 1.0, 0.0], | ||
|
||
[0.0, -1.0, 0.0], | ||
[0.0, -1.0, 0.0], | ||
[0.0, -1.0, 0.0], | ||
[0.0, -1.0, 0.0], | ||
|
||
[1.0, 0.0, 0.0], | ||
[1.0, 0.0, 0.0], | ||
[1.0, 0.0, 0.0], | ||
[1.0, 0.0, 0.0], | ||
|
||
[-1.0, 0.0, 0.0], | ||
[-1.0, 0.0, 0.0], | ||
[-1.0, 0.0, 0.0], | ||
[-1.0, 0.0, 0.0], | ||
]; | ||
|
||
let texcoords = vec![ | ||
[0.0, 0.0], | ||
[1.0, 0.0], | ||
[1.0, 1.0], | ||
[0.0, 1.0], | ||
|
||
[0.0, 0.0], | ||
[1.0, 0.0], | ||
[1.0, 1.0], | ||
[0.0, 1.0], | ||
|
||
[0.0, 0.0], | ||
[1.0, 0.0], | ||
[1.0, 1.0], | ||
[0.0, 1.0], | ||
|
||
[0.0, 0.0], | ||
[1.0, 0.0], | ||
[1.0, 1.0], | ||
[0.0, 1.0], | ||
|
||
[0.0, 0.0], | ||
[1.0, 0.0], | ||
[1.0, 1.0], | ||
[0.0, 1.0], | ||
|
||
[0.0, 0.0], | ||
[1.0, 0.0], | ||
[1.0, 1.0], | ||
[0.0, 1.0], | ||
]; | ||
|
||
let indices = Indices::U32(vec![ | ||
0, 1, 2, 0, 2, 3, // Front face | ||
4, 5, 6, 4, 6, 7, // Back face | ||
8, 9, 10, 8, 10, 11, // Top face | ||
12, 13, 14, 12, 14, 15, // Bottom face | ||
16, 17, 18, 16, 18, 19, // Right face | ||
20, 21, 22, 20, 22, 23 // Left face | ||
]); | ||
|
||
Mesh { | ||
topology: PrimitiveTopology::TriangleList, | ||
attributes: vec![ | ||
VertexAttribute::positions(vertices), | ||
VertexAttribute::normals(normals), | ||
VertexAttribute::texcoords(texcoords), | ||
], | ||
indices: Some(indices), | ||
} | ||
} | ||
} | ||
} | ||
|
||
/* | ||
/// Type of primitive topology | ||
pub topology: PrimitiveTopology, | ||
/// The list of vertex attributes | ||
pub attributes: Vec<VertexAttribute>, | ||
/// The list of optional indices referencing vertex data | ||
pub indices: Option<Indices>, | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
pub mod mesh; | ||
|
||
pub use mesh::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters