rayzor

annotate src/main.cc @ 17:79609d482762

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