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-hw2-checiu #4

Open
wants to merge 20 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 @@
**Dealine**: 15.10.2020

Please put your name here:
**Name**: .......
**Name**: Checiu Eliza
## Problem 1
### Encapsulate camera and primitives from main application logic (Points 5)
1. Fork the current repository
Expand Down
Binary file added render/eyelight.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 render/flat.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 render/mirror1.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 render/phong1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions src/CameraPerspective.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class CCameraPerspective : public ICamera
m_xAxis = normalize(m_xAxis);
m_yAxis = normalize(m_yAxis);
m_zAxis = normalize(m_zAxis);


}
virtual ~CCameraPerspective(void) = default;

Expand Down
9 changes: 8 additions & 1 deletion src/LightOmni.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "ILight.h"
#include "Ray.h"

/**
* @brief Point light source class
Expand All @@ -24,7 +25,13 @@ class CLightOmni : public ILight
virtual std::optional<Vec3f> illuminate(Ray& ray) override
{
// --- PUT YOUR CODE HERE ---
return std::nullopt;
//return std::nullopt;
ray.dir = m_org - ray.org;
ray.t = norm(ray.dir);
ray.dir = normalize(ray.dir);
ray.hit = nullptr;

return m_intensity / (ray.t * ray.t);
}


Expand Down
4 changes: 3 additions & 1 deletion src/PrimPlane.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,15 @@ class CPrimPlane : public IPrim
if (dist < Epsilon || isinf(dist) || dist > ray.t) return false;

ray.t = dist;
ray.hit = shared_from_this();
return true;
}

virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
//return Vec3f();
return m_normal;
}

private:
Expand Down
5 changes: 4 additions & 1 deletion src/PrimSphere.h
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,16 @@ class CPrimSphere : public IPrim
}

ray.t = dist;
ray.hit = shared_from_this();
return true;
}

virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
//return Vec3f();
Vec3f v_hit = ray.org + ray.t * ray.dir;
return normalize(v_hit - m_origin);
}

private:
Expand Down
5 changes: 3 additions & 2 deletions src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,15 @@ class CPrimTriangle : public IPrim
if (ray.t <= f || f < Epsilon ) return false;

ray.t = f;

ray.hit = shared_from_this();
return true;
}

virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
//return Vec3f();
return normalize(m_edge1.cross(m_edge2));
}

private:
Expand Down
19 changes: 17 additions & 2 deletions src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include "ILight.h"
#include "IPrim.h"
#include "CameraPerspective.h"
#include "ICamera.h"

// ================================ Scene Class ================================
/**
Expand All @@ -30,6 +31,7 @@ class CScene
void add(const ptr_prim_t pPrim)
{
// --- PUT YOUR CODE HERE ---
m_vpPrims.push_back(pPrim);
}
/**
* @brief Adds a new light to the scene
Expand All @@ -38,6 +40,8 @@ class CScene
void add(const ptr_light_t pLight)
{
// --- PUT YOUR CODE HERE ---
m_vpLights.push_back(pLight);

}
/**
* @brief Adds a new camera to the scene and makes it to ba active
Expand All @@ -46,6 +50,8 @@ class CScene
void add(const ptr_camera_t pCamera)
{
// --- PUT YOUR CODE HERE ---
m_vpCameras.push_back(pCamera);
m_activeCamera = m_vpCameras.size() - 1;
}
/**
* @brief Returns the container with all scene light source objects
Expand All @@ -68,7 +74,11 @@ class CScene
bool intersect(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
return false;
//return false;
bool hit = false;
for (auto& pPrim : m_vpPrims)
hit |= pPrim->intersect(ray);
return hit;
}

/**
Expand All @@ -77,6 +87,10 @@ class CScene
bool occluded(Ray& ray)
{
// --- PUT YOUR CODE HERE ---
//return false;
for (auto& pPrim : m_vpPrims)
if (pPrim->occluded(ray))
return true;
return false;
}

Expand All @@ -87,7 +101,8 @@ class CScene
Vec3f RayTrace(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
//return Vec3f();
return intersect(ray) ? ray.hit->getShader()->shade(ray) : m_bgColor;
}


Expand Down
3 changes: 2 additions & 1 deletion src/ShaderEyelight.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ class CShaderEyelight : public CShaderFlat
virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return RGB(0, 0, 0);
//return RGB(0, 0, 0);
return CShaderFlat::shade(ray) * abs(ray.dir.dot(ray.hit->getNormal(ray)));
}
};

3 changes: 2 additions & 1 deletion src/ShaderFlat.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ class CShaderFlat : public IShader
virtual Vec3f shade(const Ray& ray = Ray()) const override
{
// --- PUT YOUR CODE HERE ---
return RGB(0, 0, 0);
//return RGB(0, 0, 0);
return m_color;
}

private:
Expand Down
15 changes: 14 additions & 1 deletion src/ShaderMirror.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,20 @@ class CShaderMirror : public IShader
virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f(0,0,0);
//return Vec3f(0,0,0);

Vec3f normal = ray.hit->getNormal(ray);

if (ray.dir.dot(normal) > 0)
normal *= -1;

Vec3f r_dir = ray.dir - 2 * (ray.dir.dot(normal)) * normal;

Ray r;
r.dir = normalize(r_dir);
r.t = std::numeric_limits<double>::infinity();
r.org = ray.t * ray.dir + ray.org;
return m_scene.RayTrace(r);
}


Expand Down
46 changes: 45 additions & 1 deletion src/ShaderPhong.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,54 @@ class CShaderPhong : public CShaderFlat
{}
virtual ~CShaderPhong(void) = default;

//we will apply the formula given

virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return RGB(0, 0, 0);
//return RGB(0, 0, 0);

Vec3f normal = ray.hit->getNormal(ray);

if (normal.dot(ray.dir) > 0)
normal = -normal;

Vec3f reflect = normalize(ray.dir - 2 * normal.dot(ray.dir) * normal);

Vec3f a_Intensity(1, 1, 1);

Vec3f color = CShaderFlat::shade();

Vec3f a_Color = m_ka * color;
Vec3f r_final = a_Color.mul(a_Intensity);

Ray shadow;
shadow.org = ray.org + ray.t * ray.dir;

for (auto pLight : m_scene.getLights()) {
std::optional<Vec3f> l_Intensity = pLight->illuminate(shadow);
if (l_Intensity) {
float cos_l_Normal = shadow.dir.dot(normal);
if (cos_l_Normal > 0) {
if (m_scene.occluded(shadow))
continue;

Vec3f d_Color = m_kd * color;
r_final += (d_Color * cos_l_Normal).mul(l_Intensity.value());
}

float cos_l_Reflect = shadow.dir.dot(reflect);
if (cos_l_Reflect > 0) {
Vec3f s_Color = m_ks * RGB(1, 1, 1);
r_final += (s_Color * powf(cos_l_Reflect, m_ke)).mul(l_Intensity.value());
}
}
}

for (int i = 0; i < 3; i++)
if (r_final.val[i] > 1) r_final.val[i] = 1;

return r_final;
}


Expand Down
Loading