-
Notifications
You must be signed in to change notification settings - Fork 0
/
raytracer.c
304 lines (280 loc) · 7.45 KB
/
raytracer.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
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
304
#include "raytracer.h"
double double_max(double a, double b)
{
return a>b ? a : b;
}
hit_test intersect(ray r, sphere s)
{
hit_test result = {0}; /* {0} to quiet the compiler */
vec sc = s.center;
double sr = s.radius;
vec A = vec_sub(r.origin, sc);
double B = vec_dot(A, r.direction);
double C = vec_dot(A, A) - (sr * sr);
double D = (B * B) - C;
double t = (-B - sqrt(D));
result.miss = 1;
if (D > 0 && t > 0) {
result.miss = 0;
result.dist = t;
result.surf = s.surf;
result.shine = s.shine;
result.surf_norm = vec_norm(vec_sub(ray_position(r,t),sc));
}
return result;
}
int shadow(vec pt, light l, sphere_list *ss)
{
vec nudge = vec_scale(0.0001, l.direction);
vec lifted = vec_add(pt, nudge);
ray test_ray = {lifted, l.direction};
while (ss!=NULL) {
hit_test h = intersect(test_ray, ss->s);
if (!h.miss)
return 1;
ss = ss->next;
}
return 0;
}
int rgb_nonzero(rgb c)
{
return (c.r>0 || c.g>0 || c.b>0);
}
rgb lighting(scene s, ray r, hit_test h)
{
rgb result;
if (h.miss)
return s.bg;
vec hit_position = ray_position(r, h.dist);
if (shadow(hit_position, s.light, s.spheres)) {
result = rgb_modulate(h.surf, s.amb);
}
else {
double dot = vec_dot(h.surf_norm, s.light.direction);
double d = double_max(0, dot);
rgb diffuse_light = rgb_scale(d, s.light.color);
rgb lsum = rgb_add(s.amb, diffuse_light);
result = rgb_modulate(h.surf, lsum);
}
/**** === implement specular reflection here === ****/
if (rgb_nonzero(h.shine)) {
rgb ss;
vec N = h.surf_norm;
vec L = s.light.direction;
rgb S = h.shine;
vec R = vec_sub( vec_scale(2* vec_dot(N,L),N),L);
vec V = vec_neg(r.direction);
if (vec_dot(N,L)>0){
ss = rgb_scale( pow( double_max( vec_dot(R,V),0), 6), S);
//rgb_print(k);
}
else
ss = rgb_expr(0,0,0);
return rgb_add(result,ss);
}
return result;
}
vec logical_loc(camera c, vec p)
{
double pixw, pixh;
double lx, ly;
if (c.h>c.w)
pixh = pixw = 2.0 / c.h;
else
pixh = pixw = 2.0 / c.w;
if (p.x>c.w || p.y>c.h) {
fprintf(stderr, "logical_loc: bad position (%lg, %lg)\n", p.x, p.y);
exit(1);
}
lx = -1.0 + (pixw * p.x) + (pixw / 2.0);
ly = 1.0 - ((pixh * p.y) + (pixh / 2.0));
if (c.w>c.h)
ly -= (c.w-c.h) / 2.0 * pixh;
else
lx += (c.h-c.w) / 2.0 * pixw;
vec result = {lx, ly, 0};
return result;
}
rgb trace_ray(scene s, ray r)
{
hit_test closest_hit = {0}, /* {0} to quiet the compiler */
temp_hit = {0};
sphere_list *ss = s.spheres;
closest_hit.miss=1;
while (ss!=NULL) {
temp_hit = intersect(r, ss->s);
if (closest_hit.miss)
closest_hit = temp_hit;
else if (!(temp_hit.miss) && !(closest_hit.miss) &&
temp_hit.dist < closest_hit.dist)
closest_hit = temp_hit;
ss = ss->next;
}
return lighting(s, r, closest_hit);
}
void render(stage g)
{
int i, j;
camera c = g.c;
scene sc = g.s;
printf("P3\n");
printf("%d %d\n", c.w, c.h);
printf("255\n");
for(i=0; i < c.h; i++) {
for(j=0; j < c.w; j++) {
vec p = {j,i,0};
vec loc = logical_loc(c, p);
vec dir = vec_sub(loc, c.loc);
vec normdir = vec_norm(dir);
ray r = {c.loc, normdir};
rgb col = trace_ray(sc, r);
rgb_print_bytes(col);
}
}
}
/* ===== Project 2 Code ===== */
/* fintersect is like intersect, but uses the timestamp */
/* to compute the surface color and specular reflection */
/* color of the intersecting fsphere (if there is one) */
hit_test fintersect(ray r, fsphere s, timestamp ts)
{
hit_test result = {0}; /* {0} to quiet the compiler */
vec sc = s.center;
double sr = s.radius;
vec A = vec_sub(r.origin, sc);
double B = vec_dot(A, r.direction);
double C = vec_dot(A, A) - (sr * sr);
double D = (B * B) - C;
double t = (-B - sqrt(D));
result.miss = 1;
if (D > 0 && t > 0) {
result.miss = 0;
result.dist = t;
result.surf = s.compute_surf(s.center, ray_position(r,t), ts);
result.shine = s.compute_shine(ts);
result.surf_norm = vec_norm(vec_sub(ray_position(r,t),sc));
}
return result;
}
/* fshadow is like shadow, but needs to pass a timestamp */
/* to fintersect */
int fshadow(vec p, light l, fsphere_list *ss, timestamp ts)
{
vec nudge = vec_scale(0.0001, l.direction);
vec lifted = vec_add(p, nudge);
ray test_ray = {lifted, l.direction};
while (ss!=NULL) {
hit_test h = fintersect(test_ray, ss->s, ts);
if (!h.miss)
return 1;
ss = ss->next;
}
return 0;
}
/* flighting is like lighting, but consumes a frame */
/* rather than a scene */
rgb flighting(frame f, ray r, hit_test h)
{
rgb result;
if (h.miss)
return f.bg;
vec hit_position = ray_position(r, h.dist);
if (fshadow(hit_position, f.light, f.spheres, f.ts)) {
result = rgb_modulate(h.surf, f.amb);
}
else {
double dot = vec_dot(h.surf_norm, f.light.direction);
double d = double_max(0, dot);
rgb diffuse_light = rgb_scale(d, f.light.color);
rgb lsum = rgb_add(f.amb, diffuse_light);
result = rgb_modulate(h.surf, lsum);
}
/**** === implement specular reflection here === ****/
if (rgb_nonzero(h.shine)) {
rgb ss;
vec N = h.surf_norm;
vec L = f.light.direction;
rgb S = h.shine;
vec R = vec_sub( vec_scale(2* vec_dot(N,L),N),L);
vec V = vec_neg(r.direction);
if (vec_dot(N,L)>0){
ss = rgb_scale( pow( double_max( vec_dot(R,V),0), 6), S);
//rgb_print(k);
}
else
ss = rgb_expr(0,0,0);
return rgb_add(result,ss);
}
return result;
}
/* ftrace is like trace, but for frames rather than scenes */
rgb ftrace(ray r, frame f)
//rgb trace_ray(scene s, ray r)
{
hit_test closest_hit = {0}, /* {0} to quiet the compiler */
temp_hit = {0};
fsphere_list *ss = f.spheres;
closest_hit.miss=1;
while (ss!=NULL) {
temp_hit = fintersect(r, ss->s, f.ts);
if (closest_hit.miss)
closest_hit = temp_hit;
else if (!(temp_hit.miss) && !(closest_hit.miss) &&
temp_hit.dist < closest_hit.dist)
closest_hit = temp_hit;
ss = ss->next;
}
return flighting(f, r, closest_hit);
}
/* frender consumes a camera and a frame and renders the frame, */
/* writing a ppm file to the given filename */
/* - use fopen to open the file, and fclose when you're done */
void frender(camera c, frame f, char *filename)
//void render(stage g)
{
char name[32];
int i, j;
for (i=0; i<32; i++)
name[i]='\0';
sprintf(name, "%s.ppm", filename);
FILE *fp = fopen (name, "a");
fprintf(fp, "P3\n");
fprintf(fp, "%d %d\n", c.w, c.h);
fprintf(fp, "255\n");
for(i=0; i < c.h; i++) {
for(j=0; j < c.w; j++) {
vec p = {j,i,0};
vec loc = logical_loc(c, p);
vec dir = vec_sub(loc, c.loc);
vec normdir = vec_norm(dir);
ray r = {c.loc, normdir};
rgb col = ftrace(r, f);
//rgb_print_bytes(col);
fprintf(fp, "%d %d %d\n",bytify(col.r),bytify(col.g),bytify(col.b));
}
}
fclose(fp);
}
void frender_frames(fstage g, char *name_stem)
{
unsigned int frames = g.n_frames;
int i;
char tmp[16];
for (i=0; i<16; i++)
tmp[i]='\0';
for (i=0; i<frames; i++){
sprintf(tmp, "%s%06d", name_stem, i);
frender(g.c, g.compute_frame(i), tmp);
}
}
int main(int argc, char *argv[])
{
fstage g = get_fstage(NULL);
char name[16];
int i;
for (i=0; i<16; i++)
name[i]='\0';
sprintf(name, "Hithere");
frender_frames(g, name);
return 0;
}