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

Makeup HW4 CG Checiu #8

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
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
4 changes: 2 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ source_group("Source Files" FILES "src/main.cpp")
source_group("Source Files\\Cameras" FILES "src/ICamera.h" "src/CameraPerspective.h")
source_group("Source Files\\Lights" FILES "src/ILight.h" "src/LightOmni.h")
source_group("Source Files\\Primitives" FILES "src/IPrim.h" "src/PrimSphere.h" "src/PrimPlane.h" "src/PrimTriangle.h")
source_group("Source Files\\Solids" FILES "src/Solid.h" "src/SolidQuad.h" "src/SolidCone.h" "src/SolidSphere.h")
source_group("Source Files\\Shaders" FILES "src/IShader.h" "src/ShaderFlat.h" "src/ShaderEyelight.h" "src/ShaderPhong.h")
source_group("Source Files\\Solids" FILES "src/Solid.h" "src/SolidQuad.h" "src/SolidCone.h" "src/SolidSphere.h" "src/SolidGeosphere.h")
source_group("Source Files\\Shaders" FILES "src/IShader.h" "src/ShaderFlat.h" "src/ShaderEyelight.h" "src/ShaderPhong.h" "src/ShaderPhongBumpMapped.h")
source_group("Source Files\\Scene" FILES "src/Scene.h")
source_group("Source Files\\utilities" FILES "src/ray.h" "src/timer.h" "src/Texture.h")
source_group("Source Files\\utilities\\BSP Tree" FILES "src/BSPNode.h" "src/BSPTree.h" "src/BoundingBox.h" "src/BoundingBox.cpp")
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
# Practical Assignment 4
**Dealine**: 12.11.2020
**Dealine**: 22.12.2020

Please put your name here:
**Name:** .......
**Name:** Checiu Eliza and Abumansur Sabyrrakhim (we disccused and worked together on certain tasks, although we both wrote and compilied our own codes)

GeoSphere: I tried to write the code for it and used any resource I found but in the end it didn't really work on and so I did the normal sphere and worked on it for the assignment.

BONUS: I took the example given last year to run it and make it work. I tried to modified everything needed to make it work.

## Problem 1
### Sphere Solid (Points 25)
In this assignment we will continue working with _compound objects_: solids.
Expand Down Expand Up @@ -35,7 +40,7 @@ Proceed as follows:
6. Extend your code in ```CSolidSphere``` constructor in such a way that the triangles will be created with the additional normals. Calculate these normals (_e.g._ using the spherical coordinate system) and pass them within the triangles' and quads' constructors.
7. Test your implementation on the scene from Problem 1. Compare the difference between the Solid and Primitive spheres. Explain below why smoothed cone looks strange. How would you fix it?

**Explanation and Suggestion:** ...
**Explanation and Suggestion:** From my understanding, you can only assign one normal to the top vertex per triangle, which therefore means that there is not possible way to have smooth transition. To make it better though, I found that the method of multiple hight segments could work.

If everything is correct your images should look like this:

Expand Down
Binary file added renders/1.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/bump_mapping.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/final1.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/final11.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/final2.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/final22.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/half.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/image.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/image1.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/not_smooth.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/smooth.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/textured.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/textured_final.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/textured_img.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/white1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 9 additions & 1 deletion src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,15 @@ class CPrimSphere : public IPrim
virtual Vec2f getTextureCoords(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec2f(0, 0);
//return Vec2f(0, 0);

Vec3f hitPoint = ray.org + ray.dir * ray.t - m_origin;
float theta = acosf(hitPoint.val[1] / m_radius); // [0; Pif]

float phi = sinf(theta) > Epsilon ? acosf(hitPoint.val[0] / (m_radius * sinf(theta))) : 0; // [0; Pif]
if (hitPoint.val[2] < 0) phi = -phi; // [-Pif; Pif]
if (isnan(phi)) phi = 0;
return Vec2f(-0.5f * (1 + phi / Pif), theta / Pif);
}

virtual CBoundingBox getBoundingBox(void) const override
Expand Down
14 changes: 12 additions & 2 deletions src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,9 @@ class CPrimTriangle : public IPrim
ray.t = f;
ray.hit = shared_from_this();
// --- PUT YOUR CODE HERE ---

ray.u = lambda;
ray.v = mue;

return true;
}
Expand All @@ -83,7 +86,13 @@ class CPrimTriangle : public IPrim
{
if (m_na && m_nb && m_nc) {
// --- PUT YOUR CODE HERE ---
return Vec3f(0, 0, 0);
//return Vec3f(0, 0, 0);

Vec3f r0 = m_na.value();
Vec3f r1 = m_nb.value();
Vec3f r2 = m_nc.value();
Vec3f interp_norm = ((1.0f - ray.u - ray.v) * r0 + ray.u * r1 + ray.v * r2);
return interp_norm;
}
else
return normalize(m_edge1.cross(m_edge2));
Expand All @@ -92,7 +101,8 @@ class CPrimTriangle : public IPrim
virtual Vec2f getTextureCoords(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec2f(0, 0);
//return Vec2f(0, 0);
return(1.0f - ray.u - ray.v) * m_ta + ray.u * m_tb + ray.v * m_tc;
}

virtual CBoundingBox getBoundingBox(void) const override
Expand Down
4 changes: 3 additions & 1 deletion src/Scene.h
Original file line number Diff line number Diff line change
Expand Up @@ -133,11 +133,13 @@ class CScene
return intersect(ray) ? ray.hit->getShader()->shade(ray) : m_bgColor;
}

public:
std::vector<ptr_light_t> m_vpLights; ///< lights //eliza

private:
Vec3f m_bgColor; ///< background color
std::vector<ptr_prim_t> m_vpPrims; ///< primitives
std::vector<ptr_light_t> m_vpLights; ///< lights

std::vector<ptr_camera_t> m_vpCameras; ///< Cameras
size_t m_activeCamera = 0; //< The index of the active camera
#ifdef ENABLE_BSP
Expand Down
5 changes: 4 additions & 1 deletion src/ShaderFlat.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ class CShaderFlat : public IShader
{
if (m_pTexture) {
// --- PUT YOUR CODE HERE ---
return Vec3f(1, 1, 1);
//return Vec3f(1, 1, 1);

Vec2f uv = ray.hit->getTextureCoords(ray);
return m_pTexture->getTexel(uv);
}
else
return m_color;
Expand Down
99 changes: 99 additions & 0 deletions src/ShaderPhongBumpMapped.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#pragma once

#include "ShaderFlat.h"
#include "Scene.h"

/**
* @brief Phong Shader with procedural bump mapping
*/

const int nAreaSamples = 1;

class CShaderPhongBumpMapped : public CShaderFlat
{
public:
/**
* @brief Constructor
* @param scene Reference to the scene
* @param color The color of the object
* @param ka The ambient coefficient
* @param kd The diffuse reflection coefficients
* @param ks The specular refelection coefficients
* @param ke The shininess exponent
*/
CShaderPhongBumpMapped(CScene& scene, Vec3f color, float ka, float kd, float ks, float ke)
: CShaderFlat(color)
, m_scene(scene)
, m_ka(ka)
, m_kd(kd)
, m_ks(ks)
, m_ke(ke)
{}
virtual ~CShaderPhongBumpMapped(void) = default;

virtual Vec3f shade(const Ray& ray) const override
{
Vec3f dPdu = (1, 0, 0);
Vec3f dPdv = (0, 0, 1);
Vec3f h = ray.org + ray.t * ray.dir;
float delta_u = 0.5f * cos(3 * h[0] * sin(h[2]));
float delta_v = 0.5f * sin(13 * h[2]);

Vec3f normal0 = ray.hit->getNormal(ray);
Vec3f normal = normalize(normal0 + delta_u * dPdu + delta_v * dPdv);
if (normal.dot(ray.dir) > 0) {
normal = -normal;
}

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

Vec3f color = CShaderFlat::shade();
Vec3f ambientColor = m_ka * color;
Vec3f res = ambientColor.mul(ambientIntensity);

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

for(auto pLight : m_scene.m_vpLights)
for(int s = 0; s < nAreaSamples; s++) {
std::optional<Vec3f> lightIntensity = pLight->illuminate(shadow);
if (lightIntensity) {
float cosLightNormal = shadow.dir.dot(normal);
if (cosLightNormal > 0) {
if (m_scene.occluded(shadow)) {
continue;
}

Vec3f diffuseColor = m_kd * color;
res += (diffuseColor * cosLightNormal).mul(lightIntensity.value());
}

float cosLightReflect = shadow.dir.dot(reflect);
if (cosLightReflect > 0) {
Vec3f specularColor = m_ks * RGB(1, 1, 1); // white highlight;
res += (specularColor * powf(cosLightReflect, m_ke)).mul(lightIntensity.value());
}
}
}

if (nAreaSamples > 1) {
res /= nAreaSamples;
}

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

return res;
}

private:
CScene& m_scene;
float m_ka; ///< ambient coefficient
float m_kd; ///< diffuse reflection coefficients
float m_ks; ///< specular refelection coefficients
float m_ke; ///< shininess exponent
};
84 changes: 59 additions & 25 deletions src/SolidCone.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,32 +19,66 @@ class CSolidCone : public CSolid {
* @param height The height of the cone
* @param sides The number of sides
*/
CSolidCone(ptr_shader_t pShader, const Vec3f& origin = Vec3f::all(0), float radius = 1, float height = 1, size_t sides = 24)
CSolidCone(ptr_shader_t pShader, const Vec3f& origin = Vec3f::all(0), float radius = 1, float height = 1, size_t sides = 24)
{
const Vec3f top(0, height, 0); // The top point
Vec3f dir0(1, 0, 0); // Initial direction
Vec3f p0 = origin + radius * dir0; // Initial point
Vec3f dir1, p1; // Next point
float t0 = 0; // Initial texture coordinate
for (size_t s = 0; s < sides; s++) {
float t1 = static_cast<float>(s + 1) / sides; // Next texture coordinate: [1/sides; 1]
float alpha = -2 * Pif * t1;
dir1 = Vec3f(cosf(alpha), 0, sinf(alpha));
p1 = origin + radius * dir1;

// --- PUT YOUR CODE HERE ---
// Sides
if (height >= 0) add(std::make_shared<CPrimTriangle>(pShader, origin + top, p1, p0));
else add(std::make_shared<CPrimTriangle>(pShader, origin + top, p0, p1));

// Cap
if (height >= 0) add(std::make_shared<CPrimTriangle>(pShader, origin, p1, p0));
else add(std::make_shared<CPrimTriangle>(pShader, origin, p0, p1));

dir0 = dir1;
p0 = p1;
t0 = t1;
}
const Vec3f top(0, height, 0); // The top point
Vec3f dir0(1, 0, 0); // Initial direction
Vec3f p0 = origin + radius * dir0; // Initial point
float height_segments = sides / 12;
const Vec3f slope(0, radius / height, 0);
Vec3f n0 = normalize(dir0 + slope);
Vec3f dir1, p1, n1; // Next point
float t0 = 0; // Initial texture coordinate

for (size_t s = 0; s < sides; s++) {
float t1 = static_cast<float>(s + 1) / sides; // Next texture coordinate: [1/sides; 1]
float alpha = -2 * Pif * t1;
dir1 = Vec3f(cosf(alpha), 0, sinf(alpha));
p1 = origin + radius * dir1;
n1 = normalize(dir1 + slope);

// --- PUT YOUR CODE HERE ---
// inspired by OpenRT source code

// initial height
float h0 = 0;
for (int h = 0; h < height_segments - 1; h++) {
float h1 = static_cast<float>(h + 1) / height_segments;
add(std::make_shared<CSolidQuad>(pShader,
p0 + h0 * (top - radius * dir0),
p0 + h1 * (top - radius * dir0),
p1 + h1 * (top - radius * dir1),
p1 + h0 * (top - radius * dir1),
Vec2f(t0, 1 - h0), Vec2f(t0, 1 - h1), Vec2f(t1, 1 - h1), Vec2f(t1, 1 - h0),
n0, n0, n1, n1));

h0 = h1;
}
// Sides
if (height >= 0)
add(std::make_shared<CPrimTriangle>(pShader,
origin + top,
p1 + h0 * (top - radius * dir1),
p0 + h0 * (top - radius * dir0),
Vec2f(0.5f, 0), Vec2f(t1, 1 - h0), Vec2f(t0, 1 - h0),
normalize(n0 + n1), n1, n0));
else
add(std::make_shared<CPrimTriangle>(pShader,
origin + top,
p1 + h0 * (top - radius * dir1),
p0 + h0 * (top - radius * dir0),
Vec2f(0.5f, 0), Vec2f(t1, 1 - h0), Vec2f(t0, 1 - h0)));

// Cap
if (height >= 0) add(std::make_shared<CPrimTriangle>(pShader, origin, p1, p0, Vec2f(0.5f, 1), Vec2f(t1, 1), Vec2f(t0, 1)));
else add(std::make_shared<CPrimTriangle>(pShader, origin, p0, p1, Vec2f(0.5f, 1), Vec2f(t0, 1), Vec2f(t1, 1)));

dir0 = dir1;
p0 = p1;
n0 = n1;
t0 = t1;

}
}
virtual ~CSolidCone(void) = default;
};
Loading