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

submitting with renders #15

Open
wants to merge 3 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
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**: 30.09.20201

Please put your name here:
**Name**: .......
**Name**: Amartya Vats
## Foreword
### Implementation of a Minimal Ray Tracing System

Expand Down
Binary file added renders/environmental4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/perspective1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/perspective2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added renders/perspective3.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
46 changes: 46 additions & 0 deletions src/CameraEnvironmental.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,49 @@

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

public:

CCameraEnvironmental(Size resolution, const Vec3f& pos, const Vec3f& dir, const Vec3f& up) :
ICamera(resolution),
m_pos(pos),
m_dir(dir),
m_up(up)

{

}

virtual ~CCameraEnvironmental(void) = default;

virtual void InitRay(Ray& ray, int x, int y) {

Size res = getResolution();


float theta = Pif * (static_cast<float>(y) / res.height);
float phi = Pif * 2 * (static_cast<float>(x) / res.width);

m_zAxis = m_dir;
m_xAxis = normalize(m_zAxis.cross(m_up));
m_yAxis = normalize(m_zAxis.cross(m_xAxis));

ray.org = m_pos;
ray.dir = normalize(cosf(phi) * sinf(theta) * m_xAxis + sinf(theta) * sinf(phi) * m_zAxis - cosf(theta) * m_yAxis);
ray.t = std::numeric_limits<float>::infinity();


}

private:

Vec3f m_pos;
Vec3f m_dir;
Vec3f m_up;

Vec3f m_xAxis;
Vec3f m_yAxis;
Vec3f m_zAxis;

};
16 changes: 16 additions & 0 deletions src/CameraPerspective.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,22 @@ class CCameraPerspective : public ICamera
virtual void InitRay(Ray& ray, int x, int y) override
{
// --- PUT YOUR CODE HERE ---
Size resolution = getResolution();

float ndcx = static_cast<float>(x) / resolution.width;
float ndcy = static_cast<float>(y) / resolution.height;

float sscx = 2 * ndcx - 1;
float sscy = 2 * ndcy - 1;

Vec3f zAxis = this -> m_dir;
Vec3f xAxis = normalize(zAxis.cross(this -> m_up));
Vec3f yAxis = normalize(zAxis.cross(xAxis));

ray.org = m_pos;
ray.dir = normalize(getAspectRatio() * sscx * xAxis + sscy * yAxis + m_dir * zAxis);
ray.t = std::numeric_limits<float>::infinity();

}


Expand Down
42 changes: 41 additions & 1 deletion src/PrimDisc.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,44 @@
#include "ray.h"

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

class CPrimDisc : public IPrim {

public:

CPrimDisc(const Vec3f& origin, const Vec3f& normal, float radius):
IPrim(),
m_origin(origin),
m_normal(normal),
m_radius(radius)
{
normalize(m_normal);
}

virtual ~CPrimDisc(void) = default;

virtual bool intersect(Ray& ray) const override{
float dist = (this -> m_origin - ray.org).dot(this -> m_normal) / ray.dir.dot(this -> m_normal);
if (dist < Epsilon || isinf(dist) || ray.t < dist) {
return false;
}
Vec3f vec_from_org = (ray.org + dist * ray.dir) - m_origin;

float dist_from_org = (sqrt(vec_from_org.dot(vec_from_org)));

if (dist_from_org > m_radius) {
return false;
}

ray.t = dist;
return true;
}

private:

Vec3f m_normal;
Vec3f m_origin;
float m_radius;

};
9 changes: 8 additions & 1 deletion src/PrimPlane.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,14 @@ class CPrimPlane : public IPrim
virtual bool intersect(Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return false;
float dist = (this -> m_origin - ray.org).dot(this -> m_normal) / ray.dir.dot(this -> m_normal);

if (dist < Epsilon || isinf(dist) || ray.t < dist) {
return false;
} else {
ray.t = dist;
return true;
}
}


Expand Down
24 changes: 23 additions & 1 deletion src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,29 @@ class CPrimSphere : public IPrim
virtual bool intersect(Ray &ray) const override
{
// --- PUT YOUR CODE HERE ---
return false;
Vec3f L = this -> m_origin - ray.org;

float a = 1;
float b = static_cast<float>(-2 * ray.dir.dot(L));
float c = static_cast<float>(L.dot(L)) - static_cast<float>(this -> m_radius * this -> m_radius);

float D = b * b - 4 * a * c;

if (D < 0) {
return false;
} else {

float t1 = (-b + sqrt(D)) / (2 * a);
float t2 = (-b - sqrt(D)) / (2 * a);

if (t1 < Epsilon || t1 > ray.t) {
return false;
} else {

ray.t = t2 > Epsilon ? t2 : t1;
return true;
}
}
}


Expand Down
37 changes: 36 additions & 1 deletion src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,42 @@ class CPrimTriangle : public IPrim
virtual bool intersect(Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return false;
const Vec3f e1 = m_b - m_a;
const Vec3f e2 = m_c - m_a;

const Vec3f P = ray.dir.cross(e2);

const float deter = e1.dot(P);

if (deter < Epsilon) {
return false;
}

const float inverse_deter = 1.0f / deter;

const Vec3f T = ray.org - m_a;
float u = inverse_deter * (T.dot(P));

if (u > 1.0f || u < 0.0f) {
return false;
}


const Vec3f Q = T.cross(e1);
float v = inverse_deter * (Q.dot(ray.dir));
if (v < 0.0f || v + u >= 1.0f) {
return false;
}


float t = inverse_deter * (e2.dot(Q));

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

ray.t = t;
return true;
}


Expand Down
50 changes: 48 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,25 @@


#include "CameraPerspective.h"
#include "CameraEnvironmental.h"

#include "PrimDisc.h"
#include "PrimSphere.h"
#include "PrimPlane.h"
#include "PrimTriangle.h"

void updateIfDistanceChange(Ray&, float&, Vec3f&, Vec3f);

Mat RenderFrame(ICamera& camera)
{
// scene objects

CPrimSphere s1(Vec3f(-2, 1.7f, 0), 2);
// CPrimSphere s1(Vec3f(-2, 1.7f, 0), 2);
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
CPrimDisc d1(Vec3f(-2, 1.7f, 0), Vec3f(0, 1, 0), 1.0);

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 @@ -27,6 +33,7 @@ Mat RenderFrame(ICamera& camera)
// Initialize your ray here

// --- PUT YOUR CODE HERE ---
camera.InitRay(ray, y, x);

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

Expand All @@ -36,14 +43,49 @@ Mat RenderFrame(ICamera& camera)
*/

// --- PUT YOUR CODE HERE ---

float maxDistance = std::numeric_limits<float>::infinity();

d1.intersect(ray);
updateIfDistanceChange(ray, maxDistance, col, RGB(1, 0, 0));


s2.intersect(ray);
updateIfDistanceChange(ray, maxDistance, col, RGB(0, 1, 0));



s3.intersect(ray);
updateIfDistanceChange(ray, maxDistance, col, RGB(0, 0, 1));


p1.intersect(ray);
updateIfDistanceChange(ray, maxDistance, col, RGB(1, 1, 0));


t1.intersect(ray);
updateIfDistanceChange(ray, maxDistance, col, RGB(0, 1, 1));


t2.intersect(ray);
updateIfDistanceChange(ray, maxDistance, col, RGB(1, 1, 1));



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

img.convertTo(img, CV_8UC3, 255);
return img;
}

void updateIfDistanceChange(Ray& ray, float& newDistance, Vec3f& oldColor, Vec3f newColor) {
if (ray.t < newDistance) {
newDistance = ray.t;
oldColor = newColor;
}
}

int main(int argc, char* argv[])
{
const Size resolution(800, 600);
Expand All @@ -61,9 +103,13 @@ int main(int argc, char* argv[])
Mat img3 = RenderFrame(cam3);
imwrite("perspective3.jpg", img3);

CCameraEnvironmental cam4(resolution, Vec3f(-8, 3, 8), Vec3f(0, 0, 1), Vec3f(0, 1, 0));
Mat img4 = RenderFrame(cam4);
imwrite("environmental4.jpg", img4);

// AddeEnvironmental camera here as cam4
// Mat img4 = RenderFrame(cam4);
// imwrite("orthographic4.jpg", img4);

return 0;
}
}