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 #5

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
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**: Shahin Mammadov
## Problem 1
### Encapsulate camera and primitives from main application logic (Points 5)
1. Fork the current repository
Expand Down
Binary file added renders/flat1.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/flat2.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/flat3.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/flat4.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 4 additions & 1 deletion src/LightOmni.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@ class CLightOmni : public ILight
virtual std::optional<Vec3f> illuminate(Ray& ray) override
{
// --- PUT YOUR CODE HERE ---
return std::nullopt;
Vec3f direction = m_org - ray.org;
Vec3f intensity = m_intensity / pow(cv::norm(direction), 2);
ray.dir = normalize(direction);
return normalize(intensity);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Intensity should not be normalized...

}


Expand Down
5 changes: 4 additions & 1 deletion src/PrimPlane.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ 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 normalize(m_normal);
}

private:
Expand Down
6 changes: 5 additions & 1 deletion src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,17 @@ 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(); //old code
Vec3f normal = (ray.org+(ray.t*ray.dir))-m_origin;

return normalize(normal);
}

private:
Expand Down
4 changes: 3 additions & 1 deletion src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,14 +57,16 @@ 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_b - m_a).cross(m_c - m_a));
}

private:
Expand Down
43 changes: 39 additions & 4 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,10 @@ class CScene
void add(const ptr_camera_t pCamera)
{
// --- PUT YOUR CODE HERE ---
m_vpCameras.push_back(pCamera);
std::vector<ptr_camera_t>::iterator it = std::find(m_vpCameras.begin(), m_vpCameras.end(), pCamera);
m_activeCamera = std::distance(m_vpCameras.begin(), it);

}
/**
* @brief Returns the container with all scene light source objects
Expand All @@ -67,7 +73,17 @@ class CScene
*/
bool intersect(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
// --- PUT YOUR CODE HERE --
/*bool check = false;
for (auto& current : m_vpPrims)
check |= current->intersect(ray);
return check;
*/
for (auto i = m_vpPrims.begin(); i != m_vpPrims.end(); i++) {
if ((*i)->intersect(ray)) {
return true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

you exit the method once the first intersection was found - but you need to find the nearest one!

}
}
return false;
}

Expand All @@ -77,7 +93,7 @@ class CScene
bool occluded(Ray& ray)
{
// --- PUT YOUR CODE HERE ---
return false;
return intersect(ray);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Might be optimized here...

}

/**
Expand All @@ -87,10 +103,29 @@ class CScene
Vec3f RayTrace(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
}
/*if (intersect(ray) == true)
return RGB(1, 1, 1); //used for the first task
else
return RGB(0, 0, 0);*/

Vec3f color = m_bgColor;

for (auto primitive : m_vpPrims)
{
if (primitive->intersect(ray))
{
color = ray.hit->getShader()->shade(ray);
}
}
return color;

}

public:
std::vector<ptr_light_t> getLights()
{
return m_vpLights;
}
private:
Vec3f m_bgColor; ///< background color
std::vector<ptr_prim_t> m_vpPrims; ///< primitives
Expand Down
4 changes: 3 additions & 1 deletion src/ShaderEyelight.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@ 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);
float theta = ray.hit->getNormal(ray).dot(-ray.dir);
return fabs(theta) * CShaderFlat::shade(ray); //fabs is float
}
};

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; //returning color
}

private:
Expand Down
49 changes: 47 additions & 2 deletions src/ShaderPhong.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,55 @@ 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);
Vec3f color_ambient = CShaderFlat::shade(ray) * m_ka;
Ray ray2;
Vec3f diff_s = 0;

for (int i = 0; i < m_scene.getLights().size(); i++)
{
ray2.org = ray.org + ray.t * ray.dir;
std::optional<Vec3f> lRadiance = m_scene.getLights()[i]->illuminate(ray2);
ray2.t = std::numeric_limits<float>::infinity();

if (!m_scene.occluded(ray2))
{
if (lRadiance)
{
float theta = max(0.0f, ray2.dir.dot(ray.hit->getNormal(ray)));
diff_s += *lRadiance * theta;
}
}
}

Vec3f diffuse_color = m_kd * diff_s.mul(CShaderFlat::shade(ray));

Vec3f specular_sum = 0;
Ray ray3;

for (int i = 0; i < m_scene.getLights().size(); i++)
{
ray3.org = ray.org + ray.t * ray.dir;
std::optional<Vec3f> lRadiance = m_scene.getLights()[i]->illuminate(ray3);
ray3.t = std::numeric_limits<float>::infinity();
if (!m_scene.occluded(ray2))
{
if (lRadiance)
{
Vec3f reflected_distance = ray3.dir - 2 * (ray3.dir.dot(ray.hit->getNormal(ray))) * ray.hit->getNormal(ray);
float theta = max(0.0f, ray.dir.dot(reflected_distance));

specular_sum += *lRadiance * pow(theta, m_ke);
}
}
}

Vec3f specular_color = m_ks * RGB(1, 1, 1).mul(specular_sum);

return color_ambient + diffuse_color + specular_color;
}


private:
CScene& m_scene;
float m_ka; ///< ambient coefficient
Expand Down
30 changes: 15 additions & 15 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ Mat RenderFrame(void)
CScene scene;

// Flat surface shaders
auto shd1 = std::make_shared<CShaderFlat>(RGB(1, 0, 0)); // red surface
/*auto shd1 = std::make_shared<CShaderFlat>(RGB(1, 0, 0)); // red surface
auto shd2 = std::make_shared<CShaderFlat>(RGB(0, 1, 0)); // green surface
auto shd3 = std::make_shared<CShaderFlat>(RGB(0, 0, 1)); // blue surface
auto shd4 = std::make_shared<CShaderFlat>(RGB(1, 1, 0)); // yellow surface
auto shd5 = std::make_shared<CShaderFlat>(RGB(0, 1, 1)); // cyan surface
auto shd6 = std::make_shared<CShaderFlat>(RGB(1, 1, 1)); // white surface
auto shd6 = std::make_shared<CShaderFlat>(RGB(1, 1, 1)); // white surface*/

// EyeLight surface shaders
// auto shd1 = std::make_shared<CShaderEyelight>(RGB(1, 0, 0)); // red surface
// auto shd2 = std::make_shared<CShaderEyelight>(RGB(0, 1, 0)); // green surface
// auto shd3 = std::make_shared<CShaderEyelight>(RGB(0, 0, 1)); // blue surface
// auto shd4 = std::make_shared<CShaderEyelight>(RGB(1, 1, 0)); // yellow surface
// auto shd5 = std::make_shared<CShaderEyelight>(RGB(0, 1, 1)); // cyan surface
// auto shd6 = std::make_shared<CShaderEyelight>(RGB(1, 1, 1)); // white surface
/*auto shd1 = std::make_shared<CShaderEyelight>(RGB(1, 0, 0)); // red surface
auto shd2 = std::make_shared<CShaderEyelight>(RGB(0, 1, 0)); // green surface
auto shd3 = std::make_shared<CShaderEyelight>(RGB(0, 0, 1)); // blue surface
auto shd4 = std::make_shared<CShaderEyelight>(RGB(1, 1, 0)); // yellow surface
auto shd5 = std::make_shared<CShaderEyelight>(RGB(0, 1, 1)); // cyan surface
auto shd6 = std::make_shared<CShaderEyelight>(RGB(1, 1, 1)); // white surface*/

// Phong surface shaders
// auto shd1 = std::make_shared<CShaderPhong>(scene, RGB(1, 0, 0), 0.1f, 0.5f, 0.5f, 40); // red surface
// auto shd2 = std::make_shared<CShaderPhong>(scene, RGB(0, 1, 0), 0.1f, 0.5f, 0.5f, 40); // green surface
// auto shd3 = std::make_shared<CShaderPhong>(scene, RGB(0, 0, 1), 0.1f, 0.5f, 0.5f, 40); // blue surface
// auto shd4 = std::make_shared<CShaderPhong>(scene, RGB(1, 1, 0), 0.1f, 0.5f, 0.5f, 40); // yellow surface
// auto shd5 = std::make_shared<CShaderPhong>(scene, RGB(0, 1, 1), 0.1f, 0.5f, 0.5f, 40); // cyan surface
// auto shd6 = std::make_shared<CShaderPhong>(scene, RGB(1, 1, 1), 0.1f, 0.5f, 0.5f, 40); // white surface
auto shd1 = std::make_shared<CShaderPhong>(scene, RGB(1, 0, 0), 0.1f, 0.5f, 0.5f, 40); // red surface
auto shd2 = std::make_shared<CShaderPhong>(scene, RGB(0, 1, 0), 0.1f, 0.5f, 0.5f, 40); // green surface
auto shd3 = std::make_shared<CShaderPhong>(scene, RGB(0, 0, 1), 0.1f, 0.5f, 0.5f, 40); // blue surface
auto shd4 = std::make_shared<CShaderPhong>(scene, RGB(1, 1, 0), 0.1f, 0.5f, 0.5f, 40); // yellow surface
auto shd5 = std::make_shared<CShaderPhong>(scene, RGB(0, 1, 1), 0.1f, 0.5f, 0.5f, 40); // cyan surface
auto shd6 = std::make_shared<CShaderPhong>(scene, RGB(1, 1, 1), 0.1f, 0.5f, 0.5f, 40); // white surface*/

// Mirror shader
auto shdM = std::make_shared<CShaderMirror>(scene);
Expand Down Expand Up @@ -87,6 +87,6 @@ int main(int argc, char* argv[])
Mat img = RenderFrame();
imshow("Image", img);
waitKey();
imwrite("flat.jpg", img);
imwrite("flat4.jpg", img);
return 0;
}