mirror of
https://github.com/griffi-gh/kubi.git
synced 2024-11-22 14:58:44 -06:00
camera works
This commit is contained in:
parent
078520ebbe
commit
2056170f34
50
src/game.rs
50
src/game.rs
|
@ -1,6 +1,6 @@
|
||||||
use glium::{Surface, uniform};
|
use glium::{Surface, uniform};
|
||||||
use glium::glutin::{
|
use glium::glutin::{
|
||||||
event::{Event, WindowEvent},
|
event::{Event, WindowEvent, VirtualKeyCode},
|
||||||
event_loop::{EventLoop, ControlFlow},
|
event_loop::{EventLoop, ControlFlow},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -24,22 +24,17 @@ pub fn run() {
|
||||||
log::info!("loading assets");
|
log::info!("loading assets");
|
||||||
let assets = Assets::load_all_sync(&display);
|
let assets = Assets::load_all_sync(&display);
|
||||||
log::info!("init camera");
|
log::info!("init camera");
|
||||||
let camera = Camera {
|
let mut camera = Camera::default();
|
||||||
position: [0., 0., -1.],
|
|
||||||
direction: [0., 0., 1.],
|
|
||||||
..Default::default()
|
|
||||||
};
|
|
||||||
log::info!("game loaded");
|
log::info!("game loaded");
|
||||||
|
|
||||||
//=======================
|
//=======================
|
||||||
let vertex1 = ChunkVertex { position: [-0.5, -0.5, 1.], uv: [0., 0.], normal: [0., 1., 0.] };
|
let vertex1 = ChunkVertex { position: [-0.5, -0.5, 0.], uv: [0., 0.], normal: [0., 1., 0.] };
|
||||||
let vertex2 = ChunkVertex { position: [ 0.0, 0.5, 1.], uv: [0., 1.], normal: [0., 1., 0.] };
|
let vertex2 = ChunkVertex { position: [ 0.0, 0.5, 0.], uv: [0., 1.], normal: [0., 1., 0.] };
|
||||||
let vertex3 = ChunkVertex { position: [ 0.5, -0.25, 1.], uv: [1., 1.], normal: [0., 1., 0.] };
|
let vertex3 = ChunkVertex { position: [ 0.5, -0.25, 0.], uv: [1., 1.], normal: [0., 1., 0.] };
|
||||||
let shape = vec![vertex1, vertex2, vertex3];
|
let shape = vec![vertex1, vertex2, vertex3];
|
||||||
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
|
let vertex_buffer = glium::VertexBuffer::new(&display, &shape).unwrap();
|
||||||
//=======================
|
//=======================
|
||||||
|
|
||||||
|
|
||||||
event_loop.run(move |ev, _, control_flow| {
|
event_loop.run(move |ev, _, control_flow| {
|
||||||
#[allow(clippy::single_match, clippy::collapsible_match)]
|
#[allow(clippy::single_match, clippy::collapsible_match)]
|
||||||
match ev {
|
match ev {
|
||||||
|
@ -49,12 +44,45 @@ pub fn run() {
|
||||||
*control_flow = ControlFlow::Exit;
|
*control_flow = ControlFlow::Exit;
|
||||||
return
|
return
|
||||||
},
|
},
|
||||||
|
WindowEvent::KeyboardInput { input, .. } => {
|
||||||
|
match input.virtual_keycode {
|
||||||
|
Some(VirtualKeyCode::Up) => {
|
||||||
|
camera.pitch += 0.01;
|
||||||
|
}
|
||||||
|
Some(VirtualKeyCode::Down) => {
|
||||||
|
camera.pitch -= 0.01;
|
||||||
|
}
|
||||||
|
Some(VirtualKeyCode::Right) => {
|
||||||
|
camera.yaw -= 0.01;
|
||||||
|
}
|
||||||
|
Some(VirtualKeyCode::Left) => {
|
||||||
|
camera.yaw += 0.01;
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(VirtualKeyCode::W) => {
|
||||||
|
camera.position[2] += 0.01;
|
||||||
|
}
|
||||||
|
Some(VirtualKeyCode::S) => {
|
||||||
|
camera.position[2] -= 0.01;
|
||||||
|
}
|
||||||
|
Some(VirtualKeyCode::A) => {
|
||||||
|
camera.position[0] -= 0.01;
|
||||||
|
}
|
||||||
|
Some(VirtualKeyCode::D) => {
|
||||||
|
camera.position[0] += 0.01;
|
||||||
|
}
|
||||||
|
|
||||||
|
_ => ()
|
||||||
|
}
|
||||||
|
}
|
||||||
_ => ()
|
_ => ()
|
||||||
},
|
},
|
||||||
_ => ()
|
_ => ()
|
||||||
}
|
}
|
||||||
|
|
||||||
let mut target = display.draw();
|
let mut target = display.draw();
|
||||||
let target_dimensions = target.get_dimensions();
|
let target_dimensions = target.get_dimensions();
|
||||||
|
camera.update_direction();
|
||||||
let perspective = camera.perspective_matrix(target_dimensions);
|
let perspective = camera.perspective_matrix(target_dimensions);
|
||||||
let view = camera.view_matrix();
|
let view = camera.view_matrix();
|
||||||
target.clear_color_and_depth((0.5, 0.5, 1., 1.), 1.);
|
target.clear_color_and_depth((0.5, 0.5, 1., 1.), 1.);
|
||||||
|
@ -67,7 +95,7 @@ pub fn run() {
|
||||||
[1., 0., 0., 0.],
|
[1., 0., 0., 0.],
|
||||||
[0., 1., 0., 0.],
|
[0., 1., 0., 0.],
|
||||||
[0., 0., 1., 0.],
|
[0., 0., 1., 0.],
|
||||||
[0., 0., 0., 1.0f32]
|
[0., 0., 0., 1.0_f32]
|
||||||
],
|
],
|
||||||
view: view,
|
view: view,
|
||||||
perspective: perspective,
|
perspective: perspective,
|
||||||
|
|
|
@ -6,6 +6,8 @@
|
||||||
use std::f32::consts::PI;
|
use std::f32::consts::PI;
|
||||||
|
|
||||||
pub struct Camera {
|
pub struct Camera {
|
||||||
|
pub yaw: f32,
|
||||||
|
pub pitch: f32,
|
||||||
pub position: [f32; 3],
|
pub position: [f32; 3],
|
||||||
pub direction: [f32; 3],
|
pub direction: [f32; 3],
|
||||||
pub up: [f32; 3],
|
pub up: [f32; 3],
|
||||||
|
@ -14,6 +16,15 @@ pub struct Camera {
|
||||||
pub zfar: f32,
|
pub zfar: f32,
|
||||||
}
|
}
|
||||||
impl Camera {
|
impl Camera {
|
||||||
|
/// Update camera direction based on yaw/pitch
|
||||||
|
pub fn update_direction(&mut self) {
|
||||||
|
self.direction = [
|
||||||
|
self.yaw.cos() * self.pitch.cos(),
|
||||||
|
self.pitch.sin(),
|
||||||
|
self.yaw.sin() * self.pitch.cos(),
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
pub fn view_matrix(&self) -> [[f32; 4]; 4] {
|
pub fn view_matrix(&self) -> [[f32; 4]; 4] {
|
||||||
let position = self.position;
|
let position = self.position;
|
||||||
let direction = self.direction;
|
let direction = self.direction;
|
||||||
|
@ -62,7 +73,6 @@ impl Camera {
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Camera {
|
impl Default for Camera {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
|
@ -72,6 +82,8 @@ impl Default for Camera {
|
||||||
fov: PI / 3.,
|
fov: PI / 3.,
|
||||||
zfar: 1024.,
|
zfar: 1024.,
|
||||||
znear: 0.1,
|
znear: 0.1,
|
||||||
|
yaw: 0.,
|
||||||
|
pitch: 0.,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue