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 2 #17

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open

HW 2 #17

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 @@
**Deadline**: 21.10.2021

Please put your name here:
**Name**: .......
**Name**: HaolanZheng
## Problem 1
### Encapsulate camera and primitives from main application logic (Points 5)
1. Fork the current repository
Expand Down
11 changes: 10 additions & 1 deletion src/LightOmni.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,16 @@ class CLightOmni : public ILight
virtual std::optional<Vec3f> illuminate(Ray& ray) override
{
// --- PUT YOUR CODE HERE ---
return std::nullopt;
Vec3f LightIntensity;
//Vec3f DirectionVector;
//DirectionVector = Hitpoint - m_org;
//LightIntensity = m_intensity;
auto Direction = m_org - ray.org;
ray.dir = normalize(Direction);
ray.t = norm(Direction);
LightIntensity = m_intensity / (ray.t * ray.t);
return LightIntensity;
//return std::nullopt;
}


Expand Down
5 changes: 4 additions & 1 deletion src/PrimPlane.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,16 @@ class CPrimPlane : public IPrim

ray.t = dist;
// --- PUT YOUR CODE HERE ---
ray.hit = shared_from_this();
//ray.hit -> getShader() -> shade(ray);
return true;
}

virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
return m_normal;
//return Vec3f();
}

private:
Expand Down
9 changes: 8 additions & 1 deletion src/PrimSphere.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,20 @@ class CPrimSphere : public IPrim

ray.t = dist;
// --- PUT YOUR CODE HERE ---
ray.hit = shared_from_this();

return true;
}

virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
//assuming it is hit already so we should have ray.hit
if (ray.hit) {
Vec3f HitPoint = ray.dir* ray.t + ray.org;
return normalize(HitPoint - m_origin);
}
return Vec3f(0, 0, 0);
}

private:
Expand Down
6 changes: 5 additions & 1 deletion src/PrimTriangle.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,17 @@ class CPrimTriangle : public IPrim

ray.t = f;
// --- PUT YOUR CODE HERE ---
//update ray hit as this primative
ray.hit = shared_from_this();

return true;
}

virtual Vec3f getNormal(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
return normalize(m_edge1.cross(m_edge2));
//return Vec3f();
}

private:
Expand Down
26 changes: 24 additions & 2 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,8 @@ class CScene
void add(const ptr_camera_t pCamera)
{
// --- PUT YOUR CODE HERE ---
m_vpCameras.push_back(pCamera);
m_activeCamera = m_vpCameras.size() - 1;
}
/**
* @brief Returns the container with all scene light source objects
Expand All @@ -68,6 +72,11 @@ class CScene
bool intersect(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
for (auto Prim : m_vpPrims) {
if (Prim -> intersect(ray)) {
return true;
}
}
return false;
}

Expand All @@ -77,7 +86,15 @@ class CScene
bool occluded(Ray& ray)
{
// --- PUT YOUR CODE HERE ---
return false;
// for each primitives if no ray reach then return false
bool hit = false;
for (auto EachPri : m_vpPrims) {
/*if (EachPri -> intersect(ray)) {
return true;
}*/
hit |= EachPri->intersect(ray);
}
return hit;
}

/**
Expand All @@ -87,7 +104,12 @@ class CScene
Vec3f RayTrace(Ray& ray) const
{
// --- PUT YOUR CODE HERE ---
return Vec3f();
if (intersect(ray)) {
if (ray.hit->getShader()) {
return ray.hit->getShader()->shade(ray);
}
}
return m_bgColor;
}


Expand Down
12 changes: 11 additions & 1 deletion src/ShaderEyelight.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,17 @@ class CShaderEyelight : public CShaderFlat
virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return RGB(0, 0, 0);
//shader needs to know information about primitive normal
const auto PrimNormal = ray.hit->getNormal(ray);
auto DotResult = PrimNormal.dot(ray.dir);

//getting the angle between two vec3f by dot product
auto PrimNormal2 = PrimNormal.dot(PrimNormal);
auto RayDir2 = ray.dir.dot(ray.dir);
double Angle = acos(DotResult / (PrimNormal2 * RayDir2));
auto result = -cos(Angle) * CShaderFlat::shade();
return result;
//return RGB(0, 0, 0);
}
};

5 changes: 3 additions & 2 deletions src/ShaderFlat.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@ 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;
//return RGB(0, 0, 0);
}
Vec3f GetColor() { return m_color; };

private:
Vec3f m_color;
};
59 changes: 56 additions & 3 deletions src/ShaderPhong.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include "ShaderFlat.h"

#include "LightOmni.h"
class CShaderPhong : public CShaderFlat
{
public:
Expand All @@ -27,9 +27,62 @@ class CShaderPhong : public CShaderFlat
virtual Vec3f shade(const Ray& ray) const override
{
// --- PUT YOUR CODE HERE ---
return RGB(0, 0, 0);
}
Vec3f Color = m_color;
Vec3f AmbientColor = RGB(0, 0, 0);
//Vec3f DiffuseColor;
Vec3f SpecularColor = RGB(1, 1, 1);

Vec3f AmibentRadiance;
Vec3f RadianceArriving;

Vec3f SurfaceNormal = ray.hit->getNormal(ray);
if (SurfaceNormal.dot(ray.dir) > 0) {
SurfaceNormal *= -1;
}
//getting each light sources
auto LightSources = m_scene.getLights();

//calucalte ambient diffuse specular beforehand
Vec3f ambient = m_ka * Color;
Vec3f SumDiffuse;
Vec3f SumSpecular;

for (auto EachLight : LightSources) {

Ray TempRay, ReflectedRay;
TempRay.org = ray.org + ray.dir * ray.t;
TempRay.hit = ray.hit;
ReflectedRay = ray;
ReflectedRay.dir = ray.dir - ray.dir.dot(SurfaceNormal) * SurfaceNormal * 2;
//if occuluded then skip
//if (!m_scene.occluded(TempRay)) {
//get each light radiance
Vec3f EachRadiance = EachLight->illuminate(TempRay).value();
//for diffuse
auto CosTheta = SurfaceNormal.dot(TempRay.dir);
if (CosTheta > 0) {
SumDiffuse += EachRadiance * CosTheta;
}
//for specular
//calculating Angle
double DotResult = TempRay.dir.dot(normalize(ReflectedRay.dir));
if (DotResult > 0) {
DotResult = pow(DotResult, m_ke);
SumSpecular += EachRadiance * DotResult;
}
//}

}
SumDiffuse = SumDiffuse.mul(m_color) * m_kd;
SumSpecular = SumSpecular.mul(SpecularColor) * m_ks;
Vec3f FinalResult;
FinalResult += ambient;
//auto FinalResult = ambient + SumSpecular;
//auto FinalResult = SumSpecular;
FinalResult += SumDiffuse;
FinalResult += SumSpecular;
return FinalResult;
}

private:
CScene& m_scene;
Expand Down
46 changes: 23 additions & 23 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,32 @@ Mat RenderFrame(void)
// Define a scene
CScene scene;

// Flat surface shaders
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
//Flat surface shaders
//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

// 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
//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

// 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
//// 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

// Mirror shader
auto shdM = std::make_shared<CShaderMirror>(scene);
//auto shdM = std::make_shared<CShaderMirror>(scene);

// Add camera to scene
scene.add(std::make_shared<CCameraPerspective>(Size(800, 600), Vec3f(0,0,8), Vec3f(0,0,-1), Vec3f(0,1,0), 60));
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("shader phone with shading.jpg", img);
return 0;
}
2 changes: 1 addition & 1 deletion src/ray.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ struct Ray
Vec3f org; ///< Origin
Vec3f dir; ///< Direction
double t = std::numeric_limits<double>::infinity(); ///< Current/maximum hit distance
std::shared_ptr<const IPrim> hit = nullptr; ///< Pointer to currently closest primitive
std::shared_ptr<const IPrim> hit = nullptr; ///< Pointer to currently closest primitive
};
Binary file added src/renderPicture/shader eyelight.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 src/renderPicture/shader flat.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 src/renderPicture/shader phone with shading.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 src/renderPicture/shader phong.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.