view c-ray-mt.c @ 2:75c818c6cad1

DataSet print out
author Merten Sach <msach@mailbox.tu-berlin.de>
date Wed, 28 Sep 2011 15:13:46 +0200
parents 3840d91821c4
children 80a90f221047
line source
1 /* c-ray-mt - a simple multithreaded raytracing filter.
2 * Copyright (C) 2006 John Tsiombikas <nuclear@siggraph.org>
3 *
4 * You are free to use, modify and redistribute this program under the
5 * terms of the GNU General Public License v2 or (at your option) later.
6 * see "http://www.gnu.org/licenses/gpl.txt" for details.
7 * ---------------------------------------------------------------------
8 * Usage:
9 * compile: just type make
10 * (add any arch-specific optimizations for your compiler in CFLAGS first)
11 * run: cat scene | ./c-ray-mt [-t num-threads] >foo.ppm
12 * (on broken systems such as windows try: c-ray-mt -i scene -o foo.ppm)
13 * enjoy: display foo.ppm
14 * (with imagemagick, or use your favorite image viewer)
15 * ---------------------------------------------------------------------
16 * Scene file format:
17 * # sphere (many)
18 * s x y z rad r g b shininess reflectivity
19 * # light (many)
20 * l x y z
21 * # camera (one)
22 * c x y z fov tx ty tz
23 * ---------------------------------------------------------------------
24 */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <math.h>
29 #include <ctype.h>
30 #include <errno.h>
31 #include <pthread.h>
32 #include "VPThread_lib/VPThread.h"
34 #define VER_MAJOR 1
35 #define VER_MINOR 1
36 #define VER_STR "c-ray-mt v%d.%d\n"
38 #if !defined(unix) && !defined(__unix__)
39 #ifdef __MACH__
40 #define unix 1
41 #define __unix__ 1
42 #endif /* __MACH__ */
43 #endif /* unix */
45 /* find the appropriate way to define explicitly sized types */
46 /* for C99 or GNU libc (also mach's libc) we can use stdint.h */
47 #if (__STDC_VERSION__ >= 199900) || defined(__GLIBC__) || defined(__MACH__)
48 #include <stdint.h>
49 #elif defined(unix) || defined(__unix__) /* some UNIX systems have them in sys/types.h */
50 #include <sys/types.h>
51 #elif defined(__WIN32__) || defined(WIN32) /* the nameless one */
52 typedef unsigned __int8 uint8_t;
53 typedef unsigned __int32 uint32_t;
54 #endif /* sized type detection */
56 struct vec3 {
57 double x, y, z;
58 };
60 struct ray {
61 struct vec3 orig, dir;
62 };
64 struct material {
65 struct vec3 col; /* color */
66 double spow; /* specular power */
67 double refl; /* reflection intensity */
68 };
70 struct sphere {
71 struct vec3 pos;
72 double rad;
73 struct material mat;
74 struct sphere *next;
75 };
77 struct spoint {
78 struct vec3 pos, normal, vref; /* position, normal and view reflection */
79 double dist; /* parametric distance of intersection along the ray */
80 };
82 struct camera {
83 struct vec3 pos, targ;
84 double fov;
85 };
87 struct thread_data {
88 VirtProcr *VP;
89 int sl_start, sl_count;
90 uint32_t *pixels;
91 };
92 typedef struct thread_data thread_data;
94 void render_scanline(int xsz, int ysz, int sl, uint32_t *fb, int samples);
95 struct vec3 trace(struct ray ray, int depth);
96 struct vec3 shade(struct sphere *obj, struct spoint *sp, int depth);
97 struct vec3 reflect(struct vec3 v, struct vec3 n);
98 struct vec3 cross_product(struct vec3 v1, struct vec3 v2);
99 struct ray get_primary_ray(int x, int y, int sample);
100 struct vec3 get_sample_pos(int x, int y, int sample);
101 struct vec3 jitter(int x, int y, int s);
102 int ray_sphere(const struct sphere *sph, struct ray ray, struct spoint *sp);
103 void load_scene(FILE *fp);
104 unsigned long get_msec(void);
106 void thread_func(void *tdata, VirtProcr *VProc);
108 #define MAX_LIGHTS 16 /* maximum number of lights */
109 #define RAY_MAG 1000.0 /* trace rays of this magnitude */
110 #define MAX_RAY_DEPTH 5 /* raytrace recursion limit */
111 #define FOV 0.78539816 /* field of view in rads (pi/4) */
112 #define HALF_FOV (FOV * 0.5)
113 #define ERR_MARGIN 1e-6 /* an arbitrary error margin to avoid surface acne */
115 /* bit-shift ammount for packing each color into a 32bit uint */
116 #ifdef LITTLE_ENDIAN
117 #define RSHIFT 16
118 #define BSHIFT 0
119 #else /* big endian */
120 #define RSHIFT 0
121 #define BSHIFT 16
122 #endif /* endianess */
123 #define GSHIFT 8 /* this is the same in both byte orders */
125 /* some helpful macros... */
126 #define SQ(x) ((x) * (x))
127 #define MAX(a, b) ((a) > (b) ? (a) : (b))
128 #define MIN(a, b) ((a) < (b) ? (a) : (b))
129 #define DOT(a, b) ((a).x * (b).x + (a).y * (b).y + (a).z * (b).z)
130 #define NORMALIZE(a) do {\
131 double len = sqrt(DOT(a, a));\
132 (a).x /= len; (a).y /= len; (a).z /= len;\
133 } while(0);
135 /* global state */
136 int xres = 800;
137 int yres = 600;
138 int rays_per_pixel = 1;
139 double aspect = 1.333333;
140 struct sphere *obj_list;
141 struct vec3 lights[MAX_LIGHTS];
142 int lnum = 0;
143 struct camera cam;
145 int thread_num = 1;
146 struct thread_data *threads;
148 volatile int end = 0;
149 volatile int start = 0;
150 int32 end_mutex, end_cond;
151 int32 start_cond, start_mutex;
153 #define NRAN 1024
154 #define MASK (NRAN - 1)
155 struct vec3 urand[NRAN];
156 int irand[NRAN];
158 unsigned long rend_time, start_time;
160 const char *usage = {
161 "Usage: c-ray-mt [options]\n"
162 " Reads a scene file from stdin, writes the image to stdout, and stats to stderr.\n\n"
163 "Options:\n"
164 " -t <num> how many threads to use (default: 1)\n"
165 " -s WxH where W is the width and H the height of the image\n"
166 " -r <rays> shoot <rays> rays per pixel (antialiasing)\n"
167 " -i <file> read from <file> instead of stdin\n"
168 " -o <file> write to <file> instead of stdout\n"
169 " -h this help screen\n\n"
170 };
172 char __ProgrammName[] = "c-ray";
173 char __DataSet[255];
176 void raytrace(void *pixels, VirtProcr *Vprocr);
178 int main(int argc, char **argv) {
179 int i;
180 uint32_t *pixels;
181 FILE *infile = stdin, *outfile = stdout;
183 for(i=1; i<argc; i++) {
184 if(argv[i][0] == '-' && argv[i][2] == 0) {
185 char *sep;
186 switch(argv[i][1]) {
187 case 't':
188 if(!isdigit(argv[++i][0])) {
189 fprintf(stderr, "-t mus be followed by the number of worker threads to spawn\n");
190 return EXIT_FAILURE;
191 }
192 thread_num = atoi(argv[i]);
193 if(!thread_num) {
194 fprintf(stderr, "invalid number of threads specified: %d\n", thread_num);
195 return EXIT_FAILURE;
196 }
197 break;
199 case 's':
200 if(!isdigit(argv[++i][0]) || !(sep = strchr(argv[i], 'x')) || !isdigit(*(sep + 1))) {
201 fputs("-s must be followed by something like \"640x480\"\n", stderr);
202 return EXIT_FAILURE;
203 }
204 xres = atoi(argv[i]);
205 yres = atoi(sep + 1);
206 aspect = (double)xres / (double)yres;
207 break;
209 case 'i':
210 if(!(infile = fopen(argv[++i], "rb"))) {
211 fprintf(stderr, "failed to open input file %s: %s\n", argv[i], strerror(errno));
212 return EXIT_FAILURE;
213 }
214 break;
216 case 'o':
217 if(!(outfile = fopen(argv[++i], "wb"))) {
218 fprintf(stderr, "failed to open output file %s: %s\n", argv[i], strerror(errno));
219 return EXIT_FAILURE;
220 }
221 break;
223 case 'r':
224 if(!isdigit(argv[++i][0])) {
225 fputs("-r must be followed by a number (rays per pixel)\n", stderr);
226 return EXIT_FAILURE;
227 }
228 rays_per_pixel = atoi(argv[i]);
229 break;
231 case 'h':
232 fputs(usage, stdout);
233 return 0;
235 default:
236 fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
237 fputs(usage, stderr);
238 return EXIT_FAILURE;
239 }
240 } else {
241 fprintf(stderr, "unrecognized argument: %s\n", argv[i]);
242 fputs(usage, stderr);
243 return EXIT_FAILURE;
244 }
245 }
247 snprintf(DataSet,255,"file: %s\nsize: %dx%d\nrays per pixel: %d\nthreads: %d\n",
248 infile, xres, yres, rays_per_pixel, thread_num);
251 if(!(pixels = malloc(xres * yres * sizeof *pixels))) {
252 perror("pixel buffer allocation failed");
253 return EXIT_FAILURE;
254 }
255 load_scene(infile);
257 //This is the transition to the VMS runtime
258 VPThread__create_seed_procr_and_do_work(raytrace, (void*)pixels);
260 /* output statistics to stderr */
261 fprintf(stderr, "Rendering took: %lu seconds (%lu milliseconds)\n", rend_time / 1000, rend_time);
263 /* output the image */
264 fprintf(outfile, "P6\n%d %d\n255\n", xres, yres);
265 for(i=0; i<xres * yres; i++) {
266 fputc((pixels[i] >> RSHIFT) & 0xff, outfile);
267 fputc((pixels[i] >> GSHIFT) & 0xff, outfile);
268 fputc((pixels[i] >> BSHIFT) & 0xff, outfile);
269 }
270 fflush(outfile);
272 if(infile != stdin) fclose(infile);
273 if(outfile != stdout) fclose(outfile);
275 struct sphere *walker = obj_list;
276 while(walker) {
277 struct sphere *tmp = walker;
278 walker = walker->next;
279 free(tmp);
280 }
281 free(pixels);
282 return 0;
283 }
285 /* this is run after the VMS is set up*/
286 void raytrace(void *pixels, VirtProcr *VProc)
287 {
288 int i;
289 double sl, sl_per_thread;
291 /* initialize the random number tables for the jitter */
292 for(i=0; i<NRAN; i++) urand[i].x = (double)rand() / RAND_MAX - 0.5;
293 for(i=0; i<NRAN; i++) urand[i].y = (double)rand() / RAND_MAX - 0.5;
294 for(i=0; i<NRAN; i++) irand[i] = (int)(NRAN * ((double)rand() / RAND_MAX));
296 if(thread_num > yres) {
297 fprintf(stderr, "more threads than scanlines specified, reducing number of threads to %d\n", yres);
298 thread_num = yres;
299 }
302 if(!(threads = VPThread__malloc(thread_num * sizeof(thread_data), VProc))) {
303 perror("failed to allocate thread table");
304 exit(EXIT_FAILURE);
305 }
307 end_mutex = VPThread__make_mutex(VProc);
308 end_cond = VPThread__make_cond(end_mutex, VProc);
309 start_mutex = VPThread__make_mutex(VProc);
310 start_cond = VPThread__make_cond(start_mutex, VProc);
312 sl = 0.0;
313 sl_per_thread = (double)yres / (double)thread_num;
314 for(i=0; i<thread_num; i++) {
315 threads[i].sl_start = (int)sl;
316 sl += sl_per_thread;
317 threads[i].sl_count = (int)sl - threads[i].sl_start;
318 threads[i].pixels = (uint32_t*)pixels;
320 threads[i].VP =
321 VPThread__create_thread((VirtProcrFnPtr)thread_func,
322 (void*)(&threads[i]), VProc);
323 }
325 threads[thread_num - 1].sl_count = yres - threads[thread_num - 1].sl_start;
327 fprintf(stderr, VER_STR, VER_MAJOR, VER_MINOR);
329 // start worker threads
330 //printf("start of worker thread (%d)\n", VProc->procrID);
331 VPThread__mutex_lock(start_mutex, VProc);
332 start_time = get_msec();
333 start = 1;
334 for(i=0; i<thread_num; i++)
335 VPThread__cond_signal(start_cond, VProc);
336 VPThread__mutex_unlock(start_mutex, VProc);
338 //printf("wait for worker (%d)\n", VProc->procrID);
339 VPThread__mutex_lock(end_mutex, VProc);
340 while(end < thread_num)
341 VPThread__cond_wait(end_cond, VProc);
342 VPThread__mutex_unlock(end_mutex, VProc);
344 rend_time = get_msec() - start_time;
346 VPThread__free(threads,VProc);
347 VPThread__dissipate_thread(VProc);
348 }
350 /* render a frame of xsz/ysz dimensions into the provided framebuffer */
351 void render_scanline(int xsz, int ysz, int sl, uint32_t *fb, int samples) {
352 int i, s;
353 double rcp_samples = 1.0 / (double)samples;
355 for(i=0; i<xsz; i++) {
356 double r, g, b;
357 r = g = b = 0.0;
359 for(s=0; s<samples; s++) {
360 struct vec3 col = trace(get_primary_ray(i, sl, s), 0);
361 r += col.x;
362 g += col.y;
363 b += col.z;
364 }
366 r = r * rcp_samples;
367 g = g * rcp_samples;
368 b = b * rcp_samples;
370 fb[sl * xsz + i] = ((uint32_t)(MIN(r, 1.0) * 255.0) & 0xff) << RSHIFT |
371 ((uint32_t)(MIN(g, 1.0) * 255.0) & 0xff) << GSHIFT |
372 ((uint32_t)(MIN(b, 1.0) * 255.0) & 0xff) << BSHIFT;
373 }
374 }
376 /* trace a ray throught the scene recursively (the recursion happens through
377 * shade() to calculate reflection rays if necessary).
378 */
379 struct vec3 trace(struct ray ray, int depth) {
380 struct vec3 col;
381 struct spoint sp, nearest_sp;
382 struct sphere *nearest_obj = 0;
383 struct sphere *iter = obj_list->next;
385 /* if we reached the recursion limit, bail out */
386 if(depth >= MAX_RAY_DEPTH) {
387 col.x = col.y = col.z = 0.0;
388 return col;
389 }
391 /* find the nearest intersection ... */
392 while(iter) {
393 if(ray_sphere(iter, ray, &sp)) {
394 if(!nearest_obj || sp.dist < nearest_sp.dist) {
395 nearest_obj = iter;
396 nearest_sp = sp;
397 }
398 }
399 iter = iter->next;
400 }
402 /* and perform shading calculations as needed by calling shade() */
403 if(nearest_obj) {
404 col = shade(nearest_obj, &nearest_sp, depth);
405 } else {
406 col.x = col.y = col.z = 0.0;
407 }
409 return col;
410 }
412 /* Calculates direct illumination with the phong reflectance model.
413 * Also handles reflections by calling trace again, if necessary.
414 */
415 struct vec3 shade(struct sphere *obj, struct spoint *sp, int depth) {
416 int i;
417 struct vec3 col = {0, 0, 0};
419 /* for all lights ... */
420 for(i=0; i<lnum; i++) {
421 double ispec, idiff;
422 struct vec3 ldir;
423 struct ray shadow_ray;
424 struct sphere *iter = obj_list->next;
425 int in_shadow = 0;
427 ldir.x = lights[i].x - sp->pos.x;
428 ldir.y = lights[i].y - sp->pos.y;
429 ldir.z = lights[i].z - sp->pos.z;
431 shadow_ray.orig = sp->pos;
432 shadow_ray.dir = ldir;
434 /* shoot shadow rays to determine if we have a line of sight with the light */
435 while(iter) {
436 if(ray_sphere(iter, shadow_ray, 0)) {
437 in_shadow = 1;
438 break;
439 }
440 iter = iter->next;
441 }
443 /* and if we're not in shadow, calculate direct illumination with the phong model. */
444 if(!in_shadow) {
445 NORMALIZE(ldir);
447 idiff = MAX(DOT(sp->normal, ldir), 0.0);
448 ispec = obj->mat.spow > 0.0 ? pow(MAX(DOT(sp->vref, ldir), 0.0), obj->mat.spow) : 0.0;
450 col.x += idiff * obj->mat.col.x + ispec;
451 col.y += idiff * obj->mat.col.y + ispec;
452 col.z += idiff * obj->mat.col.z + ispec;
453 }
454 }
456 /* Also, if the object is reflective, spawn a reflection ray, and call trace()
457 * to calculate the light arriving from the mirror direction.
458 */
459 if(obj->mat.refl > 0.0) {
460 struct ray ray;
461 struct vec3 rcol;
463 ray.orig = sp->pos;
464 ray.dir = sp->vref;
465 ray.dir.x *= RAY_MAG;
466 ray.dir.y *= RAY_MAG;
467 ray.dir.z *= RAY_MAG;
469 rcol = trace(ray, depth + 1);
470 col.x += rcol.x * obj->mat.refl;
471 col.y += rcol.y * obj->mat.refl;
472 col.z += rcol.z * obj->mat.refl;
473 }
475 return col;
476 }
478 /* calculate reflection vector */
479 struct vec3 reflect(struct vec3 v, struct vec3 n) {
480 struct vec3 res;
481 double dot = v.x * n.x + v.y * n.y + v.z * n.z;
482 res.x = -(2.0 * dot * n.x - v.x);
483 res.y = -(2.0 * dot * n.y - v.y);
484 res.z = -(2.0 * dot * n.z - v.z);
485 return res;
486 }
488 struct vec3 cross_product(struct vec3 v1, struct vec3 v2) {
489 struct vec3 res;
490 res.x = v1.y * v2.z - v1.z * v2.y;
491 res.y = v1.z * v2.x - v1.x * v2.z;
492 res.z = v1.x * v2.y - v1.y * v2.x;
493 return res;
494 }
496 /* determine the primary ray corresponding to the specified pixel (x, y) */
497 struct ray get_primary_ray(int x, int y, int sample) {
498 struct ray ray;
499 float m[3][3];
500 struct vec3 i, j = {0, 1, 0}, k, dir, orig, foo;
502 k.x = cam.targ.x - cam.pos.x;
503 k.y = cam.targ.y - cam.pos.y;
504 k.z = cam.targ.z - cam.pos.z;
505 NORMALIZE(k);
507 i = cross_product(j, k);
508 j = cross_product(k, i);
509 m[0][0] = i.x; m[0][1] = j.x; m[0][2] = k.x;
510 m[1][0] = i.y; m[1][1] = j.y; m[1][2] = k.y;
511 m[2][0] = i.z; m[2][1] = j.z; m[2][2] = k.z;
513 ray.orig.x = ray.orig.y = ray.orig.z = 0.0;
514 ray.dir = get_sample_pos(x, y, sample);
515 ray.dir.z = 1.0 / HALF_FOV;
516 ray.dir.x *= RAY_MAG;
517 ray.dir.y *= RAY_MAG;
518 ray.dir.z *= RAY_MAG;
520 dir.x = ray.dir.x + ray.orig.x;
521 dir.y = ray.dir.y + ray.orig.y;
522 dir.z = ray.dir.z + ray.orig.z;
523 foo.x = dir.x * m[0][0] + dir.y * m[0][1] + dir.z * m[0][2];
524 foo.y = dir.x * m[1][0] + dir.y * m[1][1] + dir.z * m[1][2];
525 foo.z = dir.x * m[2][0] + dir.y * m[2][1] + dir.z * m[2][2];
527 orig.x = ray.orig.x * m[0][0] + ray.orig.y * m[0][1] + ray.orig.z * m[0][2] + cam.pos.x;
528 orig.y = ray.orig.x * m[1][0] + ray.orig.y * m[1][1] + ray.orig.z * m[1][2] + cam.pos.y;
529 orig.z = ray.orig.x * m[2][0] + ray.orig.y * m[2][1] + ray.orig.z * m[2][2] + cam.pos.z;
531 ray.orig = orig;
532 ray.dir.x = foo.x + orig.x;
533 ray.dir.y = foo.y + orig.y;
534 ray.dir.z = foo.z + orig.z;
536 return ray;
537 }
540 struct vec3 get_sample_pos(int x, int y, int sample) {
541 struct vec3 pt;
542 static double sf = 0.0;
544 if(sf == 0.0) {
545 sf = 1.5 / (double)xres;
546 }
548 pt.x = ((double)x / (double)xres) - 0.5;
549 pt.y = -(((double)y / (double)yres) - 0.65) / aspect;
551 if(sample) {
552 struct vec3 jt = jitter(x, y, sample);
553 pt.x += jt.x * sf;
554 pt.y += jt.y * sf / aspect;
555 }
556 return pt;
557 }
559 /* jitter function taken from Graphics Gems I. */
560 struct vec3 jitter(int x, int y, int s) {
561 struct vec3 pt;
562 pt.x = urand[(x + (y << 2) + irand[(x + s) & MASK]) & MASK].x;
563 pt.y = urand[(y + (x << 2) + irand[(y + s) & MASK]) & MASK].y;
564 return pt;
565 }
567 /* Calculate ray-sphere intersection, and return {1, 0} to signify hit or no hit.
568 * Also the surface point parameters like position, normal, etc are returned through
569 * the sp pointer if it is not NULL.
570 */
571 int ray_sphere(const struct sphere *sph, struct ray ray, struct spoint *sp) {
572 double a, b, c, d, sqrt_d, t1, t2;
574 a = SQ(ray.dir.x) + SQ(ray.dir.y) + SQ(ray.dir.z);
575 b = 2.0 * ray.dir.x * (ray.orig.x - sph->pos.x) +
576 2.0 * ray.dir.y * (ray.orig.y - sph->pos.y) +
577 2.0 * ray.dir.z * (ray.orig.z - sph->pos.z);
578 c = SQ(sph->pos.x) + SQ(sph->pos.y) + SQ(sph->pos.z) +
579 SQ(ray.orig.x) + SQ(ray.orig.y) + SQ(ray.orig.z) +
580 2.0 * (-sph->pos.x * ray.orig.x - sph->pos.y * ray.orig.y - sph->pos.z * ray.orig.z) - SQ(sph->rad);
582 if((d = SQ(b) - 4.0 * a * c) < 0.0) return 0;
584 sqrt_d = sqrt(d);
585 t1 = (-b + sqrt_d) / (2.0 * a);
586 t2 = (-b - sqrt_d) / (2.0 * a);
588 if((t1 < ERR_MARGIN && t2 < ERR_MARGIN) || (t1 > 1.0 && t2 > 1.0)) return 0;
590 if(sp) {
591 if(t1 < ERR_MARGIN) t1 = t2;
592 if(t2 < ERR_MARGIN) t2 = t1;
593 sp->dist = t1 < t2 ? t1 : t2;
595 sp->pos.x = ray.orig.x + ray.dir.x * sp->dist;
596 sp->pos.y = ray.orig.y + ray.dir.y * sp->dist;
597 sp->pos.z = ray.orig.z + ray.dir.z * sp->dist;
599 sp->normal.x = (sp->pos.x - sph->pos.x) / sph->rad;
600 sp->normal.y = (sp->pos.y - sph->pos.y) / sph->rad;
601 sp->normal.z = (sp->pos.z - sph->pos.z) / sph->rad;
603 sp->vref = reflect(ray.dir, sp->normal);
604 NORMALIZE(sp->vref);
605 }
606 return 1;
607 }
609 /* Load the scene from an extremely simple scene description file */
610 #define DELIM " \t\n"
611 void load_scene(FILE *fp) {
612 char line[256], *ptr, type;
614 obj_list = malloc(sizeof(struct sphere));
615 obj_list->next = 0;
617 while((ptr = fgets(line, 256, fp))) {
618 int i;
619 struct vec3 pos, col;
620 double rad, spow, refl;
622 while(*ptr == ' ' || *ptr == '\t') ptr++;
623 if(*ptr == '#' || *ptr == '\n') continue;
625 if(!(ptr = strtok(line, DELIM))) continue;
626 type = *ptr;
628 for(i=0; i<3; i++) {
629 if(!(ptr = strtok(0, DELIM))) break;
630 *((double*)&pos.x + i) = atof(ptr);
631 }
633 if(type == 'l') {
634 lights[lnum++] = pos;
635 continue;
636 }
638 if(!(ptr = strtok(0, DELIM))) continue;
639 rad = atof(ptr);
641 for(i=0; i<3; i++) {
642 if(!(ptr = strtok(0, DELIM))) break;
643 *((double*)&col.x + i) = atof(ptr);
644 }
646 if(type == 'c') {
647 cam.pos = pos;
648 cam.targ = col;
649 cam.fov = rad;
650 continue;
651 }
653 if(!(ptr = strtok(0, DELIM))) continue;
654 spow = atof(ptr);
656 if(!(ptr = strtok(0, DELIM))) continue;
657 refl = atof(ptr);
659 if(type == 's') {
660 struct sphere *sph = malloc(sizeof *sph);
661 sph->next = obj_list->next;
662 obj_list->next = sph;
664 sph->pos = pos;
665 sph->rad = rad;
666 sph->mat.col = col;
667 sph->mat.spow = spow;
668 sph->mat.refl = refl;
669 } else {
670 fprintf(stderr, "unknown type: %c\n", type);
671 }
672 }
673 }
676 /* provide a millisecond-resolution timer for each system */
677 #if defined(unix) || defined(__unix__)
678 #include <time.h>
679 #include <sys/time.h>
680 unsigned long get_msec(void) {
681 static struct timeval timeval, first_timeval;
683 gettimeofday(&timeval, 0);
684 if(first_timeval.tv_sec == 0) {
685 first_timeval = timeval;
686 return 0;
687 }
688 return (timeval.tv_sec - first_timeval.tv_sec) * 1000 + (timeval.tv_usec - first_timeval.tv_usec) / 1000;
689 }
690 #elif defined(__WIN32__) || defined(WIN32)
691 #include <windows.h>
692 unsigned long get_msec(void) {
693 return GetTickCount();
694 }
695 #else
696 #error "I don't know how to measure time on your platform"
697 #endif
699 void thread_func(void *tdata, VirtProcr *VProc) {
700 int i;
701 struct thread_data *td = (struct thread_data*)tdata;
703 VPThread__mutex_lock(start_mutex, VProc);
704 while(!start)
705 VPThread__cond_wait(start_cond, VProc);
706 VPThread__mutex_unlock(start_mutex, VProc);
708 for(i=0; i<td->sl_count; i++) {
709 render_scanline(xres, yres, i + td->sl_start, td->pixels, rays_per_pixel);
710 }
712 VPThread__mutex_lock(end_mutex, VProc);
713 end++;
714 VPThread__cond_signal(end_cond, VProc);
715 VPThread__mutex_unlock(end_mutex, VProc);
717 VPThread__dissipate_thread(VProc);
718 }