Skip to content

Commit

Permalink
todo
Browse files Browse the repository at this point in the history
  • Loading branch information
TadaTeruki committed Aug 17, 2024
1 parent bfbdad5 commit e1492c0
Show file tree
Hide file tree
Showing 7 changed files with 460 additions and 6 deletions.
270 changes: 270 additions & 0 deletions graphics/resources/untitled.obj
Original file line number Diff line number Diff line change
@@ -0,0 +1,270 @@
# Blender 3.4.1
# www.blender.org
mtllib untitled.mtl
o Icosphere
v 0.000000 -1.000000 0.000000
v 0.723607 -0.447220 0.525725
v -0.276388 -0.447220 0.850649
v -0.894426 -0.447216 0.000000
v -0.276388 -0.447220 -0.850649
v 0.723607 -0.447220 -0.525725
v 0.276388 0.447220 0.850649
v -0.723607 0.447220 0.525725
v -0.723607 0.447220 -0.525725
v 0.276388 0.447220 -0.850649
v 0.894426 0.447216 0.000000
v 0.000000 1.000000 0.000000
v -0.162456 -0.850654 0.499995
v 0.425323 -0.850654 0.309011
v 0.262869 -0.525738 0.809012
v 0.850648 -0.525736 0.000000
v 0.425323 -0.850654 -0.309011
v -0.525730 -0.850652 0.000000
v -0.688189 -0.525736 0.499997
v -0.162456 -0.850654 -0.499995
v -0.688189 -0.525736 -0.499997
v 0.262869 -0.525738 -0.809012
v 0.951058 0.000000 0.309013
v 0.951058 0.000000 -0.309013
v 0.000000 0.000000 1.000000
v 0.587786 0.000000 0.809017
v -0.951058 0.000000 0.309013
v -0.587786 0.000000 0.809017
v -0.587786 0.000000 -0.809017
v -0.951058 0.000000 -0.309013
v 0.587786 0.000000 -0.809017
v 0.000000 0.000000 -1.000000
v 0.688189 0.525736 0.499997
v -0.262869 0.525738 0.809012
v -0.850648 0.525736 0.000000
v -0.262869 0.525738 -0.809012
v 0.688189 0.525736 -0.499997
v 0.162456 0.850654 0.499995
v 0.525730 0.850652 0.000000
v -0.425323 0.850654 0.309011
v -0.425323 0.850654 -0.309011
v 0.162456 0.850654 -0.499995
vn 0.1024 -0.9435 0.3151
vn 0.7002 -0.6617 0.2680
vn -0.2680 -0.9435 0.1947
vn -0.2680 -0.9435 -0.1947
vn 0.1024 -0.9435 -0.3151
vn 0.9050 -0.3304 0.2680
vn 0.0247 -0.3304 0.9435
vn -0.8897 -0.3304 0.3151
vn -0.5746 -0.3304 -0.7488
vn 0.5346 -0.3304 -0.7779
vn 0.8026 -0.1256 0.5831
vn -0.3066 -0.1256 0.9435
vn -0.9921 -0.1256 -0.0000
vn -0.3066 -0.1256 -0.9435
vn 0.8026 -0.1256 -0.5831
vn 0.4089 0.6617 0.6284
vn -0.4713 0.6617 0.5831
vn -0.7002 0.6617 -0.2680
vn 0.0385 0.6617 -0.7488
vn 0.7240 0.6617 -0.1947
vn 0.2680 0.9435 -0.1947
vn 0.4911 0.7947 -0.3568
vn 0.4089 0.6617 -0.6284
vn -0.1024 0.9435 -0.3151
vn -0.1876 0.7947 -0.5773
vn -0.4713 0.6617 -0.5831
vn -0.3313 0.9435 -0.0000
vn -0.6071 0.7947 -0.0000
vn -0.7002 0.6617 0.2680
vn -0.1024 0.9435 0.3151
vn -0.1876 0.7947 0.5773
vn 0.0385 0.6617 0.7488
vn 0.2680 0.9435 0.1947
vn 0.4911 0.7947 0.3568
vn 0.7240 0.6617 0.1947
vn 0.8897 0.3304 -0.3151
vn 0.7947 0.1876 -0.5773
vn 0.5746 0.3304 -0.7488
vn -0.0247 0.3304 -0.9435
vn -0.3035 0.1876 -0.9342
vn -0.5346 0.3304 -0.7779
vn -0.9050 0.3304 -0.2680
vn -0.9822 0.1876 -0.0000
vn -0.9050 0.3304 0.2680
vn -0.5346 0.3304 0.7779
vn -0.3035 0.1876 0.9342
vn -0.0247 0.3304 0.9435
vn 0.5746 0.3304 0.7488
vn 0.7947 0.1876 0.5773
vn 0.8897 0.3304 0.3151
vn 0.3066 0.1256 -0.9435
vn 0.3035 -0.1876 -0.9342
vn 0.0247 -0.3304 -0.9435
vn -0.8026 0.1256 -0.5831
vn -0.7947 -0.1876 -0.5773
vn -0.8897 -0.3304 -0.3151
vn -0.8026 0.1256 0.5831
vn -0.7947 -0.1876 0.5773
vn -0.5746 -0.3304 0.7488
vn 0.3066 0.1256 0.9435
vn 0.3035 -0.1876 0.9342
vn 0.5346 -0.3304 0.7779
vn 0.9921 0.1256 -0.0000
vn 0.9822 -0.1876 -0.0000
vn 0.9050 -0.3304 -0.2680
vn 0.4713 -0.6617 -0.5831
vn 0.1876 -0.7947 -0.5773
vn -0.0385 -0.6617 -0.7488
vn -0.4089 -0.6617 -0.6284
vn -0.4911 -0.7947 -0.3568
vn -0.7240 -0.6617 -0.1947
vn -0.7240 -0.6617 0.1947
vn -0.4911 -0.7947 0.3568
vn -0.4089 -0.6617 0.6284
vn 0.7002 -0.6617 -0.2680
vn 0.6071 -0.7947 -0.0000
vn 0.3313 -0.9435 -0.0000
vn -0.0385 -0.6617 0.7488
vn 0.1876 -0.7947 0.5773
vn 0.4713 -0.6617 0.5831
vt 0.181819 0.000000
vt 0.909091 0.000000
vt 0.727273 0.000000
vt 0.545455 0.000000
vt 0.363637 0.000000
vt 0.272728 0.157461
vt 1.000000 0.157461
vt 0.090910 0.157461
vt 0.818182 0.157461
vt 0.636364 0.157461
vt 0.454546 0.157461
vt 0.181819 0.314921
vt 0.000000 0.314921
vt 0.909091 0.314921
vt 0.727273 0.314921
vt 0.545455 0.314921
vt 0.363637 0.314921
vt 0.454546 0.472382
vt 0.636364 0.472382
vt 0.818182 0.472382
vt 0.090910 0.472382
vt 0.272728 0.472382
vt 0.954545 0.078731
vt 0.136365 0.078731
vt 0.318182 0.078731
vt 0.227273 0.078731
vt 0.181819 0.157461
vt 0.363637 0.157461
vt 0.500000 0.078731
vt 0.409092 0.078731
vt 0.772727 0.078731
vt 0.863636 0.078731
vt 0.909091 0.157461
vt 0.590909 0.078731
vt 0.681818 0.078731
vt 0.727273 0.157461
vt 0.545455 0.157461
vt 0.318182 0.236191
vt 0.409092 0.236191
vt 0.136365 0.236191
vt 0.227273 0.236191
vt 0.863636 0.236191
vt 0.045455 0.236191
vt 0.954545 0.236191
vt 0.681818 0.236191
vt 0.772727 0.236191
vt 0.500000 0.236191
vt 0.590909 0.236191
vt 0.272728 0.314921
vt 0.090910 0.314921
vt 0.818182 0.314921
vt 0.636364 0.314921
vt 0.454546 0.314921
vt 0.136365 0.393651
vt 0.227273 0.393651
vt 0.409092 0.393651
vt 0.318182 0.393651
vt 0.863636 0.393651
vt 0.045455 0.393651
vt 0.681818 0.393651
vt 0.772727 0.393651
vt 0.500000 0.393651
vt 0.590909 0.393651
s 0
f 1/1/1 14/26/1 13/24/1
f 2/6/2 14/25/2 16/28/2
f 1/2/3 13/23/3 18/32/3
f 1/3/4 18/31/4 20/35/4
f 1/4/5 20/34/5 17/29/5
f 2/6/6 16/28/6 23/38/6
f 3/8/7 15/27/7 25/40/7
f 4/9/8 19/33/8 27/42/8
f 5/10/9 21/36/9 29/45/9
f 6/11/10 22/37/10 31/47/10
f 2/6/11 23/38/11 26/41/11
f 3/8/12 25/40/12 28/43/12
f 4/9/13 27/42/13 30/46/13
f 5/10/14 29/45/14 32/48/14
f 6/11/15 31/47/15 24/39/15
f 7/12/16 33/49/16 38/55/16
f 8/13/17 34/50/17 40/59/17
f 9/15/18 35/51/18 41/61/18
f 10/16/19 36/52/19 42/63/19
f 11/17/20 37/53/20 39/56/20
f 39/56/21 42/62/21 12/18/21
f 39/56/22 37/53/22 42/62/22
f 37/53/23 10/16/23 42/62/23
f 42/63/24 41/60/24 12/19/24
f 42/63/25 36/52/25 41/60/25
f 36/52/26 9/15/26 41/60/26
f 41/61/27 40/58/27 12/20/27
f 41/61/28 35/51/28 40/58/28
f 35/51/29 8/14/29 40/58/29
f 40/59/30 38/54/30 12/21/30
f 40/59/31 34/50/31 38/54/31
f 34/50/32 7/12/32 38/54/32
f 38/55/33 39/57/33 12/22/33
f 38/55/34 33/49/34 39/57/34
f 33/49/35 11/17/35 39/57/35
f 24/39/36 37/53/36 11/17/36
f 24/39/37 31/47/37 37/53/37
f 31/47/38 10/16/38 37/53/38
f 32/48/39 36/52/39 10/16/39
f 32/48/40 29/45/40 36/52/40
f 29/45/41 9/15/41 36/52/41
f 30/46/42 35/51/42 9/15/42
f 30/46/43 27/42/43 35/51/43
f 27/42/44 8/14/44 35/51/44
f 28/43/45 34/50/45 8/13/45
f 28/43/46 25/40/46 34/50/46
f 25/40/47 7/12/47 34/50/47
f 26/41/48 33/49/48 7/12/48
f 26/41/49 23/38/49 33/49/49
f 23/38/50 11/17/50 33/49/50
f 31/47/51 32/48/51 10/16/51
f 31/47/52 22/37/52 32/48/52
f 22/37/53 5/10/53 32/48/53
f 29/45/54 30/46/54 9/15/54
f 29/45/55 21/36/55 30/46/55
f 21/36/56 4/9/56 30/46/56
f 27/42/57 28/44/57 8/14/57
f 27/42/58 19/33/58 28/44/58
f 19/33/59 3/7/59 28/44/59
f 25/40/60 26/41/60 7/12/60
f 25/40/61 15/27/61 26/41/61
f 15/27/62 2/6/62 26/41/62
f 23/38/63 24/39/63 11/17/63
f 23/38/64 16/28/64 24/39/64
f 16/28/65 6/11/65 24/39/65
f 17/29/66 22/37/66 6/11/66
f 17/29/67 20/34/67 22/37/67
f 20/34/68 5/10/68 22/37/68
f 20/35/69 21/36/69 5/10/69
f 20/35/70 18/31/70 21/36/70
f 18/31/71 4/9/71 21/36/71
f 18/32/72 19/33/72 4/9/72
f 18/32/73 13/23/73 19/33/73
f 13/23/74 3/7/74 19/33/74
f 16/28/75 17/30/75 6/11/75
f 16/28/76 14/25/76 17/30/76
f 14/25/77 1/5/77 17/30/77
f 13/24/78 15/27/78 3/8/78
f 13/24/79 14/26/79 15/27/79
f 14/26/80 2/6/80 15/27/80
70 changes: 70 additions & 0 deletions graphics/src/camera/controller.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use cgmath::InnerSpace;

use crate::key::{KeyState, KeyStateMap};

use super::Camera;

pub struct CameraController {
speed: f32,
move_forward_pressed: bool,
move_backward_pressed: bool,
move_left_pressed: bool,
move_right_pressed: bool,
}

impl CameraController {
pub fn new(speed: f32) -> Self {
Self {
speed,
move_forward_pressed: false,
move_backward_pressed: false,
move_left_pressed: false,
move_right_pressed: false,
}
}

pub fn process_events(&mut self, key_states: KeyStateMap) {
key_states
.iter()
.for_each(|(key, state)| match key.as_str() {
"w" | "ArrowUp" => {
self.move_forward_pressed = *state != KeyState::Release;
}
"s" | "ArrowDown" => {
self.move_backward_pressed = *state != KeyState::Release;
}
"a" | "ArrowLeft" => {
self.move_left_pressed = *state != KeyState::Release;
}
"d" | "ArrowRight" => {
self.move_right_pressed = *state != KeyState::Release;
}
_ => {}
});
}

pub fn update_camera(&self, camera: &mut Camera) {
let forward = camera.target - camera.eye;
let forward_norm = forward.normalize();
let forward_mag = forward.magnitude();

if self.move_forward_pressed && forward_mag > self.speed {
camera.eye += forward_norm * self.speed;
}
if self.move_backward_pressed {
camera.eye -= forward_norm * self.speed;
}

let right = forward_norm.cross(camera.up);

let forward = camera.target - camera.eye;
let forward_mag = forward.magnitude();

if self.move_right_pressed {
camera.eye = camera.target - (forward + right * self.speed).normalize() * forward_mag;
}
if self.move_left_pressed {
camera.eye = camera.target - (forward - right * self.speed).normalize() * forward_mag;
}
}
}
28 changes: 28 additions & 0 deletions graphics/src/camera/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
pub mod controller;
mod uniform;

pub struct Camera {
pub eye: cgmath::Point3<f32>,
pub target: cgmath::Point3<f32>,
pub up: cgmath::Vector3<f32>,
pub aspect: f32,
pub fovy: f32,
pub znear: f32,
pub zfar: f32,
}

impl Camera {
pub fn build_view_projection_matrix(&self) -> cgmath::Matrix4<f32> {
let view = cgmath::Matrix4::look_at_rh(self.eye, self.target, self.up);
let proj = cgmath::perspective(cgmath::Deg(self.fovy), self.aspect, self.znear, self.zfar);
return OPENGL_TO_WGPU_MATRIX * proj * view;
}
}

#[rustfmt::skip]
pub const OPENGL_TO_WGPU_MATRIX: cgmath::Matrix4<f32> = cgmath::Matrix4::new(
1.0, 0.0, 0.0, 0.0,
0.0, 1.0, 0.0, 0.0,
0.0, 0.0, 0.5, 0.5,
0.0, 0.0, 0.0, 1.0,
);
21 changes: 21 additions & 0 deletions graphics/src/camera/uniform.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use super::Camera;


#[repr(C)]
#[derive(Debug, Copy, Clone, bytemuck::Pod, bytemuck::Zeroable)]
pub struct CameraUniform {
view_proj: [[f32; 4]; 4],
}

impl CameraUniform {
pub fn new() -> Self {
use cgmath::SquareMatrix;
Self {
view_proj: cgmath::Matrix4::identity().into(),
}
}

pub fn update_view_proj(&mut self, camera: &Camera) {
self.view_proj = camera.build_view_projection_matrix().into();
}
}
Loading

0 comments on commit e1492c0

Please sign in to comment.