aboutsummaryrefslogtreecommitdiff
path: root/src/camera/camera.rs
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-02-06 17:27:42 +0100
committerJulian T <julian@jtle.dk>2021-02-06 17:27:42 +0100
commit0d5e6bd9363d5ed5c4f28174819fc0f5fd9aa586 (patch)
treef9ecafe7350ad616486e509ed55904295f505f83 /src/camera/camera.rs
parent1e83ea211055eb234b89c69b5d03602e3fcb98fb (diff)
Reorganized scene module, and fixed bug in sphere intersect
Diffstat (limited to 'src/camera/camera.rs')
-rw-r--r--src/camera/camera.rs24
1 files changed, 16 insertions, 8 deletions
diff --git a/src/camera/camera.rs b/src/camera/camera.rs
index 7822e92..d49220b 100644
--- a/src/camera/camera.rs
+++ b/src/camera/camera.rs
@@ -40,31 +40,39 @@ pub struct Camera {
qy: Vector3f,
}
+pub struct CameraSettings {
+ pub origin: Vector3f,
+ pub target: Vector3f,
+ pub up: Vector3f,
+ pub fov: Float,
+ pub screensize: Vector2i,
+}
+
impl Camera {
/// Create a new camera look at a target
///
/// The field of view specifies how wide the image should be.
/// Currently must be between [0; 180[.
- pub fn new(origin: Vector3f, target: Vector3f, up: Vector3f, fov: Float, screensize: Vector2i) -> Camera {
- let screensize = Vector2f::from(screensize);
+ pub fn new(set: &CameraSettings) -> Camera {
+ let screensize = Vector2f::from(set.screensize);
// Calculate translation vectors
- let forward = (target - origin).norm();
- let right = up.cross(&origin).norm();
+ let forward = (set.target - set.origin).norm();
+ let right = set.up.cross(&forward).norm();
let newup = forward.cross(&right).norm();
// Calculate screen size from fov
let aspect = screensize.y / screensize.x;
- let width = 2.0 * (fov / 2.0).to_radians().tan();
+ let width = 2.0 * (set.fov / 2.0).to_radians().tan();
let height = aspect * width;
// Calculate screen scaling vectors
let qx = right * (width / (screensize.x - 1.0));
let qy = newup * (height / (screensize.y - 1.0));
- let screen_origin = forward - (right * (width/2.0)) - (newup * (height/2.0));
+ let screen_origin = forward - (right * (width/2.0)) + (newup * (height/2.0));
Camera {
- origin,
+ origin: set.origin,
screen_origin,
qx,
qy,
@@ -80,7 +88,7 @@ impl Camera {
///
/// The direction of the returned way is normalized
pub fn generate_ray(&self, point: &Vector2f) -> (Ray, Float) {
- let mut dir = self.screen_origin + (self.qx * point.x) + (self.qy * point.y);
+ let mut dir = self.screen_origin + (self.qx * point.x) - (self.qy * point.y);
dir.norm_in();
(