Skip to content

Commit

Permalink
Give camera mode its own setter method
Browse files Browse the repository at this point in the history
Remove the with_mode constructor.
  • Loading branch information
jdahlstrom committed Aug 10, 2024
1 parent cd860af commit b8400ad
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 16 deletions.
24 changes: 12 additions & 12 deletions core/src/render/cam.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,25 +63,23 @@ pub struct FirstPerson {
// Inherent impls
//

impl<M: Mode> Camera<M> {
impl Camera<()> {
/// Creates a camera with the given resolution.
pub fn new(res_x: u32, res_y: u32) -> Self
where
M: Default,
{
Self::with_mode(res_x, res_y, M::default())
}

/// Creates a camera with the given resolution and mode.
pub fn with_mode(res_x: u32, res_y: u32, mode: M) -> Self {
pub fn new(res_x: u32, res_y: u32) -> Self {
Self {
res: (res_x, res_y),
mode,
project: Default::default(),
viewport: viewport(vec2(0, 0)..vec2(res_x, res_y)),
..Default::default()
}
}

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

impl<M> Camera<M> {
/// Sets the viewport bounds of this camera.
pub fn viewport(self, bounds: impl Into<Rect<u32>>) -> Self {
let (w, h) = self.res;
Expand Down Expand Up @@ -119,7 +117,9 @@ impl<M: Mode> Camera<M> {
..self
}
}
}

impl<M: Mode> Camera<M> {
/// Renders the given geometry from the viewpoint of this camera.
pub fn render<B, Vtx: Clone, Var: Vary, Uni: Copy, Shd>(
&self,
Expand Down
3 changes: 2 additions & 1 deletion demos/src/bin/crates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,8 @@ fn main() {
},
);

let mut cam = Camera::<FirstPerson>::new(W, H)
let mut cam = Camera::new(W, H)
.mode(FirstPerson::default())
.viewport((10..W - 10, 10..H - 10))
.perspective(1.0, 0.1..1000.0);

Expand Down
6 changes: 3 additions & 3 deletions demos/src/bin/solids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,8 @@ fn main() {
bunny,
];

let camera = Camera::with_mode(W, H, scale(vec3(1.0, -1.0, -1.0)).to())
let cam = Camera::new(W, H)
.mode(scale(vec3(1.0, -1.0, -1.0)).to())
.perspective(1.5, 0.1..1000.0)
.viewport(vec2(10, 10)..vec2(W - 10, H - 10));

Expand All @@ -170,7 +171,7 @@ fn main() {
}

let obj = &objs[carousel.idx % objs.len()];
camera.render(
cam.render(
&obj.faces,
&obj.verts,
&model_to_world,
Expand All @@ -179,7 +180,6 @@ fn main() {
&mut frame.buf,
&frame.ctx,
);

Continue(())
});
}

0 comments on commit b8400ad

Please sign in to comment.