Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assignment 1 #1

Open
wants to merge 23 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@

include/types.h

/CMakeFiles
cmake_install.cmake
CMakeCache.txt
Makefile

# Ignore build directories
[Dd]ebug
[Rr]elease
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
**Deadline**: 01.10.2020

Please put your name here:
**Name**: .......
**Name**: Albrit Bendo and Eliza Checiu
## Foreword
### Implementation of a Minimal Ray Tracing System

Expand Down
45 changes: 0 additions & 45 deletions appveyor.yml

This file was deleted.

49 changes: 49 additions & 0 deletions src/CameraOrthographic.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,52 @@

// --- IMPLENET class CCameraOrthographic ---
// --- PUT YOUR CODE HERE ---
class CameraOrthographic : ICamera
{
public:

CameraOrthographic(Size& resolution, const Vec3f& pos, const Vec3f& dir, const Vec3f& up, float angle)
: ICamera(resolution)
, m_pos(pos)
, m_dir(dir)
, m_up(up)
, m_angle(angle)
{
m_xAxis = normalize(m_zAxis.cross(m_up));
m_yAxis = normalize(m_zAxis.cross(m_xAxis));
m_focus = 1 / tan((m_angle * Pif) / 180);
}

virtual ~CameraOrthographic(void) = default;

virtual bool InitRay(Ray &ray, int x, int y) override
{
float width = getResolution().width;
float height = getResolution().height;

float sscx = (2 * static_cast<float>(x) / width) - 1;
float sscy = (2 * static_cast<float>(y) / height) - 1;

ray.org = m_pos;
ray.dir = normalize(getAspectRatio() * sscx * m_xAxis + sscy * m_yAxis + m_focus * m_zAxis);
ray.t = std::numeric_limits<float>::max();

return true;
}

Vec3f getOrigin() {
return cv::Vec3f();
}

private:
Vec3f m_pos;
Vec3f m_dir;
Vec3f m_up;
float m_angle;

Vec3f m_xAxis;
Vec3f m_yAxis;
Vec3f m_zAxis;
float m_focus;
float m_aspect;
};
27 changes: 25 additions & 2 deletions src/CameraPerspective.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,35 @@ class CCameraPerspective : public ICamera
, m_pos(pos)
, m_dir(dir)
, m_up(up)
, m_angle(angle)
{
// --- PUT YOUR CODE HERE ---
m_aspect = (float) resolution.width / (float) resolution.height;
m_focus = 1 / tan((m_angle * Pif) / 180);
m_yAxis = normalize(m_up * (-1));
m_xAxis = normalize(dir.cross(m_up));
m_zAxis = normalize(m_dir);
}

virtual ~CCameraPerspective(void) = default;

virtual void InitRay(Ray& ray, int x, int y) override
virtual bool InitRay(Ray& ray, int x, int y) override
{
// --- PUT YOUR CODE HERE ---
float width = getResolution().width;
float height = getResolution().height;

float ndcx = (x + 0.5) / width; // Device coordinates
float ndcy = (y + 0.5) / height;

float sscx = (2 * ndcx - 1) * m_aspect; // Screen space coordinates
float sscy = 2 * ndcy - 1;

ray.org = m_pos;
ray.dir = normalize(sscx * m_xAxis + sscy * m_yAxis + m_focus * m_dir);
ray.t = std::numeric_limits<float>::max();

return true;
}


Expand All @@ -43,9 +64,11 @@ class CCameraPerspective : public ICamera
Vec3f m_pos; ///< Camera origin (center of projection)
Vec3f m_dir; ///< Camera viewing direction
Vec3f m_up; ///< Camera up-vector
float m_focus; ///< The focal length
float m_angle; ///< The focal length

// preprocessed values
float m_focus;
float m_aspect;
Vec3f m_xAxis; ///< Camera x-axis in WCS
Vec3f m_yAxis; ///< Camera y-axis in WCS
Vec3f m_zAxis; ///< Camera z-axis in WCS
Expand Down
5 changes: 2 additions & 3 deletions src/ICamera.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class ICamera
* @brief Constructor
* @param resolution The resolution of the camera in pixels
*/
ICamera(Size resolution)
ICamera(Size &resolution)
: m_resolution(resolution)
, m_aspectRatio(static_cast<float>(resolution.width) / resolution.height)
{}
Expand All @@ -35,7 +35,7 @@ class ICamera
* @param[in] x The x-coordinate of the pixel lying on the camera screen
* @param[in] y The y-coordinate of the pixel lying on the camera screen
*/
virtual void InitRay(Ray& ray, int x, int y) = 0;
virtual bool InitRay(Ray& ray, int x, int y) = 0;

/**
* @brief Retuns the camera resolution in pixels
Expand All @@ -55,4 +55,3 @@ class ICamera
};

using ptr_camera_t = std::shared_ptr<ICamera>;

11 changes: 8 additions & 3 deletions src/IPrim.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ struct Ray;
*/
class IPrim
{
private:
Vec3f m_color;
public:
/**
* @brief Constructor
*/
IPrim(void) = default;
IPrim() = default;

IPrim(const IPrim&) = delete;
virtual ~IPrim(void) = default;

virtual ~IPrim() = default;

const IPrim& operator=(const IPrim&) = delete;

/**
Expand All @@ -30,5 +35,5 @@ class IPrim
* @retval true If and only if a valid intersection has been found in the interval (epsilon; Ray::t)
* @retval false Otherwise
*/
virtual bool intersect(Ray& ray) const = 0;
virtual bool intersect(Ray& ray) const = 0;
};
26 changes: 25 additions & 1 deletion src/PrimDisc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,28 @@
#include "ray.h"

// --- IMPLENET class CPrimDisc ---
// --- PUT YOUR CODE HERE ---
// --- PUT YOUR CODE HERE ---

class CPrimDisc : public CPrimPlane {

private:
float m_radius;
public:
CPrimDisc(const Vec3f& origin, const Vec3f& normal, const float radius)
: CPrimPlane(origin, normal), m_radius(radius)
{}

virtual ~CPrimDisc() = default;

virtual bool CPrimDisc::intersect(Ray &ray) const {
float t = (getOrigin() - ray.org).dot(getNormal()) / ray.dir.dot(getNormal());
if (t < Epsilon || t > ray.t) return false;
if (norm(ray.org + (ray.dir * t ) - getOrigin()) > m_radius) {
return false;
}
ray.t = t;
return true;

}

}
5 changes: 4 additions & 1 deletion src/PrimPlane.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ class CPrimPlane : public IPrim
virtual bool intersect(Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return false;
float t = (m_origin - ray.org).dot(m_normal) / ray.dir.dot(m_normal);
if (t < Epsilon || t > ray.t) return false;
ray.t = t;
return true;
}


Expand Down
18 changes: 16 additions & 2 deletions src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class CPrimSphere : public IPrim
* @param origin Position of the center of the sphere
* @param radius Radius of the sphere
*/
CPrimSphere(Vec3f origin, float radius)
CPrimSphere(const Vec3f &origin, float radius) // center = origin
: IPrim()
, m_origin(origin)
, m_radius(radius)
Expand All @@ -29,7 +29,21 @@ class CPrimSphere : public IPrim
virtual bool intersect(Ray &ray) const override
{
// --- PUT YOUR CODE HERE ---
return false;
float a = ray.dir.dot(ray.dir);
float b = ray.dir.dot(ray.org - m_origin);
float c = (ray.org - m_origin).dot(ray.org - m_origin) - (m_radius * m_radius);
float discriminant = b * b - 4 * a * c;

if(discriminant < 0) return false;

float root_1 = ((-b) + sqrt(discriminant)) / 2 * a;
float root_2 = ((-b) - sqrt(discriminant)) / 2 * a;

if((root_1 < Epsilon && root_2 < Epsilon) || (root_1 > ray.t && root_2 > ray.t)) return false;
if(root_1 < root_2) ray.t = root_1;
else ray.t = root_2;

return true;
}


Expand Down
22 changes: 20 additions & 2 deletions src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class CPrimTriangle : public IPrim
* @param b Position of the second vertex
* @param c Position of the third vertex
*/
CPrimTriangle(const Vec3f& a, const Vec3f& b, const Vec3f& c)
CPrimTriangle(const Vec3f &a, const Vec3f &b, const Vec3f &c)
: IPrim()
, m_a(a)
, m_b(b)
Expand All @@ -31,7 +31,25 @@ class CPrimTriangle : public IPrim
virtual bool intersect(Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return false;
Vec3f nab = (m_b - ray.org).cross(m_a - ray.org);
Vec3f nbc = (m_c - ray.org).cross(m_b - ray.org);
Vec3f nca = (m_a - ray.org).cross(m_c - ray.org);

float sum = nab.dot(ray.dir) + nbc.dot(ray.dir) + nca.dot(ray.dir);
float lamb1 = nab.dot(ray.dir) / sum;
float lamb2 = nbc.dot(ray.dir) / sum;
float lamb3 = nca.dot(ray.dir) / sum;

if(lamb1 < 0 || lamb2 < 0 || lamb3 < 0) return false;

Vec3f p = m_a * lamb1 + m_b * lamb2 + m_c * lamb3;
float t = p[0] / ray.dir[0];

if(t < Epsilon || t > ray.t) return false;

ray.t = t;

return true;
}


Expand Down
21 changes: 11 additions & 10 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ Mat RenderFrame(ICamera& camera)
CPrimSphere s2(Vec3f(1, -1, 1), 2.2f);
CPrimSphere s3(Vec3f(3, 0.8f, -2), 2);
CPrimPlane p1(Vec3f(0, -1, 0), Vec3f(0, 1, 0));

// Add disc primitive here

CPrimTriangle t1(Vec3f(-2, 3.7f, 0), Vec3f(1, 2, 1), Vec3f(3, 2.8f, -2));
CPrimTriangle t2(Vec3f(3, 2, 3), Vec3f(3, 2, -3), Vec3f(-3, 2, -3));

Expand All @@ -25,19 +26,19 @@ Mat RenderFrame(ICamera& camera)
for (int x = 0; x < img.cols; x++) {

// Initialize your ray here
camera.InitRay(ray, x, y);

// --- PUT YOUR CODE HERE ---
Vec3f color = RGB(0, 0, 0); // background color

Vec3f col = RGB(0, 0, 0); // background color

/*
* Find closest intersection with scene
* objetcs and calculate color
*/

// --- PUT YOUR CODE HERE ---
if (s1.intersect(ray)) color = RGB(1, 0, 0);
if (s2.intersect(ray)) color = RGB(0, 1, 0);
if (s3.intersect(ray)) color = RGB(0, 0, 1);
if (p1.intersect(ray)) color = RGB(1, 1, 0);
if (t1.intersect(ray)) color = RGB(0, 1, 1);
if (t2.intersect(ray)) color = RGB(1, 1, 1);

img.at<Vec3f>(y, x) = col; // store pixel color
img.at<Vec3f>(y, x) = color; // store pixel color
}

img.convertTo(img, CV_8UC3, 255);
Expand Down