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 2 - Solution #2

Open
wants to merge 1 commit 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**: Albrit Bendo
## Problem 1
### Encapsulate camera and primitives from main application logic (Points 5)
1. Fork the current repository
Expand Down
10 changes: 8 additions & 2 deletions src/LightOmni.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,17 @@ class CLightOmni : public ILight
virtual std::optional<Vec3f> illuminate(Ray& ray) override
{
// --- PUT YOUR CODE HERE ---
return std::nullopt;
ray.dir = m_org - ray.org;
ray.t = norm(ray.dir);
ray.dir = normalize(ray.dir);
ray.hit = nullptr;

double attenuation = 1 / (ray.t*ray.t);
return m_intensity * attenuation;
}


private:
Vec3f m_intensity; ///< The emission (red, green, blue)
Vec3f m_org; ///< The light source origin
};
};
2 changes: 1 addition & 1 deletion src/PrimPlane.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class CPrimPlane : public IPrim
virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
return m_normal;
}

private:
Expand Down
2 changes: 1 addition & 1 deletion src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class CPrimSphere : public IPrim
virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
return normalize(ray.org + ray.dir * ray.t - m_origin);
}

private:
Expand Down
2 changes: 1 addition & 1 deletion src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ class CPrimTriangle : public IPrim
virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
return normalize((m_b - m_a).cross(m_c - m_a));
}

private:
Expand Down
15 changes: 14 additions & 1 deletion src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,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 +39,7 @@ 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 +48,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,6 +72,10 @@ class CScene
bool intersect(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
Copy link
Collaborator

Choose a reason for hiding this comment

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

This function is implemented wrong. You return the first found intersection, instead of checking all possible intersections.

for(auto& prim : m_vpPrims) {
if(prim->intersect(ray)) { return true; }
}

return false;
}

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

return false;
}

Expand All @@ -87,7 +99,8 @@ class CScene
Vec3f RayTrace(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
if(intersect(ray)) { return ray.hit->getShader()->shade(ray); }
Copy link
Collaborator

Choose a reason for hiding this comment

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

getShader() method throws an error because you do not fill ray.hit in your interestion algorithms.

else { return m_bgColor; }
}


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

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

private:
Expand Down
12 changes: 11 additions & 1 deletion src/ShaderMirror.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,17 @@ class CShaderMirror : public IShader
virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f(0,0,0);
Ray shade;
Vec3f nrm = ray.hit->getNormal(ray);
double ray_dnrm = ray.dir.dot(nrm);

if(ray_dnrm > 0) { nrm = -nrm; }

shade.dir = normalize(ray.dir - ray_dnrm * nrm * 2);
shade.t = std::numeric_limits<float>::max();
shade.org = ray.dir * ray.t + ray.org;

return m_scene.RayTrace(shade);
}


Expand Down
32 changes: 31 additions & 1 deletion src/ShaderPhong.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,37 @@ class CShaderPhong : public CShaderFlat
virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return RGB(0, 0, 0);
Vec3f nrm = ray.hit->getNormal(ray);
double ray_dnrm = ray.dir.dot(nrm);

if(ray_dnrm > 0) { nrm = -nrm; }

Vec3f ambient_col = CShaderFlat::shade(ray);
Vec3f diffuse_col = 0;
Vec3f specular_col = 0;

Ray shade;

int n = 1;

shade.org = ray.dir * ray.t + ray.org;

for(auto light : m_scene.getLights()) {
for(int i = 0; i < n; i ++) {
if(!light->illuminate(shade))
if(m_scene.occluded(shade))
Copy link
Collaborator

Choose a reason for hiding this comment

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

Somehow the shadows are not rendered at all

continue;

Vec3f res = 2 * shade.dir.dot(nrm) * nrm - shade.dir;

diffuse_col += *light->illuminate(shade) * std::max(0.f, shade.dir.dot(nrm));
specular_col += *light->illuminate(shade) * std::pow(std::max(0.f, res.dot(-ray.dir)), m_ke);
}
}

return m_ka * ambient_col * 1.1 +
m_kd * ambient_col.mul(diffuse_col / n) +
m_ks * RGB(1, 1, 1).mul(specular_col / n);
}


Expand Down