dungeon_crawler
view 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 source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <assert.h>
4 #include <unistd.h>
5 #include "opengl.h"
6 #include "level.h"
7 #include "camera.h"
8 #include "datapath.h"
9 #include "tileset.h"
10 #include "renderer.h"
11 #include "cfg.h"
13 bool init(int xsz, int ysz);
14 void cleanup();
15 void idle();
16 void disp();
17 void draw();
18 void view_matrix(int eye);
19 void proj_matrix(int eye);
20 void update(unsigned long msec);
21 void reshape(int x, int y);
22 void keyb(unsigned char key, int x, int y);
23 void key_release(unsigned char key, int x, int y);
24 void mouse(int bn, int state, int x, int y);
25 void motion(int x, int y);
27 static TileSet *tileset;
28 static Level *level;
30 static FpsCamera cam;
31 static bool keystate[256];
33 static float stereo_focus_dist = 0.25;
34 static float stereo_eye_sep = stereo_focus_dist / 30.0;
37 int main(int argc, char **argv)
38 {
39 glutInit(&argc, argv);
41 if(!cfg.parse_args(argc, argv)) {
42 return 1;
43 }
45 glutInitWindowSize(cfg.width, cfg.height);
46 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | (cfg.stereo ? GLUT_STEREO : 0));
47 glutCreateWindow("dungeon crawler prototype");
49 glutIdleFunc(idle);
50 glutDisplayFunc(disp);
51 glutReshapeFunc(reshape);
52 glutKeyboardFunc(keyb);
53 glutKeyboardUpFunc(key_release);
54 glutMouseFunc(mouse);
55 glutMotionFunc(motion);
57 glewInit();
59 if(!init(cfg.width, cfg.height)) {
60 return 1;
61 }
63 glutMainLoop();
64 }
66 bool init(int xsz, int ysz)
67 {
68 glEnable(GL_LIGHTING);
69 glEnable(GL_LIGHT0);
70 float ldir[] = {-1, 1, 2, 0};
71 glLightfv(GL_LIGHT0, GL_POSITION, ldir);
72 glEnable(GL_NORMALIZE);
74 glEnable(GL_DEPTH_TEST);
75 glEnable(GL_CULL_FACE);
76 glEnable(GL_MULTISAMPLE);
78 add_data_path("data");
79 add_data_path("sdr");
81 if(!init_renderer(xsz, ysz)) {
82 return false;
83 }
85 // load a tileset
86 tileset = new TileSet;
87 printf("loading tileset: %s\n", cfg.tileset_file);
88 if(!tileset->load(datafile_path(cfg.tileset_file))) {
89 return false;
90 }
91 set_active_tileset(tileset);
93 level = new Level;
94 printf("loading level: %s\n", cfg.level_file);
95 if(!level->load(datafile_path(cfg.level_file))) {
96 return false;
97 }
99 cam.input_move(0, 0.5, 0);
101 return true;
102 }
104 void cleanup()
105 {
106 delete level;
107 delete tileset;
109 destroy_renderer();
110 }
112 void idle()
113 {
114 glutPostRedisplay();
115 }
117 void disp()
118 {
119 update(glutGet(GLUT_ELAPSED_TIME));
121 if(cfg.stereo) {
122 glDrawBuffer(GL_BACK_LEFT);
124 glMatrixMode(GL_PROJECTION);
125 glLoadIdentity();
126 proj_matrix(-1);
127 glMatrixMode(GL_MODELVIEW);
128 glLoadIdentity();
129 view_matrix(-1);
131 render_deferred(draw);
133 glDrawBuffer(GL_BACK_RIGHT);
135 glMatrixMode(GL_PROJECTION);
136 glLoadIdentity();
137 proj_matrix(1);
138 glMatrixMode(GL_MODELVIEW);
139 glLoadIdentity();
140 view_matrix(1);
142 render_deferred(draw);
144 } else {
145 glMatrixMode(GL_PROJECTION);
146 glLoadIdentity();
147 proj_matrix(0);
148 glMatrixMode(GL_MODELVIEW);
149 glLoadIdentity();
150 view_matrix(0);
152 render_deferred(draw);
153 }
155 glutSwapBuffers();
156 assert(glGetError() == GL_NO_ERROR);
158 usleep(10000);
159 }
161 void draw()
162 {
163 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
164 level->draw();
165 }
167 void view_matrix(int eye)
168 {
169 float offs = stereo_eye_sep * eye * 0.5;
170 glTranslatef(-offs, 0, 0);
171 cam.use_inverse();
172 }
174 void proj_matrix(int eye)
175 {
176 static const float fov = M_PI / 4.0;
177 static const float near_clip = 0.1;
178 static const float far_clip = 100.0;
180 float top = near_clip * tan(fov * 0.5);
181 float right = top * (float)cfg.width / (float)cfg.height;
183 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
184 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
185 }
188 void update(unsigned long msec)
189 {
190 static unsigned long last_upd;
192 if(last_upd == 0) {
193 last_upd = msec;
194 }
195 float dt = (float)(msec - last_upd) / 1000.0;
197 float offs = 2.5 * dt;
198 float dx = 0, dy = 0;
200 // handle key input
201 if(keystate['w'] || keystate['W']) {
202 dy -= offs;
203 }
204 if(keystate['s'] || keystate['S']) {
205 dy += offs;
206 }
207 if(keystate['d'] || keystate['D']) {
208 dx += offs;
209 }
210 if(keystate['a'] || keystate['A']) {
211 dx -= offs;
212 }
214 cam.input_move(dx, 0, dy);
216 last_upd = msec;
217 }
219 void reshape(int x, int y)
220 {
221 glViewport(0, 0, x, y);
222 cfg.width = x;
223 cfg.height = y;
224 }
226 static bool stereo_shift_pressed;
228 void keyb(unsigned char key, int x, int y)
229 {
230 switch(key) {
231 case 27:
232 exit(0);
234 case 'z':
235 stereo_shift_pressed = true;
236 break;
238 case '\n':
239 case '\r':
240 {
241 static bool fullscr;
242 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
243 fullscr = !fullscr;
244 if(fullscr) {
245 glutFullScreen();
246 } else {
247 glutPositionWindow(20, 20);
248 }
249 }
250 }
251 break;
253 default:
254 break;
255 }
257 keystate[key] = true;
258 }
260 void key_release(unsigned char key, int x, int y)
261 {
262 switch(key) {
263 case 'z':
264 stereo_shift_pressed = false;
265 break;
267 default:
268 break;
269 }
271 keystate[key] = false;
272 }
274 static int prev_x, prev_y;
275 static bool bnstate[32];
277 void mouse(int bn, int state, int x, int y)
278 {
279 prev_x = x;
280 prev_y = y;
281 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
282 }
284 void motion(int x, int y)
285 {
286 int dx = x - prev_x;
287 int dy = y - prev_y;
288 prev_x = x;
289 prev_y = y;
291 if(stereo_shift_pressed) {
292 if(dy != 0) {
293 stereo_focus_dist += dy * 0.01;
294 stereo_eye_sep = stereo_focus_dist / 30.0;
295 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
296 glutPostRedisplay();
297 }
298 return;
299 }
301 if(bnstate[0]) {
302 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
303 glutPostRedisplay();
304 }
305 if(bnstate[2]) {
306 cam.input_zoom(dy * 0.1);
307 glutPostRedisplay();
308 }
309 }