wip chunk rendering

This commit is contained in:
griffi-gh 2023-01-17 15:31:20 +01:00
parent 096db79b12
commit 3cfd6448cc
3 changed files with 76 additions and 27 deletions

View file

@ -65,12 +65,6 @@ pub fn run() {
//======================= //=======================
let mut last_render = Instant::now(); let mut last_render = Instant::now();
let sampler_nearest = glium::uniforms::SamplerBehavior {
minify_filter: MinifySamplerFilter::Nearest,
magnify_filter: MagnifySamplerFilter::Nearest,
..Default::default()
};
event_loop.run(move |event, _, control_flow| { event_loop.run(move |event, _, control_flow| {
*control_flow = ControlFlow::Poll; *control_flow = ControlFlow::Poll;
@ -130,23 +124,27 @@ pub fn run() {
let view = state.camera.view_matrix(); let view = state.camera.view_matrix();
//Draw example triangle //Draw example triangle
target.draw(
&vertex_buffer, //Draw chunks
glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList), state.world.render(&mut target, &programs, &assets, perspective, view);
&programs.chunk,
&uniform! { // target.draw(
model: [ // &vertex_buffer,
[1., 0., 0., 0.], // glium::index::NoIndices(glium::index::PrimitiveType::TrianglesList),
[0., 1., 0., 0.], // &programs.chunk,
[0., 0., 1., 0.], // &uniform! {
[0., 0., 0., 1.0_f32] // model: [
], // [1., 0., 0., 0.],
view: view, // [0., 1., 0., 0.],
perspective: perspective, // [0., 0., 1., 0.],
tex: Sampler(&assets.textures.block_atlas, sampler_nearest) // [0., 0., 0., 1.0_f32]
}, // ],
&Default::default() // view: view,
).unwrap(); // perspective: perspective,
// tex: Sampler(&assets.textures.block_atlas, sampler_nearest)
// },
// &Default::default()
// ).unwrap();
//Finish drawing //Finish drawing
target.finish().unwrap(); target.finish().unwrap();

View file

@ -1,7 +1,17 @@
use glam::{Vec2, IVec2}; use glam::{Vec2, IVec2};
use glium::Display; use glium::{
Display, Frame, Surface, uniform,
uniforms::{
Sampler, SamplerBehavior,
MinifySamplerFilter, MagnifySamplerFilter,
}
};
use hashbrown::HashMap; use hashbrown::HashMap;
use crate::game::options::GameOptions; use crate::game::{
options::GameOptions,
shaders::Programs,
assets::Assets
};
mod chunk; mod chunk;
mod thread; mod thread;
@ -33,6 +43,41 @@ impl World {
thread: WorldThreading::new(), thread: WorldThreading::new(),
} }
} }
pub fn render(
&self,
target: &mut Frame,
programs: &Programs,
assets: &Assets,
perspective: [[f32; 4]; 4],
view: [[f32; 4]; 4]
) {
let sampler = SamplerBehavior {
minify_filter: MinifySamplerFilter::Nearest,
magnify_filter: MagnifySamplerFilter::Nearest,
..Default::default()
};
for (&position, chunk) in &self.chunks {
if let Some((_, vertex, index)) = &chunk.mesh {
target.draw(
vertex,
index,
&programs.chunk,
&uniform! {
model: [
[1., 0., 0., 0.],
[0., 1., 0., 0.],
[0., 0., 1., 0.],
[(position.x * CHUNK_SIZE as i32) as f32, 0., (position.y * CHUNK_SIZE as i32) as f32, 1.0_f32]
],
view: view,
persperctive: perspective,
tex: Sampler(&assets.textures.block_atlas, sampler)
},
&Default::default()
).unwrap();
}
}
}
pub fn update_loaded_chunks(&mut self, around_position: Vec2, options: &GameOptions, display: &Display) { pub fn update_loaded_chunks(&mut self, around_position: Vec2, options: &GameOptions, display: &Display) {
let render_dist = options.render_distance as i32 + 1; let render_dist = options.render_distance as i32 + 1;
let inside_chunk = (around_position / CHUNK_SIZE as f32).as_ivec2(); let inside_chunk = (around_position / CHUNK_SIZE as f32).as_ivec2();

View file

@ -3,7 +3,13 @@ use crate::game::world::chunk::ChunkData;
use crate::game::shaders::chunk::Vertex as ChunkVertex; use crate::game::shaders::chunk::Vertex as ChunkVertex;
pub fn generate_mesh(position: IVec2, chunk_data: ChunkData, neighbors: [ChunkData; 4]) -> (Vec<ChunkVertex>, Vec<u16>) { pub fn generate_mesh(position: IVec2, chunk_data: ChunkData, neighbors: [ChunkData; 4]) -> (Vec<ChunkVertex>, Vec<u16>) {
let vertex = Vec::new(); let mut vertex = Vec::new();
let index = Vec::new(); let mut index = Vec::new();
vertex.push(ChunkVertex { position: [-0.5, -0.5, 0.], uv: [0., 0.], normal: [0., 1., 0.] });
vertex.push(ChunkVertex { position: [ 0.0, 0.5, 0.], uv: [0., 1.], normal: [0., 1., 0.] });
vertex.push(ChunkVertex { position: [ 0.5, -0.5, 0.], uv: [1., 1.], normal: [0., 1., 0.] });
index.push(0);
index.push(1);
index.push(2);
(vertex, index) (vertex, index)
} }