partial voxel | No optimizations

This commit is contained in:
Able 2022-04-03 14:00:11 -05:00
parent 30830d4d90
commit 94b3d01dc2
6 changed files with 168 additions and 42 deletions

6
NOTES.md Normal file
View file

@ -0,0 +1,6 @@
- [ ] Crafting
- [ ] Crafting List
- [ ] Armory
- [ ] Placeable Tower Shields
- [ ] Black Powder Weaponry

View file

@ -2,13 +2,6 @@
in vec3 position; in vec3 position;
uniform uint type;
void main() { void main() {
// v_type = type;
gl_Position = vec4(position, 1.0); gl_Position = vec4(position, 1.0);
} }

View file

@ -4,7 +4,7 @@ use crate::{config::CHUNK_SIZE, vertex::Vertex, voxel::VoxelData};
#[derive(Debug, Copy, Clone, Default)] #[derive(Debug, Copy, Clone, Default)]
pub struct Chunk { pub struct Chunk {
inner_data: [[[VoxelData; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE], inner_data: [[[VoxelData; CHUNK_SIZE]; CHUNK_SIZE]; CHUNK_SIZE],
chunk_position: Coordinates, pub chunk_position: Coordinates,
} }
impl Chunk { impl Chunk {

View file

@ -3,4 +3,6 @@
pub const CHUNK_SIZE: usize = 32; pub const CHUNK_SIZE: usize = 32;
pub const PLAYER_HEIGHT: f64 = 1.7; pub const PLAYER_HEIGHT: f64 = 1.7;
pub const PARTICLE_SCALE: f32 = 0.004; pub const PARTICLE_SCALE: f32 = 0.004;
pub const VOXEL_SCALE: f32 = 0.5;

View file

@ -1,7 +1,5 @@
#![feature(const_fn_trait_bound)] #![feature(const_fn_trait_bound)]
use std::collections::HashMap;
#[macro_use] #[macro_use]
extern crate glium; extern crate glium;
@ -10,7 +8,7 @@ use glium::{
Surface, Surface,
}; };
use crate::particle_system::{generate_particle_vertexs, Particle, ParticleType}; use crate::particle_system::{generate_particle_vertexs, Particle};
pub mod chunk; pub mod chunk;
pub mod config; pub mod config;
pub mod coordinates; pub mod coordinates;
@ -21,15 +19,12 @@ pub mod voxel;
fn main() { fn main() {
let _chunk = chunk::Chunk::half_chunk_filled(); let _chunk = chunk::Chunk::half_chunk_filled();
// println!("{:?}", chunk);
bruh(); bruh();
} }
mod teapot; mod teapot;
pub type ShaderList = HashMap<String, glium::Program>;
pub fn bruh() { pub fn bruh() {
let event_loop = glutin::event_loop::EventLoop::new(); let event_loop = glutin::event_loop::EventLoop::new();
let wb = glutin::window::WindowBuilder::new() let wb = glutin::window::WindowBuilder::new()
@ -60,19 +55,6 @@ pub fn bruh() {
) )
.unwrap(); .unwrap();
let textured_particles_vertex_shader_src =
include_str!("../assets/shaders/particles/textured_particle.vert");
let textured_particles_fragment_shader_src =
include_str!("../assets/shaders/particles/textured_particle.frag");
let textured_particles_program = glium::Program::from_source(
&display,
textured_particles_vertex_shader_src,
textured_particles_fragment_shader_src,
None,
)
.unwrap();
let mut particles_vec: Vec<Particle> = Vec::new(); let mut particles_vec: Vec<Particle> = Vec::new();
for _ in 0..=10000 { for _ in 0..=10000 {
particles_vec.push(Particle::random_particle()); particles_vec.push(Particle::random_particle());
@ -136,7 +118,7 @@ pub fn bruh() {
target.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0); target.clear_color_and_depth((0.0, 0.0, 1.0, 1.0), 1.0);
{ if false {
// Teapot stage // Teapot stage
let positions = glium::VertexBuffer::new(&display, &teapot::VERTICES).unwrap(); let positions = glium::VertexBuffer::new(&display, &teapot::VERTICES).unwrap();
@ -177,6 +159,42 @@ pub fn bruh() {
.unwrap(); .unwrap();
} }
{
// Solid Voxel stage
let particle_group_color: [f32; 4] = [1.0, 1.0, 1.0, 1.0];
let shape = voxel::VoxelData::new(
0,
voxel::Light {
red: 0,
green: 0,
blue: 0,
sun: 0,
},
)
.construct_vertexes(coordinates::Coordinates {
x: 0.0,
y: 0.0,
z: 0.0,
});
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
let uniforms = uniform! {
u_color: particle_group_color,
};
target
.draw(
&vertex_buffer,
&glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
&solid_color_particles_program,
&uniforms,
&Default::default(),
)
.unwrap();
}
if render_particles { if render_particles {
// Particle Stage // Particle Stage
@ -185,28 +203,16 @@ pub fn bruh() {
let shape = generate_particle_vertexs(&mut particles_vec); let shape = generate_particle_vertexs(&mut particles_vec);
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap(); let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
let indices = glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList);
let uniforms = uniform! { let uniforms = uniform! {
u_color: particle_group_color, u_color: particle_group_color,
}; };
let shader_to_use;
let ptype = ParticleType::SolidColor;
match ptype {
ParticleType::SolidColor => {
shader_to_use = &solid_color_particles_program;
}
ParticleType::Texture => {
shader_to_use = &textured_particles_program;
todo!("Texteured particles are not implemented yet");
}
}
target target
.draw( .draw(
&vertex_buffer, &vertex_buffer,
&indices, &glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
&shader_to_use, &solid_color_particles_program,
&uniforms, &uniforms,
&Default::default(), &Default::default(),
) )

View file

@ -1,3 +1,9 @@
use crate::{
config::VOXEL_SCALE,
coordinates::{self, Coordinates},
vertex::Vertex,
};
#[repr(C)] #[repr(C)]
#[derive(Debug, Copy, Clone, Default)] #[derive(Debug, Copy, Clone, Default)]
pub struct Light { pub struct Light {
@ -12,3 +18,116 @@ pub struct VoxelData {
pub id: u32, pub id: u32,
pub light_level: Light, pub light_level: Light,
} }
impl VoxelData {
pub fn new(id: u32, light_level: Light) -> Self {
Self { id, light_level }
}
pub fn construct_vertexes(&self, coordinates: Coordinates) -> Vec<Vertex> {
let mut shape = Vec::new();
let half_voxel = VOXEL_SCALE / 2.0;
{
// Top Hexagon
let vert_center = Vertex {
position: (coordinates.x, coordinates.y, coordinates.z),
};
let vert_1 = Vertex {
position: (
coordinates.x - half_voxel,
coordinates.y,
coordinates.z + VOXEL_SCALE,
),
};
let vert_2 = Vertex {
position: (
coordinates.x + half_voxel,
coordinates.y,
coordinates.z + VOXEL_SCALE,
),
};
let vert_3 = Vertex {
position: (coordinates.x + VOXEL_SCALE, coordinates.y, coordinates.z),
};
let vert_4 = Vertex {
position: (
coordinates.x + half_voxel,
coordinates.y,
coordinates.z - VOXEL_SCALE,
),
};
let vert_5 = Vertex {
position: (
coordinates.x - half_voxel,
coordinates.y,
coordinates.z - VOXEL_SCALE,
),
};
let vert_6 = Vertex {
position: (coordinates.x - VOXEL_SCALE, coordinates.y, coordinates.z),
};
// TRIANGLE 1
shape.append(&mut vec![vert_1, vert_2, vert_center]);
// TRIANGLE 2
shape.append(&mut vec![vert_2, vert_3, vert_center]);
// TRIANGLE 3
shape.append(&mut vec![vert_3, vert_4, vert_center]);
// TRIANGLE 4
shape.append(&mut vec![vert_4, vert_5, vert_center]);
// TRIANGLE 5
shape.append(&mut vec![vert_5, vert_6, vert_center]);
// TRIANGLE 6
shape.append(&mut vec![vert_6, vert_1, vert_center]);
}
{
// Side Face 1
let vert_1 = Vertex {
position: (
coordinates.x - VOXEL_SCALE,
coordinates.y + VOXEL_SCALE,
coordinates.z,
),
};
let vert_2 = Vertex {
position: (
coordinates.x + VOXEL_SCALE,
coordinates.y + VOXEL_SCALE,
coordinates.z,
),
};
let vert_3 = Vertex {
position: (
coordinates.x - VOXEL_SCALE,
coordinates.y - VOXEL_SCALE,
coordinates.z,
),
};
let vert_4 = Vertex {
position: (
coordinates.x + VOXEL_SCALE,
coordinates.y - VOXEL_SCALE,
coordinates.z,
),
};
// TRIANGLE 1
shape.append(&mut vec![vert_1, vert_2, vert_3]);
// TRIANGLE 2
shape.append(&mut vec![vert_2, vert_3, vert_4]);
}
shape
}
}