Skip to content
This repository was archived by the owner on Dec 12, 2020. It is now read-only.

Commit de352f2

Browse files
adding planes
1 parent 4362bf8 commit de352f2

7 files changed

Lines changed: 218 additions & 8 deletions

File tree

Assignments/Ray-Tracing/Ray Tracing.vcxproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@
169169
<ClCompile Include="src\BuilderUtility.cpp" />
170170
<ClCompile Include="src\Camera.cpp" />
171171
<ClCompile Include="src\Model.cpp" />
172+
<ClCompile Include="src\Plane.cpp" />
172173
<ClCompile Include="src\Sphere.cpp" />
173174
<ClCompile Include="src\Light.cpp" />
174175
<ClCompile Include="src\RayTracing.cpp" />
@@ -180,6 +181,7 @@
180181
<ClInclude Include="src\BuilderUtility.h" />
181182
<ClInclude Include="src\Camera.h" />
182183
<ClInclude Include="src\Model.h" />
184+
<ClInclude Include="src\Plane.h" />
183185
<ClInclude Include="src\Sphere.h" />
184186
<ClInclude Include="src\Light.h" />
185187
<ClInclude Include="src\Scene.h" />

Assignments/Ray-Tracing/Ray Tracing.vcxproj.filters

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,9 @@
5151
<ClCompile Include="src\BuilderUtility.cpp">
5252
<Filter>Source Files</Filter>
5353
</ClCompile>
54+
<ClCompile Include="src\Plane.cpp">
55+
<Filter>Source Files</Filter>
56+
</ClCompile>
5457
</ItemGroup>
5558
<ItemGroup>
5659
<ClInclude Include="src\SceneFile.h">
@@ -77,5 +80,8 @@
7780
<ClInclude Include="src\BuilderUtility.h">
7881
<Filter>Header Files</Filter>
7982
</ClInclude>
83+
<ClInclude Include="src\Plane.h">
84+
<Filter>Header Files</Filter>
85+
</ClInclude>
8086
</ItemGroup>
8187
</Project>
0 Bytes
Binary file not shown.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2017 Chris McArthur, prince.chrismc(at)gmail(dot)com
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
#include "Plane.h"
26+
#include "glm\geometric.hpp"
27+
28+
bool Plane::TestIntersection(const glm::vec3& cam_pos, const glm::vec3& ray_dir, glm::vec3* out_intersection, float* out_distance) const
29+
{
30+
auto normal = glm::normalize(m_Normal);
31+
float denom = glm::dot(normal, ray_dir);
32+
33+
if (abs(denom) > 1e-6)
34+
{
35+
glm::vec3 p0l0 = m_Pos - ray_dir;
36+
37+
*out_distance = glm::dot(p0l0, normal) / denom;
38+
*out_intersection = cam_pos + ray_dir * (*out_distance);
39+
40+
return (*out_distance >= 0);
41+
}
42+
43+
return false;
44+
}
45+
46+
Plane::Builder& Plane::Builder::ParsePlane(const std::string& data)
47+
{
48+
std::string cut = data.substr(2, data.length() - 4);
49+
50+
for (std::string attribute : ParseParams(cut))
51+
{
52+
if (attribute.find("pos:") == 0)
53+
{
54+
m_Pos = ParseVec3(attribute.substr(5));
55+
}
56+
else if (attribute.find("nor:") == 0)
57+
{
58+
m_Normal = ParseVec3(attribute.substr(5));
59+
}
60+
else if (attribute.find("amb:") == 0)
61+
{
62+
m_Amb = ParseVec3(attribute.substr(5));
63+
}
64+
else if (attribute.find("dif:") == 0)
65+
{
66+
m_Dif = ParseVec3(attribute.substr(5));
67+
}
68+
else if (attribute.find("spe:") == 0)
69+
{
70+
m_Spe = ParseVec3(attribute.substr(5));
71+
}
72+
else if (attribute.find("shi:") == 0)
73+
{
74+
m_Shine = ParseDouble(attribute.substr(5));
75+
}
76+
}
77+
78+
return *this;
79+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
/*
2+
MIT License
3+
4+
Copyright (c) 2017 Chris McArthur, prince.chrismc(at)gmail(dot)com
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in all
14+
copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
SOFTWARE.
23+
*/
24+
25+
#pragma once
26+
27+
#include "BuilderUtility.h"
28+
#include "glm\vec3.hpp"
29+
#include <string>
30+
31+
class Plane
32+
{
33+
public:
34+
Plane() = default;
35+
Plane(const glm::vec3& pos, const glm::vec3& nor, const glm::vec3& amb, const glm::vec3& dif, const glm::vec3& spe, const double& shine) :
36+
m_Pos(pos), m_Normal(nor), m_Amb(amb), m_Dif(dif), m_Spe(spe), m_Shine(shine) {}
37+
38+
bool TestIntersection(const glm::vec3& cam_pos, const glm::vec3& ray_dir, glm::vec3* out_intersection, float* out_distance) const;
39+
40+
glm::vec3 GetPosition() const { return m_Pos; }
41+
glm::vec3 GetNormal() const { return m_Normal; }
42+
glm::vec3 GetAmbientlight() const { return m_Amb; }
43+
glm::vec3 GetDiffusion() const { return m_Dif; }
44+
glm::vec3 GetSpecular() const { return m_Spe; }
45+
double GetShine() const { return m_Shine; }
46+
47+
class Builder : private BuilderUtility
48+
{
49+
public:
50+
Builder() = default;
51+
Builder(const Builder&) = delete;
52+
void operator=(const Builder&) = delete;
53+
54+
Builder& ParsePlane(const std::string& data);
55+
Plane GetPlane() { return Plane(m_Pos, m_Normal, m_Amb, m_Dif, m_Spe, m_Shine); }
56+
57+
private:
58+
glm::vec3 m_Pos;
59+
glm::vec3 m_Normal;
60+
glm::vec3 m_Amb;
61+
glm::vec3 m_Dif;
62+
glm::vec3 m_Spe;
63+
double m_Shine;
64+
};
65+
66+
private:
67+
glm::vec3 m_Pos;
68+
glm::vec3 m_Normal;
69+
glm::vec3 m_Amb;
70+
glm::vec3 m_Dif;
71+
glm::vec3 m_Spe;
72+
double m_Shine;
73+
};

Assignments/Ray-Tracing/src/Scene.cpp

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ Scene::Scene(const char* path) : SceneFile(path)
6666
}
6767
}
6868

69+
{
70+
std::string plane_attr = "";
71+
while ((plane_attr = GetAttributes("plane")) != "")
72+
{
73+
m_Planes.push_back(Plane::Builder().ParsePlane(plane_attr).GetPlane());
74+
}
75+
}
76+
6977
GenerateScene();
7078
}
7179
}
@@ -130,7 +138,6 @@ void Scene::GenerateScene()
130138
glm::vec3 lightAddition = light.GetColor() * (target.m_Sphere.GetDiffusion()*ln + (target.m_Sphere.GetSpecular()*rv));
131139
pixelColor += lightAddition;
132140
}
133-
134141
break;
135142
case IntersectingObject::TRIANGLE:
136143
if (IsLightObstructed(&light, &target))
@@ -150,14 +157,36 @@ void Scene::GenerateScene()
150157
float rv = glm::dot(reflection, v);
151158
if (ln < 0) { ln = 0; }
152159
if (rv < 0) { rv = 0; }
153-
rv = pow(rv, target.m_Triangle.GetShine());
160+
rv = std::pow(rv, target.m_Triangle.GetShine());
154161

155162
pixelColor += target.m_Triangle.GetAmbientlight();
156163
glm::vec3 lightAddition = light.GetColor()*(target.m_Triangle.GetDiffusion()*ln + target.m_Triangle.GetSpecular()*rv);
157164
pixelColor += lightAddition;
158165
}
159166
break;
167+
case IntersectingObject::PLANE:
168+
if (IsLightObstructed(&light, &target))
169+
{
170+
pixelColor += target.m_Plane.GetAmbientlight();
171+
}
172+
else
173+
{
174+
glm::vec3 normal = glm::normalize(target.m_Plane.GetNormal());
175+
glm::vec3 v = -rayDirection;
176+
glm::vec3 light_direction = glm::normalize(target.m_Point - light.GetPosition());
177+
glm::vec3 reflection = glm::reflect(light_direction, normal);
178+
float ln = glm::dot(normal, light_direction);
179+
float rv = glm::dot(reflection, v);
180+
if (ln < 0) { ln = 0; }
181+
if (rv < 0) { rv = 0; }
160182

183+
rv = std::pow(rv, target.m_Plane.GetShine());
184+
pixelColor += target.m_Plane.GetAmbientlight();
185+
glm::vec3 lightAddition = light.GetColor()*(target.m_Plane.GetDiffusion()*ln + target.m_Plane.GetSpecular()*rv);
186+
pixelColor += lightAddition;
187+
188+
}
189+
break;
161190
default:
162191
break;
163192
}
@@ -206,6 +235,20 @@ Scene::IntersectingObject Scene::FindNearestIntersectingObject(glm::vec3 ray_dir
206235
}
207236
}
208237

238+
for (Plane plane : m_Planes)
239+
{
240+
float distance;
241+
glm::vec3 intersectpoint;
242+
243+
if (plane.TestIntersection(m_Camera.GetPosition(), ray_dir, &intersectpoint, &distance))
244+
{
245+
if (target.m_ObjType == IntersectingObject::INVALID || distance < target.m_Distance)
246+
{
247+
target = IntersectingObject(intersectpoint, distance, plane);
248+
}
249+
}
250+
}
251+
209252
return target;
210253
}
211254

@@ -227,5 +270,10 @@ bool Scene::IsLightObstructed(Light* light, IntersectingObject* target)
227270
if (triangle.TestIntersection(lightRayWithBias, lightRay, &intersectpoint, &distance)) return true;
228271
}
229272

273+
for (Plane plane : m_Planes)
274+
{
275+
if(plane.TestIntersection(lightRayWithBias, lightRay, &intersectpoint, &distance)) return true;
276+
}
277+
230278
return false;
231279
}

Assignments/Ray-Tracing/src/Scene.h

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ SOFTWARE.
2929
#include "Light.h"
3030
#include "Sphere.h"
3131
#include "Triangle.h"
32-
#include "Model.h"
32+
#include "Plane.h"
3333

3434
#include "CImg.h"
3535

@@ -44,23 +44,25 @@ class Scene : private SceneFile
4444
Camera m_Camera;
4545
std::vector<Light> m_Lights;
4646
std::vector<Sphere> m_Spheres;
47-
std::vector<Model> m_Models;
47+
std::vector<Plane> m_Planes;
4848
std::vector<Triangle> m_Triangles;
4949

5050
cimg_library::CImg<float> m_Img;
5151

5252
struct IntersectingObject
5353
{
54-
enum ObjectType { INVALID, SPHERE, TRIANGLE };
54+
enum ObjectType { INVALID, SPHERE, TRIANGLE, PLANE };
5555

56-
IntersectingObject(glm::vec3 point, float dis, Sphere& sphere) : m_Point(point), m_Distance(dis), m_Sphere(sphere), m_Triangle(), m_ObjType(IntersectingObject::SPHERE) {}
57-
IntersectingObject(glm::vec3 point, float dis, Triangle& triangle) : m_Point(point), m_Distance(dis), m_Sphere(), m_Triangle(triangle), m_ObjType(IntersectingObject::TRIANGLE) {}
58-
IntersectingObject() : m_Point(0.0f), m_Distance(0.0f), m_Sphere(), m_Triangle(), m_ObjType(IntersectingObject::INVALID) {}
56+
IntersectingObject(glm::vec3 point, float dis, Sphere& sphere) : m_Point(point), m_Distance(dis), m_Sphere(sphere), m_ObjType(IntersectingObject::SPHERE) {}
57+
IntersectingObject(glm::vec3 point, float dis, Triangle& triangle) : m_Point(point), m_Distance(dis), m_Triangle(triangle), m_ObjType(IntersectingObject::TRIANGLE) {}
58+
IntersectingObject(glm::vec3 point, float dis, Plane& plane) : m_Point(point), m_Distance(dis), m_Plane(plane), m_ObjType(IntersectingObject::PLANE) {}
59+
IntersectingObject() : m_Point(0.0f), m_Distance(0.0f), m_Sphere(), m_Triangle(), m_Plane(), m_ObjType(IntersectingObject::INVALID) {}
5960

6061
glm::vec3 m_Point;
6162
float m_Distance;
6263
Sphere m_Sphere;
6364
Triangle m_Triangle;
65+
Plane m_Plane;
6466
ObjectType m_ObjType;
6567
};
6668

0 commit comments

Comments
 (0)