-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRayTracer.cpp
More file actions
303 lines (270 loc) · 7.82 KB
/
RayTracer.cpp
File metadata and controls
303 lines (270 loc) · 7.82 KB
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
#include <iostream>
#include <fstream>
#include <string>
#include <glm/glm.hpp>
#include <random>
#include <chrono>
#include <limits>
class Material;
struct HitRecord{
float t;
glm::vec3 p;
glm::vec3 normal;
Material * material;
};
struct Ray{
Ray(){}
Ray( glm::vec3 const a, glm::vec3 const b) :
Origin(a), Direction(b)
{}
glm::vec3
getPointAtParameter(float t){
return Origin + t*Direction;
}
glm::vec3 Origin, Direction;
};
class Hitable{
public:
virtual bool
hit( Ray & r, float t_min, float t_max, HitRecord & ref) = 0;
};
class Material{
public:
virtual bool
scatter( Ray rIn, HitRecord const & rec, glm::vec3 & attenuation, Ray & scattered) = 0;
};
glm::vec3 randomInUnitSphere(){
static std::random_device rd;
static std::mt19937 mt(rd());
static std::uniform_real_distribution<float> dist(0.0f, 1.0f);
glm::vec3 p(2.0f);
while( (glm::dot(p,p)) >= 1.0f ){
p = 2.0f * glm::vec3(dist(mt),dist(mt),dist(mt)) - glm::vec3(1.0f);
}
return p;
}
class Lambertian : public Material {
public:
Lambertian( glm::vec3 b) : m_Blubb(b)
{}
bool
scatter(Ray rIn, HitRecord const & rec, glm::vec3 &attenuation, Ray &scattered){
auto target = rec.p + rec.normal + randomInUnitSphere();
scattered = Ray(rec.p, target-rec.p);
attenuation = m_Blubb;
return true;
}
glm::vec3 m_Blubb;
};
glm::vec3
reflect( glm::vec3 & v, glm::vec3 & n) {
return (v - 2.0f * glm::dot(v,n)*n);
}
bool
refract(glm::vec3 const & v, glm::vec3 const & n, float niOverNt, glm::vec3 & refracted )
{
glm::vec3 uv = glm::normalize(v);
float dt = glm::dot(uv, n);
float discriminant = 1.0f - niOverNt * niOverNt * (1.0f -dt * dt); // Wat?
if (discriminant>0.0f) {
refracted = niOverNt * ( uv - n*dt ) - n *std::sqrt(discriminant);
return true;
}
else {
return false;
}
}
float schlick( float cos, float ref_idx ) {
float r0 = ( 1.0f - ref_idx) / (1.0f + ref_idx );
r0 = r0*r0;
return r0 + (1.0f-r0) * pow((1-cos),5);
}
class Dielectric : public Material {
public:
Dielectric( float ri ) : ref_idx( ri )
{}
virtual bool
scatter(Ray rIn, const HitRecord &rec, glm::vec3 &attenuation, Ray &scattered) {
glm::vec3 outward_normal;
glm::vec3 reflected = glm::reflect( rIn.Direction, rec.normal );
float nOverNt;
attenuation = glm::vec3(1.0f, 1.0f, 1.0f);
glm::vec3 refracted;
if ( glm::dot(rIn.Direction, rec.normal) > 0 ) {
outward_normal = -rec.normal;
nOverNt = ref_idx;
}
else {
outward_normal = rec.normal;
nOverNt = 1.0f/ref_idx;
}
if (refract( rIn.Direction, outward_normal, nOverNt, refracted)) {
scattered = Ray(rec.p, refracted);
}
else {
scattered = Ray(rec.p, reflected);
return false;
}
return true;
}
float ref_idx;
};
class Metal : public Material {
public:
Metal(glm::vec3 b ) : m_Blubb(b)
{}
bool
scatter(Ray rIn, HitRecord const & rec, glm::vec3 &attenuation, Ray &scattered){
glm::vec3 reflected = -glm::reflect(rIn.Direction, rec.normal);
scattered = Ray(rec.p, reflected);
attenuation = m_Blubb;
return (glm::dot(scattered.Direction, rec.normal)>0);
}
glm::vec3 m_Blubb;
};
class Sphere : public Hitable {
public:
Sphere(){}
Sphere(glm::vec3 const & center, float radius, Material * m)
: m_Center(center), m_Radius(radius), m_MaterialPtr(m)
{}
virtual bool
hit( Ray & r, float t_min, float t_max, HitRecord & ref);
glm::vec3 m_Center;
float m_Radius;
Material * m_MaterialPtr;
};
bool
Sphere::hit( Ray &r, float t_min, float t_max, HitRecord &ref)
{
auto cc = r.Origin - m_Center;
//Reminder ax² + 2bx +c = 0 Mitternachtsformel^^
float a = glm::dot(r.Direction, r.Direction);
float b = glm::dot(cc,r.Direction);
float c = glm::dot(cc,cc) - m_Radius*m_Radius;
float diskriminante = b*b - a* c;
if (diskriminante > 0.0f){
float temp = (-b + sqrt(diskriminante)/ a);
if(temp > t_min && temp < t_max){
ref.t = temp;
ref.p = r.getPointAtParameter(ref.t);
ref.normal = (ref.p - m_Center)/m_Radius;
ref.material = m_MaterialPtr;
return true;
}
temp = (-b - sqrt(diskriminante)/ a);
if(temp > t_min && temp < t_max){
ref.t = temp;
ref.p = r.getPointAtParameter(ref.t);
ref.normal = (ref.p - m_Center)/m_Radius;
ref.material = m_MaterialPtr;
return true;
}
}
return false;
}
class HitableList : public Hitable{
public:
HitableList(){}
HitableList( Hitable **l, int n)
: m_List(l), m_Size(n)
{}
virtual bool
hit( Ray & r, float t_min, float t_max, HitRecord & ref);
Hitable ** m_List;
int m_Size;
};
bool
HitableList::hit(Ray &r, float t_min, float t_max, HitRecord &ref){
HitRecord tempRec;
bool hitAnything = false;
float closest = t_max;
for ( int i = 0; i<m_Size; ++i) {
if(m_List[i]->hit( r, t_min, closest, tempRec)) {
hitAnything = true;
closest = tempRec.t;
ref = tempRec;
}
}
return hitAnything;
}
glm::vec3
colorFunc(Ray & ray, Hitable * world, int depth){
HitRecord hitRec;
if( world != nullptr && world->hit(ray, 0.001f,std::numeric_limits<float>::max(), hitRec )) {
Ray scattered;
glm::vec3 attenuation;
if( depth < 50 && hitRec.material->scatter( ray, hitRec, attenuation, scattered)) {
return (attenuation * colorFunc(scattered, world, depth+1));
}
else {
return glm::vec3(0.0f);
}
}
else{
glm::vec3 normRay = glm::normalize(ray.Direction);
auto t = 0.5f * (normRay.y + 1.0f);
return (1.0f-t)*glm::vec3(1.0f) + t * glm::vec3(0.5f, 0.7f, 1.0f);
}
}
class Camera {
public:
Camera(glm::vec3 blc, glm::vec3 hor, glm::vec3 ver, glm::vec3 orig)
: m_BlCorner(blc),
m_Horizontal(hor),
m_Vertical(ver),
m_Origin(orig)
{}
Ray getRay( float u, float v) {
return Ray(m_Origin, glm::normalize(m_BlCorner + u*m_Horizontal+v*m_Vertical));
}
glm::vec3 m_BlCorner;
glm::vec3 m_Horizontal;
glm::vec3 m_Vertical;
glm::vec3 m_Origin;
};
int main() {
std::string fileName = "rayImage.ppm";
std::ofstream out(fileName, std::ios::out);
int x = 200;
int y = 100;
float s = 200.0f;
glm::vec3 blCorner(-2.0f, -1.0f, -1.0f);
glm::vec3 horizontal(4.0f, 0.0f, 0.0f);
glm::vec3 vertical(0.0f,2.0f,0.0f);
glm::vec3 origin(0.0f,0.0f,0.0f);
Camera cam(blCorner, horizontal, vertical, origin);
out << "P3\n" << x << " " << y << "\n255\n";
Hitable *list[4];
glm::vec3 center1(0.0f, 0.0f, -1.0f);
glm::vec3 center2(0.0f, -100.5f, -1.0f);
glm::vec3 center3(-1.1f, 0.1f, -1.0f);
glm::vec3 center4(1.1f, 0.2f, -1.3f);
list[0] = new Sphere(center1,0.5f, new Lambertian( glm::vec3(0.8f,0.3f,0.3f)));
list[1] = new Sphere(center2,100.0f, new Lambertian(glm::vec3(0.8f, 0.8f, 0.0f)));
list[2] = new Sphere(center3,0.4f, new Metal(glm::vec3(0.8f, 0.6f, 0.8f)));
list[3] = new Sphere(center4,0.4f, new Dielectric(1.3f));
Hitable * world = new HitableList(list, 4);
std::random_device rd;
std::mt19937 mt(rd());
std::uniform_real_distribution<float> dist(0.0f, 1.0f);
for ( int j = y-1; j >= 0; --j){
std::cout << "Status: " << j << "\n";
for (int i = 0; i < x; ++i){
glm::vec3 col(0.0f, 0.0f, 0.0f);
for ( int k = 0; k < s; k++) {
float u = static_cast<float>((i+dist(mt))/x);
float v = static_cast<float>((j+dist(mt))/y);
auto ray = cam.getRay(u,v);
col += colorFunc(ray,world,0);
}
col = col/s;
col = glm::vec3( sqrt(col[0]), sqrt(col[1]), sqrt(col[2]));
unsigned int ir = static_cast<unsigned int>(255.99f * col[0]);
unsigned int ig = static_cast<unsigned int>(255.99f * col[1]);
unsigned int ib = static_cast<unsigned int>(255.99f * col[2]);
out << ir << " " << ig << " " << ib << "\n";
}
}
out.close();
}