Skip to content

Commit

Permalink
Refactoring done
Browse files Browse the repository at this point in the history
  • Loading branch information
Philip Abernethy committed Sep 12, 2015
1 parent 69f339a commit 4d37a3f
Show file tree
Hide file tree
Showing 45 changed files with 477 additions and 507 deletions.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ project(Ray_Tracer)

set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11")

set(SOURCE_FILES lib/whitted_rt.cpp lib/whitted_rt.h lib/math/vec4.cpp lib/math/vec4.h lib/math/vec2.cpp lib/math/vec2.h lib/math/mat4.cpp lib/math/mat4.h lib/camera/perspective_camera.cpp lib/camera/perspective_camera.h lib/geometry/ray.h lib/geometry/sphere.cpp lib/geometry/sphere.h lib/geometry/shape.cpp lib/geometry/shape.h lib/math/helper.cpp lib/math/helper.h lib/geometry/geometry_exception.cpp lib/geometry/geometry_exception.h lib/light/light.cpp lib/light/light.h lib/light/parallel_light.cpp lib/light/parallel_light.h lib/geometry/material/material.h lib/geometry/material/phong_material.cpp lib/geometry/material/phong_material.h lib/geometry/material/solid_material.h lib/geometry/point.cpp lib/geometry/point.h lib/geometry/direction.cpp lib/geometry/direction.h lib/geometry/normal.cpp lib/geometry/normal.h lib/geometry/transform.cpp lib/geometry/transform.h lib/geometry/intersection.h lib/geometry/color.cpp lib/geometry/color.h lib/geometry/material/lambertian_material.cpp lib/geometry/material/lambertian_material.h lib/light/ambient_light.cpp lib/light/ambient_light.h lib/camera/camera.h)
set(SOURCE_FILES lib/whitted_rt.cpp lib/whitted_rt.h lib/math/vec4.cpp lib/math/vec4.h lib/math/vec2.cpp lib/math/vec2.h lib/math/mat4.cpp lib/math/mat4.h lib/camera/perspective_camera.cpp lib/camera/perspective_camera.h lib/geometry/ray.h lib/geometry/sphere.cpp lib/geometry/sphere.h lib/geometry/shape.cpp lib/geometry/shape.h lib/math/helper.cpp lib/math/helper.h lib/light/light.cpp lib/light/light.h lib/light/parallel_light.cpp lib/light/parallel_light.h lib/geometry/material/material.h lib/geometry/material/phong_material.cpp lib/geometry/material/phong_material.h lib/geometry/material/solid_material.h lib/geometry/point.cpp lib/geometry/point.h lib/geometry/direction.cpp lib/geometry/direction.h lib/geometry/normal.cpp lib/geometry/normal.h lib/geometry/transform.cpp lib/geometry/transform.h lib/geometry/intersection.h lib/geometry/color.cpp lib/geometry/color.h lib/geometry/material/lambertian_material.cpp lib/geometry/material/lambertian_material.h lib/light/ambient_light.cpp lib/light/ambient_light.h lib/camera/camera.h lib/camera/camera.cpp lib/geometry/ray.cpp lib/geometry/material/solid_material.cpp)
add_executable(Ray_Tracer ${SOURCE_FILES} main.cpp)
add_executable(Test ${SOURCE_FILES} test.cpp)
target_link_libraries(Ray_Tracer png16)
34 changes: 21 additions & 13 deletions classes.dot
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ digraph Classes {
- cam : camera\l
- lights : std::vector\<light *\>\l
- scene : std::vector\<shape *\>\l
- max_bounces : int\l|
- max_bounces : unsigned long\l|
+ whitted_rt(bg_color : color &,
cam : camera &,
lights : std::vector\<light *\> &,
Expand All @@ -40,10 +40,16 @@ digraph Classes {
camera [
label = "{camera|
# transforms : transform\l
# resolution : unsigned long[2]\l
# data : std::vector\<color\>\l|
- camera()\l
+ get_ray(x : int, y : int) : ray &\l
# resolution : std::array\<unsigned long, 2\>\l
# data : std::vector\<std::vector\<std::vector\<color\>\>\>\l|
# camera(position : point &,
look_at : point &,
up : direction &,
resolution : std::array<unsigned long, 2> &,
samples : unsigned long &)\l
+ camera()\l
+ get_rays(x : int, y : int) : ray &\l
+ set_data(x : unsigned long, y : unsigned long, data : std::vector\<color\>) : void\l
+ get_pixel(x : int, y : int) : color\l
}"
]
Expand All @@ -54,22 +60,24 @@ digraph Classes {
+ perspective_camera(position : point &,
look_at : point &,
up : direction &,
fov : float,
resolution : int *)\l
fov : float &,
resolution : std::array<unsigned long, 2> &,
samples : unsigned long &)\l
}"
]
ray [
label = "{ray|
+ o : point\l
+ d : direction\l
+ d : direction\l|
+ ray(o : point &, d : direction &)\l
}"
]
shape_ [
label = "{
shape|
- transforms : transform\l
- matrl : material *\l|
+ shape(matrl : material *)\l
# transforms : transform\l
# matrl : material *\l|
# shape(matrl : material *)\l
+ intersect_full(r : ray) : intersection\l
+ intersect_shadow(r : ray) : bool\l
+ shade(lcol : color, l : direction, n : normal, v : direction, pos : vec2, internal : bool) : color\l
Expand Down Expand Up @@ -102,7 +110,7 @@ digraph Classes {
- diffuse : float\l|
+ lambertian_material(col : color,
ambient : float,
diffuse : float)
diffuse : float)\l
}"
]
phong_material [
Expand All @@ -116,7 +124,7 @@ digraph Classes {
ambient : float,
diffuse : float,
specular : float,
exponent : float)
exponent : float)\l
}"
]
light [
Expand Down
44 changes: 44 additions & 0 deletions lib/camera/camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Created by chais on 09.09.15.
//

#include "camera.h"

camera::camera(const point &position, const point &look_at, const direction &up,
const std::array<unsigned long, 2> &resolution, const unsigned long &samples) : resolution(resolution) {
assert(resolution[0] > 0 && resolution[1] > 0);
assert(samples > 0);
direction dir = normalise(look_at-position);
direction left = cross(normalise(up), dir);
direction new_up = cross(dir, left);
std::array<std::array<float, 4>, 4> d = {{
{left[0], new_up[0], dir[0], position[0]},
{left[1], new_up[1], dir[1], position[1]},
{left[2], new_up[2], dir[2], position[2]},
{0, 0, 0, 1}
}};
mat4 *m = new mat4(d);
transforms = transform(m);
data.resize(resolution[0]);
for (auto &i : data) {
i.resize(resolution[1]);
for (auto &j : i)
j.resize(samples);
}
}

void camera::set_data(const unsigned long &x, const unsigned long &y, const std::vector<color> data) {
this->data[x][y] = data;
}

color *camera::get_pixel(const unsigned long &x, const unsigned long &y) const {
color *out = new color();
for (auto &i : this->data[x][y])
*out += i;
*out = *out*(1/this->data[x][y].capacity());
return out;
}

const std::array<unsigned long, 2> &camera::get_resolution() const {
return resolution;
}
67 changes: 61 additions & 6 deletions lib/camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,69 @@
class camera {
protected:
transform transforms;
unsigned long resolution[2];
std::vector<color> data;
std::array<unsigned long, 2> resolution;
std::vector<std::vector<std::vector<color>>> data;

camera() { };

/**
* @brief Explicit constructor
*
* Initialises the values common to all camera implementations. Transforms and resolution are set as given. Memory
* for the data variable is allocated according to the resolution.
*
* @param &position the position of the camera
*
* @param &look_at the position the camera is centred on
*
* @param &up the up direction of the image
*
* @param &resolution the resolution of the image
*
* @param &samples the number of samples per pixel
*/
camera(const point &position, const point &look_at, const direction &up,
const std::array<unsigned long, 2> &resolution, const unsigned long &samples);

virtual camera() { };
public:
virtual ray &get_ray(const int &x, const int &y) const = 0;
/**
* @brief Grants access to the initial rays of the camera
*
* Calculates the rays for the given image coordinates and returns them.
*
* @param &x the x coordinate
*
* @param &y the y coordinate
*
* @return a std::vector containing all rays contributing to the pixel with the given coordinates
*/
virtual std::vector<ray> *get_rays(const unsigned long &x, const unsigned long &y) = 0;

/**
* @brief Sets the data for the designated pixel
*
* @param &x the X coordinate
*
* @param &y the Y coordinate
*
* @param &data the std::vector of colors contributing the the pixel
*/
void set_data(const unsigned long &x, const unsigned long &y, const std::vector<color> data);

/**
* @brief Returns the color of a pixel
*
* Calculates the final color of a pixel from all colors contributing to it.
*
* @param &x the x coordinate
*
* @param &y the y coordinate
*
* @return the final color of the pixel with the given coordinates
*/
color *get_pixel(const unsigned long &x, const unsigned long &y) const;

virtual color &get_pixel(const int &x, const int &y) const = 0;
const std::array<unsigned long, 2> &get_resolution() const;
};

#endif //RAY_TRACER_CAMERA_H
#endif //RAY_TRACER_CAMERA_H
48 changes: 15 additions & 33 deletions lib/camera/perspective_camera.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,23 @@
#include "perspective_camera.h"
#include "../math/mat4.h"

perspective_camera::perspective_camera() {
std::array<std::array<float, 4>, 4> d = {{
{-1, 0, 0, 0},
{0, 1, 0, 0},
{0, 0, -1, 0},
{0, 0, 0, 1}
}};
mat4 m = mat4(d);
transforms = transform(mat4(m), mat4(m));
resolution = {1024, 768};
data.reserve(1024*768);
fov = 75;
perspective_camera::perspective_camera() : perspective_camera(point(0, 0, 0), point(0, 0, -1), direction(0, 1, 0),
{1024, 768}, 1, 45) {
}

perspective_camera::perspective_camera(const point &position, const point &look_at, const direction &up,
const float fov, const unsigned long resolution[2]) : resolution(resolution),
fov(fov) {
direction dir = normalise(look_at-position);
direction left = normalise(cross(normalise(up), dir));
direction new_up = cross(dir, left);
std::array<std::array<float, 4>, 4> d = {{
{left[0], new_up[0], dir[0], position[0]},
{left[1], new_up[1], dir[1], position[1]},
{left[2], new_up[2], dir[2], position[2]},
{0, 0, 0, 1}
}};
mat4 m = mat4(d);
transforms = transform(m, invert(m));
data.reserve(resolution[0]*resolution[1]);
const std::array<unsigned long, 2> &resolution, const unsigned long &samples,
const float &fov) :
camera(position, look_at, up, resolution, samples), fov(fov) {
assert(0 < fov && fov < 90);
stepwidth = tan(helper::to_radians(this->fov))/(resolution[0]/2);
start = direction(0, 0, 1)+direction(1, 0, 0)*(((resolution[0]-1)/2.0)*stepwidth)+
direction(0, 1, 0)*(((resolution[1]-1)/2.0)*stepwidth);
}

std::ostream &operator<<(std::ostream &out, const perspective_camera &cam) {
std::vector<ray> *perspective_camera::get_rays(const unsigned long &x, const unsigned long &y) {
// TODO extend to support multisampling
std::vector<ray> *out = new std::vector<ray>();
out->push_back(this->transforms(
ray(point(), this->start+direction(-1, 0, 0)*(this->stepwidth*x)+direction(0, -1, 0)*(this->stepwidth*y))));
return out;
}

double perspective_camera::getFov() const {
return 0;
}
}
55 changes: 29 additions & 26 deletions lib/camera/perspective_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,51 @@
// Created by chais on 10.07.15.
//

#ifndef RAY_TRACER_CAMERA_H
#define RAY_TRACER_CAMERA_H
#ifndef RAY_TRACER_PERSPECTIVE_CAMERA_H
#define RAY_TRACER_PERSPECTIVE_CAMERA_H

#include "../geometry/ray.h"
#include "camera.h"
#include <ostream>
#include <vector>
#include <cmath>

class perspective_camera : public camera {
private:
float fov;
double stepwidth;
direction start;
public:
/**
* @brief Default constructor
*
* Creates a perspective camera at world origin looking down the negative Z-axis.
* Creates a perspective camera with the following specification:
* \p position = [0, 0, 0]
* \p look_at = [0, 0, -1]
* \p up = [0, 1, 0]
* \p resolution = [1024, 768]
* \p samples = 1
* \p fov = 45
*/
perspective_camera();

perspective_camera(const point &position, const point &look_at, const direction &up, const float fov,
const unsigned long resolution[2]);

/**
* @brief Puts m description of the camera on the stream
* Puts the names of all variables followed by their values on the stream, one variable per line
*/
friend std::ostream &operator<<(std::ostream &out, const perspective_camera &cam);

/**
* @brief Grants easy access to the camera's rays
* Allows access to the rays cast by the camera. Index 0 is the upper left corner pixel, 1 the next one to the right.
* @param i the index to access
* @brief Explicit constructor
*
* Creates a perspective camera with the given parameters
*
* @param &position the position of the camera in world coordinates
*
* @param &look_at the position the camera is centered on in world coordinates
*
* @param &up the up direction of the image
*
* @param &resolution the std::array with the x and y resolution of the image
*
* @param &samples the number of samples per pixel
*
* @param &fov the half horizontal field-of-view angle of the camera
*/
ray &operator[](const int i);
perspective_camera(const point &position, const point &look_at, const direction &up,
const std::array<unsigned long, 2> &resolution, const unsigned long &samples, const float &fov);

/**
* @brief Returns the camera's Field Of View angle
* The FOV is the angle between the center of the picture and the right border.
* @return the field of view
*/
double getFov() const;
virtual std::vector<ray> *get_rays(const unsigned long &x, const unsigned long &y);
};

#endif //RAY_TRACER_CAMERA_H
13 changes: 10 additions & 3 deletions lib/geometry/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ color operator+(const color &lhs, const color &rhs) {
return color(lhs[0]+rhs[0], lhs[1]+rhs[1], lhs[2]+rhs[2]);
}

color &operator+=(color &lhs, const color &rhs) {
lhs[0]+=rhs[0];
lhs[1]+=rhs[1];
lhs[2]+=rhs[2];
return lhs;
}

color operator*(const color &lhs, const float &rhs) {
return color(lhs[0]*rhs, lhs[1]*rhs, lhs[2]*rhs);
}
Expand All @@ -53,9 +60,9 @@ color scale(const color &col, const color &sf) {

std::array<int, 3> rgb(const color &col) {
std::array<int, 3> out = {{
col[0] > 255 ? 255 : std::round(col[0]),
col[1] > 255 ? 255 : std::round(col[1]),
col[2] > 255 ? 255 : std::round(col[2])
col[0] > 1 ? 255 : int(std::round(col[0]*255)),
col[1] > 1 ? 255 : int(std::round(col[1]*255)),
col[2] > 1 ? 255 : int(std::round(col[2]*255))
}};
return out;
}
3 changes: 3 additions & 0 deletions lib/geometry/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <cmath>
#include <array>
#include <cassert>
#include <ostream>

/**
* @name color
Expand All @@ -33,6 +34,8 @@ class color {

color operator+(const color &lhs, const color &rhs);

color &operator+=(color &lhs, const color &rhs);

color operator*(const color &lhs, const float &rhs);

color operator*(const float &lhs, const color &rhs);
Expand Down
Loading

0 comments on commit 4d37a3f

Please sign in to comment.