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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
|
#include <stdbool.h>
#include <stdlib.h>
#include <assert.h>
#include <stdio.h>
#include "vector.h"
#include <SDL/SDL.h>
#include <SDL/SDL_video.h>
#define WIDTH 320
#define HEIGHT 240
#define MAX(a, b) ((a>b)?(a):(b))
#define MIN(a, b) ((a<b)?(a):(b))
#define SQR(a) ((a)*(a))
#define SIGN(x) ((x)<0?-1:1)
#define MAX_BOUNCES 10
#define MOVE_FACTOR .1
#define MOUSELOOK
struct rgb_t { unsigned char r, g, b; };
struct object_t {
enum { SPHERE, TRI } type;
union {
struct { vector center; scalar radius; } sphere;
struct { vector points[3]; } tri;
};
struct rgb_t color;
int specularity; /* 0-255 */
};
struct light_t {
vector position;
scalar intensity;
};
struct scene_t {
struct rgb_t bg;
struct object_t *objects;
size_t n_objects;
struct light_t *lights;
size_t n_lights;
scalar ambient;
};
struct camera_t {
vector origin;
vector direction; /* direction to center of camera */
scalar fov_x, fov_y; /* radians */
};
/* { o, d } form a ray */
/* point of intersection is *t * d units away */
bool object_intersects(struct object_t *obj, vector o, vector d, scalar *t)
{
assert(o.type == RECT);
assert(d.type == RECT);
switch(obj->type)
{
case SPHERE:
{
scalar a = SQR(d.rect.x) + SQR(d.rect.y) + SQR(d.rect.z),
b = 2 * ((o.rect.x - obj->sphere.center.rect.x) * d.rect.x +
(o.rect.y - obj->sphere.center.rect.y) * d.rect.y +
(o.rect.z - obj->sphere.center.rect.z) * d.rect.z),
c = SQR(o.rect.x - obj->sphere.center.rect.x) + SQR(o.rect.y - obj->sphere.center.rect.y) + SQR(o.rect.z - obj->sphere.center.rect.z) - SQR(obj->sphere.radius);
scalar disc = b*b - 4*a*c;
if(disc < 0)
{
//printf("no intersection (%f)\n", disc);
return false;
}
scalar t1 = (-b - sqrt(disc)) / (2*a), t2 = (-b + sqrt(disc)) / (2*a);
/* both are negative */
if(t1 < 0 && t2 < 0)
{
//printf("no intersection\n");
return false;
}
/* one is negative */
if(t1 * t2 < 0)
{
//printf("camera is inside sphere (%f, %f)!\n", t1, t2);
*t = MAX(t1, t2);
return true;
}
vector prod = d;
prod = vect_mul(prod, t2);
prod = vect_add(prod, o);
//printf("ray from (%f, %f, %f) intersects sphere at point %f, %f, %f (%f)\n", o->rect.x, o->rect.y, o->rect.z, prod.rect.x, prod.rect.y, prod.rect.z, vect_abs(&prod));
*t = MIN(t1, t2);
return true;
}
}
}
vector normal_at_point(vector pt, const struct object_t *obj)
{
vector normal;
switch(obj->type)
{
case SPHERE:
{
normal = vect_sub(pt, obj->sphere.center);
break;
}
default:
assert(false);
}
return vect_normalize(normal);
}
/* return the direction of the reflected ray */
vector reflect_ray(vector pt, vector d, vector normal, const struct object_t *obj)
{
scalar c = -2 * vect_dot(d, normal);
vector reflected = normal;
reflected = vect_mul(reflected, c);
reflected = vect_add(reflected, d);
return reflected;
}
struct rgb_t blend(struct rgb_t a, struct rgb_t b, unsigned char alpha)
{
struct rgb_t ret;
unsigned char r1, g1, b1;
ret.r = (((int)a.r * alpha) + ((int)b.r * (255 - alpha))) / 255;
ret.g = (((int)a.g * alpha) + ((int)b.g * (255 - alpha))) / 255;
ret.b = (((int)a.b * alpha) + ((int)b.b * (255 - alpha))) / 255;
return ret;
}
static const struct object_t *scene_intersections(const struct scene_t *scene,
vector orig, vector d, scalar *dist, const struct object_t *avoid)
{
*dist = -1;
const struct object_t *obj = NULL;
/* check intersections */
for(int i = 0; i < scene->n_objects; ++i)
{
/* avoid intersections with the same object */
if(avoid == scene->objects + i)
continue;
scalar t;
if(object_intersects(scene->objects + i, orig, d, &t))
{
if(*dist < 0 || t < *dist)
{
*dist = t;
obj = scene->objects + i;
}
}
}
return obj;
}
struct rgb_t trace_ray(const struct scene_t *scene, vector orig, vector d, int max_iters, const struct object_t *avoid)
{
struct rgb_t primary = scene->bg;
scalar hit_dist; /* distance from camera in terms of d */
const struct object_t *hit_obj = scene_intersections(scene, orig, d, &hit_dist, avoid);
struct rgb_t reflected = {0, 0, 0};
int specular = 255;
if(hit_obj)
{
//printf("pow!\n");
primary = hit_obj->color;
/* shade */
scalar shade_total = 0;
vector pt = d;
pt = vect_mul(pt, hit_dist);
pt = vect_add(pt, orig);
vector normal = normal_at_point(pt, hit_obj);
for(int i = 0; i < scene->n_lights; ++i)
{
/* get vector to light */
vector light_dir = scene->lights[i].position;
light_dir = vect_sub(light_dir, pt);
scalar light_dist = vect_abs(light_dir);
light_dir = vect_normalize(light_dir);
/* see if light is occluded */
scalar nearest;
const struct object_t *obj = scene_intersections(scene, pt, light_dir, &nearest, hit_obj);
if(obj && nearest < light_dist)
continue;
scalar shade = vect_dot(normal, light_dir);
if(shade > 0)
shade_total += shade * scene->lights[i].intensity;
}
if(shade_total > 1)
shade_total = 1;
scalar diffuse = 1 - scene->ambient;
primary.r *= (scene->ambient + diffuse * shade_total);
primary.g *= (scene->ambient + diffuse * shade_total);
primary.b *= (scene->ambient + diffuse * shade_total);
specular = 255 - hit_obj->specularity;
/* reflections */
if(specular != 255 && max_iters > 0)
{
vector ref = reflect_ray(pt, d, normal, hit_obj);
reflected = trace_ray(scene, pt, ref, max_iters - 1, hit_obj);
}
}
return blend(primary, reflected, specular);
}
unsigned char *render_scene(int w, int h, const struct scene_t *scene,
const struct camera_t *cam)
{
unsigned char *fb = malloc(w * h * 3);
scalar scale_x = cam->fov_x / w, scale_y = cam->fov_y / h;
for(int y = 0; y < h; ++y)
{
for(int x = 0; x < w; ++x)
{
/* trace a ray from the camera into the scene */
/* figure out how to rotate the offset vector to suit the
* current pixel */
/* rot in range of [-fov / 2, fov / 2) */
scalar rot_x = (x - w / 2) * scale_x, rot_y = (y - h / 2) * scale_y;
/* rotate the offset vector */
vector d = cam->direction;
vect_to_sph(&d);
d.sph.elevation -= rot_y;
d.sph.azimuth += rot_x;
vect_to_rect(&d);
//printf("(%d, %d) maps to (%f, %f, %f)\n", x, y, d.rect.x, d.rect.y, d.rect.z);
/* cam->origin and d now form the camera ray */
struct rgb_t color = trace_ray(scene, cam->origin, d, MAX_BOUNCES, NULL);
fb[y * w * 3 + 3 * x] = color.r;
fb[y * w * 3 + 3 * x + 1] = color.g;
fb[y * w * 3 + 3 * x + 2] = color.b;
}
}
return fb;
}
int main()
{
vector test = (vector) { RECT, { 0, 1, 1 } };
vect_to_sph(&test);
//printf("1, 0, 0, is r = %f, elev = %f, azi = %f", test.sph.r, test.sph.elevation, test.sph.azimuth);
//return 0;
struct scene_t scene;
scene.bg.r = 0xa0;
scene.bg.g = 0xa0;
scene.bg.b = 0xa0;
scene.ambient = .2;
struct object_t sph[3];
sph[0].type = SPHERE;
sph[0].sphere.center = (vector) { RECT, {0, 0, 1 } };
sph[0].sphere.radius = 1;
sph[0].color = (struct rgb_t){0, 0, 0xff};
sph[0].specularity = 20;
sph[1].type = SPHERE;
sph[1].sphere.center = (vector) { RECT, {0, 0, -1 } };
sph[1].sphere.radius = 1;
sph[1].color = (struct rgb_t){0, 0xe0, 0};
sph[1].specularity = 0xef;
sph[2].type = SPHERE;
sph[2].sphere.center = (vector) { RECT, {0, 0, -3 } };
sph[2].sphere.radius = 1;
sph[2].color = (struct rgb_t){0, 0xe0, 0};
sph[2].specularity = 0x60;
struct light_t lights[1];
lights[0].position = (vector) { RECT, {0, 10, -10} };
lights[0].intensity = 2;
scene.objects = sph;
scene.n_objects = 3;
scene.lights = lights;
scene.n_lights = 1;
struct camera_t cam;
cam.origin = (vector){ RECT, {-5, 0, 0} };
cam.direction = (vector){ RECT, {0, 0, 1} };
cam.fov_x = M_PI / 2;
cam.fov_y = M_PI / 2 * HEIGHT / WIDTH;
#if 0
unsigned char *fb = render_scene(WIDTH, HEIGHT, &scene, &cam);
FILE *f = fopen("test.ppm", "w");
fprintf(f, "P6\n%d %d\n%d\n", WIDTH, HEIGHT, 255);
fwrite(fb, WIDTH * HEIGHT, 3, f);
fclose(f);
free(fb);
return 0;
#else
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface *screen = SDL_SetVideoMode(WIDTH, HEIGHT, 24, SDL_SWSURFACE);
SDL_EnableKeyRepeat(500, 50);
while(1)
{
#ifdef MOUSELOOK
/* mouse look */
int x, y;
unsigned mouse = SDL_GetMouseState(&x, &y);
x -= WIDTH / 2;
y -= HEIGHT / 2;
vect_to_sph(&cam.direction);
cam.direction.sph.azimuth += M_PI/30 * SIGN(x)*SQR((scalar)x / WIDTH);
cam.direction.sph.elevation += M_PI/30 * SIGN(y)*SQR((scalar)y / HEIGHT);
#endif
unsigned char *fb = render_scene(WIDTH, HEIGHT, &scene, &cam);
memcpy(screen->pixels, fb, WIDTH * HEIGHT * 3);
SDL_UpdateRect(screen, 0, 0, 0, 0);
free(fb);
SDL_Event e;
//printf("camera at %f, %f, %f\n", cam.origin.rect.x, cam.origin.rect.y, cam.origin.rect.z);
while(SDL_PollEvent(&e))
{
switch(e.type)
{
case SDL_QUIT:
return 0;
case SDL_KEYDOWN:
switch(e.key.keysym.sym)
{
case SDLK_LEFT:
cam.origin.rect.z += .1;
break;
case SDLK_RIGHT:
cam.origin.rect.z -= .1;
break;
case SDLK_UP:
cam.origin.rect.y += .1;
break;
case SDLK_DOWN:
cam.origin.rect.y -= .1;
break;
case SDLK_w:
cam.origin = vect_add(cam.origin, vect_mul(cam.direction, MOVE_FACTOR));
break;
case SDLK_s:
cam.origin = vect_sub(cam.origin, vect_mul(cam.direction, MOVE_FACTOR));
break;
case SDLK_MINUS:
cam.fov_x += M_PI/36;
cam.fov_y = cam.fov_x * HEIGHT/WIDTH;
break;
case SDLK_EQUALS:
cam.fov_x -= M_PI/36;
cam.fov_y = cam.fov_x * HEIGHT/WIDTH;
break;
case SDLK_a:
{
vect_to_sph(&cam.direction);
cam.direction.sph.azimuth -= M_PI/180;
break;
}
case SDLK_d:
{
vect_to_sph(&cam.direction);
cam.direction.sph.azimuth += M_PI/180;
break;
}
}
break;
}
}
}
#endif
}
|