cuboid and cube pipeline working

This commit is contained in:
zomseffen 2024-09-15 14:02:00 +02:00
parent 4494b68c26
commit 912659bb52
13 changed files with 230 additions and 162 deletions

View file

@ -17,12 +17,14 @@ fn main() {
println!("cargo::rerun-if-changed=shaders/frag_cuboid.spv");
println!("cargo::rerun-if-changed=shaders/vert_cuboid.spv");
#[allow(unused_must_use)]
std::fs::remove_file("shaders/geo_cube.spv");
std::fs::remove_file("shaders/frag_cube.spv");
std::fs::remove_file("shaders/vert_cube.spv");
std::fs::remove_file("shaders/geo_cuboid.spv");
std::fs::remove_file("shaders/frag_cuboid.spv");
std::fs::remove_file("shaders/vert_cuboid.spv");
#[warn(unused_must_use)]
// probably need to check the os and have different versions
let mut command = Command::new("./shaders/compile.bat");
let output = command.output().expect("Failed to execute command");

View file

@ -2,6 +2,7 @@
layout(location = 0) in vec3 fragColor;
layout(location = 1) in vec2 fragTexCoord;
layout(location = 2) in vec3 fragSize;
layout(location = 0) out vec4 outColor;
layout(binding = 1) uniform sampler2D texSampler;

View file

@ -27,22 +27,22 @@ void main () {
float scalar_back = dot(normal_back, vec4(1, 0, 0, 0));
if (scalar_back <= 0 || ignore_scalars){
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
@ -53,22 +53,22 @@ void main () {
vec4 normal_front = geom_rotation * vec4(0, 0, 1, 0);
float scalar_front = dot(normal_front, vec4(1, 0, 0, 0));
if (scalar_front <= 0 || ignore_scalars) {
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
@ -79,22 +79,22 @@ void main () {
float scalar_up = dot(normal_up, vec4(1, 0, 0, 0));
if (scalar_up <= 0 || ignore_scalars) {
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
@ -105,22 +105,22 @@ void main () {
float scalar_down = dot(normal_down, vec4(1, 0, 0, 0));
if (scalar_down <= 0 || ignore_scalars) {
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
@ -131,22 +131,22 @@ void main () {
float scalar_left = dot(normal_left, vec4(1, 0, 0, 0));
if (scalar_left <= 0 || ignore_scalars) {
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(-geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(-geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
@ -157,22 +157,22 @@ void main () {
float scalar_right = dot(normal_right, vec4(1, 0, 0, 0));
if (scalar_right <= 0 || ignore_scalars) {
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, -geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, -geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, geoSize[0].y, geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * vec4(geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
gl_Position = ubo.proj * ubo.view * (gl_in[0].gl_Position + geom_rotation * 0.5 * vec4(geoSize[0].x, geoSize[0].y, -geoSize[0].z, 0));
fragColor = geoColor[0];
fragTexCoord = geoTexCoord[0];
EmitVertex();

Binary file not shown.

Binary file not shown.

View file

@ -8,8 +8,8 @@ use std::ptr::copy_nonoverlapping as memcpy;
pub type Mat4 = cgmath::Matrix4<f32>;
use crate::app_data;
use crate::vertex;
use crate::command_buffer;
use crate::vertex::VertexContainer;
pub unsafe fn create_buffer(
instance: &Instance,
@ -44,13 +44,13 @@ pub unsafe fn create_buffer(
Ok((buffer, buffer_memory))
}
pub unsafe fn create_vertex_buffer(
pub unsafe fn create_vertex_buffer<const COUNT: usize, T: VertexContainer<COUNT>>(
instance: &Instance,
device: &Device,
data: &app_data::AppData,
vertices: &Vec<vertex::Vertex>
vertices: &Vec<T>
) -> Result<(vk::Buffer, vk::DeviceMemory)> {
let size = (size_of::<vertex::Vertex>() * vertices.len()) as u64;
let size = (size_of::<T>() * vertices.len()) as u64;
let (staging_buffer, staging_buffer_memory) = create_buffer(
instance,

View file

@ -67,22 +67,45 @@ pub unsafe fn create_command_buffers(device: &Device, data: &mut app_data::AppDa
device.cmd_begin_render_pass(
*command_buffer, &info, vk::SubpassContents::INLINE);
device.cmd_bind_pipeline(
*command_buffer, vk::PipelineBindPoint::GRAPHICS, data.pipeline_cube);
if scene_handler.vertices.len() != 0 {
device.cmd_bind_pipeline(
*command_buffer, vk::PipelineBindPoint::GRAPHICS, data.pipeline_cube);
device.cmd_bind_vertex_buffers(*command_buffer, 0, &[scene_handler.vertex_buffer], &[0]);
device.cmd_bind_index_buffer(*command_buffer, scene_handler.index_buffer, 0, vk::IndexType::UINT32);
device.cmd_bind_vertex_buffers(*command_buffer, 0, &[scene_handler.vertex_buffer_cube], &[0]);
device.cmd_bind_index_buffer(*command_buffer, scene_handler.index_buffer_cube, 0, vk::IndexType::UINT32);
device.cmd_bind_descriptor_sets(
*command_buffer,
vk::PipelineBindPoint::GRAPHICS,
data.pipeline_layout,
0,
&[data.descriptor_sets[i]],
&[],
);
device.cmd_draw_indexed(*command_buffer, scene_handler.indices.len() as u32, 1, 0, 0, 0);
device.cmd_bind_descriptor_sets(
*command_buffer,
vk::PipelineBindPoint::GRAPHICS,
data.pipeline_layout,
0,
&[data.descriptor_sets[i]],
&[],
);
device.cmd_draw_indexed(*command_buffer, scene_handler.indices_cube.len() as u32, 1, 0, 0, 0);
}
if scene_handler.sized_vertices.len() != 0 {
device.cmd_bind_pipeline(
*command_buffer, vk::PipelineBindPoint::GRAPHICS, data.pipeline_cuboid);
device.cmd_bind_vertex_buffers(*command_buffer, 0, &[scene_handler.vertex_buffer_cuboid], &[0]);
device.cmd_bind_index_buffer(*command_buffer, scene_handler.index_buffer_cuboid, 0, vk::IndexType::UINT32);
device.cmd_bind_descriptor_sets(
*command_buffer,
vk::PipelineBindPoint::GRAPHICS,
data.pipeline_layout,
0,
&[data.descriptor_sets[i]],
&[],
);
device.cmd_draw_indexed(*command_buffer, scene_handler.indices_cuboid.len() as u32, 1, 0, 0, 0);
}
device.cmd_end_render_pass(*command_buffer);

View file

@ -47,12 +47,12 @@ pub fn load_model(data: &mut app_data::AppData, scene_handler: &mut scene::Scene
};
if let Some(index) = unique_vertices.get(&vertex) {
scene_handler.indices.push(*index as u32);
scene_handler.indices_cube.push(*index as u32);
} else {
let index = scene_handler.vertices.len();
unique_vertices.insert(vertex, index);
scene_handler.vertices.push(vertex);
scene_handler.indices.push(index as u32);
scene_handler.indices_cube.push(index as u32);
}
}
}

View file

@ -32,6 +32,7 @@ use std::mem::size_of;
use std::ptr::copy_nonoverlapping as memcpy;
use std::time::Instant;
use crate::vertex::VertexContainer;
pub mod app_data;
pub mod errors;

View file

@ -66,58 +66,58 @@ impl Drawable for Cube {
// top
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 0);
scene.indices.push(start_index as u32 + 2);
scene.indices_cube.push(start_index as u32 + 3);
scene.indices_cube.push(start_index as u32 + 0);
scene.indices_cube.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 1);
scene.indices.push(start_index as u32 + 0);
scene.indices_cube.push(start_index as u32 + 3);
scene.indices_cube.push(start_index as u32 + 1);
scene.indices_cube.push(start_index as u32 + 0);
// bottom
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 7);
scene.indices_cube.push(start_index as u32 + 6);
scene.indices_cube.push(start_index as u32 + 4);
scene.indices_cube.push(start_index as u32 + 7);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 7);
scene.indices_cube.push(start_index as u32 + 4);
scene.indices_cube.push(start_index as u32 + 5);
scene.indices_cube.push(start_index as u32 + 7);
// left
scene.indices.push(start_index as u32 + 0);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 2);
scene.indices_cube.push(start_index as u32 + 0);
scene.indices_cube.push(start_index as u32 + 4);
scene.indices_cube.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 4);
scene.indices_cube.push(start_index as u32 + 6);
scene.indices_cube.push(start_index as u32 + 2);
scene.indices_cube.push(start_index as u32 + 4);
// right
scene.indices.push(start_index as u32 + 1);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 5);
scene.indices_cube.push(start_index as u32 + 1);
scene.indices_cube.push(start_index as u32 + 3);
scene.indices_cube.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 7);
scene.indices_cube.push(start_index as u32 + 5);
scene.indices_cube.push(start_index as u32 + 3);
scene.indices_cube.push(start_index as u32 + 7);
// near
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 2);
scene.indices_cube.push(start_index as u32 + 6);
scene.indices_cube.push(start_index as u32 + 3);
scene.indices_cube.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 7);
scene.indices_cube.push(start_index as u32 + 3);
scene.indices_cube.push(start_index as u32 + 6);
scene.indices_cube.push(start_index as u32 + 7);
// far
scene.indices.push(start_index as u32 + 0);
scene.indices.push(start_index as u32 + 1);
scene.indices.push(start_index as u32 + 4);
scene.indices_cube.push(start_index as u32 + 0);
scene.indices_cube.push(start_index as u32 + 1);
scene.indices_cube.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 1);
scene.indices_cube.push(start_index as u32 + 5);
scene.indices_cube.push(start_index as u32 + 4);
scene.indices_cube.push(start_index as u32 + 1);
}
if *topology == vk::PrimitiveTopology::POINT_LIST {
scene.vertices.push(vertex::Vertex::new(
@ -125,7 +125,7 @@ impl Drawable for Cube {
self.color,
self.tex_coord
));
scene.indices.push(start_index as u32);
scene.indices_cube.push(start_index as u32);
}
}
}

View file

@ -16,109 +16,117 @@ impl Drawable for Cuboid {
fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene) {
if *topology == vk::PrimitiveTopology::TRIANGLE_LIST {
// 0 top left far
scene.vertices.push(vertex::Vertex::new(
scene.sized_vertices.push(vertex::SizedVertex::new(
vec3(self.pos.x as f32 - 0.5 * self.size.x, self.pos.y as f32 + 0.5 * self.size.y, self.pos.z as f32 + 0.5 * self.size.z),
self.color,
self.tex_coord
self.tex_coord,
self.size,
));
// 1 top right far
scene.vertices.push(vertex::Vertex::new(
scene.sized_vertices.push(vertex::SizedVertex::new(
vec3(self.pos.x as f32 + 0.5 * self.size.x, self.pos.y as f32 + 0.5 * self.size.y, self.pos.z as f32 + 0.5 * self.size.z),
self.color,
self.tex_coord
self.tex_coord,
self.size,
));
// 2 top left near
scene.vertices.push(vertex::Vertex::new(
scene.sized_vertices.push(vertex::SizedVertex::new(
vec3(self.pos.x as f32 - 0.5 * self.size.x, self.pos.y as f32 - 0.5 * self.size.y, self.pos.z as f32 + 0.5 * self.size.z),
self.color,
self.tex_coord
self.tex_coord,
self.size,
));
// 3 top right near
scene.vertices.push(vertex::Vertex::new(
scene.sized_vertices.push(vertex::SizedVertex::new(
vec3(self.pos.x as f32 + 0.5 * self.size.x, self.pos.y as f32 - 0.5 * self.size.y, self.pos.z as f32 + 0.5 * self.size.z),
self.color,
self.tex_coord
self.tex_coord,
self.size,
));
// 4 bottom left far
scene.vertices.push(vertex::Vertex::new(
scene.sized_vertices.push(vertex::SizedVertex::new(
vec3(self.pos.x as f32 - 0.5 * self.size.x, self.pos.y as f32 + 0.5 * self.size.y, self.pos.z as f32 - 0.5 * self.size.z),
self.color,
self.tex_coord
self.tex_coord,
self.size,
));
// 5 bottom right far
scene.vertices.push(vertex::Vertex::new(
scene.sized_vertices.push(vertex::SizedVertex::new(
vec3(self.pos.x as f32 + 0.5 * self.size.x, self.pos.y as f32 + 0.5 * self.size.y, self.pos.z as f32 - 0.5 * self.size.z),
self.color,
self.tex_coord
self.tex_coord,
self.size,
));
// 6 bottom left near
scene.vertices.push(vertex::Vertex::new(
scene.sized_vertices.push(vertex::SizedVertex::new(
vec3(self.pos.x as f32 - 0.5 * self.size.x, self.pos.y as f32 - 0.5 * self.size.y, self.pos.z as f32 - 0.5 * self.size.z),
self.color,
self.tex_coord
self.tex_coord,
self.size,
));
// 7 bottom right near
scene.vertices.push(vertex::Vertex::new(
scene.sized_vertices.push(vertex::SizedVertex::new(
vec3(self.pos.x as f32 + 0.5 * self.size.x, self.pos.y as f32 - 0.5 * self.size.y, self.pos.z as f32 - 0.5 * self.size.z),
self.color,
self.tex_coord
self.tex_coord,
self.size,
));
// top
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 0);
scene.indices.push(start_index as u32 + 2);
scene.indices_cuboid.push(start_index as u32 + 3);
scene.indices_cuboid.push(start_index as u32 + 0);
scene.indices_cuboid.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 1);
scene.indices.push(start_index as u32 + 0);
scene.indices_cuboid.push(start_index as u32 + 3);
scene.indices_cuboid.push(start_index as u32 + 1);
scene.indices_cuboid.push(start_index as u32 + 0);
// bottom
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 7);
scene.indices_cuboid.push(start_index as u32 + 6);
scene.indices_cuboid.push(start_index as u32 + 4);
scene.indices_cuboid.push(start_index as u32 + 7);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 7);
scene.indices_cuboid.push(start_index as u32 + 4);
scene.indices_cuboid.push(start_index as u32 + 5);
scene.indices_cuboid.push(start_index as u32 + 7);
// left
scene.indices.push(start_index as u32 + 0);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 2);
scene.indices_cuboid.push(start_index as u32 + 0);
scene.indices_cuboid.push(start_index as u32 + 4);
scene.indices_cuboid.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 4);
scene.indices_cuboid.push(start_index as u32 + 6);
scene.indices_cuboid.push(start_index as u32 + 2);
scene.indices_cuboid.push(start_index as u32 + 4);
// right
scene.indices.push(start_index as u32 + 1);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 5);
scene.indices_cuboid.push(start_index as u32 + 1);
scene.indices_cuboid.push(start_index as u32 + 3);
scene.indices_cuboid.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 7);
scene.indices_cuboid.push(start_index as u32 + 5);
scene.indices_cuboid.push(start_index as u32 + 3);
scene.indices_cuboid.push(start_index as u32 + 7);
// near
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 2);
scene.indices_cuboid.push(start_index as u32 + 6);
scene.indices_cuboid.push(start_index as u32 + 3);
scene.indices_cuboid.push(start_index as u32 + 2);
scene.indices.push(start_index as u32 + 3);
scene.indices.push(start_index as u32 + 6);
scene.indices.push(start_index as u32 + 7);
scene.indices_cuboid.push(start_index as u32 + 3);
scene.indices_cuboid.push(start_index as u32 + 6);
scene.indices_cuboid.push(start_index as u32 + 7);
// far
scene.indices.push(start_index as u32 + 0);
scene.indices.push(start_index as u32 + 1);
scene.indices.push(start_index as u32 + 4);
scene.indices_cuboid.push(start_index as u32 + 0);
scene.indices_cuboid.push(start_index as u32 + 1);
scene.indices_cuboid.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 5);
scene.indices.push(start_index as u32 + 4);
scene.indices.push(start_index as u32 + 1);
scene.indices_cuboid.push(start_index as u32 + 5);
scene.indices_cuboid.push(start_index as u32 + 4);
scene.indices_cuboid.push(start_index as u32 + 1);
}
if *topology == vk::PrimitiveTopology::POINT_LIST {
scene.sized_vertices.push(vertex::SizedVertex::new(
@ -127,7 +135,7 @@ impl Drawable for Cuboid {
self.tex_coord,
self.size,
));
scene.indices.push(start_index as u32);
scene.indices_cuboid.push(start_index as u32);
}
}
}

View file

@ -5,9 +5,7 @@ use anyhow::Result;
use cgmath::{vec2, vec3, Vector3};
use std::cell::RefCell;
use std::collections::HashMap;
use std::rc::Rc;
use rustc_hash::FxHashMap;
use crate::app_data::AppData;
use crate::buffer;
@ -28,19 +26,26 @@ const MIN_CHUNK_SIZE: usize = CHUNK_SIZE / (2 as usize).pow(MAX_TREE_DEPTH as u3
pub struct Scene {
pub vertices: Vec<vertex::Vertex>,
pub sized_vertices: Vec<vertex::SizedVertex>,
pub indices: Vec<u32>,
pub indices_cube: Vec<u32>,
pub indices_cuboid: Vec<u32>,
pub vertex_buffer: vk::Buffer,
pub vertex_buffer_memory: vk::DeviceMemory,
pub vertex_buffer_cube: vk::Buffer,
pub vertex_buffer_memory_cube: vk::DeviceMemory,
pub index_buffer: vk::Buffer,
pub index_buffer_memory: vk::DeviceMemory,
pub index_buffer_cube: vk::Buffer,
pub index_buffer_memory_cube: vk::DeviceMemory,
pub vertex_buffer_cuboid: vk::Buffer,
pub vertex_buffer_memory_cuboid: vk::DeviceMemory,
pub index_buffer_cuboid: vk::Buffer,
pub index_buffer_memory_cuboid: vk::DeviceMemory,
}
impl Scene {
pub unsafe fn prepare_data(&mut self, instance: &vulkanalia::Instance, device: &vulkanalia::Device, data: &AppData) -> Result<()> {
let mut rng = rand::thread_rng();
let grid_size = CHUNK_SIZE as i32;
let grid_size = 50; //CHUNK_SIZE as i32;
// todo store the chunks somewhere (or only use them as intermediary for neighbouthood calculation idc)
let mut oct_tree = OctTree::create(CHUNK_SIZE)?;
@ -62,40 +67,59 @@ impl Scene {
let oct_tree_iter = OctTreeIter::create(&oct_tree)?;
for item in oct_tree_iter {
let sized_index = self.sized_vertices.len();
let index = self.vertices.len();
match item {
Some(cube) => {
/*let cuboid = Cuboid {
pos: cube.pos,
color: cube.color,
tex_coord: cube.tex_coord,
size: Vector3 {x: 1.0, y: 1.0, z: 1.0},
};
cuboid.draw(&data.topology, index, self);*/
cube.draw(&data.topology, index, self);
if (cube.pos.x + cube.pos.y) as usize % 2 == 0{
/**/
let cuboid = Cuboid {
pos: cube.pos,
color: cube.color,
tex_coord: cube.tex_coord,
size: Vector3 {x: 1.0, y: 1.0, z: 1.0},
};
cuboid.draw(&data.topology, sized_index, self);
}
else {
cube.draw(&data.topology, index, self);
}
}
None => {}
}
}
(self.vertex_buffer, self.vertex_buffer_memory) = buffer::create_vertex_buffer(instance, device, &data, &self.vertices)?;
(self.index_buffer, self.index_buffer_memory) = buffer::create_index_buffer(&instance, &device, &data, &self.indices)?;
if self.vertices.len() != 0 {
(self.vertex_buffer_cube, self.vertex_buffer_memory_cube) = buffer::create_vertex_buffer(instance, device, &data, &self.vertices)?;
(self.index_buffer_cube, self.index_buffer_memory_cube) = buffer::create_index_buffer(&instance, &device, &data, &self.indices_cube)?;
}
if self.sized_vertices.len() != 0 {
(self.vertex_buffer_cuboid, self.vertex_buffer_memory_cuboid) = buffer::create_vertex_buffer(instance, device, &data, &self.sized_vertices)?;
(self.index_buffer_cuboid, self.index_buffer_memory_cuboid) = buffer::create_index_buffer(&instance, &device, &data, &self.indices_cuboid)?;
}
Ok(())
}
pub unsafe fn destroy(&mut self, device: &vulkanalia::Device) {
device.destroy_buffer(self.index_buffer, None);
device.free_memory(self.index_buffer_memory, None);
device.destroy_buffer(self.index_buffer_cube, None);
device.free_memory(self.index_buffer_memory_cube, None);
device.destroy_buffer(self.vertex_buffer, None);
device.free_memory(self.vertex_buffer_memory, None);
device.destroy_buffer(self.vertex_buffer_cube, None);
device.free_memory(self.vertex_buffer_memory_cube, None);
device.destroy_buffer(self.index_buffer_cuboid, None);
device.free_memory(self.index_buffer_memory_cuboid, None);
device.destroy_buffer(self.vertex_buffer_cuboid, None);
device.free_memory(self.vertex_buffer_memory_cuboid, None);
}
}
#[derive(Clone, Debug)]
#[allow(non_snake_case)]
struct OctTree {
pub child_XYZ: Option<Rc<RefCell<Self>>>,
pub child_xYZ: Option<Rc<RefCell<Self>>>,
@ -110,6 +134,7 @@ struct OctTree {
size: usize,
}
#[warn(non_snake_case)]
impl OctTree {
pub fn create(size: usize) -> Result<Self> {

View file

@ -8,6 +8,11 @@ use cgmath;
pub type Vec2 = cgmath::Vector2<f32>;
pub type Vec3 = cgmath::Vector3<f32>;
pub trait VertexContainer<const COUNT: usize> {
fn binding_description() -> vk::VertexInputBindingDescription;
fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; COUNT];
}
#[repr(C)]
#[derive(Copy, Clone, Debug)]
pub struct Vertex {
@ -20,8 +25,10 @@ impl Vertex {
pub const fn new(pos: Vec3, color: Vec3, tex_coord: Vec2) -> Self {
Self { pos, color, tex_coord }
}
}
pub fn binding_description() -> vk::VertexInputBindingDescription {
impl VertexContainer<3> for Vertex {
fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::builder()
.binding(0)
.stride(size_of::<Vertex>() as u32)
@ -29,7 +36,7 @@ impl Vertex {
.build()
}
pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
let pos = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(0)
@ -91,16 +98,17 @@ impl SizedVertex {
pub const fn new(pos: Vec3, color: Vec3, tex_coord: Vec2, size: Vec3) -> Self {
Self { pos, color, tex_coord, size }
}
pub fn binding_description() -> vk::VertexInputBindingDescription {
}
impl VertexContainer<4> for SizedVertex {
fn binding_description() -> vk::VertexInputBindingDescription {
vk::VertexInputBindingDescription::builder()
.binding(0)
.stride(size_of::<Vertex>() as u32)
.stride(size_of::<SizedVertex>() as u32)
.input_rate(vk::VertexInputRate::VERTEX)
.build()
}
pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 4] {
fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 4] {
let pos = vk::VertexInputAttributeDescription::builder()
.binding(0)
.location(0)