aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJulian T <julian@jtle.dk>2021-02-03 14:23:14 +0100
committerJulian T <julian@jtle.dk>2021-02-03 14:23:14 +0100
commitf02aabd4db7232f927622b87ea885491e6651b44 (patch)
tree0a219e2f5b65edd7cead6ff751058a2ec7431ab1
parent2b4adacea3c76b8b33dc5f50d296dd4a61107c16 (diff)
Work on camera implementation using Transformationscamera-using-transform
Not sure if this is really neccesary, will take that later, now it is saved on this commit. The reason it ditched it is that it requires the ability to inverse matrixes, which is really complicated
-rw-r--r--src/camera/camera.rs40
-rw-r--r--src/camera/mod.rs2
-rw-r--r--src/core/matrix4x4.rs31
-rw-r--r--src/core/transform.rs17
-rw-r--r--src/lib.rs1
-rw-r--r--src/render/mod.rs4
6 files changed, 94 insertions, 1 deletions
diff --git a/src/camera/camera.rs b/src/camera/camera.rs
new file mode 100644
index 0000000..102e57b
--- /dev/null
+++ b/src/camera/camera.rs
@@ -0,0 +1,40 @@
+//! Implements ray generation
+//!
+//! Rays are generated by transforming between different spaces summed below.
+//!
+//! - *World space*: Where objects are located in the scene, this will be 3d coordinates
+//! - *Camera space*: Originated at the origin of the camera, with z mapped to the viewing
+//! direction
+//!
+//! Camera space will be represented in different ways:
+//! - *Screen space*: Everything in the screen space is projected onto the film, when z=0 this will
+//! correspond
+//! - *Normalized Device Coordinates(NDC) space*: Is defined as (0, 0, 0) as the upper-left corner of
+//! the film, and (1, 1, 0) to be the bottom right corner.
+//! - *Raster space*: Same as NDC space but, (1, 1, 0) is mapped to (width, height, 0)
+//!
+//! More in depth description is in pbr-book chapter 6
+use crate::Float;
+use crate::core::{Transform, Ray, Vector2i, Bound2f};
+
+/// General implementation for all projective based cameras
+struct ProjectiveCamera {
+ cam2wor: Transform,
+ cam2scr: Transform,
+}
+
+impl ProjectiveCamera {
+ fn new(cam2wor: Transform, cam2scr: Transform, resolution: &Vector2i, screenwindow: &Bound2f) -> Self {
+ let screensize = screenwindow.diagonal();
+ // Calculate the conversion from screen space to raster space
+ let scr2ras = Transform::new_scale(resolution.x as Float, resolution.y as Float, 1.0) *
+ Transform::new_scale(1.0 / (screensize.x), 1.0 / (screensize.y), 1.0) *
+ Transform::new_translate(-screenwindow.min.x, -screenwindow.min.y, 0.0);
+ let ras2scr =
+
+ Self {
+ cam2wor,
+ cam2scr,
+ }
+ }
+}
diff --git a/src/camera/mod.rs b/src/camera/mod.rs
index 4865a36..41f9598 100644
--- a/src/camera/mod.rs
+++ b/src/camera/mod.rs
@@ -7,4 +7,4 @@
pub mod film;
//pub mod filter;
-//pub mod camera;
+pub mod camera;
diff --git a/src/core/matrix4x4.rs b/src/core/matrix4x4.rs
index 521d7ab..f8c685d 100644
--- a/src/core/matrix4x4.rs
+++ b/src/core/matrix4x4.rs
@@ -1,4 +1,5 @@
use crate::{Number, Float};
+use std::ops;
pub struct Matrix4x4<T: Number> {
pub m: [[T; 4]; 4],
@@ -20,6 +21,36 @@ impl<T: Number> Matrix4x4<T> {
],
}
}
+
+ pub fn initial(initial: T) -> Self {
+ Self {
+ m : [ [initial; 4]; 4]
+ }
+ }
+
+ pub fn inverse(&self) -> Self {
+
+ }
+}
+
+impl<T: Number> ops::Mul for &Matrix4x4<T> {
+ type Output = Matrix4x4<T>;
+
+ fn mul(self, op: Self) -> Self::Output {
+ let mut res = Self::Output::initial(Default::default());
+
+ // Not very fast
+ for i in 0..4 {
+ for j in 0..4 {
+ res.m[i][j] = self.m[i][0] * op.m[0][j] +
+ self.m[i][1] * op.m[1][j] +
+ self.m[i][2] * op.m[2][j] +
+ self.m[i][3] * op.m[3][j];
+ }
+ }
+
+ res
+ }
}
impl Matrix4x4f {
diff --git a/src/core/transform.rs b/src/core/transform.rs
index f64710c..337d9b1 100644
--- a/src/core/transform.rs
+++ b/src/core/transform.rs
@@ -17,6 +17,7 @@
use super::matrix4x4::Matrix4x4f;
use crate::Float;
use crate::core::Vector3f;
+use std::ops;
pub struct Transform {
m: Matrix4x4f,
@@ -56,6 +57,22 @@ impl Transform {
Vector3f::new_xyz(x, y, z)
}
+
+ pub fn inverse(&self) -> Self {
+ Transform {
+ m: self.m.inverse(),
+ }
+ }
+}
+
+impl ops::Mul for Transform {
+ type Output = Transform;
+
+ fn mul(self, op: Self) -> Self::Output {
+ Transform {
+ m: &self.m * &op.m
+ }
+ }
}
// Creation of different transformations
diff --git a/src/lib.rs b/src/lib.rs
index df9bc1d..e9ca2f8 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -10,6 +10,7 @@ use std::cmp;
/// This is used in Bound and Vectors
pub trait Number:
Copy +
+ Default +
cmp::PartialOrd +
Sub<Output = Self> +
Add<Output = Self> +
diff --git a/src/render/mod.rs b/src/render/mod.rs
new file mode 100644
index 0000000..ee4e8bc
--- /dev/null
+++ b/src/render/mod.rs
@@ -0,0 +1,4 @@
+
+struct SimpleRender {
+
+}