Skip to content

Commit

Permalink
Rename cam::Mode to Transform which is more descriptive
Browse files Browse the repository at this point in the history
  • Loading branch information
jdahlstrom committed Jan 1, 2025
1 parent 414b8d1 commit 50fd6e9
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 23 deletions.
29 changes: 16 additions & 13 deletions core/src/render/cam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,17 @@ use super::{
VertexShader, View, ViewToProj, World, WorldToView,
};

/// Camera movement mode.
///
/// TODO Rename to something more specific (e.g. `Motion`?)
pub trait Mode {
/// Trait for different camera motion transforms.
pub trait Transform {
/// Returns the current world-to-view matrix of this camera mode.
fn world_to_view(&self) -> Mat4x4<WorldToView>;
}

/// Type to manage the world-to-viewport transformation.
#[derive(Copy, Clone, Debug, Default)]
pub struct Camera<M> {
pub struct Camera<Tf> {
/// The movement mode of the camera.
pub mode: M,
pub transform: Tf,
/// Viewport width and height.
pub dims: Dims,
/// Projection matrix.
Expand Down Expand Up @@ -85,9 +83,14 @@ impl Camera<()> {
}
}

pub fn mode<M: Mode>(self, mode: M) -> Camera<M> {
pub fn transform<M: Transform>(self, mode: M) -> Camera<M> {
let Self { dims, project, viewport, .. } = self;
Camera { mode, dims, project, viewport }
Camera {
transform: mode,
dims,
project,
viewport,
}
}
}

Expand Down Expand Up @@ -131,10 +134,10 @@ impl<M> Camera<M> {
}
}

impl<M: Mode> Camera<M> {
impl<M: Transform> Camera<M> {
/// Returns the composed camera and projection matrix.
pub fn world_to_project(&self) -> Mat4x4<RealToProj<World>> {
self.mode.world_to_view().then(&self.project)
self.transform.world_to_view().then(&self.project)
}

/// Renders the given geometry from the viewpoint of this camera.
Expand Down Expand Up @@ -262,7 +265,7 @@ impl Orbit {
//

#[cfg(feature = "fp")]
impl Mode for FirstPerson {
impl Transform for FirstPerson {
fn world_to_view(&self) -> Mat4x4<WorldToView> {
let &Self { pos, heading, .. } = self;
let fwd_move = az_alt(heading.az(), turns(0.0)).to_cart();
Expand All @@ -278,7 +281,7 @@ impl Mode for FirstPerson {
}

#[cfg(feature = "fp")]
impl Mode for Orbit {
impl Transform for Orbit {
fn world_to_view(&self) -> Mat4x4<WorldToView> {
// TODO Figure out how to do this with orient
//let fwd = self.dir.to_cart().normalize();
Expand All @@ -294,7 +297,7 @@ impl Mode for Orbit {
}
}

impl Mode for Mat4x4<WorldToView> {
impl Transform for Mat4x4<WorldToView> {
fn world_to_view(&self) -> Mat4x4<WorldToView> {
*self
}
Expand Down
2 changes: 1 addition & 1 deletion demos/src/bin/bezier.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use re::geom::Ray;
use std::{mem::swap, ops::ControlFlow::Continue};

use re::prelude::*;

use re::geom::Ray;
use re::math::rand::{Distrib, Uniform, VectorsOnUnitDisk, Xorshift64};

use re_front::{dims::SVGA_800_600, minifb::Window, Frame};
Expand Down
10 changes: 6 additions & 4 deletions demos/src/bin/crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use core::ops::ControlFlow::*;
use re::prelude::*;

use re::math::color::gray;
use re::render::{cam::FirstPerson, Batch, Camera, ModelToProj};
use re::render::{
cam::FirstPerson, shader::Shader, Batch, Camera, ModelToProj,
};

use re_front::sdl2::Window;
use re_geom::solids::Box;
Expand Down Expand Up @@ -32,7 +34,7 @@ fn main() {

let (w, h) = win.dims;
let mut cam = Camera::new(win.dims)
.mode(FirstPerson::default())
.transform(FirstPerson::default())
.viewport((10..w - 10, 10..h - 10))
.perspective(1.0, 0.1..1000.0);

Expand Down Expand Up @@ -61,8 +63,8 @@ fn main() {
let d_az = turns(ms.x() as f32) * -0.001;
let d_alt = turns(ms.y() as f32) * 0.001;

cam.mode.rotate(d_az, d_alt);
cam.mode
cam.transform.rotate(d_az, d_alt);
cam.transform
.translate(cam_vel.mul(frame.dt.as_secs_f32()));

let flip = scale3(1.0, -1.0, -1.0).to();
Expand Down
2 changes: 1 addition & 1 deletion demos/src/bin/solids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn main() {

let (w, h) = win.dims;
let cam = Camera::new(win.dims)
.mode(scale3(1.0, -1.0, -1.0).to())
.transform(scale3(1.0, -1.0, -1.0).to())
.perspective(1.5, 0.1..1000.0)
.viewport(vec2(10, 10)..vec2(w - 10, h - 10));

Expand Down
8 changes: 5 additions & 3 deletions demos/src/bin/sprites.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use core::{array::from_fn, ops::ControlFlow::Continue};
use re::prelude::*;

use re::math::rand::{Distrib, PointsInUnitBall, Xorshift64};
use re::render::{cam::Mode, render, Model, ModelToView, ViewToProj};
use re::render::{
cam::Transform, render, shader::Shader, Model, ModelToView, ViewToProj,
};
use re_front::minifb::Window;

fn main() {
Expand Down Expand Up @@ -51,7 +53,7 @@ fn main() {

let (w, h) = win.dims;
let cam = Camera::new(win.dims)
.mode(translate(0.5 * Vec3::Z).to())
.transform(translate(0.5 * Vec3::Z).to())
.perspective(1.0, 1e-2..1e3)
.viewport(vec2(10, 10)..vec2(w - 10, h - 10));

Expand All @@ -61,7 +63,7 @@ fn main() {
let modelview = rotate_x(theta * 0.2)
.then(&rotate_z(theta * 0.14))
.to()
.then(&cam.mode.world_to_view());
.then(&cam.transform.world_to_view());

render(
&tris,
Expand Down
4 changes: 3 additions & 1 deletion demos/src/bin/square.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ use std::ops::ControlFlow::*;
use re::prelude::*;

use re::math::{pt2, pt3};
use re::render::{render, tex::SamplerClamp, Context, ModelToProj};
use re::render::{
render, shader::Shader, tex::SamplerClamp, Context, ModelToProj,
};

use re_front::minifb::Window;

Expand Down

0 comments on commit 50fd6e9

Please sign in to comment.