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

Assignment2 #3

Open
wants to merge 4 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
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
# Practical Assignment 2
**Dealine**: 15.10.2020

Please put your name here:
**Name**: .......

**Name**: Katrin von Seggern
## Problem 1
### Encapsulate camera and primitives from main application logic (Points 5)
1. Fork the current repository
Expand Down
9 changes: 8 additions & 1 deletion src/LightOmni.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,14 @@ class CLightOmni : public ILight
virtual std::optional<Vec3f> illuminate(Ray& ray) override
{
// --- PUT YOUR CODE HERE ---
return std::nullopt;
//formula from lecture Shading: Rendering Equation & BRDF and with help in tutorial
//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 attenuation * m_intensity;
}


Expand Down
3 changes: 2 additions & 1 deletion src/PrimPlane.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ class CPrimPlane : public IPrim
virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
//return Vec3f();
return m_normal;
}

private:
Expand Down
4 changes: 3 additions & 1 deletion src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,9 @@ class CPrimSphere : public IPrim
virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
//return Vec3f();
//taken from code in Wednesday's tutorial
return normalize(ray.org + ray.dir * ray.t - m_origin);
}

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

private:
Expand Down
23 changes: 21 additions & 2 deletions 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_activeCamera + 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is valid approach, but only in case, when you initialize m_activeCamera to be equal to -1 on construction. This also requires to have m_activeCamera variable of type int, not size_t.
How it is implemented now, it shows an error on execution.

}
/**
* @brief Returns the container with all scene light source objects
Expand All @@ -68,6 +72,12 @@ class CScene
bool intersect(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
bool hit = false;
for (auto pPrim : m_vpPrims) {
if (pPrim->intersect(ray)) {
hit = true;
}
}
return false;
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is obviously wrong. You return false all the time - so nothing can be rendered! Did you even check any results?

}

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

/**
Expand All @@ -87,7 +105,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;
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

getShader() causes error here, because you never initialize ray.hit in the intersection algorithms



Expand Down
5 changes: 4 additions & 1 deletion src/ShaderEyelight.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ class CShaderEyelight : public CShaderFlat
virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return RGB(0, 0, 0);
//using ray.hit->getNormal(ray) to retrieve the surface normal of the primitive
Vec3f N = ray.hit->getNormal(ray);
Vec3f R = normalize(ray.dir);
return abs(N.dot(R))*CShaderFlat::shade();
}
};

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
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);
//return Vec3f(0,0,0);
Vec3f N=ray.hit->getNormal(ray);
if (ray.dir.dot(N) > 0) {
N = N * -1;
}
Vec3f R = ray.dir - 2 * (ray.dir.dot(N)) * N;
Ray reflectedRay;
reflectedRay.dir = normalize(R);
reflectedRay.t = std::numeric_limits<float>::max();
reflectedRay.org = ray.dir * ray.t + ray.org;
return m_scene.RayTrace(reflectedRay);
}


Expand Down
39 changes: 38 additions & 1 deletion src/ShaderPhong.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,44 @@ class CShaderPhong : public CShaderFlat
virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return RGB(0, 0, 0);
//return RGB(0, 0, 0);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Phong images are too dark
No shadows are rendered

Vec3f normal = ray.hit->getNormal(ray);
if (ray.dir.dot(normal) > 0) {
normal *= -1;
}
//ambient intensity
Vec3f ambienceIntensity(1, 1, 1);

//ambient color c_a
Vec3f reflect = normalize(ray.dir - 2 * (ray.dir.dot(normal)) * normal);
Vec3f color = CShaderFlat::shade();
Vec3f c_a = m_ka * color;
Vec3f endproduct = c_a.mul(ambienceIntensity);
Vec3f res = c_a.mul(ambienceIntensity);


Ray shadow;
shadow.org = ray.org + ray.t * ray.dir;
for (auto pLight : m_scene.getLights()) {
std::optional<Vec3f> lightIntensity = pLight->illuminate(shadow);
float cosLightNormal = shadow.dir.dot(normal);
if (cosLightNormal > 0) {
if (m_scene.occluded(shadow)) {
continue;
}
Vec3f diffuseColor = m_kd * color;
res += (diffuseColor * cosLightNormal).mul(cosLightNormal).mul(lightIntensity.value());
}
float cosLightReflect = shadow.dir.dot(reflect);
if (cosLightReflect > 0) {
Vec3f specularColor = m_ks * RGB(1, 1, 1);
res += (specularColor * powf(cosLightReflect, m_ke)).mul(lightIntensity.value());
}

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


Expand Down