Skip to content

Commit

Permalink
Added input file reading and support for meshes.
Browse files Browse the repository at this point in the history
- Scenes can now be read from XML files, three examples are included.
- Geometry objects (shapes) can be specified from OBJ files. Parsing
  materials from OBJ is not currently supported.
- PugiXML added as additional, external dependency. Tiny OBJ Loader is
  included directly, as source code.
- Lambertien material fixed.
  • Loading branch information
Philip Abernethy committed Sep 20, 2015
1 parent 4d37a3f commit 12e2cac
Show file tree
Hide file tree
Showing 46 changed files with 1,771 additions and 128 deletions.
18 changes: 16 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,21 @@ 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/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)
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/shapes/sphere.cpp lib/geometry/shapes/sphere.h lib/geometry/shapes/shape.cpp
lib/geometry/shapes/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 lib/light/point_light.cpp lib/light/point_light.h
lib/parser.cpp lib/parser.h lib/geometry/shapes/mesh.cpp lib/geometry/shapes/mesh.h
lib/geometry/shapes/triangle.cpp lib/geometry/shapes/triangle.h lib/tiny_obj_loader.cpp lib/tiny_obj_loader.h)
add_executable(Ray_Tracer ${SOURCE_FILES} main.cpp)
add_executable(Test ${SOURCE_FILES} test.cpp)
target_link_libraries(Ray_Tracer png16)
target_link_libraries(Ray_Tracer png16 pugixml)
target_link_libraries(Test png16 pugixml)
42 changes: 42 additions & 0 deletions example1.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE scene SYSTEM "scene.dtd">

<scene output_file="example1.png">
<background_color r="0.0" g="0.0" b="0.0"/>
<camera type="perspective">
<position x="0.0" y="0.0" z="1.0"/>
<lookat x="0.0" y="0.0" z="-2.5"/>
<up x="0.0" y="1.0" z="0.0"/>
<horizontal_fov angle="45"/>
<resolution horizontal="512" vertical="512"/>
<max_bounces n="8"/>
</camera>
<lights>
<ambient_light>
<color r="1.0" g="1.0" b="1.0"/>
</ambient_light>
</lights>
<surfaces>
<sphere radius="1.0">
<position x="-2.1" y="0.0" z="-3.0"/>
<material type="phong">
<color r="0.17" g="0.18" b="0.50"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
</material>
</sphere>
<sphere radius="1.0">
<position x="0.0" y="0.0" z="-3.0"/>
<material type="phong">
<color r="0.5" g="0.17" b="0.18"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
</material>
</sphere>
<sphere radius="1.0">
<position x="2.1" y="0.0" z="-3.0"/>
<material type="phong">
<color r="0.18" g="0.50" b="0.17"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
</material>
</sphere>
</surfaces>
</scene>
46 changes: 46 additions & 0 deletions example2.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE scene SYSTEM "scene.dtd">

<scene output_file="example2.png">
<background_color r="0.0" g="0.0" b="0.0"/>
<camera type="perspective">
<position x="0.0" y="0.0" z="1.0"/>
<lookat x="0.0" y="0.0" z="-2.5"/>
<up x="0.0" y="1.0" z="0.0"/>
<horizontal_fov angle="45"/>
<resolution horizontal="512" vertical="512"/>
<max_bounces n="8"/>
</camera>
<lights>
<ambient_light>
<color r="1.0" g="1.0" b="1.0"/>
</ambient_light>
<parallel_light>
<color r="1.0" g="1.0" b="1.0"/>
<direction x="0.0" y="-3.0" z="-1.0"/>
</parallel_light>
</lights>
<surfaces>
<sphere radius="1.0">
<position x="-2.1" y="0.0" z="-3.0"/>
<material type="phong">
<color r="0.17" g="0.18" b="0.50"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
</material>
</sphere>
<sphere radius="1.0">
<position x="0.0" y="0.0" z="-3.0"/>
<material type="phong">
<color r="0.5" g="0.17" b="0.18"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
</material>
</sphere>
<sphere radius="1.0">
<position x="2.1" y="0.0" z="-3.0"/>
<material type="phong">
<color r="0.18" g="0.50" b="0.17"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
</material>
</sphere>
</surfaces>
</scene>
52 changes: 52 additions & 0 deletions example3.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?xml version="1.0" standalone="no" ?>
<!DOCTYPE scene SYSTEM "scene.dtd">

<scene output_file="example3.png">
<background_color r="0.0" g="0.0" b="0.0"/>
<camera type="perspective">
<position x="0.0" y="0.0" z="1.0"/>
<lookat x="0.0" y="0.0" z="-2.5"/>
<up x="0.0" y="1.0" z="0.0"/>
<horizontal_fov angle="45"/>
<resolution horizontal="512" vertical="512"/>
<max_bounces n="8"/>
</camera>
<lights>
<ambient_light>
<color r="1.0" g="1.0" b="1.0"/>
</ambient_light>
<parallel_light>
<color r="1.0" g="1.0" b="1.0"/>
<direction x="0.0" y="-3.0" z="-1.0"/>
</parallel_light>
</lights>
<surfaces>
<sphere radius="1.0">
<position x="-2.1" y="0.0" z="-3.0"/>
<material type="phong">
<color r="0.17" g="0.18" b="0.50"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
</material>
</sphere>
<sphere radius="1.0">
<position x="0.0" y="0.0" z="-3.0"/>
<material type="lambertian">
<color r="0.5" g="0.17" b="0.18"/>
<lambertian ka="0.3" kd="0.9"/> <!--ks="1.0" exponent="200"/>-->
</material>
</sphere>
<sphere radius="1.0">
<position x="2.1" y="0.0" z="-3.0"/>
<material type="phong">
<color r="0.18" g="0.50" b="0.17"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="200"/>
</material>
</sphere>
<mesh name="open_room.obj">
<material type="phong">
<color r="0.5" g="0.5" b="0.5"/>
<phong ka="0.3" kd="0.9" ks="1.0" exponent="20"/>
</material>
</mesh>
</surfaces>
</scene>
3 changes: 2 additions & 1 deletion lib/camera/camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
#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) {
const std::array<unsigned long, 2> &resolution, const unsigned long max_bounces,
const unsigned long &samples) : resolution(resolution), max_bounces(max_bounces) {
assert(resolution[0] > 0 && resolution[1] > 0);
assert(samples > 0);
direction dir = normalise(look_at-position);
Expand Down
9 changes: 6 additions & 3 deletions lib/camera/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,14 @@
#include "../geometry/transform.h"
#include "../geometry/ray.h"
#include "../geometry/color.h"
#include "../math/helper.h"

class camera {
protected:
transform transforms;
std::array<unsigned long, 2> resolution;
std::vector<std::vector<std::vector<color>>> data;
const unsigned long max_bounces = 0;

camera() { };

Expand All @@ -23,9 +25,9 @@ class camera {
* 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 &offset the offset of the camera
*
* @param &look_at the position the camera is centred on
* @param &look_at the offset the camera is centred on
*
* @param &up the up direction of the image
*
Expand All @@ -34,7 +36,8 @@ class camera {
* @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);
const std::array<unsigned long, 2> &resolution, const unsigned long max_bounces,
const unsigned long &samples);

public:
/**
Expand Down
8 changes: 4 additions & 4 deletions lib/camera/perspective_camera.cpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
#include "perspective_camera.h"

perspective_camera::perspective_camera() : perspective_camera(point(0, 0, 0), point(0, 0, -1), direction(0, 1, 0),
{1024, 768}, 1, 45) {
{1024, 768}, 10, 1, 45) {
}

perspective_camera::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) :
camera(position, look_at, up, resolution, samples), fov(fov) {
const std::array<unsigned long, 2> &resolution, const unsigned long max_bounces,
const unsigned long &samples, const float &fov) :
camera(position, look_at, up, resolution, max_bounces, 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)+
Expand Down
7 changes: 4 additions & 3 deletions lib/camera/perspective_camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ class perspective_camera : public camera {
*
* Creates a perspective camera with the given parameters
*
* @param &position the position of the camera in world coordinates
* @param &offset the offset of the camera in world coordinates
*
* @param &look_at the position the camera is centered on in world coordinates
* @param &look_at the offset the camera is centered on in world coordinates
*
* @param &up the up direction of the image
*
Expand All @@ -44,7 +44,8 @@ class perspective_camera : public camera {
* @param &fov the half horizontal field-of-view angle of the camera
*/
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);
const std::array<unsigned long, 2> &resolution, const unsigned long max_bounces,
const unsigned long &samples, const float &fov);

virtual std::vector<ray> *get_rays(const unsigned long &x, const unsigned long &y);
};
Expand Down
4 changes: 2 additions & 2 deletions lib/geometry/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ color::color(const color &in) {
}

float &color::operator[](const unsigned long i) {
assert(0 <= i <= 2);
assert(0 <= i && i <= 2);
return this->v[i];
}

const float &color::operator[](const unsigned long i) const {
assert(0 <= i <= 2);
assert(0 <= i && i <= 2);
return this->v[i];
}

Expand Down
15 changes: 13 additions & 2 deletions lib/geometry/material/lambertian_material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,17 @@ std::ostream &operator<<(std::ostream &out, const lambertian_material &a) {
}

color lambertian_material::shade(const color &lcol, const direction &l, const normal &n, const direction &v, const vec2 &pos,
const bool internal) {
const bool internal) const {
if (l != direction()) {
// Directional light
if (!internal) {
float phi = std::max<float>(0.0, dot(n, l));
if (phi > 0) {
return scale(this->col, lcol*(this->diffuse*phi));
}
}
} else
// Ambient light
return scale(this->col, lcol*this->ambient);
return color();
}
}
3 changes: 2 additions & 1 deletion lib/geometry/material/lambertian_material.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ class lambertian_material : public solid_material {
public:
lambertian_material(color col, float ambient, float diffuse);
friend std::ostream &operator<<(std::ostream &out, const lambertian_material &a);
virtual color shade(const color &lcol, const direction &l, const normal &n, const direction &v, const vec2 &pos, const bool internal);
virtual color shade(const color &lcol, const direction &l, const normal &n, const direction &v, const vec2 &pos,
const bool internal) const;
};

#endif //RAY_TRACER_LAMBERTIAN_MATERIAL_H
5 changes: 3 additions & 2 deletions lib/geometry/material/material.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
*/
class material {
protected:
material() {};
material() { };
public:
/**
* @brief Shading
Expand All @@ -32,7 +32,8 @@ class material {
* to.
* @return the base color multiplied by a factor according to the viewing situation
*/
virtual color shade(const color &lcol, const direction &l, const normal &n, const direction &v, const vec2 &pos, const bool internal) = 0;
virtual color shade(const color &lcol, const direction &l, const normal &n, const direction &v, const vec2 &pos,
const bool internal) const = 0;
};

#endif //RAY_TRACER_MATERIAL_H
10 changes: 5 additions & 5 deletions lib/geometry/material/phong_material.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

#include "phong_material.h"

phong_material::phong_material(color col, float ambient, float diffuse, float specular,
float exponent) : solid_material(col), ambient(ambient), diffuse(diffuse), specular(specular),
exponent(exponent) { }
phong_material::phong_material(const color &col, const float &ambient, const float &diffuse, const float &specular,
const float &exponent) : solid_material(col), ambient(ambient), diffuse(diffuse),
specular(specular), exponent(exponent) { }

std::ostream &operator<<(std::ostream &out, const phong_material &a) {
out << "Phong material: Color: " << a.col << " Ambient intensity: " << a.ambient << ", Diffuse intensity: " <<
Expand All @@ -16,15 +16,15 @@ std::ostream &operator<<(std::ostream &out, const phong_material &a) {
}

color phong_material::shade(const color &lcol, const direction &l, const normal &n, const direction &v, const vec2 &pos,
const bool internal) {
const bool internal) const {
if (l != direction()) {
// Directional light
if (!internal) {
float phi = std::max<float>(0.0, dot(n, l));
if (phi > 0) {
direction r = n*(2*phi)-l;
return scale(this->col, lcol*(this->diffuse*phi))+
lcol*(std::pow(std::max<float>(0.0, dot(v, r)), this->exponent)*this->specular);
lcol*(std::pow(std::max<float>(0.0, dot(v, r)), this->exponent)*this->specular);
}
}
} else
Expand Down
6 changes: 4 additions & 2 deletions lib/geometry/material/phong_material.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@ struct phong_material : public solid_material {
private:
float ambient, diffuse, specular, exponent;
public:
phong_material(color col, float ambient, float diffuse, float specular, float exponent);
phong_material(const color &col, const float &ambient, const float &diffuse, const float &specular,
const float &exponent);

friend std::ostream &operator<<(std::ostream &out, const phong_material &a);

virtual color shade(const color &lcol, const direction &l, const normal &n, const direction &v, const vec2 &pos, const bool internal);
virtual color shade(const color &lcol, const direction &l, const normal &n, const direction &v, const vec2 &pos,
const bool internal) const ;
};

#endif //RAY_TRACER_PHONG_H
6 changes: 5 additions & 1 deletion lib/geometry/point.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,8 @@ point &operator+=(point &lhs, const direction &rhs) {

direction operator-(const point &lhs, const point &rhs) {
return direction(dynamic_cast<const vec4 &>(lhs)-dynamic_cast<const vec4 &>(rhs));
}
}

point operator-(const point &lhs, const direction &rhs) {
return point(dynamic_cast<const vec4 &>(lhs)-dynamic_cast<const vec4 &>(rhs));
}
10 changes: 10 additions & 0 deletions lib/geometry/point.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,14 @@ point &operator+=(point &lhs, const direction &rhs);
*/
direction operator-(const point &lhs, const point &rhs);

/**
* @brief Addition operator
* Subtracts a direction vector from a point vector. The result is a point vector again. This is equivalent to
* lhs+(-rhs).
* @param &lhs the point vector
* @param &rhs the direction vector
* @return the resulting point vector
*/
point operator-(const point &lhs, const direction &rhs);

#endif //RAY_TRACER_POINT_H
Loading

0 comments on commit 12e2cac

Please sign in to comment.