From 912659bb5292522a3b21f61da2e78e328026ef6b Mon Sep 17 00:00:00 2001 From: zomseffen Date: Sun, 15 Sep 2024 14:02:00 +0200 Subject: [PATCH] cuboid and cube pipeline working --- build.rs | 2 + shaders/cuboid.frag | 1 + shaders/cuboid.geom | 48 +++++++-------- shaders/frag_cuboid.spv | Bin 796 -> 852 bytes shaders/geo_cuboid.spv | Bin 16684 -> 17180 bytes src/buffer.rs | 8 +-- src/command_buffer.rs | 49 +++++++++++---- src/load_model.rs | 4 +- src/main.rs | 1 + src/primitives/cube.rs | 74 +++++++++++------------ src/primitives/rec_cuboid.rs | 114 +++++++++++++++++++---------------- src/scene.rs | 71 +++++++++++++++------- src/vertex.rs | 20 ++++-- 13 files changed, 230 insertions(+), 162 deletions(-) diff --git a/build.rs b/build.rs index 413ecb6..789fb7b 100644 --- a/build.rs +++ b/build.rs @@ -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"); diff --git a/shaders/cuboid.frag b/shaders/cuboid.frag index f53c648..61688ff 100644 --- a/shaders/cuboid.frag +++ b/shaders/cuboid.frag @@ -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; diff --git a/shaders/cuboid.geom b/shaders/cuboid.geom index 6419b62..ba46b08 100644 --- a/shaders/cuboid.geom +++ b/shaders/cuboid.geom @@ -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(); diff --git a/shaders/frag_cuboid.spv b/shaders/frag_cuboid.spv index 1ebe14020d9b8fcd611df7ae9387f181d318fae3..f9b5e3727b567bd187ab3499ef2401550a4855e3 100644 GIT binary patch delta 184 zcmbQkc7=_XnMs+Qfq{{Mn}L@>dLpl>JSPJS0|OZ6CT8XVnGBpj%mc)tKrA*fcDtw; zkdaoDm>!&2l?r4sumYjfWKl+EMUadf5HkU>JJ1j*Agu(%AOPl1&SX3-Y7La(28w`G oGXk+B&Y9g0{g zbQY8Za+QD>1l)m&B_~g0Jk2TzR4O*PkV#ol94IaZ#0o$;36LTn0Et;c`I3{jGR+47 DyF(6u diff --git a/shaders/geo_cuboid.spv b/shaders/geo_cuboid.spv index 8e45c97e3d0c86cdcb43847efce8e545b71cdf14..977dfbd51f2a7dae5ea0133a21431b72193fc55c 100644 GIT binary patch literal 17180 zcmZ{r1(+RG5=LJj2?2t;2MO+OK_)-~1cFPDVUkS3z$6nU6I}9ecXxMpcXxMpw-scU z-92ZzD-`{`ef!<*zy7LwyYH;BA8n~eX|@OqP} z)dmgOa>&x-yGAa(;!4X}Fl}veFWtH6YJIz-p2TBX+Q#ZH#~8pE#2CidfziSk$(WvM zYiH`#J-aosd#%XLlX7#S`!XiyKR(mLJW$WtR6JAjprJzt4Ozc=#JF+I~){ZgFogG~*U2Pq1v{!A4+KgPc9@{pmqjOBtgi)hfJBJS6jr%jBS9gwdZA{0= z)^@JF`H%IUX{?2uRO|lsY-`;sd!F>T&W_y*uehJjJYjrmbN3qKcWoKj+R5D}ug$DK zyaU~xtk z@3=Sn)$e|O{kE}PTej`XNjs;=xd-M9Y;PO4UfcMtma!vRC*|CWxe4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{pKLf#X zo@>1S45H5OKP}Yu$@~5@n5%!j|ESHxkaI6$&%_MR46f#U7C8CnnYHZ6IUfQiA32{5 z>^%9%`Rw3kIiCZ*(2AVTY0a}l4(Fnd9Oj(Q4X+kCoChoy^X3J|yqx>_;MJmU{?wOq zz5u*htiK>wE^@vQ*n7I3TF&{x)b7KcI%jLn7XiChwaCh%V9!d;^EKy-fpgBS?Nc}B zi-W5x$t)-aLmg&SQ%a|=B)yj zi+QVpV_we1YVc|?Z*{O-DQA&BaD#PtL{0 zaPpChO~B5Rk6dgjS3XZXljpOUJq@kM#VByj#pcxEQ_jT}@M_`Hmi95i-_h3P{c(>aty5pl!8Y(};rq5=@8Nv4?E4sMpWB`~=j6V&1G_i1@O^u*XQSqM>ibx5_TAb( zb$#CfT=jiNIQj71+J1fC3Ea*#&UhSnXE^!r-8%ce3w71^UE$cv89PAIyIk2`*-5hj+ zt2sCTPCjyAZNE7<5Zul+=b#HtK5}54-+u;E`_ACi-xB}#t8TEq#IyHM%Z-3@oylN1 z``zzBV7Ykz=>^M0{}iyC{j<8*KlSAt zJOHm2eGjI-?E6FTYT@(4VDI64we0gF)IO&@b`RViH;Ow)t zed_xB1i0$+lW_9kv$g&D{1mvIYxenRIQj6|I{W+#b=Bu*;pAOcpPwsxvd_=M$%oG` zfSo5FKEDWV=JQMNg;x0dGIjR(73yX_zY4Dw{=Js^vd^!>t3}@%sW1EdCcIjFzTX1N zh3{{Jy{GG`W#8YS_POn;b58pHF4(=Ph41fyJsUO8Q{UeQXWy;uQ`h$oz*XNrgp&{7 mt?k$MkHGC*v+p0n$%pUO`TgZkYJc;+ep~Cu|9?=w+W!Du)y7-^ diff --git a/src/buffer.rs b/src/buffer.rs index 0b56a18..65df11b 100644 --- a/src/buffer.rs +++ b/src/buffer.rs @@ -8,8 +8,8 @@ use std::ptr::copy_nonoverlapping as memcpy; pub type Mat4 = cgmath::Matrix4; 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>( instance: &Instance, device: &Device, data: &app_data::AppData, - vertices: &Vec + vertices: &Vec ) -> Result<(vk::Buffer, vk::DeviceMemory)> { - let size = (size_of::() * vertices.len()) as u64; + let size = (size_of::() * vertices.len()) as u64; let (staging_buffer, staging_buffer_memory) = create_buffer( instance, diff --git a/src/command_buffer.rs b/src/command_buffer.rs index 8d7c005..f9c5a68 100644 --- a/src/command_buffer.rs +++ b/src/command_buffer.rs @@ -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_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_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); diff --git a/src/load_model.rs b/src/load_model.rs index 0ae2e84..1eac723 100644 --- a/src/load_model.rs +++ b/src/load_model.rs @@ -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); } } } diff --git a/src/main.rs b/src/main.rs index 74ca914..5e1ad72 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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; diff --git a/src/primitives/cube.rs b/src/primitives/cube.rs index f7fba65..685ea01 100644 --- a/src/primitives/cube.rs +++ b/src/primitives/cube.rs @@ -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); } } } \ No newline at end of file diff --git a/src/primitives/rec_cuboid.rs b/src/primitives/rec_cuboid.rs index ffdd4e5..f376d81 100644 --- a/src/primitives/rec_cuboid.rs +++ b/src/primitives/rec_cuboid.rs @@ -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); } } } \ No newline at end of file diff --git a/src/scene.rs b/src/scene.rs index 032e299..a552210 100644 --- a/src/scene.rs +++ b/src/scene.rs @@ -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, pub sized_vertices: Vec, - pub indices: Vec, + pub indices_cube: Vec, + pub indices_cuboid: Vec, - 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>>, pub child_xYZ: Option>>, @@ -110,6 +134,7 @@ struct OctTree { size: usize, } +#[warn(non_snake_case)] impl OctTree { pub fn create(size: usize) -> Result { diff --git a/src/vertex.rs b/src/vertex.rs index d51ac03..a94ddec 100644 --- a/src/vertex.rs +++ b/src/vertex.rs @@ -8,6 +8,11 @@ use cgmath; pub type Vec2 = cgmath::Vector2; pub type Vec3 = cgmath::Vector3; +pub trait VertexContainer { + 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::() 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::() as u32) + .stride(size_of::() 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)