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

eyden-tracer-01 (makeup) #7

Open
wants to merge 8 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**: 14.12.2020

Please put your name here:
**Name**: .......
**Name**: Abumansur Sabyrrakhim
## 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.
34 changes: 33 additions & 1 deletion src/CameraEnvironmental.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,37 @@
#include "ICamera.h"
#include "ray.h"

// --- IMPLENET class CCameraEnvironmental ---
// --- IMPLEMENT class CCameraEnvironmental ---
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suspicion for plagiarism

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

class CCameraEnvironmental : public ICamera
{
public:
CCameraEnvironmental(Size resolution, const Vec3f& pos, float angle)
: ICamera(resolution)
, m_pos(pos)
{}

virtual ~CCameraEnvironmental(void) = default;
virtual void InitRay(Ray& ray, int x, int y) override {

auto height = getResolution().height;
auto width = getResolution().width;

// finding the normalized
double theta = 2 * Pif * x / width;
double phi = Pif * y / height;

double x1 = sin(phi) * cos(theta);
double y1 = sin(phi) * sin(theta);
double z = cos(phi);
Vec3f spherePoint = Vec3f(x1, y1, z);

ray.org = m_pos;
ray.dir = normalize(spherePoint - m_pos);
ray.t = std :: numeric_limits<float> :: infinity();
}
private:
// camera origin
Vec3f m_pos;
};
18 changes: 15 additions & 3 deletions src/CameraPerspective.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,25 @@ class CCameraPerspective : public ICamera
, m_up(up)
{
// --- PUT YOUR CODE HERE ---
m_zAxis = m_dir;
m_xAxis = normalize(m_zAxis.cross(m_up));
m_yAxis = normalize(m_zAxis.cross(m_xAxis));
m_focus = 1.0f / tanf(angle * Pif / 360);
}
virtual ~CCameraPerspective(void) = default;

virtual void InitRay(Ray& ray, int x, int y) override
{
virtual void InitRay(Ray& ray, int x, int y) override {
// --- PUT YOUR CODE HERE ---
}
Size resolution = getResolution();

// scrSpCoord - screen space coordinates
float scrSpCoord_x = (2 * static_cast<float>(x) / resolution.width) - 1;
float scrSpCoord_y = (2 * static_cast<float>(y) / resolution.width) - 1;

ray.org = m_pos;
ray.dir = normalize(getAspectRatio() * scrSpCoord_x * m_xAxis + scrSpCoord_y * m_yAxis + m_focus * m_zAxis);
ray.t = std :: numeric_limits<float> :: infinity();
}


private:
Expand Down
3 changes: 2 additions & 1 deletion src/IPrim.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class IPrim
* @brief Constructor
*/
IPrim(void) = default;
IPrim(const IPrim&) = delete;
// IPrim(const IPrim&) = delete;
IPrim(const IPrim&) = default;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should not modify this file...

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, professor, but it was giving me an error that function I was referencing does not exist.

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

Expand Down
40 changes: 38 additions & 2 deletions src/PrimDisc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,41 @@
#include "IPrim.h"
#include "ray.h"

// --- IMPLENET class CPrimDisc ---
// --- PUT YOUR CODE HERE ---
// --- IMPLEMENT class CPrimDisc ---
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suspicion for plagiarism

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just corrected the typo.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I meant the whole implementation for disc primitive

// --- PUT YOUR CODE HERE ---
class CPrimDisc: public IPrim {
public:
// constructor
CPrimDisc(const Vec3f& origin, const Vec3f normal, double radius) : IPrim(), m_origin(origin), m_normal(normal), m_radius(radius) {}

// destructor
virtual ~CPrimDisc(void) = default;

// intersect method
virtual bool intersect(Ray& ray) const override {
auto plane = CPrimPlane(m_origin, m_normal);
Ray r_copy = ray;
if(!plane.intersect(r_copy)) {
return false;
}

auto pt = r_copy.org + r_copy.dir * r_copy.t;
auto distance = norm(pt - m_origin);
if(distance > m_radius) {
return false;
}

if(r_copy.t > ray.t) {
return false;
}

ray.t = r_copy.t;
return true;
}


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

if(ray.t < distance) {
return false;
}
ray.t = distance;
return true;
}


Expand Down
25 changes: 24 additions & 1 deletion src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,30 @@ 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 = 2 * ray.dir.dot(ray.org - m_origin);
float c = (ray.org - m_origin).dot(ray.org - m_origin) - m_radius * m_radius;

// Using discriminant
float D = b * b - 4 * a * c;
if(D < 0) {
return false;
}
float x1 = (-b + sqrt(D)) / (2 * a);
float x2 = (-b - sqrt(D)) / (2 * a);

if((x1 < Epsilon && x2 < Epsilon) || (x1 > ray.t && x2 > ray.t)) {
return false;
}

if(x1 > x2) {
ray.t = x2;
}
else {
ray.t = x1;
}

return true;
}


Expand Down
23 changes: 22 additions & 1 deletion src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,28 @@ class CPrimTriangle : public IPrim
virtual bool intersect(Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return false;
Vec3f n_ab = (m_b - ray.org).cross(m_a - ray.org);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suspicion for plagiarism

Vec3f n_bc = (m_c - ray.org).cross(m_b - ray.org);
Vec3f n_ca = (m_a - ray.org).cross(m_c - ray.org);

float area = n_ab.dot(ray.dir) + n_bc.dot(ray.dir) + n_ca.dot(ray.dir);
float lambda1 = n_ab.dot(ray.dir) / area;
float lambda2 = n_bc.dot(ray.dir) / area;
float lambda3 = n_ca.dot(ray.dir) / area;

if(lambda1 < 0 || lambda2 < 0 || lambda3 < 0) {
return false;
}

Vec3f p = m_a * lambda1 + m_b * lambda2 + m_c *lambda3;
float t = p[0] / ray.dir[0];

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

ray.t = t;
return true;
}


Expand Down
66 changes: 43 additions & 23 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,40 +4,59 @@
#include "PrimSphere.h"
#include "PrimPlane.h"
#include "PrimTriangle.h"
#include "PrimDisc.h"

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

CPrimSphere s1(Vec3f(-2, 1.7f, 0), 2);
CPrimSphere s2(Vec3f(1, -1, 1), 2.2f);
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 s1(Vec3f(-2, 1.7f, 0), Vec3f(0, 1, 0), 2);

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));

Mat img(camera.getResolution(), CV_32FC3); // image array
Ray ray; // primary ray


// colors
const auto background = RGB(0, 0, 0); // background color
const auto red = RGB(1, 0, 0); // s1
const auto green = RGB(0, 1, 0); // s2
const auto blue = RGB(0, 0, 1); // s3
const auto yellow = RGB(1, 1, 0); // p1
const auto cyan = RGB(0, 1, 1); // t1
const auto white = RGB(1, 1, 1); // t2

for(int y = 0; y< img.rows; y++)
for (int x = 0; x < img.cols; x++) {

// Initialize your ray here

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

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

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

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

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

camera.InitRay(ray, x, y);
img.at<Vec3f>(y, x) = background;
if (s1.intersect(ray)) {
img.at<Vec3f>(y, x) = red;
}
if (s2.intersect(ray)) {
img.at<Vec3f>(y, x) = green;
}
if (s3.intersect(ray)) {
img.at<Vec3f>(y, x) = blue;
}
if (p1.intersect(ray)) {
img.at<Vec3f>(y, x) = yellow;
}
if (t1.intersect(ray)) {
img.at<Vec3f>(y, x) = cyan;
}
if (t2.intersect(ray)) {
img.at<Vec3f>(y, x) = white;
}
// img.at<Vec3f>(y, x) = col; // store pixel color
}

img.convertTo(img, CV_8UC3, 255);
Expand All @@ -46,7 +65,7 @@ Mat RenderFrame(ICamera& camera)

int main(int argc, char* argv[])
{
const Size resolution(800, 600);
const Size resolution(1000, 800);
// render three images with different camera settings

CCameraPerspective cam1(resolution, Vec3f(0, 0, 10), Vec3f(0, 0, -1), Vec3f(0, 1, 0), 60);
Expand All @@ -61,9 +80,10 @@ int main(int argc, char* argv[])
Mat img3 = RenderFrame(cam3);
imwrite("perspective3.jpg", img3);

// AddeEnvironmental camera here as cam4
// Mat img4 = RenderFrame(cam4);
// imwrite("orthographic4.jpg", img4);
// Add Environmental camera here as cam4
CCameraEnvironmental cam4(resolution, Vec3f(-8, 3, 8), 45);
Mat img4 = RenderFrame(cam4);
imwrite("environmental4.jpg", img4);

return 0;
}
}