2023-01-22 10:01:54 -06:00
|
|
|
use shipyard::{Unique, NonSendSync, UniqueView, UniqueViewMut, View, IntoIter};
|
2023-01-19 16:28:07 -06:00
|
|
|
use glium::{
|
2023-01-22 10:01:54 -06:00
|
|
|
Display, Surface, uniform,
|
|
|
|
DrawParameters,
|
|
|
|
uniforms::{
|
|
|
|
Sampler,
|
|
|
|
SamplerBehavior,
|
|
|
|
MinifySamplerFilter,
|
2023-01-22 15:31:16 -06:00
|
|
|
MagnifySamplerFilter,
|
|
|
|
SamplerWrapFunction
|
2023-01-22 10:01:54 -06:00
|
|
|
},
|
|
|
|
draw_parameters::{
|
|
|
|
Depth,
|
|
|
|
DepthTest,
|
|
|
|
PolygonMode,
|
|
|
|
BackfaceCullingMode,
|
|
|
|
},
|
2023-01-19 16:28:07 -06:00
|
|
|
glutin::{
|
|
|
|
event_loop::EventLoop,
|
|
|
|
window::WindowBuilder,
|
|
|
|
ContextBuilder, GlProfile
|
2023-01-22 10:01:54 -06:00
|
|
|
},
|
2023-01-19 16:28:07 -06:00
|
|
|
};
|
2023-01-19 17:45:46 -06:00
|
|
|
use glam::Vec3;
|
2023-01-22 10:01:54 -06:00
|
|
|
use crate::{
|
|
|
|
camera::Camera,
|
|
|
|
prefabs::{
|
|
|
|
ChunkShaderPrefab,
|
|
|
|
BlockTexturesPrefab,
|
|
|
|
},
|
|
|
|
world::{
|
|
|
|
ChunkStorage,
|
|
|
|
ChunkMeshStorage,
|
|
|
|
chunk::CHUNK_SIZE,
|
|
|
|
},
|
|
|
|
};
|
2023-01-19 16:28:07 -06:00
|
|
|
|
|
|
|
#[derive(Unique)]
|
|
|
|
pub struct RenderTarget(pub glium::Frame);
|
|
|
|
|
2023-01-19 17:45:46 -06:00
|
|
|
#[derive(Unique)]
|
|
|
|
pub struct BackgroundColor(pub Vec3);
|
|
|
|
|
2023-01-19 16:28:07 -06:00
|
|
|
#[derive(Unique)]
|
2023-01-21 18:51:23 -06:00
|
|
|
pub struct Renderer {
|
2023-01-19 16:28:07 -06:00
|
|
|
pub display: Display
|
|
|
|
}
|
2023-01-21 18:51:23 -06:00
|
|
|
impl Renderer {
|
2023-01-19 16:28:07 -06:00
|
|
|
pub fn init(event_loop: &EventLoop<()>) -> Self {
|
|
|
|
log::info!("initializing display");
|
|
|
|
let wb = WindowBuilder::new()
|
|
|
|
.with_title("uwu")
|
|
|
|
.with_maximized(true);
|
|
|
|
let cb = ContextBuilder::new()
|
|
|
|
.with_depth_buffer(24)
|
|
|
|
.with_gl_profile(GlProfile::Core);
|
|
|
|
let display = Display::new(wb, cb, event_loop)
|
|
|
|
.expect("Failed to create a glium Display");
|
|
|
|
Self { display }
|
|
|
|
}
|
2023-01-19 14:58:59 -06:00
|
|
|
}
|
2023-01-19 17:45:46 -06:00
|
|
|
|
2023-01-22 10:01:54 -06:00
|
|
|
pub fn clear_background(
|
|
|
|
mut target: NonSendSync<UniqueViewMut<RenderTarget>>,
|
|
|
|
color: UniqueView<BackgroundColor>,
|
|
|
|
) {
|
2023-01-19 17:45:46 -06:00
|
|
|
target.0.clear_color_srgb_and_depth((color.0.x, color.0.y, color.0.z, 1.), 1.);
|
|
|
|
}
|
2023-01-22 10:01:54 -06:00
|
|
|
|
|
|
|
pub fn draw_world(
|
|
|
|
mut target: NonSendSync<UniqueViewMut<RenderTarget>>,
|
|
|
|
chunks: UniqueView<ChunkStorage>,
|
|
|
|
meshes: NonSendSync<UniqueView<ChunkMeshStorage>>,
|
|
|
|
program: NonSendSync<UniqueView<ChunkShaderPrefab>>,
|
|
|
|
texture: NonSendSync<UniqueView<BlockTexturesPrefab>>,
|
|
|
|
camera: View<Camera>,
|
|
|
|
) {
|
|
|
|
let camera = camera.iter().next().expect("No cameras in the scene");
|
|
|
|
let draw_parameters = DrawParameters {
|
|
|
|
depth: Depth {
|
|
|
|
test: DepthTest::IfLess,
|
|
|
|
write: true,
|
|
|
|
..Default::default()
|
|
|
|
},
|
2023-01-22 14:24:35 -06:00
|
|
|
polygon_mode: PolygonMode::Fill, //Change to Line for wireframe
|
|
|
|
backface_culling: BackfaceCullingMode::CullCounterClockwise,
|
2023-01-22 10:01:54 -06:00
|
|
|
..Default::default()
|
|
|
|
};
|
|
|
|
let texture_sampler = Sampler(&texture.0, SamplerBehavior {
|
|
|
|
minify_filter: MinifySamplerFilter::Linear,
|
|
|
|
magnify_filter: MagnifySamplerFilter::Nearest,
|
|
|
|
max_anisotropy: 8,
|
2023-01-22 15:31:16 -06:00
|
|
|
wrap_function: (SamplerWrapFunction::Clamp, SamplerWrapFunction::Clamp, SamplerWrapFunction::Clamp),
|
2023-01-22 10:01:54 -06:00
|
|
|
..Default::default()
|
|
|
|
});
|
|
|
|
let view = camera.view_matrix.to_cols_array_2d();
|
|
|
|
let perspective = camera.perspective_matrix.to_cols_array_2d();
|
|
|
|
|
|
|
|
for (&position, chunk) in &chunks.chunks {
|
|
|
|
if let Some(key) = chunk.mesh_index {
|
|
|
|
let mesh = meshes.get(key).expect("Mesh index pointing to nothing");
|
|
|
|
let world_position = (position.as_vec3() * CHUNK_SIZE as f32).to_array();
|
|
|
|
target.0.draw(
|
|
|
|
&mesh.vertex_buffer,
|
|
|
|
&mesh.index_buffer,
|
|
|
|
&program.0,
|
|
|
|
&uniform! {
|
|
|
|
position_offset: world_position,
|
|
|
|
view: view,
|
|
|
|
perspective: perspective,
|
|
|
|
tex: texture_sampler
|
|
|
|
},
|
|
|
|
&draw_parameters
|
|
|
|
).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|