-
Notifications
You must be signed in to change notification settings - Fork 0
/
raymarching.c
105 lines (96 loc) · 2.65 KB
/
raymarching.c
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raymarching.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: jserrano <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2020/10/01 09:38:55 by jserrano #+# #+# */
/* Updated: 2020/11/04 17:30:07 by jserrano ### ########.fr */
/* */
/* ************************************************************************** */
#include "raymarching.h"
void gen_ray(t_data *param, int x, int y, int boolean)
{
int i;
i = -1;
while (++i < 3)
param->cam.ray.o[i] = param->cam.p[i] - (param->scr.x / 2 - x) *
param->cam.vx[i] + (param->scr.y / 2 - y) * param->cam.vy[i];
i = -1;
while (++i < 3)
param->cam.ray.v_o[i] = param->cam.ray.o[i] - param->cam.o[i];
if (boolean)
param->cam.ray.mod = mod(param->cam.ray.v_o);
i = -1;
while (++i < 3)
param->cam.ray.v_o[i] /= param->cam.ray.mod;
}
static int is_bounce(t_data *param, int i)
{
double dist;
double tray;
int j;
dist = 2;
tray = 0;
while (dist > 0.001)
{
j = -1;
while (++j < 3)
param->cam.ray.o[j] += param->cam.ray.v_l[j] * dist;
tray += dist;
if (tray > param->cam.ray.mod)
{
j = -1;
while (++j < 3)
param->cam.ray.ray_rgb_l[j] += param->l[i]->rgb[j] /
pow(param->cam.ray.mod / 2000, 2);
return (1);
}
dist = obj_dist(param);
}
return (0);
}
int bounce_ray(t_data *param, int i)
{
double dist;
double tray;
int j;
j = -1;
while (++j < 3)
{
param->cam.ray.o[j] = param->cam.ray.o_prev[j];
param->cam.ray.v_l[j] = param->l[i]->o[j] - param->cam.ray.o[j];
}
param->cam.ray.mod = mod(param->cam.ray.v_l);
j = -1;
while (++j < 3)
param->cam.ray.v_l[j] /= param->cam.ray.mod;
return (is_bounce(param, i));
}
int is_hit(t_data *param)
{
double dist;
double tray;
int j;
dist = 2;
tray = 0;
j = -1;
while (++j < 3)
param->cam.ray.ray_rgb_l[j] = 0;
while (dist > 0.0001)
{
tray += dist;
if (tray > 214747)
return (0);
j = -1;
while (++j < 3)
param->cam.ray.o[j] += param->cam.ray.v_o[j] * dist;
dist = obj_dist(param);
}
hex_to_rgb(param->cam.ray.obj_c, param->cam.ray.ray_rgb_o);
j = -1;
while (++j < 3)
param->cam.ray.o_prev[j] = param->cam.ray.o[j];
return (1);
}