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

hw4 submission #4

Open
wants to merge 2 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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,7 @@ doc/html
./vscode

.DS_Store

.idea
cmake-build-debug

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
**Dealine**: 12.11.2020

Please put your name here:
**Name:** .......
**Name:** Otmane Sabir
## Problem 1
### Sphere Solid (Points 25)
In this assignment we will continue working with _compound objects_: solids.
Expand Down Expand Up @@ -36,6 +36,9 @@ Proceed as follows:
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:** ...
The smoothed cone result doesn't look the same for me but I believe it's because of how I compute the tip normal. I think that in the screenshot the tip is always facing upwards (in the direction of the cone)
while for me I recompute it to "spin" around the cone each time. I generally just average the two bottom normals them add the height to them which approximately lands on the tip.
My approach makes the smoothing a little more obvious especially when we start getting closer to the tip but this can be countered by increasing the number of sides we use.

If everything is correct your images should look like this:

Expand Down
Binary file added renders/barney.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/green_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/green_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_object.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: 4 additions & 2 deletions src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,10 @@ class CPrimSphere : public IPrim

virtual Vec2f getTextureCoords(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec2f(0, 0);
auto p = normalize(getNormal(ray));
float longitude = atan2(p.val[2], p.val[0]); // this is the longitude value
float latitude = acos(p.val[1]);
return Vec2f((longitude / Pi) * 0.5 + 0.5, (latitude / Pi));
}

virtual CBoundingBox getBoundingBox(void) const override
Expand Down
11 changes: 6 additions & 5 deletions src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,26 @@ class CPrimTriangle : public IPrim

ray.t = f;
ray.hit = shared_from_this();
// --- PUT YOUR CODE HERE ---
ray.u = lambda;
ray.v = mue;

return true;
}

virtual Vec3f getNormal(const Ray& ray) const override
{
if (m_na && m_nb && m_nc) {
// --- PUT YOUR CODE HERE ---
return Vec3f(0, 0, 0);
Vec3f normal = (1 - ray.u - ray.v)*m_na.value() + ray.u * m_nb.value() + ray.v*m_nc.value();
return normalize(normal);
}
else
return normalize(m_edge1.cross(m_edge2));
}

virtual Vec2f getTextureCoords(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec2f(0, 0);
// Again using the barycentric coordinates to interpolate the texture coords.
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
3 changes: 1 addition & 2 deletions src/ShaderFlat.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,7 @@ class CShaderFlat : public IShader
virtual Vec3f shade(const Ray& ray) const override
{
if (m_pTexture) {
// --- PUT YOUR CODE HERE ---
return Vec3f(1, 1, 1);
return m_pTexture->getTexel(ray.hit->getTextureCoords(ray));
}
else
return m_color;
Expand Down
26 changes: 20 additions & 6 deletions src/SolidCone.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,24 +22,38 @@ class CSolidCone : public CSolid {
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
const Vec3f slope(0, radius, 0);
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

// Here the majority of the code was already here I only added the normal and texture computations.
// What happens? well I compute the normal vector for each of the point such that they all point outside
// of the cone. I do this by calculating the vector from the origin to the point that creates one of the triangles
// and normalizing it. Afterwards I average the two bottom points and shift the vector up-wards towards the level
// of the tip.

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));
Vec3f n_p0 = normalize((p0 - origin));
Vec3f n_p1 = normalize((p1 - origin));
Vec3f n_top = normalize((n_p0 + n_p1) / 2 + top);
Vec2f t_p0, t_p1;
t_p0 = Vec2f(t0, 1 - t0);
t_p1 = Vec2f(t1, 1 - t0);

// Sides
if (height >= 0) add(std::make_shared<CPrimTriangle>(pShader, origin + top, p1, p0, Vec2f(0, 0), t_p1, t_p0, n_top, n_p1, n_p0));
else add(std::make_shared<CPrimTriangle>(pShader, origin + top, p0, p1, Vec2f(0, 0), t_p0, t_p1, n_top, n_p0, n_p1));

// Cap
if (height >= 0) add(std::make_shared<CPrimTriangle>(pShader, origin, p1, p0));
else add(std::make_shared<CPrimTriangle>(pShader, origin, p0, p1));
if (height >= 0) add(std::make_shared<CPrimTriangle>(pShader, origin, p1, p0, Vec2f(0, 1), t_p1, t_p0, -n_top, n_p1, n_p0));
else add(std::make_shared<CPrimTriangle>(pShader, origin, p0, p1, Vec2f(0, 1), t_p0, t_p1, -n_top, n_p0, n_p1));

dir0 = dir1;
p0 = p1;
Expand Down
58 changes: 57 additions & 1 deletion src/SolidSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,63 @@ class CSolidSphere : public CSolid
public:
CSolidSphere(ptr_shader_t pShader, const Vec3f& origin = Vec3f::all(0), float radius = 1, size_t sides = 24)
{
// --- PUT YOUR CODE HERE ---
//(x, y, z) = (sin(Pi * m/M) cos(2Pi * n/N), sin(Pi * m/M) sin(2Pi * n/N), cos(Pi * m/M))
m_origin = origin;
m_radius = radius;
m_sides = sides;

// This is quite straight forwards - The function creates 4 points and creates a quad from them
// It uses UV coordinates to so. Except at the cap where it creates triangles from the "north/south poles"
// to the points from a layer below.
// The drawback is that all points are computed many times - an alterantive is to first create the points and
// then iterate through them and create the shapes. However, I chose this method since it was the naive way
// and the easiest to understand.
for (int i = 0; i < sides; i++)
{
for (int j = 0; j < sides; j++)
{
if (i == 0 || i == sides - 1)
{
Vec3f a = Vec3f(origin.val[0], origin.val[1] + (i == 0 ? radius : -radius), origin.val[2]);
Vec2f t_a = Vec2f(0, i == 0 ? 1 : 0);
Vec3f n_a = normalize(origin - a);
Vec3f b, c, n_b, n_c;
Vec2f t_b, t_c;
computeCoords(i + 1, j, b, t_b, n_b);
computeCoords(i + 1, j + 1, c, t_c, n_c);
add(std::make_shared<CPrimTriangle>(pShader, a, b, c, t_a, t_b, t_c, n_a, n_b, n_c));
}
Vec3f a, b, c, d, n_a, n_b, n_c, n_d;;
Vec2f t_a, t_b, t_c, t_d;
computeCoords(i, j, a, t_a, n_a);
computeCoords(i + 1, j, b, t_b, n_b);
computeCoords(i, j + 1, c, t_c, n_c);
computeCoords(i + 1, j + 1, d, t_d, n_d);
add(std::make_shared<CSolidQuad>(pShader, a, b, d, c, t_a, t_b, t_d, t_c, n_a, n_b, n_d, n_c));
}
}
}
virtual ~CSolidSphere(void) = default;

private:
// I know we were asked to only add code in areas where it says so but this
// just separates the math from the rest of the logic. I hope that's okay.
// It's also private so it shouldn't affect anything.
Vec3f m_origin;
int m_sides;
float m_radius;

void computeCoords(int i, int j, Vec3f &pt, Vec2f &tex, Vec3f &normal)
{
tex = Vec2f((float)i / ((float)m_sides + 1.0f),(1.0f - (float)(j + 1)) /(float)m_sides);
// Convert to spherical coordinates:
float theta = tex.val[0] * 2.0f * (float)Pi;
float phi = (tex.val[1] - 0.5f) * (float)Pi;

float c = cos(phi);

// Usual formula for a vector in spherical coordinates.
pt = Vec3f(c * cos(theta), sin(phi), c * sin(theta)) * m_radius;
normal = normalize(pt - m_origin);
}
};
56 changes: 45 additions & 11 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,39 @@
#include "LightOmni.h"
#include "timer.h"


Mat RenderConfusedBarney()
{
const Size resolution(1200, 600);
// Define a scene
CScene scene;
auto pCamera = std::make_shared<CCameraPerspective>(resolution, Vec3f(0, 10, 55.0f), Vec3f(0, 0, -1), Vec3f(0, 1, 0), 30);
const std::string barney = "/Users/otmanesabir/Desktop/S5/CG/solutions/eyden-tracer-04/data/barney.obj";
Mat barneyShirt = imread("/Users/otmanesabir/Desktop/S5/CG/solutions/eyden-tracer-04/data/barney.bmp");
if (barneyShirt.empty()) printf("ERROR: Texture file is not found!\n");
auto pTexture = std::make_shared<CTexture>(barneyShirt);
auto barneyShader = std::make_shared<CShaderEyelight>(pTexture);
CSolid mrBarney = CSolid(barneyShader, barney);

scene.add(pCamera);
scene.add(mrBarney);
scene.add(std::make_shared<CLightOmni>(Vec3f(100, 100, 100), Vec3f(0, 30, 0), true));
// Build BSPTree
scene.buildAccelStructure(20, 3);

Mat img(resolution, CV_32FC3); // image array
Ray ray; // primary ray

for (int y = 0; y < img.rows; y++)
for (int x = 0; x < img.cols; x++) {
scene.getActiveCamera()->InitRay(ray, x, y); // initialize ray
img.at<Vec3f>(y, x) = scene.RayTrace(ray);
}

img.convertTo(img, CV_8UC3, 255);
return img;
}

Mat RenderFrame(void)
{
// Camera resolution
Expand All @@ -37,17 +70,18 @@ Mat RenderFrame(void)
#endif

// Texture
Mat earth = imread(dataPath + "1_earth_8k.jpg");
Mat earth = imread("/Users/otmanesabir/Desktop/S5/CG/solutions/eyden-tracer-04/data/1_earth_8k.jpg");
if (earth.empty()) printf("ERROR: Texture file is not found!\n");
auto pTexture = std::make_shared<CTexture>(earth);

// Shaders
auto pShader = std::make_shared<CShaderEyelight>(RGB(0.5f, 1, 0));
auto tShader = std::make_shared<CShaderEyelight>(pTexture);
auto pShader = std::make_shared<CShaderEyelight>(RGB(1, 1, 1));

// Geometry
CSolidCone solid_cone(pShader, Vec3f(10, -4, 0), 4, 8);
CSolidSphere solid_sphere(pShader, Vec3f(0, 0, 0), 4, 36);
auto prim_sphere = std::make_shared<CPrimSphere>(pShader, Vec3f(-10, 0, 0), 4);
CSolidCone solid_cone(tShader, Vec3f(10, -4, 0), 4, 8);
CSolidSphere solid_sphere(tShader, Vec3f(0, 0, 0), 4, 36);
auto prim_sphere = std::make_shared<CPrimSphere>(tShader, Vec3f(-10, 0, 0), 4);

// Add everything to the scene
scene.add(pCamera);
Expand All @@ -73,11 +107,11 @@ Mat RenderFrame(void)

int main(int argc, char* argv[])
{
DirectGraphicalModels::Timer::start("Rendering...");
Mat img = RenderFrame();
DirectGraphicalModels::Timer::stop();
imshow("Image", img);
waitKey();
imwrite("image.jpg", img);
DirectGraphicalModels::Timer::start("Rendering...");
Mat img = RenderFrame();
DirectGraphicalModels::Timer::stop();
imwrite("/Users/otmanesabir/Desktop/S5/CG/solutions/eyden-tracer-04/renders/test_run.jpg", img);
imshow("Image", img);
waitKey();
return 0;
}