-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSimpleRayTracer.cpp
112 lines (85 loc) · 3.68 KB
/
SimpleRayTracer.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
//
// Created by prof on 12.03.18.
//
#include <cfloat>
#include <cmath>
#include "SimpleRayTracer.h"
#include "Camera.h"
SimpleRayTracer::SimpleRayTracer(unsigned int m_MaxDepth):m_MaxDepth(m_MaxDepth) {}
void SimpleRayTracer::traceScene(const Scene &SceneModel, RGBImage &Image) {
Camera c = Camera(-8, 1, 1, 0.75, 640, 480);
for(unsigned int x = 0; x < Image.width(); ++x){
for (unsigned int y = 0; y < Image.height(); ++y) {
Image.setPixelColor(x,y, Color());
Image.setPixelColor(x,y, trace(SceneModel, c.position(), c.generateRay(x,y), 2));
}
}
}
Color SimpleRayTracer::trace(const Scene &SceneModel, const Vector &o, const Vector &d, int depth) {
if(depth<=0) {
return {};
}
float nearest_intersect = FLT_MAX;
float s = FLT_MAX;
Triangle temp_triangle;
Color temp_color = Color();
Vector intersect_point;
unsigned int temp_i = 0;
for (unsigned int i = 0; i < SceneModel.getTriangleCount(); ++i){
temp_triangle = SceneModel.getTriangle(i);
if(o.triangleIntersection(d, temp_triangle.A, temp_triangle.B, temp_triangle.C, s)){
if(s < nearest_intersect) {
nearest_intersect = s;
temp_i = i;
}
}
}
if(temp_i == -1)
return temp_color;
intersect_point = o + d * nearest_intersect;
temp_triangle = SceneModel.getTriangle(temp_i);
for (unsigned int l = 0; l < SceneModel.getLightCount() && (s != FLT_MAX); ++l) {
bool visible = true;
Vector light_position = SceneModel.getLight(l).Position;
Vector between_pos_light = light_position - intersect_point;
float distance = between_pos_light.length();
between_pos_light.normalize();
for (unsigned int i = 0; i < SceneModel.getTriangleCount() && visible; ++i) {
Triangle angle_iter = SceneModel.getTriangle(i);
if(intersect_point.triangleIntersection(between_pos_light, angle_iter.A, angle_iter.B, angle_iter.C, s)){
if(s < distance && s > 0.f + 1e-2)
visible = false;
}
}
if(visible){
temp_color += localIllumination(
intersect_point,
o,
temp_triangle.calcNormal(intersect_point),
SceneModel.getLight(l),
*temp_triangle.pMtrl);
}
}
if(temp_triangle.pMtrl->getReflectivity(intersect_point) == 0){
return temp_color;
}
Vector reflection_vector = d.reflection(temp_triangle.calcNormal(intersect_point));
temp_color += trace(SceneModel,
intersect_point,
reflection_vector.normalize(),
--depth)
* temp_triangle.pMtrl->getReflectivity(intersect_point);
//Maybe add transmission here
if(temp_color.R == 0, temp_color.G == 0, temp_color.B == 0){}
return temp_color;
}
Color SimpleRayTracer::localIllumination(const Vector &SurfacePoint, const Vector &Eye, const Vector &Normal,
const PointLight &Light, const Material &Material) {
Vector L = Light.Position - SurfacePoint;
Color diffuseComp = Light.Intensity * Material.getDiffuseCoeff(SurfacePoint) * fmax(0, Normal.dot(L) / (Normal.length() * L.length()));
Vector E = Eye - SurfacePoint;
Vector R = (-L).reflection(Normal);
Color specularComp = Light.Intensity * Material.getSpecularCoeff(SurfacePoint) * powf(fmax(0, E.dot(R) / (E.length() * R.length())), Material.getSpecularExp(SurfacePoint));
Color ambientComp = Material.getAmbientCoeff(SurfacePoint);
return diffuseComp + specularComp;
}