diff options
Diffstat (limited to 'src/camera')
-rw-r--r-- | src/camera/camera.rs | 24 | ||||
-rw-r--r-- | src/camera/mod.rs | 2 |
2 files changed, 17 insertions, 9 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(); ( diff --git a/src/camera/mod.rs b/src/camera/mod.rs index 1391cea..7c36307 100644 --- a/src/camera/mod.rs +++ b/src/camera/mod.rs @@ -9,5 +9,5 @@ pub mod film; //pub mod filter; pub mod camera; -pub use camera::Camera; +pub use camera::{Camera, CameraSettings}; pub use film::Film; |