Skip to content

Commit a9479c0

Browse files
[render] minor refactor, wip camera struct
1 parent bf06650 commit a9479c0

File tree

6 files changed

+60
-7
lines changed

6 files changed

+60
-7
lines changed

crates/eqx_core/src/util_macros.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// TODO: remove these. Path concatenation should be done in runtime
12
#[macro_export]
23
macro_rules! shader_src_from {
34
($path:expr) => {{

crates/eqx_render/src/lib.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
1+
/*!
2+
This crate `eqx_render` equips more advanced algorithms for rendering.
3+
Basic rendering facilities are included in `eqx_window`.
4+
*/
5+
16
pub fn add(left: u64, right: u64) -> u64 {
27
left + right
38
}

crates/eqx_window/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ edition = "2021"
77
anyhow = "1.0"
88
bytemuck = { version = "1.22", features = ["derive"] }
99
eqx_core = { path = "../eqx_core", version = "0.1.0" }
10-
cgmath = "0.18"
10+
glam = "0.30"
1111
eqx_app = { path = "../eqx_app", version = "0.1.0" }
1212
image = { version = "0.25", default-features = false, features = ["png"] }
1313
log = "0.4"

crates/eqx_window/src/camera.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
pub use glam::{Mat4, Vec3};
2+
3+
pub struct Camera {
4+
pub pos: Vec3,
5+
pub target: Vec3,
6+
pub up: Vec3,
7+
pub aspect: f32,
8+
pub fov: f32,
9+
pub near_clip: f32,
10+
pub far_clip: f32,
11+
}
12+
13+
impl Camera {
14+
fn view_projection_matrix(&self) -> Mat4 {
15+
#[rustfmt::skip]
16+
pub const OPENGL_TO_WGPU_MATRIX: glam::Mat4 = glam::Mat4::from_cols(
17+
glam::Vec4::new(1.0, 0.0, 0.0, 0.0),
18+
glam::Vec4::new(0.0, 1.0, 0.0, 0.0),
19+
glam::Vec4::new(0.0, 0.0, 0.5, 0.0),
20+
glam::Vec4::new(0.0, 0.0, 0.5, 1.0),
21+
);
22+
23+
let view = Mat4::look_at_rh(self.pos, self.target, self.up);
24+
let projection = Mat4::perspective_rh(self.aspect, self.fov, self.near_clip, self.far_clip);
25+
26+
OPENGL_TO_WGPU_MATRIX * projection * view
27+
}
28+
}

crates/eqx_window/src/lib.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1+
/*!
2+
`eqx_window` provides window creation and basic rendering utilities (camera, texture, light, etc.)
3+
There is a separate crate `eqx_render` for deeper rendering algorithms
4+
*/
5+
6+
mod camera;
17
mod primitives;
28
mod texture;
3-
mod window;
9+
mod window_display;
410

511
pub mod prelude {
6-
pub use crate::window::WindowDisplay;
12+
pub use crate::window_display::WindowDisplay;
713
}
Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
1+
use crate::camera::Camera;
12
use crate::primitives::{Vertex, TEST_INDICES, TEST_VERTICES};
23
use crate::texture::Texture;
34
use eqx_app::prelude::Module;
4-
use wgpu::util::{BufferInitDescriptor, DeviceExt};
5+
use eqx_core::shader_src_from;
56

7+
use glam::Vec3;
8+
use wgpu::util::{BufferInitDescriptor, DeviceExt};
69
use winit::{
710
event::*,
811
event_loop::EventLoop,
912
keyboard::{KeyCode, PhysicalKey},
13+
window::Window as WInitWindow,
1014
window::WindowBuilder,
1115
};
1216

13-
use eqx_core::shader_src_from;
14-
use winit::window::Window as WInitWindow;
15-
1617
const VS_MAIN: &str = "vs_main";
1718
const FS_MAIN: &str = "fs_main";
1819

@@ -27,6 +28,7 @@ struct State<'a> {
2728
vertex_buffer: wgpu::Buffer,
2829
index_buffer: wgpu::Buffer,
2930
texture_bind_group: wgpu::BindGroup,
31+
camera: Camera,
3032
}
3133

3234
impl<'a> State<'a> {
@@ -189,6 +191,16 @@ impl<'a> State<'a> {
189191
usage: wgpu::BufferUsages::INDEX,
190192
});
191193

194+
let camera = Camera {
195+
pos: Vec3::new(0.0, 1.0, 2.0),
196+
target: Vec3::new(0.0, 0.0, 0.0),
197+
up: Vec3::new(0.0, 1.0, 0.0),
198+
aspect: config.width as f32 / config.height as f32,
199+
fov: 45.0,
200+
near_clip: 0.1,
201+
far_clip: 100.0,
202+
};
203+
192204
Self {
193205
surface,
194206
device,
@@ -200,6 +212,7 @@ impl<'a> State<'a> {
200212
vertex_buffer,
201213
index_buffer,
202214
texture_bind_group,
215+
camera,
203216
}
204217
}
205218

0 commit comments

Comments
 (0)