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