minor refactor, start working on chunk border rendering

This commit is contained in:
griffi-gh 2023-01-30 01:04:13 +01:00
parent 0b4363ef88
commit 852fee3607
8 changed files with 72 additions and 12 deletions

8
shaders/colored.frag Normal file
View file

@ -0,0 +1,8 @@
#version 150 core
out vec4 color;
uniform vec4 u_color;
void main() {
color = u_color;
}

11
shaders/colored.vert Normal file
View file

@ -0,0 +1,11 @@
#version 150 core
in vec3 position;
uniform mat4 model;
uniform mat4 perspective;
uniform mat4 view;
void main() {
mat4 modelview = view * model;
gl_Position = perspective * modelview * vec4(position, 1.);
}

View file

@ -1,10 +1,10 @@
#version 150 core #version 150 core
in vec3 position; in vec3 position;
uniform vec3 u_position; uniform ivec3 u_position;
uniform mat4 perspective; uniform mat4 perspective;
uniform mat4 view; uniform mat4 view;
void main() { void main() {
gl_Position = perspective * view * vec4(position + u_position, 1.); gl_Position = perspective * view * vec4(position + vec3(u_position), 1.);
} }

View file

@ -59,10 +59,14 @@ pub struct ChunkShaderPrefab(pub Program);
#[derive(Unique)] #[derive(Unique)]
pub struct SelBoxShaderPrefab(pub Program); pub struct SelBoxShaderPrefab(pub Program);
#[derive(Unique)]
pub struct BasicColoredShaderPrefab(pub Program);
pub fn load_prefabs( pub fn load_prefabs(
storages: AllStoragesView, storages: AllStoragesView,
renderer: NonSendSync<UniqueView<Renderer>> renderer: NonSendSync<UniqueView<Renderer>>
) { ) {
log::info!("Loading textures...");
storages.add_unique_non_send_sync(BlockTexturesPrefab( storages.add_unique_non_send_sync(BlockTexturesPrefab(
load_texture2darray_prefab::<BlockTexture, _>( load_texture2darray_prefab::<BlockTexture, _>(
"./assets/blocks/".into(), "./assets/blocks/".into(),
@ -70,8 +74,11 @@ pub fn load_prefabs(
MipmapsOption::AutoGeneratedMipmaps MipmapsOption::AutoGeneratedMipmaps
) )
)); ));
log::info!("Compiling shaders...");
storages.add_unique_non_send_sync(ChunkShaderPrefab( storages.add_unique_non_send_sync(ChunkShaderPrefab(
include_shader_prefab!( include_shader_prefab!(
"world",
"../shaders/world.vert", "../shaders/world.vert",
"../shaders/world.frag", "../shaders/world.frag",
&renderer.display &renderer.display
@ -79,9 +86,18 @@ pub fn load_prefabs(
)); ));
storages.add_unique_non_send_sync(SelBoxShaderPrefab( storages.add_unique_non_send_sync(SelBoxShaderPrefab(
include_shader_prefab!( include_shader_prefab!(
"selection_box",
"../shaders/selection_box.vert", "../shaders/selection_box.vert",
"../shaders/selection_box.frag", "../shaders/selection_box.frag",
&renderer.display &renderer.display
) )
)); ));
storages.add_unique_non_send_sync(BasicColoredShaderPrefab(
include_shader_prefab!(
"colored",
"../shaders/colored.vert",
"../shaders/colored.frag",
&renderer.display
)
));
} }

View file

@ -1,11 +1,10 @@
macro_rules! include_shader_prefab { macro_rules! include_shader_prefab {
($vert: literal, $frag: literal, $geom: literal, $facade: expr) => { ($name: literal, $vert: literal, $frag: literal, $geom: literal, $facade: expr) => {
{ {
use ::glium::Program; use ::glium::Program;
log::info!("↓↓↓ compiling shader prefab ↓↓↓"); log::info!("compiling shader {}", $name);
log::info!("{} {} {}", $vert, $frag, $geom);
Program::from_source( Program::from_source(
&*$facade, &*$facade,
include_str!($vert), include_str!($vert),
@ -14,11 +13,10 @@ macro_rules! include_shader_prefab {
).expect("Failed to compile gpu program") ).expect("Failed to compile gpu program")
} }
}; };
($vert: literal, $frag: literal, $facade: expr) => { ($name: literal, $vert: literal, $frag: literal, $facade: expr) => {
{ {
use ::glium::Program; use ::glium::Program;
log::info!("↓↓↓ compiling shader prefab ↓↓↓"); log::info!("compiling shader {}", $name);
log::info!("{} {}", $vert, $frag);
Program::from_source( Program::from_source(
&*$facade, &*$facade,
include_str!($vert), include_str!($vert),

View file

@ -12,7 +12,7 @@ pub fn load_texture2darray_prefab<
facade: &E, facade: &E,
mipmaps: MipmapsOption, mipmaps: MipmapsOption,
) -> SrgbTexture2dArray { ) -> SrgbTexture2dArray {
log::info!("↓↓↓ loading textures {} ↓↓↓", directory.as_os_str().to_str().unwrap()); log::info!("started loading {}", directory.as_os_str().to_str().unwrap());
//Load raw images //Load raw images
let tex_files: Vec<&'static str> = T::iter().map(|x| x.file_name()).collect(); let tex_files: Vec<&'static str> = T::iter().map(|x| x.file_name()).collect();
let raw_images: Vec<RawImage2d<u8>> = tex_files.par_iter().map(|&file_name| { let raw_images: Vec<RawImage2d<u8>> = tex_files.par_iter().map(|&file_name| {

View file

@ -1,3 +1,4 @@
use glam::Mat4;
use shipyard::{View, IntoIter, NonSendSync, UniqueViewMut, UniqueView}; use shipyard::{View, IntoIter, NonSendSync, UniqueViewMut, UniqueView};
use glium::{ use glium::{
Surface, Surface,
@ -33,7 +34,7 @@ pub fn render_selection_box(
&program.0, &program.0,
&uniform! { &uniform! {
u_color: [0., 0., 0., 0.5_f32], u_color: [0., 0., 0., 0.5_f32],
u_position: lookat.block_position.as_vec3().to_array(), u_position: lookat.block_position.to_array(),
perspective: camera.perspective_matrix.to_cols_array_2d(), perspective: camera.perspective_matrix.to_cols_array_2d(),
view: camera.view_matrix.to_cols_array_2d(), view: camera.view_matrix.to_cols_array_2d(),
}, },

View file

@ -22,6 +22,7 @@ use crate::{
prefabs::{ prefabs::{
ChunkShaderPrefab, ChunkShaderPrefab,
BlockTexturesPrefab, BlockTexturesPrefab,
BasicColoredShaderPrefab,
}, },
world::{ world::{
ChunkStorage, ChunkStorage,
@ -29,7 +30,7 @@ use crate::{
chunk::CHUNK_SIZE, chunk::CHUNK_SIZE,
}, },
}; };
use super::RenderTarget; use super::{RenderTarget, primitives::SimpleBoxBuffers};
#[derive(Clone, Copy)] #[derive(Clone, Copy)]
pub struct ChunkVertex { pub struct ChunkVertex {
@ -105,3 +106,28 @@ pub fn draw_world(
} }
} }
} }
// this doesn't use culling!
// pub fn draw_chunk_borders(
// mut target: NonSendSync<UniqueViewMut<RenderTarget>>,
// chunks: UniqueView<ChunkStorage>,
// buffers: NonSendSync<UniqueView<SimpleBoxBuffers>>,
// program: NonSendSync<UniqueView<BasicShaderPrefab>>,
// camera: View<Camera>,
// ) {
// for (&position, chunk) in &chunks.chunks {
// let world_position = position.as_vec3() * CHUNK_SIZE as f32;
// target.0.draw(
// &buffers.0,
// &buffers.1,
// &program.0,
// &uniform! {
// position_offset: world_position.to_array(),
// view: view,
// perspective: perspective,
// tex: texture_sampler,
// },
// &draw_parameters
// ).unwrap();
// }
// }