rayzor
view src/main.cc @ 19:252999cd1a3f
added reflection and refraction
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 15 Apr 2014 00:59:37 +0300 |
parents | 859ccadca671 |
children |
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <signal.h>
5 #include <errno.h>
6 #include <float.h>
7 #include <direct.h>
8 #include "inttypes.h"
9 #include "gfx.h"
10 #include "keyb.h"
11 #include "mouse.h"
12 #include "logger.h"
13 #include "scene.h"
14 #include "rayzor.h"
15 #include "screen.h"
16 #include "modeller.h"
17 #include "renderer.h"
18 #include "scrman.h"
19 #include "timer.h"
21 #ifdef __DOS__
22 #undef USE_ASM_SWAPBUF
23 #endif
25 #ifdef USE_ASM_SWAPBUF
26 // defined in swapbuf.asm
27 extern "C" void swap_buffers_asm(void *dest, void *src, int xsz, int ysz, int bpp);
28 #endif
30 static bool init();
31 static void cleanup();
32 static void display();
33 static void swap_buffers();
34 static void draw_cursor(uint32_t *buf, int mx, int my);
35 static void screenshot();
36 static void handle_keyboard();
37 static void handle_mouse();
38 static bool parse_args(int argc, char **argv);
39 static void sig(int s);
41 uint32_t *fb_pixels;
42 int fb_width = 640;
43 int fb_height = 480;
44 int fb_bpp = 32;
45 Scene *scene;
47 static bool novideo;
48 static void *fb;
49 static int rbits, gbits, bbits;
50 static int rshift, gshift, bshift;
51 static unsigned int rmask, gmask, bmask;
53 static bool use_asm_swap = true;
54 static bool use_mouse;
55 static int mouse_x, mouse_y;
56 static bool quit;
57 static bool cap_shot;
59 int main(int argc, char **argv)
60 {
61 unsigned long start_msec, msec;
62 unsigned long nframes = 0;
64 if(!parse_args(argc, argv)) {
65 return 1;
66 }
67 if(!init()) {
68 return 1;
69 }
71 start_msec = get_msec();
73 // main loop
74 for(;;) {
75 handle_keyboard();
76 handle_mouse();
77 if(quit) break;
79 display();
80 ++nframes;
82 if(novideo) break;
83 }
85 msec = get_msec() - start_msec;
87 cleanup();
89 printf("Average framerate: %g\n", (float)nframes / ((float)msec / 1000.0f));
90 printf("Thank you for using Rayzor!\n");
91 return 0;
92 }
94 void quit_app()
95 {
96 quit = true;
97 }
99 static bool init()
100 {
101 signal(SIGINT, sig);
102 signal(SIGSEGV, sig);
103 signal(SIGILL, sig);
104 signal(SIGFPE, sig);
106 #ifdef __WATCOM__
107 // mask all fpe except invalid op
108 _control87(~_EM_INVALID, _MCW_EM);
109 #endif
111 init_timer(128);
113 if(!novideo) {
114 if(kb_init(32) == -1) {
115 fprintf(stderr, "failed to initialize keyboard driver\n");
116 return false;
117 }
119 if(!(fb = set_video_mode(fb_width, fb_height, fb_bpp))) {
120 set_text_mode();
121 fprintf(stderr, "failed to set video mode: %dx%d %dbpp\n", fb_width, fb_height, fb_bpp);
122 return false;
123 }
124 fb_bpp = get_color_depth();
125 get_color_bits(&rbits, &gbits, &bbits);
126 get_color_shift(&rshift, &gshift, &bshift);
127 get_color_mask(&rmask, &gmask, &bmask);
129 printlog("video resolution: %dx%d\n", fb_width, fb_height);
130 printlog("bpp: %d (%d %d %d)\n", fb_bpp, rbits, gbits, bbits);
131 printlog("shift: %d %d %d\n", rshift, gshift, bshift);
132 printlog("mask: %x %x %x\n", rmask, gmask, bmask);
134 if(have_mouse()) {
135 use_mouse = true;
136 set_mouse_limits(0, 0, fb_width - 1, fb_height - 1);
137 }
138 } else {
139 logger_output(stdout);
140 printlog("novideo (debug) mode\n");
141 fb_bpp = 24;
142 rbits = gbits = bbits = 8;
143 }
145 fb_pixels = new uint32_t[fb_width * fb_height * 4];
146 if(!fb_pixels) {
147 return false;
148 }
150 scene = new Scene;
152 Sphere *sph = new Sphere;
153 sph->mtl.diffuse = Vector3(1.0, 0.3, 0.1);
154 sph->mtl.roughness = 0.4;
155 scene->add(sph);
157 Box *box = new Box;
158 box->mtl.diffuse = Vector3(0.1, 0.4, 1.0);
159 box->mtl.specular = Vector3(0.3, 0.6, 1.0);
160 box->mtl.roughness = 0.2;
161 box->mtl.reflect = 0.8;
162 box->set_position(Vector3(0, -1.1, 0));
163 box->set_scaling(Vector3(4, 0.1, 4));
164 scene->add(box);
166 Light *lt = new Light;
167 lt->set_intensity(0.8);
168 lt->set_position(Vector3(-10, 10, 10));
169 scene->add(lt);
171 Modeller *modeller = new Modeller;
172 if(!modeller->init()) {
173 return false;
174 }
175 add_screen(modeller);
177 Renderer *renderer = new Renderer;
178 if(!renderer->init()) {
179 return false;
180 }
181 add_screen(renderer);
183 activate_screen(modeller); // start the modeller screen
184 return true;
185 }
187 static void cleanup()
188 {
189 delete scene;
190 delete [] fb_pixels;
192 destroy_screens();
194 if(!novideo) {
195 set_text_mode();
196 kb_shutdown();
197 }
198 }
200 static void display()
201 {
202 Screen *scr = active_screen();
203 if(scr) {
204 scr->update();
205 scr->draw();
206 }
208 // draw the mouse cursor
209 if(use_mouse) {
210 draw_cursor(fb_pixels, mouse_x, mouse_y);
211 }
213 if(cap_shot) {
214 screenshot();
215 cap_shot = false;
216 }
218 if(!novideo) {
219 wait_vsync();
220 #ifdef USE_ASM_SWAPBUF
221 swap_buffers_asm(fb, fb_pixels, fb_width, fb_height, fb_bpp);
222 #else
223 swap_buffers();
224 #endif
225 }
226 }
228 #define PACK_RGB(r, g, b) \
229 ((((r) << rshift) & rmask) | \
230 (((g) << gshift) & gmask) | \
231 (((b) << bshift) & bmask))
233 #define UNPACK_RED(c) (((c) >> 16) & 0xff)
234 #define UNPACK_GREEN(c) (((c) >> 8) & 0xff)
235 #define UNPACK_BLUE(c) ((c) & 0xff)
237 static void swap_buffers()
238 {
239 uint32_t *src = fb_pixels;
240 int num_pixels = fb_width * fb_height;
242 switch(fb_bpp) {
243 case 32:
244 memcpy(fb, fb_pixels, num_pixels * 4);
245 break;
247 case 24:
248 {
249 unsigned char *dest = (unsigned char*)fb;
250 for(int i=0; i<num_pixels-1; i++) {
251 *((uint32_t*)dest) = *src++;
252 dest += 3;
253 }
254 *dest++ = UNPACK_RED(*src);
255 *dest++ = UNPACK_GREEN(*src);
256 *dest++ = UNPACK_BLUE(*src);
257 }
258 break;
260 case 16:
261 {
262 uint16_t *dest = (uint16_t*)fb;
263 for(int i=0; i<num_pixels; i++) {
264 uint32_t c = *src++;
265 unsigned char r = UNPACK_RED(c);
266 unsigned char g = UNPACK_GREEN(c);
267 unsigned char b = UNPACK_BLUE(c);
269 *dest++ = (((r) << 8) & 0xf800) |
270 (((g) << 3) & 0x7e0) |
271 (((b) >> 3) & 0x1f);
272 }
273 }
274 break;
276 default:
277 break;
278 }
279 }
281 static void draw_cursor(uint32_t *buf, int mx, int my)
282 {
283 uint32_t *cptr = buf + my * fb_width + mx;
284 int i, cw[2] = {4, 4}, ch[2] = {4, 4};
286 if(mx < cw[0]) cw[0] = mx;
287 if(my < ch[0]) ch[0] = my;
288 if(fb_width - mx < cw[1]) cw[1] = fb_width - mx - 1;
289 if(fb_height - my < ch[1]) ch[1] = fb_height - my - 1;
291 for(i=1; i<cw[0]; i++) {
292 int idx = -i;
293 cptr[idx] = 0xffffff;
294 }
295 for(i=1; i<cw[1]; i++) {
296 int idx = i;
297 cptr[idx] = 0xffffff;
298 }
299 for(i=1; i<ch[0]; i++) {
300 int idx = -i * fb_width;
301 cptr[idx] = 0xffffff;
302 }
303 for(i=1; i<ch[1]; i++) {
304 int idx = i * fb_width;
305 cptr[idx] = 0xffffff;
306 }
307 }
309 #define PPM_COMMENT "screenshot saved by the rayzor modeller/renderer"
310 static void screenshot()
311 {
312 static int shotidx = -1;
313 FILE *fp;
314 char fname[PATH_MAX];
316 if(shotidx == -1) {
317 DIR *dir;
318 struct dirent *dent;
320 shotidx = 0;
321 if((dir = opendir("."))) {
322 while((dent = readdir(dir))) {
323 int i, num;
324 for(i=0; dent->d_name[i]; i++) {
325 fname[i] = tolower(dent->d_name[i]);
326 }
327 fname[i] = 0;
329 if(sscanf(fname, "shot%d.ppm", &num) == 1 && num > shotidx) {
330 shotidx = num;
331 }
332 }
333 closedir(dir);
334 }
335 }
337 sprintf(fname, "shot%04d.ppm", ++shotidx);
338 if(!(fp = fopen(fname, "wb"))) {
339 printlog("failed to save screenshot %s: %s\n", fname, strerror(errno));
340 return;
341 }
343 fprintf(fp, "P6\n# " PPM_COMMENT "\n%d %d\n255\n", fb_width, fb_height);
344 for(int i=0; i<fb_width * fb_height; i++) {
345 uint32_t c = fb_pixels[i];
346 fputc(UNPACK_RED(c), fp);
347 fputc(UNPACK_GREEN(c), fp);
348 fputc(UNPACK_BLUE(c), fp);
349 }
351 fclose(fp);
352 }
354 static void handle_keyboard()
355 {
356 int key;
357 Screen *scr = active_screen();
359 if(novideo) return;
361 while((key = kb_getkey()) != -1) {
362 switch(key) {
363 case '`':
364 use_asm_swap = !use_asm_swap;
365 break;
367 case 'q':
368 case 'x':
369 if(kb_isdown(KB_ALT)) {
370 quit_app();
371 }
372 break;
374 case KB_F12:
375 cap_shot = true;
376 break;
378 default:
379 break;
380 }
381 scr->handle_keyboard(key, true); // TODO also generate release events...
382 }
383 }
385 static void handle_mouse()
386 {
387 static int prev_mx, prev_my, prev_bnmask, bndiff;
388 int mx, my, bnmask;
389 Screen *scr = active_screen();
391 if(!use_mouse || novideo) return;
393 bnmask = read_mouse(&mx, &my);
394 if(scr && (bndiff = bnmask ^ prev_bnmask)) {
395 for(int i=0; i<8; i++) {
396 int bit = 1 << i;
397 if(bndiff & bit) {
398 scr->handle_mbutton(i, bnmask & bit, mx, my);
399 }
400 }
401 }
402 prev_bnmask = bnmask;
404 if(scr && (mx != prev_mx || my != prev_my)) {
405 scr->handle_mmotion(mx, my);
406 }
407 prev_mx = mx;
408 prev_my = my;
410 mouse_x = mx;
411 mouse_y = my;
412 }
415 static struct {
416 int opt;
417 const char *lopt;
418 const char *desc;
419 } options[] = {
420 {'s', "size", "resolution <xres>x<yres>[:bpp]"},
421 {'n', "novid", "don't switch video mode (for debugging)"},
422 {'h', "help", "print usage information and exit"},
423 {-1, 0, 0}
424 };
426 static void print_usage(const char *argv0)
427 {
428 printf("%s usage\n", argv0);
429 for(int i=0; options[i].opt != -1; i++) {
430 printf(" -%c, -%s: %s\n", options[i].opt, options[i].lopt, options[i].desc);
431 }
432 exit(0);
433 }
435 static bool parse_args(int argc, char **argv)
436 {
437 for(int i=1; i<argc; i++) {
438 if(argv[i][0] == '-') {
439 int opt = -1;
441 for(int j=0; options[j].opt != -1; j++) {
442 if(argv[i][2] == 0) {
443 if(argv[i][1] == options[j].opt) {
444 opt = options[j].opt;
445 break;
446 }
447 } else {
448 if(strcmp(argv[i] + 1, options[j].lopt) == 0) {
449 opt = options[j].opt;
450 break;
451 }
452 }
453 }
455 switch(opt) {
456 case 's':
457 if(sscanf(argv[++i], "%dx%d:%d", &fb_width, &fb_height, &fb_bpp) < 2) {
458 fprintf(stderr, "%s must be followed by a resolution: WxH\n", argv[i - 1]);
459 return false;
460 }
461 break;
463 case 'n':
464 novideo = true;
465 break;
467 case 'h':
468 print_usage(argv[0]); // doesn't return
469 break;
471 default:
472 fprintf(stderr, "unknown option: %s\n", argv[i]);
473 return false;
474 }
475 } else {
476 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
477 return false;
478 }
479 }
480 return true;
481 }
483 static void sig(int s)
484 {
485 cleanup();
486 fprintf(stderr, "signal caught: %d\n", s);
488 #ifdef __WATCOM__
489 if(s == SIGFPE) {
490 unsigned int st = _status87();
491 fprintf(stderr, "fpu status: %x\n", st);
492 }
493 #endif
495 exit(1);
496 }