-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
49 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,52 @@ | ||
use super::*; | ||
|
||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
pub enum Camera2dFov { | ||
Vertical(f32), | ||
Horizontal(f32), | ||
MinSide(f32), | ||
MaxSide(f32), | ||
} | ||
impl Camera2dFov { | ||
pub fn value_mut(&mut self) -> &mut f32 { | ||
match self { | ||
Camera2dFov::Vertical(value) => value, | ||
Camera2dFov::Horizontal(value) => value, | ||
Camera2dFov::MinSide(value) => value, | ||
Camera2dFov::MaxSide(value) => value, | ||
} | ||
} | ||
pub fn value(&self) -> f32 { | ||
match *self { | ||
Camera2dFov::Vertical(value) => value, | ||
Camera2dFov::Horizontal(value) => value, | ||
Camera2dFov::MinSide(value) => value, | ||
Camera2dFov::MaxSide(value) => value, | ||
} | ||
} | ||
} | ||
|
||
/// 2-dimensional camera. | ||
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)] | ||
pub struct Camera2d { | ||
pub center: vec2<f32>, | ||
pub rotation: Angle<f32>, | ||
pub fov: f32, | ||
pub fov: Camera2dFov, | ||
} | ||
|
||
impl AbstractCamera2d for Camera2d { | ||
fn view_matrix(&self) -> mat3<f32> { | ||
mat3::rotate(-self.rotation) * mat3::translate(-self.center) | ||
} | ||
fn projection_matrix(&self, framebuffer_size: vec2<f32>) -> mat3<f32> { | ||
mat3::scale(vec2(2.0 * framebuffer_size.y / framebuffer_size.x, 2.0) / self.fov) | ||
let aspect = framebuffer_size.aspect(); | ||
let (vertical, fov) = match self.fov { | ||
Camera2dFov::Vertical(fov) => (true, fov), | ||
Camera2dFov::Horizontal(fov) => (false, fov), | ||
Camera2dFov::MinSide(fov) => (aspect > 1.0, fov), | ||
Camera2dFov::MaxSide(fov) => (aspect < 1.0, fov), | ||
}; | ||
let vertical_fov = if vertical { fov } else { fov / aspect }; | ||
mat3::scale(vec2(2.0 / aspect, 2.0) / vertical_fov) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters