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_CG #6

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


Expand Down
4 changes: 3 additions & 1 deletion src/PrimPlane.h
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 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();
// n - normal
Vec3f n = (ray.org + (ray.t*ray.dir)) - m_origin;

return normalize(n);
}

private:
Expand Down
3 changes: 2 additions & 1 deletion 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 normalize((m_b - m_a).cross(m_c - m_a));
}

private:
Expand Down
44 changes: 40 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;
}
}
return false;
}

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

/**
Expand All @@ -87,10 +103,30 @@ class CScene
Vec3f RayTrace(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
}
/*
// the code for the 1st problem
for (auto it : m_vpPrims) {
if (it -> intersect(ray)) {
c = ray.hit -> getShader() -> shade(ray);
}
}*/
// c - color
Vec3f c = m_bgColor;

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

}

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);
// theta
float t = ray.hit -> getNormal(ray).dot(-ray.dir);
return fabs(t) * CShaderFlat :: shade(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
2 changes: 1 addition & 1 deletion src/ShaderMirror.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ 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);
}


Expand Down
43 changes: 41 additions & 2 deletions src/ShaderPhong.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,49 @@ class CShaderPhong : public CShaderFlat
virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return RGB(0, 0, 0);
Vec3f colorAmbient = CShaderFlat :: shade(ray) * m_ka;
Ray ray_2;
Vec3f diff_s = 0;

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

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

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

Vec3f specular_sum = 0;
Ray ray3;

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

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

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

return colorAmbient + diffuse_color + specularColor;
}


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

// Flat surface shaders
// Flat surface shader
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
Expand All @@ -27,20 +27,20 @@ Mat RenderFrame(void)
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;
}