Mercurial > cgi-bin > hgwebdir.cgi > PR > Applications > Vthread > Vthread__Best_Effort_Msg__Bench
view c-ray-mt.c @ 1:3840d91821c4
VPThread version is working
| author | Merten Sach <msach@mailbox.tu-berlin.de> |
|---|---|
| date | Tue, 16 Aug 2011 20:31:31 +0200 |
| parents | 4ae1d7ffb1ae |
| children | 75c818c6cad1 |
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 }
248 if(!(pixels = malloc(xres * yres * sizeof *pixels))) {
249 perror("pixel buffer allocation failed");
250 return EXIT_FAILURE;
251 }
252 load_scene(infile);
254 //This is the transition to the VMS runtime
255 VPThread__create_seed_procr_and_do_work(raytrace, (void*)pixels);
257 /* output statistics to stderr */
258 fprintf(stderr, "Rendering took: %lu seconds (%lu milliseconds)\n", rend_time / 1000, rend_time);
260 /* output the image */
261 fprintf(outfile, "P6\n%d %d\n255\n", xres, yres);
262 for(i=0; i<xres * yres; i++) {
263 fputc((pixels[i] >> RSHIFT) & 0xff, outfile);
264 fputc((pixels[i] >> GSHIFT) & 0xff, outfile);
265 fputc((pixels[i] >> BSHIFT) & 0xff, outfile);
266 }
267 fflush(outfile);
269 if(infile != stdin) fclose(infile);
270 if(outfile != stdout) fclose(outfile);
272 struct sphere *walker = obj_list;
273 while(walker) {
274 struct sphere *tmp = walker;
275 walker = walker->next;
276 free(tmp);
277 }
278 free(pixels);
279 return 0;
280 }
282 /* this is run after the VMS is set up*/
283 void raytrace(void *pixels, VirtProcr *VProc)
284 {
285 int i;
286 double sl, sl_per_thread;
288 /* initialize the random number tables for the jitter */
289 for(i=0; i<NRAN; i++) urand[i].x = (double)rand() / RAND_MAX - 0.5;
290 for(i=0; i<NRAN; i++) urand[i].y = (double)rand() / RAND_MAX - 0.5;
291 for(i=0; i<NRAN; i++) irand[i] = (int)(NRAN * ((double)rand() / RAND_MAX));
293 if(thread_num > yres) {
294 fprintf(stderr, "more threads than scanlines specified, reducing number of threads to %d\n", yres);
295 thread_num = yres;
296 }
299 if(!(threads = VPThread__malloc(thread_num * sizeof(thread_data), VProc))) {
300 perror("failed to allocate thread table");
301 exit(EXIT_FAILURE);
302 }
304 end_mutex = VPThread__make_mutex(VProc);
305 end_cond = VPThread__make_cond(end_mutex, VProc);
306 start_mutex = VPThread__make_mutex(VProc);
307 start_cond = VPThread__make_cond(start_mutex, VProc);
309 sl = 0.0;
310 sl_per_thread = (double)yres / (double)thread_num;
311 for(i=0; i<thread_num; i++) {
312 threads[i].sl_start = (int)sl;
313 sl += sl_per_thread;
314 threads[i].sl_count = (int)sl - threads[i].sl_start;
315 threads[i].pixels = (uint32_t*)pixels;
317 threads[i].VP =
318 VPThread__create_thread((VirtProcrFnPtr)thread_func,
319 (void*)(&threads[i]), VProc);
320 }
322 threads[thread_num - 1].sl_count = yres - threads[thread_num - 1].sl_start;
324 fprintf(stderr, VER_STR, VER_MAJOR, VER_MINOR);
326 // start worker threads
327 //printf("start of worker thread (%d)\n", VProc->procrID);
328 VPThread__mutex_lock(start_mutex, VProc);
329 start_time = get_msec();
330 start = 1;
331 for(i=0; i<thread_num; i++)
332 VPThread__cond_signal(start_cond, VProc);
333 VPThread__mutex_unlock(start_mutex, VProc);
335 //printf("wait for worker (%d)\n", VProc->procrID);
336 VPThread__mutex_lock(end_mutex, VProc);
337 while(end < thread_num)
338 VPThread__cond_wait(end_cond, VProc);
339 VPThread__mutex_unlock(end_mutex, VProc);
341 rend_time = get_msec() - start_time;
343 VPThread__free(threads,VProc);
344 VPThread__dissipate_thread(VProc);
345 }
347 /* render a frame of xsz/ysz dimensions into the provided framebuffer */
348 void render_scanline(int xsz, int ysz, int sl, uint32_t *fb, int samples) {
349 int i, s;
350 double rcp_samples = 1.0 / (double)samples;
352 for(i=0; i<xsz; i++) {
353 double r, g, b;
354 r = g = b = 0.0;
356 for(s=0; s<samples; s++) {
357 struct vec3 col = trace(get_primary_ray(i, sl, s), 0);
358 r += col.x;
359 g += col.y;
360 b += col.z;
361 }
363 r = r * rcp_samples;
364 g = g * rcp_samples;
365 b = b * rcp_samples;
367 fb[sl * xsz + i] = ((uint32_t)(MIN(r, 1.0) * 255.0) & 0xff) << RSHIFT |
368 ((uint32_t)(MIN(g, 1.0) * 255.0) & 0xff) << GSHIFT |
369 ((uint32_t)(MIN(b, 1.0) * 255.0) & 0xff) << BSHIFT;
370 }
371 }
373 /* trace a ray throught the scene recursively (the recursion happens through
374 * shade() to calculate reflection rays if necessary).
375 */
376 struct vec3 trace(struct ray ray, int depth) {
377 struct vec3 col;
378 struct spoint sp, nearest_sp;
379 struct sphere *nearest_obj = 0;
380 struct sphere *iter = obj_list->next;
382 /* if we reached the recursion limit, bail out */
383 if(depth >= MAX_RAY_DEPTH) {
384 col.x = col.y = col.z = 0.0;
385 return col;
386 }
388 /* find the nearest intersection ... */
389 while(iter) {
390 if(ray_sphere(iter, ray, &sp)) {
391 if(!nearest_obj || sp.dist < nearest_sp.dist) {
392 nearest_obj = iter;
393 nearest_sp = sp;
394 }
395 }
396 iter = iter->next;
397 }
399 /* and perform shading calculations as needed by calling shade() */
400 if(nearest_obj) {
401 col = shade(nearest_obj, &nearest_sp, depth);
402 } else {
403 col.x = col.y = col.z = 0.0;
404 }
406 return col;
407 }
409 /* Calculates direct illumination with the phong reflectance model.
410 * Also handles reflections by calling trace again, if necessary.
411 */
412 struct vec3 shade(struct sphere *obj, struct spoint *sp, int depth) {
413 int i;
414 struct vec3 col = {0, 0, 0};
416 /* for all lights ... */
417 for(i=0; i<lnum; i++) {
418 double ispec, idiff;
419 struct vec3 ldir;
420 struct ray shadow_ray;
421 struct sphere *iter = obj_list->next;
422 int in_shadow = 0;
424 ldir.x = lights[i].x - sp->pos.x;
425 ldir.y = lights[i].y - sp->pos.y;
426 ldir.z = lights[i].z - sp->pos.z;
428 shadow_ray.orig = sp->pos;
429 shadow_ray.dir = ldir;
431 /* shoot shadow rays to determine if we have a line of sight with the light */
432 while(iter) {
433 if(ray_sphere(iter, shadow_ray, 0)) {
434 in_shadow = 1;
435 break;
436 }
437 iter = iter->next;
438 }
440 /* and if we're not in shadow, calculate direct illumination with the phong model. */
441 if(!in_shadow) {
442 NORMALIZE(ldir);
444 idiff = MAX(DOT(sp->normal, ldir), 0.0);
445 ispec = obj->mat.spow > 0.0 ? pow(MAX(DOT(sp->vref, ldir), 0.0), obj->mat.spow) : 0.0;
447 col.x += idiff * obj->mat.col.x + ispec;
448 col.y += idiff * obj->mat.col.y + ispec;
449 col.z += idiff * obj->mat.col.z + ispec;
450 }
451 }
453 /* Also, if the object is reflective, spawn a reflection ray, and call trace()
454 * to calculate the light arriving from the mirror direction.
455 */
456 if(obj->mat.refl > 0.0) {
457 struct ray ray;
458 struct vec3 rcol;
460 ray.orig = sp->pos;
461 ray.dir = sp->vref;
462 ray.dir.x *= RAY_MAG;
463 ray.dir.y *= RAY_MAG;
464 ray.dir.z *= RAY_MAG;
466 rcol = trace(ray, depth + 1);
467 col.x += rcol.x * obj->mat.refl;
468 col.y += rcol.y * obj->mat.refl;
469 col.z += rcol.z * obj->mat.refl;
470 }
472 return col;
473 }
475 /* calculate reflection vector */
476 struct vec3 reflect(struct vec3 v, struct vec3 n) {
477 struct vec3 res;
478 double dot = v.x * n.x + v.y * n.y + v.z * n.z;
479 res.x = -(2.0 * dot * n.x - v.x);
480 res.y = -(2.0 * dot * n.y - v.y);
481 res.z = -(2.0 * dot * n.z - v.z);
482 return res;
483 }
485 struct vec3 cross_product(struct vec3 v1, struct vec3 v2) {
486 struct vec3 res;
487 res.x = v1.y * v2.z - v1.z * v2.y;
488 res.y = v1.z * v2.x - v1.x * v2.z;
489 res.z = v1.x * v2.y - v1.y * v2.x;
490 return res;
491 }
493 /* determine the primary ray corresponding to the specified pixel (x, y) */
494 struct ray get_primary_ray(int x, int y, int sample) {
495 struct ray ray;
496 float m[3][3];
497 struct vec3 i, j = {0, 1, 0}, k, dir, orig, foo;
499 k.x = cam.targ.x - cam.pos.x;
500 k.y = cam.targ.y - cam.pos.y;
501 k.z = cam.targ.z - cam.pos.z;
502 NORMALIZE(k);
504 i = cross_product(j, k);
505 j = cross_product(k, i);
506 m[0][0] = i.x; m[0][1] = j.x; m[0][2] = k.x;
507 m[1][0] = i.y; m[1][1] = j.y; m[1][2] = k.y;
508 m[2][0] = i.z; m[2][1] = j.z; m[2][2] = k.z;
510 ray.orig.x = ray.orig.y = ray.orig.z = 0.0;
511 ray.dir = get_sample_pos(x, y, sample);
512 ray.dir.z = 1.0 / HALF_FOV;
513 ray.dir.x *= RAY_MAG;
514 ray.dir.y *= RAY_MAG;
515 ray.dir.z *= RAY_MAG;
517 dir.x = ray.dir.x + ray.orig.x;
518 dir.y = ray.dir.y + ray.orig.y;
519 dir.z = ray.dir.z + ray.orig.z;
520 foo.x = dir.x * m[0][0] + dir.y * m[0][1] + dir.z * m[0][2];
521 foo.y = dir.x * m[1][0] + dir.y * m[1][1] + dir.z * m[1][2];
522 foo.z = dir.x * m[2][0] + dir.y * m[2][1] + dir.z * m[2][2];
524 orig.x = ray.orig.x * m[0][0] + ray.orig.y * m[0][1] + ray.orig.z * m[0][2] + cam.pos.x;
525 orig.y = ray.orig.x * m[1][0] + ray.orig.y * m[1][1] + ray.orig.z * m[1][2] + cam.pos.y;
526 orig.z = ray.orig.x * m[2][0] + ray.orig.y * m[2][1] + ray.orig.z * m[2][2] + cam.pos.z;
528 ray.orig = orig;
529 ray.dir.x = foo.x + orig.x;
530 ray.dir.y = foo.y + orig.y;
531 ray.dir.z = foo.z + orig.z;
533 return ray;
534 }
537 struct vec3 get_sample_pos(int x, int y, int sample) {
538 struct vec3 pt;
539 static double sf = 0.0;
541 if(sf == 0.0) {
542 sf = 1.5 / (double)xres;
543 }
545 pt.x = ((double)x / (double)xres) - 0.5;
546 pt.y = -(((double)y / (double)yres) - 0.65) / aspect;
548 if(sample) {
549 struct vec3 jt = jitter(x, y, sample);
550 pt.x += jt.x * sf;
551 pt.y += jt.y * sf / aspect;
552 }
553 return pt;
554 }
556 /* jitter function taken from Graphics Gems I. */
557 struct vec3 jitter(int x, int y, int s) {
558 struct vec3 pt;
559 pt.x = urand[(x + (y << 2) + irand[(x + s) & MASK]) & MASK].x;
560 pt.y = urand[(y + (x << 2) + irand[(y + s) & MASK]) & MASK].y;
561 return pt;
562 }
564 /* Calculate ray-sphere intersection, and return {1, 0} to signify hit or no hit.
565 * Also the surface point parameters like position, normal, etc are returned through
566 * the sp pointer if it is not NULL.
567 */
568 int ray_sphere(const struct sphere *sph, struct ray ray, struct spoint *sp) {
569 double a, b, c, d, sqrt_d, t1, t2;
571 a = SQ(ray.dir.x) + SQ(ray.dir.y) + SQ(ray.dir.z);
572 b = 2.0 * ray.dir.x * (ray.orig.x - sph->pos.x) +
573 2.0 * ray.dir.y * (ray.orig.y - sph->pos.y) +
574 2.0 * ray.dir.z * (ray.orig.z - sph->pos.z);
575 c = SQ(sph->pos.x) + SQ(sph->pos.y) + SQ(sph->pos.z) +
576 SQ(ray.orig.x) + SQ(ray.orig.y) + SQ(ray.orig.z) +
577 2.0 * (-sph->pos.x * ray.orig.x - sph->pos.y * ray.orig.y - sph->pos.z * ray.orig.z) - SQ(sph->rad);
579 if((d = SQ(b) - 4.0 * a * c) < 0.0) return 0;
581 sqrt_d = sqrt(d);
582 t1 = (-b + sqrt_d) / (2.0 * a);
583 t2 = (-b - sqrt_d) / (2.0 * a);
585 if((t1 < ERR_MARGIN && t2 < ERR_MARGIN) || (t1 > 1.0 && t2 > 1.0)) return 0;
587 if(sp) {
588 if(t1 < ERR_MARGIN) t1 = t2;
589 if(t2 < ERR_MARGIN) t2 = t1;
590 sp->dist = t1 < t2 ? t1 : t2;
592 sp->pos.x = ray.orig.x + ray.dir.x * sp->dist;
593 sp->pos.y = ray.orig.y + ray.dir.y * sp->dist;
594 sp->pos.z = ray.orig.z + ray.dir.z * sp->dist;
596 sp->normal.x = (sp->pos.x - sph->pos.x) / sph->rad;
597 sp->normal.y = (sp->pos.y - sph->pos.y) / sph->rad;
598 sp->normal.z = (sp->pos.z - sph->pos.z) / sph->rad;
600 sp->vref = reflect(ray.dir, sp->normal);
601 NORMALIZE(sp->vref);
602 }
603 return 1;
604 }
606 /* Load the scene from an extremely simple scene description file */
607 #define DELIM " \t\n"
608 void load_scene(FILE *fp) {
609 char line[256], *ptr, type;
611 obj_list = malloc(sizeof(struct sphere));
612 obj_list->next = 0;
614 while((ptr = fgets(line, 256, fp))) {
615 int i;
616 struct vec3 pos, col;
617 double rad, spow, refl;
619 while(*ptr == ' ' || *ptr == '\t') ptr++;
620 if(*ptr == '#' || *ptr == '\n') continue;
622 if(!(ptr = strtok(line, DELIM))) continue;
623 type = *ptr;
625 for(i=0; i<3; i++) {
626 if(!(ptr = strtok(0, DELIM))) break;
627 *((double*)&pos.x + i) = atof(ptr);
628 }
630 if(type == 'l') {
631 lights[lnum++] = pos;
632 continue;
633 }
635 if(!(ptr = strtok(0, DELIM))) continue;
636 rad = atof(ptr);
638 for(i=0; i<3; i++) {
639 if(!(ptr = strtok(0, DELIM))) break;
640 *((double*)&col.x + i) = atof(ptr);
641 }
643 if(type == 'c') {
644 cam.pos = pos;
645 cam.targ = col;
646 cam.fov = rad;
647 continue;
648 }
650 if(!(ptr = strtok(0, DELIM))) continue;
651 spow = atof(ptr);
653 if(!(ptr = strtok(0, DELIM))) continue;
654 refl = atof(ptr);
656 if(type == 's') {
657 struct sphere *sph = malloc(sizeof *sph);
658 sph->next = obj_list->next;
659 obj_list->next = sph;
661 sph->pos = pos;
662 sph->rad = rad;
663 sph->mat.col = col;
664 sph->mat.spow = spow;
665 sph->mat.refl = refl;
666 } else {
667 fprintf(stderr, "unknown type: %c\n", type);
668 }
669 }
670 }
673 /* provide a millisecond-resolution timer for each system */
674 #if defined(unix) || defined(__unix__)
675 #include <time.h>
676 #include <sys/time.h>
677 unsigned long get_msec(void) {
678 static struct timeval timeval, first_timeval;
680 gettimeofday(&timeval, 0);
681 if(first_timeval.tv_sec == 0) {
682 first_timeval = timeval;
683 return 0;
684 }
685 return (timeval.tv_sec - first_timeval.tv_sec) * 1000 + (timeval.tv_usec - first_timeval.tv_usec) / 1000;
686 }
687 #elif defined(__WIN32__) || defined(WIN32)
688 #include <windows.h>
689 unsigned long get_msec(void) {
690 return GetTickCount();
691 }
692 #else
693 #error "I don't know how to measure time on your platform"
694 #endif
696 void thread_func(void *tdata, VirtProcr *VProc) {
697 int i;
698 struct thread_data *td = (struct thread_data*)tdata;
700 VPThread__mutex_lock(start_mutex, VProc);
701 while(!start)
702 VPThread__cond_wait(start_cond, VProc);
703 VPThread__mutex_unlock(start_mutex, VProc);
705 for(i=0; i<td->sl_count; i++) {
706 render_scanline(xres, yres, i + td->sl_start, td->pixels, rays_per_pixel);
707 }
709 VPThread__mutex_lock(end_mutex, VProc);
710 end++;
711 VPThread__cond_signal(end_cond, VProc);
712 VPThread__mutex_unlock(end_mutex, VProc);
714 VPThread__dissipate_thread(VProc);
715 }
