Merge branch '3-add-color-to-the-triangles' into 'dev'
Resolve "Add color to the triangles" See merge request Steins7/iv!4
This commit is contained in:
commit
673ba2ba8c
@ -15,6 +15,7 @@ where
|
|||||||
{
|
{
|
||||||
pipelines: Vec<SubenginePipeline<'a, I, W, O>>,
|
pipelines: Vec<SubenginePipeline<'a, I, W, O>>,
|
||||||
mouse_pos: [f32; 2],
|
mouse_pos: [f32; 2],
|
||||||
|
color: [f32; 3],
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<I, W, O> Controller<'_, I, W, O>
|
impl<I, W, O> Controller<'_, I, W, O>
|
||||||
@ -39,6 +40,7 @@ where
|
|||||||
Controller {
|
Controller {
|
||||||
pipelines: pipelines_vec,
|
pipelines: pipelines_vec,
|
||||||
mouse_pos: [0.5, 0.5],
|
mouse_pos: [0.5, 0.5],
|
||||||
|
color: [1.0, 1.0, 1.0],
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -62,10 +64,17 @@ where
|
|||||||
}
|
}
|
||||||
for input_key in &input_keys {
|
for input_key in &input_keys {
|
||||||
match input_key {
|
match input_key {
|
||||||
Key::MouseMove{x,y} => self.mouse_pos = [
|
Key::MouseMove{x,y} => {
|
||||||
(x/1280.0 * 2.0 - 1.0) as f32,
|
self.mouse_pos = [
|
||||||
(y/720.0 * 2.0 - 1.0) as f32,
|
(x/1280.0 * 2.0 - 1.0) as f32,
|
||||||
],
|
(y/720.0 * 2.0 - 1.0) as f32,
|
||||||
|
];
|
||||||
|
self.color = [
|
||||||
|
(x/1280.0) as f32,
|
||||||
|
(y/720.0) as f32,
|
||||||
|
(((x + y)/2.0)/((1280.0 + 720.0)/2.0)) as f32,
|
||||||
|
];
|
||||||
|
},
|
||||||
Key::Close => return,
|
Key::Close => return,
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
@ -83,14 +92,18 @@ where
|
|||||||
points: [self.mouse_pos, [-0.5, 0.5], [-0.5, -0.5]],
|
points: [self.mouse_pos, [-0.5, 0.5], [-0.5, -0.5]],
|
||||||
};
|
};
|
||||||
|
|
||||||
debug!("Triangle : {:#?}", triangle);
|
let colors = [
|
||||||
|
[self.color[0], self.color[1], self.color[2]],
|
||||||
|
[self.color[2], self.color[0], self.color[1]],
|
||||||
|
[self.color[1], self.color[2], self.color[0]],
|
||||||
|
];
|
||||||
|
|
||||||
for (renderer, output) in &mut pipeline.renderers {
|
for (renderer, output) in &mut pipeline.renderers {
|
||||||
// match renderer.draw_clear_frame(output, self.color) {
|
// match renderer.draw_clear_frame(output, self.color) {
|
||||||
// Err(err) => warn!("{}", err),
|
// Err(err) => warn!("{}", err),
|
||||||
// _ => (),
|
// _ => (),
|
||||||
// }
|
// }
|
||||||
match renderer.draw_triangle_frame(output, triangle) {
|
match renderer.draw_triangle_frame(output, triangle, colors) {
|
||||||
Err(err) => warn!("{}", err),
|
Err(err) => warn!("{}", err),
|
||||||
_ => (),
|
_ => (),
|
||||||
};
|
};
|
||||||
|
|||||||
@ -182,7 +182,10 @@ where
|
|||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn draw_triangle_frame<W, O>(&mut self, output: &RefCell<O>, triangle: Triangle)
|
pub fn draw_triangle_frame<W, O>(&mut self,
|
||||||
|
output: &RefCell<O>,
|
||||||
|
triangle: Triangle,
|
||||||
|
colors: [[f32; 3]; 3])
|
||||||
-> Result<(), &'static str>
|
-> Result<(), &'static str>
|
||||||
where
|
where
|
||||||
W: raw_window_handle::HasRawWindowHandle,
|
W: raw_window_handle::HasRawWindowHandle,
|
||||||
@ -223,6 +226,8 @@ where
|
|||||||
trace!("Uploading triangle data...");
|
trace!("Uploading triangle data...");
|
||||||
let points = triangle.points_flat();
|
let points = triangle.points_flat();
|
||||||
self.pipelines[0].write_vertex_buffer(&self.gpu, 0, (&points).to_vec())?; //TODO meh
|
self.pipelines[0].write_vertex_buffer(&self.gpu, 0, (&points).to_vec())?; //TODO meh
|
||||||
|
let colors_flat = colors.iter().flatten().copied().collect();
|
||||||
|
self.pipelines[0].write_vertex_buffer(&self.gpu, 1, colors_flat)?;
|
||||||
|
|
||||||
trace!("Recording CommandBuffer...");
|
trace!("Recording CommandBuffer...");
|
||||||
unsafe {
|
unsafe {
|
||||||
@ -237,7 +242,7 @@ where
|
|||||||
frame.command_buffer.begin_primary(CommandBufferFlags::ONE_TIME_SUBMIT);
|
frame.command_buffer.begin_primary(CommandBufferFlags::ONE_TIME_SUBMIT);
|
||||||
|
|
||||||
const TRIANGLE_CLEAR: ClearValue =
|
const TRIANGLE_CLEAR: ClearValue =
|
||||||
ClearValue {color : ClearColor{float32 : [0.1, 0.2, 0.3, 1.0]}};
|
ClearValue {color : ClearColor{float32 : [0.5, 0.5, 0.5, 1.0]}};
|
||||||
|
|
||||||
frame.command_buffer.begin_render_pass(
|
frame.command_buffer.begin_render_pass(
|
||||||
&swap_system.render_pass,
|
&swap_system.render_pass,
|
||||||
|
|||||||
@ -16,27 +16,31 @@ mod attachement;
|
|||||||
use self::attachement::Attachement;
|
use self::attachement::Attachement;
|
||||||
|
|
||||||
const VERTEX_SOURCE: &str =
|
const VERTEX_SOURCE: &str =
|
||||||
"#version 440 core
|
"#version 450 core
|
||||||
|
|
||||||
layout (location = 0) in vec2 position;
|
layout (location = 0) in vec2 position;
|
||||||
|
layout (location = 1) in vec3 color;
|
||||||
|
|
||||||
out gl_PerVertex {
|
layout (location = 0) out gl_PerVertex {
|
||||||
vec4 gl_Position;
|
vec4 gl_Position;
|
||||||
};
|
};
|
||||||
|
layout (location = 1) out vec3 frag_color;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
gl_Position = vec4(position, 0.0, 1.0);
|
gl_Position = vec4(position, 0.0, 1.0);
|
||||||
|
frag_color = color;
|
||||||
}";
|
}";
|
||||||
|
|
||||||
const FRAGMENT_SOURCE: &str =
|
const FRAGMENT_SOURCE: &str =
|
||||||
"#version 440 core
|
"#version 450 core
|
||||||
|
|
||||||
layout (location = 0) out vec4 frag_color;
|
layout (location = 0) out vec4 frag_color;
|
||||||
|
layout (location = 1) in vec3 color;
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
frag_color = vec4(0.5, 0.5, 0.5, 0.5);
|
frag_color = vec4(color, 1);
|
||||||
}";
|
}";
|
||||||
|
|
||||||
//--Pipeline implementation-------------------------------------------------------------------------
|
//--Pipeline implementation-------------------------------------------------------------------------
|
||||||
@ -137,19 +141,34 @@ where
|
|||||||
|
|
||||||
trace!("Creating PrimitiveAssembler...");
|
trace!("Creating PrimitiveAssembler...");
|
||||||
let buffers: Vec<VertexBufferDesc> =
|
let buffers: Vec<VertexBufferDesc> =
|
||||||
vec![VertexBufferDesc {
|
vec![
|
||||||
|
VertexBufferDesc {
|
||||||
binding: 0,
|
binding: 0,
|
||||||
stride: (size_of::<f32>()*2) as u32,
|
stride: (size_of::<f32>()*2) as u32,
|
||||||
rate: VertexInputRate::Vertex,
|
rate: VertexInputRate::Vertex,
|
||||||
|
},
|
||||||
|
VertexBufferDesc {
|
||||||
|
binding: 1,
|
||||||
|
stride: (size_of::<f32>()*3) as u32,
|
||||||
|
rate: VertexInputRate::Vertex,
|
||||||
}];
|
}];
|
||||||
let attributes: Vec<AttributeDesc> =
|
let attributes: Vec<AttributeDesc> =
|
||||||
vec![AttributeDesc {
|
vec![
|
||||||
|
AttributeDesc {
|
||||||
location: 0,
|
location: 0,
|
||||||
binding: 0,
|
binding: 0,
|
||||||
element: Element {
|
element: Element {
|
||||||
format: Format::Rgb32Sfloat,
|
format: Format::Rgb32Sfloat,
|
||||||
offset: 0,
|
offset: 0,
|
||||||
},
|
},
|
||||||
|
},
|
||||||
|
AttributeDesc {
|
||||||
|
location: 1,
|
||||||
|
binding: 1,
|
||||||
|
element: Element {
|
||||||
|
format: Format::Rgb32Sfloat,
|
||||||
|
offset: 0,
|
||||||
|
},
|
||||||
}];
|
}];
|
||||||
let input_assembler = InputAssemblerDesc {
|
let input_assembler = InputAssemblerDesc {
|
||||||
primitive: Primitive::TriangleList, //TODO switch to strips
|
primitive: Primitive::TriangleList, //TODO switch to strips
|
||||||
@ -276,7 +295,10 @@ where
|
|||||||
.destroy_shader_module(fragment_shader_module);
|
.destroy_shader_module(fragment_shader_module);
|
||||||
};
|
};
|
||||||
|
|
||||||
let vertex_buffers = vec![Attachement::new(gpu)?];
|
let vertex_buffers = vec![
|
||||||
|
Attachement::new(gpu, (size_of::<f32>()*2*3) as u64)?,
|
||||||
|
Attachement::new(gpu, (size_of::<f32>()*3*3) as u64)?,
|
||||||
|
];
|
||||||
|
|
||||||
Ok( Pipeline {
|
Ok( Pipeline {
|
||||||
set_layout,
|
set_layout,
|
||||||
|
|||||||
@ -31,9 +31,7 @@ where
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(gpu: &mut Gpu<B>) -> Result<Attachement<B>, &'static str> {
|
pub fn new(gpu: &mut Gpu<B>, size: u64) -> Result<Attachement<B>, &'static str> {
|
||||||
use std::mem::size_of;
|
|
||||||
|
|
||||||
use gfx_hal::{
|
use gfx_hal::{
|
||||||
device::Device,
|
device::Device,
|
||||||
adapter::PhysicalDevice,
|
adapter::PhysicalDevice,
|
||||||
@ -43,7 +41,7 @@ where
|
|||||||
debug!("Creating attachement...");
|
debug!("Creating attachement...");
|
||||||
let mut buffer = unsafe {
|
let mut buffer = unsafe {
|
||||||
gpu.device()
|
gpu.device()
|
||||||
.create_buffer((size_of::<f32>()*2*3) as u64, Usage::VERTEX)
|
.create_buffer(size, Usage::VERTEX)
|
||||||
.map_err(|_| "Could not create buffer")?
|
.map_err(|_| "Could not create buffer")?
|
||||||
};
|
};
|
||||||
|
|
||||||
@ -75,7 +73,7 @@ where
|
|||||||
.allocate_memory(memory_type, requirements.size)
|
.allocate_memory(memory_type, requirements.size)
|
||||||
.map_err(|_| "Could not allocate buffer memory...")?
|
.map_err(|_| "Could not allocate buffer memory...")?
|
||||||
},
|
},
|
||||||
requirements.size,
|
size,
|
||||||
)};
|
)};
|
||||||
|
|
||||||
trace!("Binding memory to buffer...");
|
trace!("Binding memory to buffer...");
|
||||||
@ -111,12 +109,15 @@ where
|
|||||||
.map_memory(&self.memory, Segment::ALL)
|
.map_memory(&self.memory, Segment::ALL)
|
||||||
.map_err(|_| "Could not map buffer memory")?;
|
.map_err(|_| "Could not map buffer memory")?;
|
||||||
|
|
||||||
//debug!("before : {}", std::ptr::read(mapped_memory as *mut f32));
|
//debug!("data : {:?}", data);
|
||||||
|
//debug!("before : {:?}", std::slice::from_raw_parts(mapped_memory as *mut f32,
|
||||||
|
// (self.size/4) as usize));
|
||||||
|
|
||||||
std::ptr::copy_nonoverlapping(data.as_ptr() as *const u8,
|
std::ptr::copy_nonoverlapping(data.as_ptr() as *const u8,
|
||||||
mapped_memory, self.size as usize);
|
mapped_memory, self.size as usize);
|
||||||
|
|
||||||
//debug!("after : {}", std::ptr::read(mapped_memory as *mut f32));
|
//debug!("after : {:?}", std::slice::from_raw_parts(mapped_memory as *mut f32,
|
||||||
|
// (self.size/4) as usize));
|
||||||
|
|
||||||
//manual deref for ManuallyDrop to not cause troubles
|
//manual deref for ManuallyDrop to not cause troubles
|
||||||
let memory_ref: &B::Memory = &self.memory;
|
let memory_ref: &B::Memory = &self.memory;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user