Skip to content

Commit

Permalink
fix unnormal camera rotating
Browse files Browse the repository at this point in the history
  • Loading branch information
BreakingLead committed Aug 29, 2024
1 parent 6068ffd commit 6b23725
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 49 deletions.
14 changes: 5 additions & 9 deletions blockworld-client/block/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,15 @@ use blockworld_utils::ResourceLocation;
pub type NumberID = u32;

pub trait Block: Send + Sync + 'static {
fn texture_location(&self) -> ResourceLocation;
fn hardness(&self) -> f32;
fn material(&self) -> Material;
}

macro_rules! def_basic_block {
($name:ident, $texture:literal, $hardness:literal, $material:expr) => {
($name:ident, $hardness:literal, $material:expr) => {
#[derive(Eq, PartialEq, Clone, Copy)]
pub struct $name;
impl Block for $name {
fn texture_location(&self) -> ResourceLocation {
$texture.into()
}
fn hardness(&self) -> f32 {
$hardness
}
Expand All @@ -26,10 +22,10 @@ macro_rules! def_basic_block {
};
}

def_basic_block!(Air, "null", 1.5, Material::Air);
def_basic_block!(Stone, "stone", 1.5, Material::Solid);
def_basic_block!(Grass, "grass", 0.6, Material::Solid);
def_basic_block!(Dirt, "dirt", 0.5, Material::Solid);
def_basic_block!(Air, 1.5, Material::Air);
def_basic_block!(Stone, 1.5, Material::Solid);
def_basic_block!(Grass, 0.6, Material::Solid);
def_basic_block!(Dirt, 0.5, Material::Solid);

#[derive(Debug, Default, Clone, Copy)]
pub enum Material {
Expand Down
11 changes: 0 additions & 11 deletions blockworld-client/game/input_manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ use winit::{
/// Tracker for the pressing keys
#[derive(Default, Debug)]
pub struct InputManager {
pub mouse_delta: Vec2,
pub pressing_keys: HashSet<Key>,
}

Expand All @@ -18,12 +17,6 @@ impl InputManager {
self.pressing_keys.contains(&key)
}

pub fn handle_device_event(&mut self, event: &winit::event::DeviceEvent) {
if let winit::event::DeviceEvent::MouseMotion { delta } = event {
self.mouse_delta = vec2(delta.0 as f32, delta.1 as f32);
}
}

pub fn handle_key_event(&mut self, event: &KeyEvent) {
let key = &event.logical_key;
match event.state {
Expand All @@ -35,8 +28,4 @@ impl InputManager {
}
}
}

pub fn get_mouse_delta(&self) -> Vec2 {
self.mouse_delta
}
}
4 changes: 2 additions & 2 deletions blockworld-client/renderer/camera.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,8 @@ impl Camera {

pub fn update_rotation(&mut self, delta: Vec2) {
let sensitivity = 0.002;
self.yaw += delta.x * sensitivity;
self.pitch += delta.y * sensitivity;
self.yaw -= delta.x * sensitivity;
self.pitch -= delta.y * sensitivity;
if self.pitch >= f32::to_radians(89.9) {
self.pitch = f32::to_radians(89.9);
} else if self.pitch <= f32::to_radians(-89.9) {
Expand Down
6 changes: 3 additions & 3 deletions blockworld-client/renderer/chunk/render_chunk.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Deref;

use glam::*;
use log::info;
use wgpu::{util::DeviceExt, Device};
Expand Down Expand Up @@ -34,11 +36,9 @@ impl RenderChunk {
if block_id != "blockworld:air" {
let block = BLOCK_REGISTRY.get(&block_id.as_str().into());
if let Some(block) = block {
let texture_location = block.texture_location();

let cull_mask = chunk.exist_neighbor(x, y, z);
let (a, b) = BLOCK_ATLAS
.query_uv(&texture_location)
.query_uv(&block_id.deref().into())
.unwrap_or((vec2(0.0, 0.0), vec2(1.0, 1.0)));

let mut add = |d: BlockFaceDirection| {
Expand Down
39 changes: 33 additions & 6 deletions blockworld-client/renderer/wgpu/window_init.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ use anyhow::Context;
use application::ApplicationHandler;
use event::WindowEvent;
use event_loop::{ActiveEventLoop, EventLoop};
use keyboard::KeyCode;
use glam::vec2;
use keyboard::{KeyCode, PhysicalKey};
use log::*;
use window::{Window, WindowId};
use winit::*;
Expand Down Expand Up @@ -55,9 +56,14 @@ impl ApplicationHandler for WindowApplication {
_device_id: winit::event::DeviceId,
event: winit::event::DeviceEvent,
) {
self.render_state_mut()
.input_manager
.handle_device_event(&event);
match event {
winit::event::DeviceEvent::MouseMotion { delta } => self
.render_state_mut()
.world_renderer
.camera
.update_rotation(vec2(delta.0 as f32, delta.1 as f32)),
_ => (),
}
}

/// Process a window event.
Expand Down Expand Up @@ -93,16 +99,37 @@ impl ApplicationHandler for WindowApplication {
.input_manager
.handle_key_event(&event);

let key = event.logical_key;
let key = event.physical_key;

// ! NOT IDEAL
// ! FIX LATER
if key == keyboard::Key::Named(keyboard::NamedKey::F1)
if key == PhysicalKey::Code(KeyCode::F1)
&& event.state == event::ElementState::Released
{
self.render_state_mut().world_renderer.debug_mode =
!self.render_state().world_renderer.debug_mode;
}

if key == PhysicalKey::Code(KeyCode::F2)
&& event.state == event::ElementState::Released
{
// only for test, remove later
static mut GRAB_MODE: bool = true;
if unsafe { GRAB_MODE } {
self.render_state_mut()
.window
.set_cursor_grab(window::CursorGrabMode::Confined)
.unwrap();
} else {
self.render_state_mut()
.window
.set_cursor_grab(window::CursorGrabMode::None)
.unwrap();
}
self.render_state_mut()
.window
.set_cursor_visible(!unsafe { GRAB_MODE });
}
}
_ => (),
}
Expand Down
21 changes: 9 additions & 12 deletions blockworld-client/renderer/world_renderer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub struct WorldRenderer {
diffuse_texture: BindableTexture,
pub depth_texture: TextureWithView,

camera: Camera,
pub camera: Camera,
matrix_uniform: Uniform<RawMat4>,

chunks: Box<ChunkArray>,
Expand Down Expand Up @@ -116,7 +116,6 @@ impl WorldRenderer {
pub fn update(&mut self, queue: &Queue, input: &InputManager) {
// Move the camera based on user input
self.camera.update(MovementRecord::mk(input));
self.camera.update_rotation(input.get_mouse_delta());

// Update the uniform buffer with the new camera matrix
self.matrix_uniform.update(self.camera.build_mvp());
Expand All @@ -137,16 +136,14 @@ impl WorldRenderer {
}

pub fn render<'rpass>(&'rpass self, rpass: &mut RenderPass<'rpass>) {
// check debug mode
// if self.debug_mode {
// // render with wireframe
// rpass.set_pipeline(&self.wireframe_pipeline.pipeline);
// } else {
// // render with texture
// rpass.set_pipeline(&self.main_pipeline.pipeline);
// }

rpass.set_pipeline(&self.main_pipeline.pipeline);
if self.debug_mode {
// render with wireframe
rpass.set_pipeline(&self.wireframe_pipeline.pipeline);
} else {
// render with texture
rpass.set_pipeline(&self.main_pipeline.pipeline);
}

rpass.set_bind_group(0, &self.diffuse_texture.bind_group, &[]);
rpass.set_bind_group(1, &self.matrix_uniform.bind_group, &[]);

Expand Down
12 changes: 6 additions & 6 deletions blockworld-client/world/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,22 +122,22 @@ impl Chunk {
{
BlockFaceDirection::empty()
} else {
if self.is_air(x - 1, y, z) {
if !self.is_air(x - 1, y, z) {
m |= BlockFaceDirection::XN;
}
if self.is_air(x + 1, y, z) {
if !self.is_air(x + 1, y, z) {
m |= BlockFaceDirection::XP;
}
if self.is_air(x, y - 1, z) {
if !self.is_air(x, y - 1, z) {
m |= BlockFaceDirection::YN;
}
if self.is_air(x, y + 1, z) {
if !self.is_air(x, y + 1, z) {
m |= BlockFaceDirection::YP;
}
if self.is_air(x, y, z - 1) {
if !self.is_air(x, y, z - 1) {
m |= BlockFaceDirection::ZN;
}
if self.is_air(x, y, z + 1) {
if !self.is_air(x, y, z + 1) {
m |= BlockFaceDirection::ZP;
}
m
Expand Down

0 comments on commit 6b23725

Please sign in to comment.