From ae488d090e3ebd2744c54ad1f27bd7560b749dff Mon Sep 17 00:00:00 2001 From: griffi-gh Date: Wed, 18 Jan 2023 01:24:47 +0100 Subject: [PATCH] fix issues with refactor --- src/game/assets/textures.rs | 16 ++++++++++++++++ src/game/blocks.rs | 32 +++++++++++++++++-------------- src/game/shaders/glsl/chunk.vert | 5 +++-- src/game/world.rs | 8 +------- src/game/world/thread/mesh_gen.rs | 4 ++-- 5 files changed, 40 insertions(+), 25 deletions(-) diff --git a/src/game/assets/textures.rs b/src/game/assets/textures.rs index b75c066..15fe37e 100644 --- a/src/game/assets/textures.rs +++ b/src/game/assets/textures.rs @@ -78,3 +78,19 @@ impl Textures { } } } + +#[derive(Clone, Copy, Debug)] +#[repr(u8)] +pub enum BlockTexture { + Stone = 0, + Dirt = 1, + GrassTop = 2, + GrassSide = 3, + Sand = 4, + Bedrock = 5, + Tree = 6, + TreeTop = 7, + Leaf = 8, + Torch = 9, + TallGrass = 10, +} diff --git a/src/game/blocks.rs b/src/game/blocks.rs index b5c0fa1..9db47b6 100644 --- a/src/game/blocks.rs +++ b/src/game/blocks.rs @@ -1,5 +1,9 @@ use strum::{EnumIter, IntoEnumIterator}; -use crate::game::items::Item; +use crate::game::{ + items::Item, + assets::textures::BlockTexture, +}; + #[derive(Clone, Copy, Debug)] pub enum CollisionType { @@ -18,15 +22,15 @@ pub enum RenderType { #[derive(Clone, Copy, Debug)] pub struct BlockTextures { - pub top: u8, - pub bottom: u8, - pub left: u8, - pub right: u8, - pub back: u8, - pub front: u8, + pub top: BlockTexture, + pub bottom: BlockTexture, + pub left: BlockTexture, + pub right: BlockTexture, + pub back: BlockTexture, + pub front: BlockTexture, } impl BlockTextures { - pub const fn all(tex: u8) -> Self { + pub const fn all(tex: BlockTexture) -> Self { Self { top: tex, bottom: tex, @@ -36,7 +40,7 @@ impl BlockTextures { front: tex, } } - pub const fn top_sides_bottom(top: u8, sides: u8, bottom: u8) -> Self { + pub const fn top_sides_bottom(top: BlockTexture, sides: BlockTexture, bottom: BlockTexture) -> Self { Self { top, bottom, @@ -65,7 +69,7 @@ impl BlockDescriptor { id: "default", collision: Some(CollisionType::Solid), raycast_collision: true, - render: Some((RenderType::OpaqueBlock, BlockTextures::all(0))), + render: Some((RenderType::OpaqueBlock, BlockTextures::all(BlockTexture::Stone))), item: None } } @@ -105,7 +109,7 @@ impl Block { id: "stone", collision: Some(CollisionType::Solid), raycast_collision: true, - render: Some((RenderType::OpaqueBlock, BlockTextures::all(1))), + render: Some((RenderType::OpaqueBlock, BlockTextures::all(BlockTexture::Stone))), item: Some(Item::StoneBlock) }, Self::Dirt => BlockDescriptor { @@ -113,7 +117,7 @@ impl Block { id: "dirt", collision: Some(CollisionType::Solid), raycast_collision: true, - render: Some((RenderType::OpaqueBlock, BlockTextures::all(2))), + render: Some((RenderType::OpaqueBlock, BlockTextures::all(BlockTexture::Dirt))), item: Some(Item::DirtBlock) }, Self::Grass => BlockDescriptor { @@ -121,7 +125,7 @@ impl Block { id: "grass", collision: Some(CollisionType::Solid), raycast_collision: true, - render: Some((RenderType::OpaqueBlock, BlockTextures::top_sides_bottom(0, 3, 2))), + render: Some((RenderType::OpaqueBlock, BlockTextures::top_sides_bottom(BlockTexture::GrassTop, BlockTexture::GrassSide, BlockTexture::Dirt))), item: Some(Item::DirtBlock) }, Self::Sand => BlockDescriptor { @@ -129,7 +133,7 @@ impl Block { id: "sand", collision: Some(CollisionType::Solid), raycast_collision: true, - render: Some((RenderType::OpaqueBlock, BlockTextures::all(4))), //this is not a sand tex + render: Some((RenderType::OpaqueBlock, BlockTextures::all(BlockTexture::Sand))), //this is not a sand tex item: Some(Item::StoneBlock) } } diff --git a/src/game/shaders/glsl/chunk.vert b/src/game/shaders/glsl/chunk.vert index b9a51ab..91e2b39 100644 --- a/src/game/shaders/glsl/chunk.vert +++ b/src/game/shaders/glsl/chunk.vert @@ -7,12 +7,13 @@ in uint tex_index; out vec2 v_uv; out vec3 v_normal; flat out uint v_tex_index; -uniform vec3 position_offset; +uniform vec2 position_offset; uniform mat4 perspective; uniform mat4 view; void main() { v_normal = normal; v_tex_index = tex_index; - gl_Position = perspective * view * vec4(position, 1.0) * vec4(position_offset, 1.0); + v_uv = uv; + gl_Position = perspective * view * (vec4(position, 1.0) + vec4(position_offset.x, 0., position_offset.y, 0.)); } diff --git a/src/game/world.rs b/src/game/world.rs index 57a489e..794e782 100644 --- a/src/game/world.rs +++ b/src/game/world.rs @@ -86,13 +86,7 @@ impl World { &mesh.index_buffer, &programs.chunk, &uniform! { - model: [ - [1., 0., 0., 0.], - [0., 1., 0., 0.], - [0., 0., 1., 0.], - //[0., 0., 0., 1.0_f32] - [(position.x * CHUNK_SIZE as i32) as f32, 0., (position.y * CHUNK_SIZE as i32) as f32, 1.0_f32] - ], + position_offset: (position.as_vec2() * CHUNK_SIZE as f32).to_array(), view: view, perspective: perspective, tex: Sampler(&assets.textures.blocks, sampler) diff --git a/src/game/world/thread/mesh_gen.rs b/src/game/world/thread/mesh_gen.rs index cafc76b..ad32b11 100644 --- a/src/game/world/thread/mesh_gen.rs +++ b/src/game/world/thread/mesh_gen.rs @@ -120,7 +120,7 @@ pub fn generate_mesh(position: IVec2, chunk_data: ChunkData, neighbors: [ChunkDa }; if show { let texures = descriptor.render.unwrap().1; - let texture_index = match face { + let block_texture = match face { CubeFace::Top => texures.top, CubeFace::Front => texures.front, CubeFace::Left => texures.left, @@ -128,7 +128,7 @@ pub fn generate_mesh(position: IVec2, chunk_data: ChunkData, neighbors: [ChunkDa CubeFace::Back => texures.back, CubeFace::Bottom => texures.bottom, }; - builer.add_face(face, coord, texture_index); + builer.add_face(face, coord, block_texture as u8); } } }