-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraytrace.c
186 lines (170 loc) · 5.04 KB
/
raytrace.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
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
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* raytrace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: mfilipch <[email protected]> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2016/11/05 00:26:57 by mfilipch #+# #+# */
/* Updated: 2016/11/05 00:26:59 by mfilipch ### ########.fr */
/* */
/* ************************************************************************** */
#include "raytracer.h"
#include <stdio.h>
/*
** for each shape check if intersects path of light, return 0 color if any does
** depending on the distance to the light calcualte light intensity
** time measurement:
** #include <time.h>
** #include <sys/time.h>
*/
t_3d cast_shadray(t_data *d, t_3d p1, t_lght lght, int depth)
{
int i;
double ratio;
t_3d p;
i = -1;
while (++i < d->nshp)
{
p = intersect(d, p1, lght.o, i);
if (v_dscal(v_dd2v(p1, p), v_dd2v(p1, lght.o)) > 0.1 &&
v_dmodsq(v_dd2v(p1, p)) <= v_dmodsq(v_dd2v(p1, lght.o)))
{
if (depth == 1)
return (v_dsop(lght.spctr, 0, '='));
}
}
if (v_dmodsq(v_dd2v(p1, lght.o)) > 0 && (ratio = (lght.l * lght.l)
/ v_dmodsq(v_dd2v(p1, lght.o))))
{
return (v_dsop(lght.spctr, (ratio * lght.in), '*'));
}
return (lght.spctr);
}
/*
** For each shape check if in the way of the ray, pick the closest one
** Return the intersection point and shape number
*/
t_3d cast_primray(t_data *d, t_3d p2, int *n)
{
int i;
double min_dist;
double dist;
t_3d p;
t_3d pi;
i = -1;
*n = -1;
pi = d->pos;
min_dist = d->max_dist;
while (++i < d->nshp)
{
p = (intersect(d, d->pos, (p2), i));
if (v_dmodsq(v_dd2v(d->pos, p)) > 0.1)
{
if ((dist = sqrt(v_dmodsq(v_dd2v(d->pos, p)))) < min_dist)
{
min_dist = dist;
*n = i;
pi = p;
}
}
}
return (pi);
}
/*
** Trace primary ray to the first object
** If intersected, see if there's any objects in between for each light
** Add gloss effect
** Mix clolors: color after shape absorption + gloss color
*/
t_3di trace_one(t_data *d, t_3d p, int i, int n)
{
t_3d col;
t_3d colinc;
t_3d colgl;
double ratio;
t_3d norm;
col = v_dsop(p, 0, '=');
p = cast_primray(d, v_dvop(d->vwp, p, '+'), &n);
if (n >= 0)
while (++i < d->nlght)
{
colgl = v_dsop(colgl, 0, '=');
colinc = cast_shadray(d, (p), d->lght[i], 1);
norm = get_normal(d->shps[n], p);
ratio = v_dscal(norm, v_dd2v(p, d->lght[i].o))\
/ v_dmod(v_dd2v(p, d->lght[i].o)) / v_dmod(norm);
colinc = v_dsop(colinc, ratio, '*');
if (d->shps[n].n > 1.01 && v_dmodsq(colinc) > 1 && i > 0)
colgl = add_gloss(v_dd2v(p, d->pos),
p, d->lght[i], d->shps[n]);
col = v_dvop(col, v_dsop(colgl, ratio, '*'), '+');
col.x = col.x + colinc.x * (1.0 - d->shps[n].mu.x);
col.x = (col.x > 255) ? 255 : col.x;
col.y = col.y + colinc.y * (1.0 - d->shps[n].mu.y);
col.y = (col.y > 255) ? 255 : col.y;
col.z = col.z + colinc.z * (1.0 - d->shps[n].mu.z);
col.z = (col.z > 255) ? 255 : col.z;
}
return (v_d2i(col));
}
/*
** For each thread in THRD_N call functiom to trace ray
** time measurement:
** struct timeval start,finish;
** double elapsed;
** gettimeofday(&start, NULL);
** gettimeofday(&finish, NULL);
** elapsed = (finish.tv_sec - start.tv_sec);
** elapsed += (finish.tv_usec - start.tv_usec) / 1000000.0;
** printf("THRD: %d TIME: %.2f\n", i, elapsed);
*/
void *trace_thread(void *dt)
{
t_3di p1;
t_3di col;
pthread_t id;
t_data *d;
int i;
d = (t_data*)dt;
i = -1;
id = pthread_self();
while (++i < THRD_N)
if (pthread_equal(id, d->pth[i]))
break ;
p1.y = (i * YS) / THRD_N - 1;
while (++p1.y < ((i + 1) * YS) / THRD_N && (p1.x = -1))
while (++p1.x < XS)
{
col = trace_one(d, (v_dvop(v_dsop(d->ox, (p1.x - XS / 2), '*')
, v_dsop(d->oy, (p1.y - YS / 2), '*'), '+')), -1, -1);
draw_pixel(d, p1.x, p1.y, (col.x << 16) + (col.y << 8) + col.z);
}
pthread_exit(NULL);
}
/*
** Split work into THRD_N threads and call function to trace for each thread
** Join threads after complition before continue
** time measurement:
** struct timeval start,finish;
** double elapsed;
** gettimeofday(&start, NULL);
** printf("\n");
** gettimeofday(&finish, NULL);
** elapsed = (finish.tv_sec - start.tv_sec);
** elapsed += (finish.tv_usec - start.tv_usec) / 1000000.0;
** printf("THRD: %d TIME: %.2f\n", i, elapsed);
*/
void raytrace(t_data *d)
{
int i;
(d && d->img_p) ? mlx_destroy_image(d->mlx, d->img_p) : 0;
d->img_p = mlx_new_image(d->mlx, XS, YS);
d->img_p0 = mlx_get_data_addr(d->img_p, &(d->bpp), &(d->ls), &(d->endian));
i = -1;
while (++i < THRD_N)
pthread_create(&(d->pth[i]), NULL, trace_thread, (void *)d);
i = -1;
while (++i < THRD_N)
pthread_join(d->pth[i], NULL);
}