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

Hw Submission #12

Open
wants to merge 9 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**: 12.11.2020

Please put your name here:
**Name:** .......
**Name:** Jackson Whiteley
## Problem 1
### Sphere Solid (Points 25)
In this assignment we will continue working with _compound objects_: solids.
Expand Down
Binary file added renders/p1.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/p2.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/p34.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion src/LightOmni.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "ILight.h"
#include "Ray.h"
#include "ray.h"

/**
* @brief Point light source class
Expand Down
5 changes: 3 additions & 2 deletions src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ class CPrimSphere : public IPrim

virtual Vec2f getTextureCoords(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec2f(0, 0);
Vec3f intersect = (ray.org + ray.t*ray.dir) - m_origin;
return Vec2f( atan2f(intersect[0], intersect[2])/(2*Pif),
acosf(MIN(intersect[1], m_radius)/m_radius)/Pif);
}

virtual CBoundingBox getBoundingBox(void) const override
Expand Down
9 changes: 4 additions & 5 deletions src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,24 @@ 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);
return normalize((1-ray.u-ray.v)*m_na.value() + ray.u*m_nb.value() + ray.v*m_nc.value());
}
else
return normalize(m_edge1.cross(m_edge2));
}

virtual Vec2f getTextureCoords(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec2f(0, 0);
return (1-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
28 changes: 21 additions & 7 deletions src/SolidCone.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,25 +24,39 @@ class CSolidCone : public CSolid {
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
Vec3f n0 = 0.5 * (normalize(p0 - (origin + top)) + normalize(p0 - origin));
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;

// --- PUT YOUR CODE HERE ---
n1 = 0.5 * (normalize(p1 - (origin + top)) + normalize(p1 - origin));

// 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));
if (height >= 0)
add(std::make_shared<CPrimTriangle>(pShader, origin + top, p1, p0,
Vec2f(0, 0), Vec2f(t1, 1), Vec2f(t0, 1),
Vec3f(0, 1, 0), n1, n0));
else
add(std::make_shared<CPrimTriangle>(pShader, origin + top, p0, p1,
Vec2f(0, 0), Vec2f(t0, 1), Vec2f(t1, 1),
Vec3f(0, 1, 0), n0, n1));

// 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, 0), Vec2f(t1, 1), Vec2f(t0, 1),
Vec3f(0, -1, 0), n1, n0));
else
add(std::make_shared<CPrimTriangle>(pShader, origin, p0, p1,
Vec2f(0, 0), Vec2f(t0, 1), Vec2f(t1, 1),
Vec3f(0, -1, 0), n0, n1));

dir0 = dir1;
p0 = p1;
n0 = n1;
t0 = t1;
}
}
Expand Down
59 changes: 58 additions & 1 deletion src/SolidSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,64 @@ 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 ---
const Vec3f top = origin + Vec3f(0, radius, 0); //the top point
const Vec3f bottom = origin - Vec3f(0, radius, 0); //the bottom point
int sliceHeight = static_cast<int>(sides)/2 - 1;

//initial slices points and normals
Vec3f dir;
Vec3f p0[sliceHeight];
Vec3f n0[sliceHeight];
Vec2f t0[sliceHeight];
for (int i = 0; i < sliceHeight; i++) { //rotate over pi, skip end points
float phi = -1 * Pif * (static_cast<float>(i+1) / (sliceHeight+1));
dir = Vec3f(sinf(phi), cosf(phi), 0);

p0[i] = origin + radius*dir;
n0[i] = (p0[i] - origin)/radius;
t0[i] = Vec2f(0, static_cast<float>(i+1)/(sides/2));
}

//loop for each vertical slice
Vec3f p1[sliceHeight]; //next slices points
Vec3f n1[sliceHeight];
Vec2f t1[sliceHeight];
for (size_t s = 0; s < sides; s++) {

//next slices points and normals
float theta = -2 * Pif * static_cast<float>(s + 1) / sides;
for (int i = 0; i < sliceHeight; i++) {
float phi = -1 * Pif * (static_cast<float>(i+1) / (sides/2));
dir = Vec3f(sinf(phi)*cosf(theta), cosf(phi), sinf(phi)*sinf(theta));

p1[i] = origin + radius*dir;
n1[i] = (p1[i] - origin)/radius;
t1[i] = Vec2f( static_cast<float>(s+1)/sides,
static_cast<float>(i+1)/(sides/2));
}

//top cap
add(std::make_shared<CPrimTriangle>(pShader, top, p1[0], p0[0],
Vec2f(0, 0), t1[0], t0[0], Vec3f(0, -1, 0), n1[0], n0[0]));

//vertical slice of sides
for (int i = 0; i < sliceHeight-1; i++) {
add(std::make_shared<CSolidQuad>(pShader, p0[i], p0[i+1], p1[i+1], p1[i],
t0[i], t0[i+1], t1[i+1], t1[i], n0[i], n0[i+1], n1[i+1], n1[i]));
}

//bottom cap
add(std::make_shared<CPrimTriangle>(pShader, bottom, p1[sliceHeight-1], p0[sliceHeight-1],
Vec2f(0, 1), t1[sliceHeight-1], t0[sliceHeight-1],
Vec3f(0, 1, 0), n1[sliceHeight-1], n0[sliceHeight-1]));

//transfer points and normals
for (int i = 0; i < sliceHeight; i++) {
p0[i] = p1[i];
n0[i] = n1[i];
t0[i] = t1[i];
}
}
}
virtual ~CSolidSphere(void) = default;
};
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Mat RenderFrame(void)
#ifdef WIN32
const std::string dataPath = "../data/";
#else
const std::string dataPath = "../../data/";
const std::string dataPath = "../data/";
#endif

// Texture
Expand All @@ -42,7 +42,7 @@ Mat RenderFrame(void)
auto pTexture = std::make_shared<CTexture>(earth);

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

// Geometry
CSolidCone solid_cone(pShader, Vec3f(10, -4, 0), 4, 8);
Expand Down