pre double pipeline commit. The following will attempt to add another rendering stage for textured cuboids.
This commit is contained in:
parent
31d56ded3f
commit
c5bcd148ca
22 changed files with 854 additions and 53 deletions
41
build.rs
Normal file
41
build.rs
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
use std::process::Command;
|
||||||
|
use std::io::{self, Write};
|
||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
fn main() {
|
||||||
|
println!("cargo::rerun-if-changed=shaders/cube.frag");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/cube.geom");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/cube.vert");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/geo_cube.spv");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/frag_cube.spv");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/vert_cube.spv");
|
||||||
|
|
||||||
|
println!("cargo::rerun-if-changed=shaders/cuboid.frag");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/cuboid.geom");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/cuboid.vert");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/geo_cuboid.spv");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/frag_cuboid.spv");
|
||||||
|
println!("cargo::rerun-if-changed=shaders/vert_cuboid.spv");
|
||||||
|
|
||||||
|
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");
|
||||||
|
// 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");
|
||||||
|
println!("status: {}", output.status);
|
||||||
|
io::stdout().write_all(&output.stdout).unwrap();
|
||||||
|
io::stderr().write_all(&output.stderr).unwrap();
|
||||||
|
|
||||||
|
assert!(output.status.success());
|
||||||
|
|
||||||
|
assert!(Path::new("shaders/geo_cube.spv").exists());
|
||||||
|
assert!(Path::new("shaders/frag_cube.spv").exists());
|
||||||
|
assert!(Path::new("shaders/vert_cube.spv").exists());
|
||||||
|
assert!(Path::new("shaders/geo_cuboid.spv").exists());
|
||||||
|
assert!(Path::new("shaders/frag_cuboid.spv").exists());
|
||||||
|
assert!(Path::new("shaders/vert_cuboid.spv").exists());
|
||||||
|
}
|
|
@ -1,3 +1,7 @@
|
||||||
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/shader.vert -o shaders/vert.spv
|
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/cube.vert -o shaders/vert_cube.spv
|
||||||
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/shader.frag -o shaders/frag.spv
|
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/cube.frag -o shaders/frag_cube.spv
|
||||||
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/shader.geom -o shaders/geo.spv
|
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/cube.geom -o shaders/geo_cube.spv
|
||||||
|
|
||||||
|
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/cuboid.vert -o shaders/vert_cuboid.spv
|
||||||
|
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/cuboid.frag -o shaders/frag_cuboid.spv
|
||||||
|
C:/VulkanSDK/1.3.280.0/Bin/glslc.exe shaders/cuboid.geom -o shaders/geo_cuboid.spv
|
11
shaders/cuboid.frag
Normal file
11
shaders/cuboid.frag
Normal file
|
@ -0,0 +1,11 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 fragColor;
|
||||||
|
layout(location = 1) in vec2 fragTexCoord;
|
||||||
|
|
||||||
|
layout(location = 0) out vec4 outColor;
|
||||||
|
layout(binding = 1) uniform sampler2D texSampler;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
outColor = vec4(fragColor, 1); //texture(texSampler, fragTexCoord);
|
||||||
|
}
|
181
shaders/cuboid.geom
Normal file
181
shaders/cuboid.geom
Normal file
|
@ -0,0 +1,181 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
layout(points) in;
|
||||||
|
layout(triangle_strip, max_vertices=12) out;
|
||||||
|
|
||||||
|
layout(binding = 0) uniform UniformBufferObject {
|
||||||
|
mat4 model;
|
||||||
|
mat4 geom_rot;
|
||||||
|
mat4 view;
|
||||||
|
mat4 proj;
|
||||||
|
bool[16] use_geom_shader;
|
||||||
|
} ubo;
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 geoColor[];
|
||||||
|
layout(location = 1) in vec2 geoTexCoord[];
|
||||||
|
layout(location = 2) in vec3 geoSize[];
|
||||||
|
|
||||||
|
layout(location = 0) out vec3 fragColor;
|
||||||
|
layout(location = 1) out vec2 fragTexCoord;
|
||||||
|
|
||||||
|
bool ignore_scalars = false;
|
||||||
|
|
||||||
|
void main () {
|
||||||
|
mat4 geom_rotation = ubo.geom_rot;
|
||||||
|
//back
|
||||||
|
vec4 normal_back = geom_rotation * vec4(0, 0, -1, 0);
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
fragColor = geoColor[0];
|
||||||
|
fragTexCoord = geoTexCoord[0];
|
||||||
|
EmitVertex();
|
||||||
|
EndPrimitive();
|
||||||
|
}
|
||||||
|
|
||||||
|
//front
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
fragColor = geoColor[0];
|
||||||
|
fragTexCoord = geoTexCoord[0];
|
||||||
|
EmitVertex();
|
||||||
|
EndPrimitive();
|
||||||
|
}
|
||||||
|
//up
|
||||||
|
vec4 normal_up = geom_rotation * vec4(0, 1, 0, 0);
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
fragColor = geoColor[0];
|
||||||
|
fragTexCoord = geoTexCoord[0];
|
||||||
|
EmitVertex();
|
||||||
|
EndPrimitive();
|
||||||
|
}
|
||||||
|
//down
|
||||||
|
vec4 normal_down = geom_rotation * vec4(0, -1, 0, 0);
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
fragColor = geoColor[0];
|
||||||
|
fragTexCoord = geoTexCoord[0];
|
||||||
|
EmitVertex();
|
||||||
|
EndPrimitive();
|
||||||
|
}
|
||||||
|
//left
|
||||||
|
vec4 normal_left = geom_rotation * vec4(-1, 0, 0, 0);
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
fragColor = geoColor[0];
|
||||||
|
fragTexCoord = geoTexCoord[0];
|
||||||
|
EmitVertex();
|
||||||
|
EndPrimitive();
|
||||||
|
}
|
||||||
|
//right
|
||||||
|
vec4 normal_right = geom_rotation * vec4(1, 0, 0, 0);
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
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));
|
||||||
|
fragColor = geoColor[0];
|
||||||
|
fragTexCoord = geoTexCoord[0];
|
||||||
|
EmitVertex();
|
||||||
|
EndPrimitive();
|
||||||
|
}
|
||||||
|
}
|
30
shaders/cuboid.vert
Normal file
30
shaders/cuboid.vert
Normal file
|
@ -0,0 +1,30 @@
|
||||||
|
#version 450
|
||||||
|
|
||||||
|
layout(binding = 0) uniform UniformBufferObject {
|
||||||
|
mat4 model;
|
||||||
|
mat4 geom_rot;
|
||||||
|
mat4 view;
|
||||||
|
mat4 proj;
|
||||||
|
bool[16] use_geom_shader;
|
||||||
|
} ubo;
|
||||||
|
|
||||||
|
|
||||||
|
layout(location = 0) in vec3 inPosition;
|
||||||
|
layout(location = 1) in vec3 inColor;
|
||||||
|
layout(location = 2) in vec2 inTexCoord;
|
||||||
|
layout(location = 3) in vec3 inSize;
|
||||||
|
|
||||||
|
layout(location = 0) out vec3 geoColor;
|
||||||
|
layout(location = 1) out vec2 geoTexCoord;
|
||||||
|
layout(location = 2) out vec3 geoSize;
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
if (ubo.use_geom_shader[0]) {
|
||||||
|
gl_Position = ubo.geom_rot * ubo.model * vec4(inPosition, 1.0);
|
||||||
|
} else {
|
||||||
|
gl_Position = ubo.proj * ubo.view * ubo.geom_rot * ubo.model * vec4(inPosition, 1.0);
|
||||||
|
}
|
||||||
|
geoColor = inColor;
|
||||||
|
geoTexCoord = inTexCoord;
|
||||||
|
geoSize = inSize;
|
||||||
|
}
|
BIN
shaders/frag_cuboid.spv
Normal file
BIN
shaders/frag_cuboid.spv
Normal file
Binary file not shown.
BIN
shaders/geo_cuboid.spv
Normal file
BIN
shaders/geo_cuboid.spv
Normal file
Binary file not shown.
BIN
shaders/vert_cuboid.spv
Normal file
BIN
shaders/vert_cuboid.spv
Normal file
Binary file not shown.
|
@ -642,9 +642,9 @@ unsafe fn create_logical_device(
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Result<()> {
|
unsafe fn create_pipeline(device: &Device, data: &mut app_data::AppData) -> Result<()> {
|
||||||
let vert = include_bytes!("../shaders/vert.spv");
|
let vert = include_bytes!("../shaders/vert_cube.spv");
|
||||||
let geo = include_bytes!("../shaders/geo.spv");
|
let geo = include_bytes!("../shaders/geo_cube.spv");
|
||||||
let frag = include_bytes!("../shaders/frag.spv");
|
let frag = include_bytes!("../shaders/frag_cube.spv");
|
||||||
|
|
||||||
let vert_shader_module = create_shader_module(device, &vert[..])?;
|
let vert_shader_module = create_shader_module(device, &vert[..])?;
|
||||||
let geo_shader_module = create_shader_module(device, &geo[..])?;
|
let geo_shader_module = create_shader_module(device, &geo[..])?;
|
||||||
|
|
|
@ -1 +0,0 @@
|
||||||
pub mod cube;
|
|
|
@ -2,6 +2,7 @@ use vulkanalia::prelude::v1_0::*;
|
||||||
use cgmath::vec3;
|
use cgmath::vec3;
|
||||||
use crate::vertex;
|
use crate::vertex;
|
||||||
use crate::scene::Scene;
|
use crate::scene::Scene;
|
||||||
|
use crate::primitives::drawable::Drawable;
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Cube{
|
pub struct Cube{
|
||||||
|
@ -10,8 +11,8 @@ pub struct Cube{
|
||||||
pub tex_coord: vertex::Vec2
|
pub tex_coord: vertex::Vec2
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cube {
|
impl Drawable for Cube {
|
||||||
pub fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene) {
|
fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene) {
|
||||||
if *topology == vk::PrimitiveTopology::TRIANGLE_LIST {
|
if *topology == vk::PrimitiveTopology::TRIANGLE_LIST {
|
||||||
// 0 top left far
|
// 0 top left far
|
||||||
scene.vertices.push(vertex::Vertex::new(
|
scene.vertices.push(vertex::Vertex::new(
|
||||||
|
|
6
src/primitives/drawable.rs
Normal file
6
src/primitives/drawable.rs
Normal file
|
@ -0,0 +1,6 @@
|
||||||
|
use vulkanalia::prelude::v1_0::*;
|
||||||
|
use crate::scene::Scene;
|
||||||
|
|
||||||
|
pub trait Drawable {
|
||||||
|
fn draw(& self, topology: &vk::PrimitiveTopology, start_index: usize, scene: &mut Scene);
|
||||||
|
}
|
3
src/primitives/mod.rs
Normal file
3
src/primitives/mod.rs
Normal file
|
@ -0,0 +1,3 @@
|
||||||
|
pub mod cube;
|
||||||
|
pub mod rec_cuboid;
|
||||||
|
pub mod drawable;
|
133
src/primitives/rec_cuboid.rs
Normal file
133
src/primitives/rec_cuboid.rs
Normal file
|
@ -0,0 +1,133 @@
|
||||||
|
use vulkanalia::prelude::v1_0::*;
|
||||||
|
use cgmath::vec3;
|
||||||
|
use crate::vertex;
|
||||||
|
use crate::scene::Scene;
|
||||||
|
use crate::primitives::drawable::Drawable;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug)]
|
||||||
|
pub struct Cuboid{
|
||||||
|
pub pos: vertex::Vec3,
|
||||||
|
pub color: vertex::Vec3,
|
||||||
|
pub tex_coord: vertex::Vec2,
|
||||||
|
pub size: vertex::Vec3,
|
||||||
|
}
|
||||||
|
|
||||||
|
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(
|
||||||
|
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
|
||||||
|
));
|
||||||
|
// 1 top right far
|
||||||
|
scene.vertices.push(vertex::Vertex::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
|
||||||
|
));
|
||||||
|
// 2 top left near
|
||||||
|
scene.vertices.push(vertex::Vertex::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
|
||||||
|
));
|
||||||
|
// 3 top right near
|
||||||
|
scene.vertices.push(vertex::Vertex::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
|
||||||
|
));
|
||||||
|
|
||||||
|
// 4 bottom left far
|
||||||
|
scene.vertices.push(vertex::Vertex::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
|
||||||
|
));
|
||||||
|
// 5 bottom right far
|
||||||
|
scene.vertices.push(vertex::Vertex::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
|
||||||
|
));
|
||||||
|
// 6 bottom left near
|
||||||
|
scene.vertices.push(vertex::Vertex::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
|
||||||
|
));
|
||||||
|
// 7 bottom right near
|
||||||
|
scene.vertices.push(vertex::Vertex::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
|
||||||
|
));
|
||||||
|
|
||||||
|
|
||||||
|
// 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.push(start_index as u32 + 3);
|
||||||
|
scene.indices.push(start_index as u32 + 1);
|
||||||
|
scene.indices.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.push(start_index as u32 + 4);
|
||||||
|
scene.indices.push(start_index as u32 + 5);
|
||||||
|
scene.indices.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.push(start_index as u32 + 6);
|
||||||
|
scene.indices.push(start_index as u32 + 2);
|
||||||
|
scene.indices.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.push(start_index as u32 + 5);
|
||||||
|
scene.indices.push(start_index as u32 + 3);
|
||||||
|
scene.indices.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.push(start_index as u32 + 3);
|
||||||
|
scene.indices.push(start_index as u32 + 6);
|
||||||
|
scene.indices.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.push(start_index as u32 + 5);
|
||||||
|
scene.indices.push(start_index as u32 + 4);
|
||||||
|
scene.indices.push(start_index as u32 + 1);
|
||||||
|
}
|
||||||
|
if *topology == vk::PrimitiveTopology::POINT_LIST {
|
||||||
|
scene.sized_vertices.push(vertex::SizedVertex::new(
|
||||||
|
self.pos,
|
||||||
|
self.color,
|
||||||
|
self.tex_coord,
|
||||||
|
self.size,
|
||||||
|
));
|
||||||
|
scene.indices.push(start_index as u32);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
409
src/scene.rs
409
src/scene.rs
|
@ -1,24 +1,33 @@
|
||||||
|
use anyhow::Ok;
|
||||||
use vulkanalia::prelude::v1_0::*;
|
use vulkanalia::prelude::v1_0::*;
|
||||||
use anyhow::Result;
|
use anyhow::Result;
|
||||||
|
|
||||||
use cgmath::{vec2, vec3};
|
use cgmath::{vec2, vec3, Vector3};
|
||||||
|
|
||||||
|
use std::cell::RefCell;
|
||||||
use std::collections::HashMap;
|
use std::collections::HashMap;
|
||||||
|
use std::rc::Rc;
|
||||||
use rustc_hash::FxHashMap;
|
use rustc_hash::FxHashMap;
|
||||||
|
|
||||||
use crate::app_data::AppData;
|
use crate::app_data::AppData;
|
||||||
use crate::buffer;
|
use crate::buffer;
|
||||||
|
use crate::primitives::rec_cuboid::Cuboid;
|
||||||
use crate::vertex;
|
use crate::vertex;
|
||||||
use crate::primitives::cube::Cube;
|
use crate::primitives::cube::Cube;
|
||||||
|
use crate::primitives::drawable::Drawable;
|
||||||
|
|
||||||
extern crate rand;
|
extern crate rand;
|
||||||
use rand::Rng;
|
use rand::Rng;
|
||||||
|
|
||||||
const CHUNK_SIZE: usize = 500;
|
const CHUNK_SIZE_EXPONENT: u32 = 10;
|
||||||
|
const CHUNK_SIZE: usize = (2 as usize).pow(CHUNK_SIZE_EXPONENT);
|
||||||
|
const MAX_TREE_DEPTH: usize = 8;
|
||||||
|
const MIN_CHUNK_SIZE: usize = CHUNK_SIZE / (2 as usize).pow(MAX_TREE_DEPTH as u32);
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default)]
|
#[derive(Clone, Debug, Default)]
|
||||||
pub struct Scene {
|
pub struct Scene {
|
||||||
pub vertices: Vec<vertex::Vertex>,
|
pub vertices: Vec<vertex::Vertex>,
|
||||||
|
pub sized_vertices: Vec<vertex::SizedVertex>,
|
||||||
pub indices: Vec<u32>,
|
pub indices: Vec<u32>,
|
||||||
|
|
||||||
pub vertex_buffer: vk::Buffer,
|
pub vertex_buffer: vk::Buffer,
|
||||||
|
@ -34,7 +43,7 @@ impl Scene {
|
||||||
let grid_size = CHUNK_SIZE as i32;
|
let grid_size = CHUNK_SIZE as i32;
|
||||||
|
|
||||||
// todo store the chunks somewhere (or only use them as intermediary for neighbouthood calculation idc)
|
// todo store the chunks somewhere (or only use them as intermediary for neighbouthood calculation idc)
|
||||||
let mut chunks = vec![Chunk::create()?];
|
let mut oct_tree = OctTree::create(CHUNK_SIZE)?;
|
||||||
|
|
||||||
//todo use the 14 vertice box method. Not using geometry shaders seems to be faster... make this a setting?
|
//todo use the 14 vertice box method. Not using geometry shaders seems to be faster... make this a setting?
|
||||||
// have cube elements with a method asking for vertices, while giving a primitive type -> method for preferred primitive type as well as one collecting all primitives
|
// have cube elements with a method asking for vertices, while giving a primitive type -> method for preferred primitive type as well as one collecting all primitives
|
||||||
|
@ -46,17 +55,25 @@ impl Scene {
|
||||||
color: vec3(shade, 1.0, shade),
|
color: vec3(shade, 1.0, shade),
|
||||||
tex_coord: vec2(0.0, 0.0)
|
tex_coord: vec2(0.0, 0.0)
|
||||||
};
|
};
|
||||||
chunks[0].set_cube(cube);
|
|
||||||
|
oct_tree.set_cube(cube.clone());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
let chunk = &chunks[0];
|
let oct_tree_iter = OctTreeIter::create(&oct_tree)?;
|
||||||
let chunk_iter = ChunkIter::create(chunk)?;
|
for item in oct_tree_iter {
|
||||||
for item in chunk_iter {
|
|
||||||
let index = self.vertices.len();
|
let index = self.vertices.len();
|
||||||
match item {
|
match item {
|
||||||
Some(cube) => {
|
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);
|
cube.draw(&data.topology, index, self);
|
||||||
|
|
||||||
}
|
}
|
||||||
None => {}
|
None => {}
|
||||||
}
|
}
|
||||||
|
@ -79,16 +96,47 @@ impl Scene {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
struct Chunk {
|
struct OctTree {
|
||||||
//todo change to hashmap?
|
pub child_XYZ: Option<Rc<RefCell<Self>>>,
|
||||||
blocks: HashMap<cgmath::Vector3<u32>, Cube, rustc_hash::FxBuildHasher>,
|
pub child_xYZ: Option<Rc<RefCell<Self>>>,
|
||||||
|
pub child_xyZ: Option<Rc<RefCell<Self>>>,
|
||||||
|
pub child_XyZ: Option<Rc<RefCell<Self>>>,
|
||||||
|
pub child_XYz: Option<Rc<RefCell<Self>>>,
|
||||||
|
pub child_xYz: Option<Rc<RefCell<Self>>>,
|
||||||
|
pub child_xyz: Option<Rc<RefCell<Self>>>,
|
||||||
|
pub child_Xyz: Option<Rc<RefCell<Self>>>,
|
||||||
|
|
||||||
|
pub blocks: Vec<Option<Cube>>,
|
||||||
|
|
||||||
|
size: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Chunk {
|
impl OctTree {
|
||||||
pub fn create() -> Result<Self> {
|
pub fn create(size: usize) -> Result<Self> {
|
||||||
let mut map: HashMap<cgmath::Vector3<u32>, Cube, rustc_hash::FxBuildHasher> = FxHashMap::default();
|
let mut blocks: Vec<Option<Cube>> = vec![];
|
||||||
|
if size == MIN_CHUNK_SIZE {
|
||||||
|
for _ in 0..MIN_CHUNK_SIZE {
|
||||||
|
for _ in 0..MIN_CHUNK_SIZE {
|
||||||
|
for _ in 0..MIN_CHUNK_SIZE {
|
||||||
|
blocks.push(None);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
blocks: map
|
child_XYZ: None,
|
||||||
|
child_xYZ: None,
|
||||||
|
child_xyZ: None,
|
||||||
|
child_XyZ: None,
|
||||||
|
child_XYz: None,
|
||||||
|
child_xYz: None,
|
||||||
|
child_xyz: None,
|
||||||
|
child_Xyz: None,
|
||||||
|
|
||||||
|
blocks: blocks,
|
||||||
|
|
||||||
|
size,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,62 +144,335 @@ impl Chunk {
|
||||||
let x = cube.pos.x as usize;
|
let x = cube.pos.x as usize;
|
||||||
let y = cube.pos.y as usize;
|
let y = cube.pos.y as usize;
|
||||||
let z = cube.pos.z as usize;
|
let z = cube.pos.z as usize;
|
||||||
assert!(x < CHUNK_SIZE, "x value out of range!");
|
assert!(x < self.size, "x value out of range!");
|
||||||
assert!(y < CHUNK_SIZE, "y value out of range!");
|
assert!(y < self.size, "y value out of range!");
|
||||||
assert!(z < CHUNK_SIZE, "z value out of range!");
|
assert!(z < self.size, "z value out of range!");
|
||||||
self.blocks.insert(vec3(x as u32, y as u32, z as u32), cube);
|
self.set_cube_internal(cube, x, y, z);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_cube_internal(&mut self, cube: Cube, x: usize, y: usize, z: usize) {
|
||||||
|
if self.size > MIN_CHUNK_SIZE {
|
||||||
|
let mid_point = self.size / 2;
|
||||||
|
if x >= mid_point {
|
||||||
|
if y >= mid_point {
|
||||||
|
if z >= mid_point {
|
||||||
|
match &self.child_XYZ {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().set_cube_internal(cube, x - mid_point, y - mid_point, z - mid_point);
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||||
|
child.set_cube_internal(cube, x - mid_point, y - mid_point, z - mid_point);
|
||||||
|
self.child_XYZ = Some(Rc::new(RefCell::new(child)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
match &self.child_XYz {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().set_cube_internal(cube, x - mid_point, y - mid_point, z);
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||||
|
child.set_cube_internal(cube, x - mid_point, y - mid_point, z);
|
||||||
|
self.child_XYz = Some(Rc::new(RefCell::new(child)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if z >= mid_point {
|
||||||
|
match &self.child_XyZ {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().set_cube_internal(cube, x - mid_point, y, z - mid_point);
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||||
|
child.set_cube_internal(cube, x - mid_point, y, z - mid_point);
|
||||||
|
self.child_XyZ = Some(Rc::new(RefCell::new(child)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
match &self.child_Xyz {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().set_cube_internal(cube, x - mid_point, y, z);
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||||
|
child.set_cube_internal(cube, x - mid_point, y, z);
|
||||||
|
self.child_Xyz = Some(Rc::new(RefCell::new(child)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if y >= mid_point {
|
||||||
|
if z >= mid_point {
|
||||||
|
match &self.child_xYZ {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().set_cube_internal(cube, x, y - mid_point, z - mid_point);
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||||
|
child.set_cube_internal(cube, x, y - mid_point, z - mid_point);
|
||||||
|
self.child_xYZ = Some(Rc::new(RefCell::new(child)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
match &self.child_xYz {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().set_cube_internal(cube, x, y - mid_point, z);
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||||
|
child.set_cube_internal(cube, x, y - mid_point, z);
|
||||||
|
self.child_xYz = Some(Rc::new(RefCell::new(child)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if z >= mid_point {
|
||||||
|
match &self.child_xyZ {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().set_cube_internal(cube, x, y, z - mid_point);
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||||
|
child.set_cube_internal(cube, x, y, z - mid_point);
|
||||||
|
self.child_xyZ = Some(Rc::new(RefCell::new(child)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
match &self.child_xyz {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().set_cube_internal(cube, x, y, z);
|
||||||
|
},
|
||||||
|
None => {
|
||||||
|
let mut child = OctTree::create(self.size / 2).unwrap();
|
||||||
|
child.set_cube_internal(cube, x, y, z);
|
||||||
|
self.child_xyz = Some(Rc::new(RefCell::new(child)));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] = Some(cube);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear_cube(&mut self, x: usize, y: usize, z: usize) {
|
pub fn clear_cube(&mut self, x: usize, y: usize, z: usize) {
|
||||||
assert!(x < CHUNK_SIZE, "x value out of range!");
|
assert!(x < self.size, "x value out of range!");
|
||||||
assert!(y < CHUNK_SIZE, "y value out of range!");
|
assert!(y < self.size, "y value out of range!");
|
||||||
assert!(z < CHUNK_SIZE, "z value out of range!");
|
assert!(z < self.size, "z value out of range!");
|
||||||
self.blocks.remove(&vec3(x as u32, y as u32, z as u32));
|
//self.blocks.remove(&vec3(x as u32, y as u32, z as u32));
|
||||||
|
|
||||||
|
self.clear_cube_internal(x, y, z)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn clear_cube_internal(&mut self, x: usize, y: usize, z: usize) {
|
||||||
|
if self.size > MIN_CHUNK_SIZE {
|
||||||
|
let mid_point = self.size / 2;
|
||||||
|
if x >= mid_point {
|
||||||
|
if y >= mid_point {
|
||||||
|
if z >= mid_point {
|
||||||
|
match &self.child_XYZ {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().clear_cube_internal(x - mid_point, y - mid_point, z - mid_point);
|
||||||
|
},
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
match &self.child_XYz {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().clear_cube_internal(x - mid_point, y - mid_point, z);
|
||||||
|
},
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if z >= mid_point {
|
||||||
|
match &self.child_XyZ {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().clear_cube_internal(x - mid_point, y, z - mid_point);
|
||||||
|
},
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
match &self.child_Xyz {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().clear_cube_internal(x - mid_point, y, z);
|
||||||
|
},
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if y >= mid_point {
|
||||||
|
if z >= mid_point {
|
||||||
|
match &self.child_xYZ {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().clear_cube_internal(x, y - mid_point, z - mid_point);
|
||||||
|
},
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
match &self.child_xYz {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().clear_cube_internal(x, y - mid_point, z);
|
||||||
|
},
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
if z >= mid_point {
|
||||||
|
match &self.child_xyZ {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().clear_cube_internal(x, y, z - mid_point);
|
||||||
|
},
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
match &self.child_xyz {
|
||||||
|
Some(child) => {
|
||||||
|
child.borrow_mut().clear_cube_internal(x, y, z);
|
||||||
|
},
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self.blocks[z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE + y * MIN_CHUNK_SIZE + x] = None;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct ChunkIter<'a> {
|
struct OctTreeIter<'a> {
|
||||||
iter_x: usize,
|
iter_x: usize,
|
||||||
iter_y: usize,
|
iter_y: usize,
|
||||||
iter_z: usize,
|
iter_z: usize,
|
||||||
chunk: &'a Chunk
|
todo: Vec<Rc<RefCell<OctTree>>>,
|
||||||
|
chunk: &'a OctTree
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> ChunkIter<'a> {
|
impl<'a> OctTreeIter<'a> {
|
||||||
pub fn create(chunk: &'a Chunk) -> Result<Self> {
|
pub fn create(chunk: &'a OctTree) -> Result<Self> {
|
||||||
|
let mut out = Self {
|
||||||
Ok(Self {
|
|
||||||
iter_x: 0,
|
iter_x: 0,
|
||||||
iter_y: 0,
|
iter_y: 0,
|
||||||
iter_z: 0,
|
iter_z: 0,
|
||||||
|
todo: vec![],
|
||||||
chunk
|
chunk
|
||||||
})
|
};
|
||||||
|
out.add_todo(&chunk);
|
||||||
|
Ok(out)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn add_todo(&mut self, oct_tree: &OctTree) {
|
||||||
|
match &oct_tree.child_XYZ {
|
||||||
|
Some(child) => {
|
||||||
|
self.todo.push(child.clone());
|
||||||
|
},
|
||||||
|
None => {},
|
||||||
|
};
|
||||||
|
match &oct_tree.child_xYZ {
|
||||||
|
Some(child) => {
|
||||||
|
self.todo.push(child.clone());
|
||||||
|
},
|
||||||
|
None => {},
|
||||||
|
};
|
||||||
|
match &oct_tree.child_xyZ {
|
||||||
|
Some(child) => {
|
||||||
|
self.todo.push(child.clone());
|
||||||
|
},
|
||||||
|
None => {},
|
||||||
|
};
|
||||||
|
match &oct_tree.child_XyZ {
|
||||||
|
Some(child) => {
|
||||||
|
self.todo.push(child.clone());
|
||||||
|
},
|
||||||
|
None => {},
|
||||||
|
};
|
||||||
|
match &oct_tree.child_XYz {
|
||||||
|
Some(child) => {
|
||||||
|
self.todo.push(child.clone());
|
||||||
|
},
|
||||||
|
None => {},
|
||||||
|
};
|
||||||
|
match &oct_tree.child_xYz {
|
||||||
|
Some(child) => {
|
||||||
|
self.todo.push(child.clone());
|
||||||
|
},
|
||||||
|
None => {},
|
||||||
|
};
|
||||||
|
match &oct_tree.child_xyz {
|
||||||
|
Some(child) => {
|
||||||
|
self.todo.push(child.clone());
|
||||||
|
},
|
||||||
|
None => {},
|
||||||
|
};
|
||||||
|
match &oct_tree.child_Xyz {
|
||||||
|
Some(child) => {
|
||||||
|
self.todo.push(child.clone());
|
||||||
|
},
|
||||||
|
None => {},
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a> Iterator for ChunkIter<'a> {
|
impl<'a> Iterator for OctTreeIter<'a> {
|
||||||
type Item = Option<&'a Cube>;
|
type Item = Option<Cube>;
|
||||||
|
|
||||||
fn next(&mut self) -> Option<Self::Item> {
|
fn next(&mut self) -> Option<Self::Item> {
|
||||||
if self.iter_x < CHUNK_SIZE && self.iter_y < CHUNK_SIZE && self.iter_z < CHUNK_SIZE {
|
if self.todo.len() != 0 {
|
||||||
let result = self.chunk.blocks.get(&vec3(self.iter_x as u32, self.iter_y as u32, self.iter_z as u32));
|
while self.todo.last().unwrap().borrow().blocks.len() == 0 {
|
||||||
|
let oct_tree = self.todo.pop().unwrap();
|
||||||
|
self.add_todo(&oct_tree.borrow());
|
||||||
|
}
|
||||||
|
|
||||||
self.iter_x += 1;
|
if self.iter_x < MIN_CHUNK_SIZE && self.iter_y < MIN_CHUNK_SIZE && self.iter_z < MIN_CHUNK_SIZE {
|
||||||
if self.iter_x >= CHUNK_SIZE {
|
let result = self.todo.last().unwrap().borrow().blocks[self.iter_x + self.iter_y * MIN_CHUNK_SIZE + self.iter_z * MIN_CHUNK_SIZE * MIN_CHUNK_SIZE].clone();
|
||||||
self.iter_x = 0;
|
|
||||||
self.iter_y += 1;
|
self.iter_x += 1;
|
||||||
|
if self.iter_x >= MIN_CHUNK_SIZE {
|
||||||
|
self.iter_x = 0;
|
||||||
|
self.iter_y += 1;
|
||||||
|
}
|
||||||
|
if self.iter_y >= MIN_CHUNK_SIZE {
|
||||||
|
self.iter_y = 0;
|
||||||
|
self.iter_z += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if self.iter_z == MIN_CHUNK_SIZE {
|
||||||
|
self.todo.pop();
|
||||||
|
self.iter_x = 0;
|
||||||
|
self.iter_y = 0;
|
||||||
|
self.iter_z = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
return Some(result)
|
||||||
}
|
}
|
||||||
if self.iter_y >= CHUNK_SIZE {
|
|
||||||
self.iter_y = 0;
|
|
||||||
self.iter_z += 1;
|
|
||||||
}
|
|
||||||
return Some(result.clone())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
self.iter_x = 0;
|
self.iter_x = 0;
|
||||||
self.iter_y = 0;
|
self.iter_y = 0;
|
||||||
self.iter_z = 0;
|
self.iter_z = 0;
|
||||||
|
self.add_todo(&self.chunk);
|
||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -76,4 +76,75 @@ impl Hash for Vertex {
|
||||||
self.tex_coord[0].to_bits().hash(state);
|
self.tex_coord[0].to_bits().hash(state);
|
||||||
self.tex_coord[1].to_bits().hash(state);
|
self.tex_coord[1].to_bits().hash(state);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
#[derive(Copy, Clone, Debug)]
|
||||||
|
pub struct SizedVertex {
|
||||||
|
pub pos: Vec3,
|
||||||
|
pub color: Vec3,
|
||||||
|
pub tex_coord: Vec2,
|
||||||
|
pub size: Vec3,
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
||||||
|
vk::VertexInputBindingDescription::builder()
|
||||||
|
.binding(0)
|
||||||
|
.stride(size_of::<Vertex>() as u32)
|
||||||
|
.input_rate(vk::VertexInputRate::VERTEX)
|
||||||
|
.build()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn attribute_descriptions() -> [vk::VertexInputAttributeDescription; 3] {
|
||||||
|
let pos = vk::VertexInputAttributeDescription::builder()
|
||||||
|
.binding(0)
|
||||||
|
.location(0)
|
||||||
|
.format(vk::Format::R32G32B32_SFLOAT)
|
||||||
|
.offset(0)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let color = vk::VertexInputAttributeDescription::builder()
|
||||||
|
.binding(0)
|
||||||
|
.location(1)
|
||||||
|
.format(vk::Format::R32G32B32_SFLOAT)
|
||||||
|
.offset(size_of::<Vec3>() as u32)
|
||||||
|
.build();
|
||||||
|
|
||||||
|
let tex_coord = vk::VertexInputAttributeDescription::builder()
|
||||||
|
.binding(0)
|
||||||
|
.location(2)
|
||||||
|
.format(vk::Format::R32G32_SFLOAT)
|
||||||
|
.offset((size_of::<Vec3>() + size_of::<Vec3>()) as u32)
|
||||||
|
.build();
|
||||||
|
[pos, color, tex_coord]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
impl PartialEq for SizedVertex {
|
||||||
|
fn eq(&self, other: &Self) -> bool {
|
||||||
|
self.pos == other.pos
|
||||||
|
&& self.color == other.color
|
||||||
|
&& self.tex_coord == other.tex_coord
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Eq for SizedVertex {}
|
||||||
|
|
||||||
|
impl Hash for SizedVertex {
|
||||||
|
fn hash<H: Hasher>(&self, state: &mut H) {
|
||||||
|
self.pos[0].to_bits().hash(state);
|
||||||
|
self.pos[1].to_bits().hash(state);
|
||||||
|
self.pos[2].to_bits().hash(state);
|
||||||
|
self.color[0].to_bits().hash(state);
|
||||||
|
self.color[1].to_bits().hash(state);
|
||||||
|
self.color[2].to_bits().hash(state);
|
||||||
|
self.tex_coord[0].to_bits().hash(state);
|
||||||
|
self.tex_coord[1].to_bits().hash(state);
|
||||||
|
}
|
||||||
}
|
}
|
Loading…
Reference in a new issue