dungeon_crawler
diff prototype/src/main.cc @ 18:5c41e6fcb300
- commandline arguments
- stereoscopic rendering
- FBO fixed
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 21 Aug 2012 03:17:48 +0300 |
parents | d98240a13793 |
children | fa8f89d06f6f |
line diff
1.1 --- a/prototype/src/main.cc Mon Aug 20 06:11:58 2012 +0300 1.2 +++ b/prototype/src/main.cc Tue Aug 21 03:17:48 2012 +0300 1.3 @@ -8,12 +8,15 @@ 1.4 #include "datapath.h" 1.5 #include "tileset.h" 1.6 #include "renderer.h" 1.7 +#include "cfg.h" 1.8 1.9 bool init(int xsz, int ysz); 1.10 void cleanup(); 1.11 void idle(); 1.12 void disp(); 1.13 void draw(); 1.14 +void view_matrix(int eye); 1.15 +void proj_matrix(int eye); 1.16 void update(unsigned long msec); 1.17 void reshape(int x, int y); 1.18 void keyb(unsigned char key, int x, int y); 1.19 @@ -27,23 +30,21 @@ 1.20 static FpsCamera cam; 1.21 static bool keystate[256]; 1.22 1.23 -static const char *level_file = "0.level"; 1.24 +static float stereo_focus_dist = 0.25; 1.25 +static float stereo_eye_sep = stereo_focus_dist / 30.0; 1.26 + 1.27 1.28 int main(int argc, char **argv) 1.29 { 1.30 - int xsz, ysz; 1.31 - 1.32 glutInit(&argc, argv); 1.33 1.34 - if(argc > 1) { 1.35 - level_file = argv[1]; 1.36 + if(!cfg.parse_args(argc, argv)) { 1.37 + return 1; 1.38 } 1.39 1.40 - glutInitWindowSize(800, 600); 1.41 - glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE); 1.42 - glutCreateWindow("prototype"); 1.43 - xsz = glutGet(GLUT_WINDOW_WIDTH); 1.44 - ysz = glutGet(GLUT_WINDOW_HEIGHT); 1.45 + glutInitWindowSize(cfg.width, cfg.height); 1.46 + glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0)); 1.47 + glutCreateWindow("dungeon crawler prototype"); 1.48 1.49 glutIdleFunc(idle); 1.50 glutDisplayFunc(disp); 1.51 @@ -55,7 +56,7 @@ 1.52 1.53 glewInit(); 1.54 1.55 - if(!init(xsz, ysz)) { 1.56 + if(!init(cfg.width, cfg.height)) { 1.57 return 1; 1.58 } 1.59 1.60 @@ -83,14 +84,15 @@ 1.61 1.62 // load a tileset 1.63 tileset = new TileSet; 1.64 - if(!tileset->load(datafile_path("default.tileset"))) { 1.65 + printf("loading tileset: %s\n", cfg.tileset_file); 1.66 + if(!tileset->load(datafile_path(cfg.tileset_file))) { 1.67 return false; 1.68 } 1.69 set_active_tileset(tileset); 1.70 1.71 level = new Level; 1.72 - printf("loading level: %s\n", level_file); 1.73 - if(!level->load(datafile_path(level_file))) { 1.74 + printf("loading level: %s\n", cfg.level_file); 1.75 + if(!level->load(datafile_path(cfg.level_file))) { 1.76 return false; 1.77 } 1.78 1.79 @@ -116,13 +118,39 @@ 1.80 { 1.81 update(glutGet(GLUT_ELAPSED_TIME)); 1.82 1.83 - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.84 + if(cfg.stereo) { 1.85 + glDrawBuffer(GL_BACK_LEFT); 1.86 1.87 - glMatrixMode(GL_MODELVIEW); 1.88 - glLoadIdentity(); 1.89 - cam.use_inverse(); 1.90 + glMatrixMode(GL_PROJECTION); 1.91 + glLoadIdentity(); 1.92 + proj_matrix(-1); 1.93 + glMatrixMode(GL_MODELVIEW); 1.94 + glLoadIdentity(); 1.95 + view_matrix(-1); 1.96 1.97 - render_deferred(draw); 1.98 + render_deferred(draw); 1.99 + 1.100 + glDrawBuffer(GL_BACK_RIGHT); 1.101 + 1.102 + glMatrixMode(GL_PROJECTION); 1.103 + glLoadIdentity(); 1.104 + proj_matrix(1); 1.105 + glMatrixMode(GL_MODELVIEW); 1.106 + glLoadIdentity(); 1.107 + view_matrix(1); 1.108 + 1.109 + render_deferred(draw); 1.110 + 1.111 + } else { 1.112 + glMatrixMode(GL_PROJECTION); 1.113 + glLoadIdentity(); 1.114 + proj_matrix(0); 1.115 + glMatrixMode(GL_MODELVIEW); 1.116 + glLoadIdentity(); 1.117 + view_matrix(0); 1.118 + 1.119 + render_deferred(draw); 1.120 + } 1.121 1.122 glutSwapBuffers(); 1.123 assert(glGetError() == GL_NO_ERROR); 1.124 @@ -132,9 +160,31 @@ 1.125 1.126 void draw() 1.127 { 1.128 + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 1.129 level->draw(); 1.130 } 1.131 1.132 +void view_matrix(int eye) 1.133 +{ 1.134 + float offs = stereo_eye_sep * eye * 0.5; 1.135 + glTranslatef(-offs, 0, 0); 1.136 + cam.use_inverse(); 1.137 +} 1.138 + 1.139 +void proj_matrix(int eye) 1.140 +{ 1.141 + static const float fov = M_PI / 4.0; 1.142 + static const float near_clip = 0.1; 1.143 + static const float far_clip = 100.0; 1.144 + 1.145 + float top = near_clip * tan(fov * 0.5); 1.146 + float right = top * (float)cfg.width / (float)cfg.height; 1.147 + 1.148 + float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist); 1.149 + glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip); 1.150 +} 1.151 + 1.152 + 1.153 void update(unsigned long msec) 1.154 { 1.155 static unsigned long last_upd; 1.156 @@ -169,16 +219,39 @@ 1.157 void reshape(int x, int y) 1.158 { 1.159 glViewport(0, 0, x, y); 1.160 - glMatrixMode(GL_PROJECTION); 1.161 - glLoadIdentity(); 1.162 - gluPerspective(45.0, (float)x / (float)y, 0.25, 100.0); 1.163 + cfg.width = x; 1.164 + cfg.height = y; 1.165 } 1.166 1.167 +static bool stereo_shift_pressed; 1.168 + 1.169 void keyb(unsigned char key, int x, int y) 1.170 { 1.171 switch(key) { 1.172 case 27: 1.173 exit(0); 1.174 + 1.175 + case 'z': 1.176 + stereo_shift_pressed = true; 1.177 + break; 1.178 + 1.179 + case '\n': 1.180 + case '\r': 1.181 + { 1.182 + static bool fullscr; 1.183 + if(glutGetModifiers() & GLUT_ACTIVE_ALT) { 1.184 + fullscr = !fullscr; 1.185 + if(fullscr) { 1.186 + glutFullScreen(); 1.187 + } else { 1.188 + glutPositionWindow(20, 20); 1.189 + } 1.190 + } 1.191 + } 1.192 + break; 1.193 + 1.194 + default: 1.195 + break; 1.196 } 1.197 1.198 keystate[key] = true; 1.199 @@ -186,6 +259,15 @@ 1.200 1.201 void key_release(unsigned char key, int x, int y) 1.202 { 1.203 + switch(key) { 1.204 + case 'z': 1.205 + stereo_shift_pressed = false; 1.206 + break; 1.207 + 1.208 + default: 1.209 + break; 1.210 + } 1.211 + 1.212 keystate[key] = false; 1.213 } 1.214 1.215 @@ -206,6 +288,16 @@ 1.216 prev_x = x; 1.217 prev_y = y; 1.218 1.219 + if(stereo_shift_pressed) { 1.220 + if(dy != 0) { 1.221 + stereo_focus_dist += dy * 0.01; 1.222 + stereo_eye_sep = stereo_focus_dist / 30.0; 1.223 + printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep); 1.224 + glutPostRedisplay(); 1.225 + } 1.226 + return; 1.227 + } 1.228 + 1.229 if(bnstate[0]) { 1.230 cam.input_rotate(dy * 0.01, dx * 0.01, 0); 1.231 glutPostRedisplay();