-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathminirt.c
More file actions
60 lines (56 loc) · 1.48 KB
/
minirt.c
File metadata and controls
60 lines (56 loc) · 1.48 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
#include <mlx.h>
#include "minirt.h"
void raytrace(t_res res)
{
t_ray ray;
int i;
int j;
j = 0;
i = 0;
while (i < res.x)
{
while (j < res.y)
{
compute_ray(i , j, &ray);
j++;
}
i++;
}
}
for (int j = 0; j < imageHeight; ++j) {
for (int i = 0; i < imageWidth; ++i) {
// compute primary ray direction
Ray primRay;
computePrimRay(i, j, &primRay);
// shoot prim ray in the scene and search for intersection
Point pHit;
Normal nHit;
float minDist = INFINITY;
Object object = NULL;
for (int k = 0; k < objects.size(); ++k) {
if (Intersect(objects[k], primRay, &pHit, &nHit)) {
float distance = Distance(eyePosition, pHit);
if (distance < minDistance) {
object = objects[k];
minDistance = distance; // update min distance
}
}
}
if (object != NULL) {
// compute illumination
Ray shadowRay;
shadowRay.direction = lightPosition - pHit;
bool isShadow = false;
for (int k = 0; k < objects.size(); ++k) {
if (Intersect(objects[k], shadowRay)) {
isInShadow = true;
break;
}
}
}
if (!isInShadow)
pixels[i][j] = object->color * light.brightness;
else
pixels[i][j] = 0;
}
}