dungeon_crawler
view prototype/src/main.cc @ 23:fa8f89d06f6f
progress with light rendering
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Thu, 23 Aug 2012 00:10:10 +0300 |
parents | 5c41e6fcb300 |
children | e122ba214ee1 |
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(level);
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(level);
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(level);
153 }
155 glutSwapBuffers();
156 assert(glGetError() == GL_NO_ERROR);
158 usleep(10000);
159 }
161 void view_matrix(int eye)
162 {
163 float offs = stereo_eye_sep * eye * 0.5;
164 glTranslatef(-offs, 0, 0);
165 cam.use_inverse();
166 }
168 void proj_matrix(int eye)
169 {
170 static const float fov = M_PI / 4.0;
171 static const float near_clip = 0.1;
172 static const float far_clip = 100.0;
174 float top = near_clip * tan(fov * 0.5);
175 float right = top * (float)cfg.width / (float)cfg.height;
177 float frust_shift = -(float)eye * (stereo_eye_sep * 0.5 * near_clip / stereo_focus_dist);
178 glFrustum(-right + frust_shift, right + frust_shift, -top, top, near_clip, far_clip);
179 }
182 void update(unsigned long msec)
183 {
184 static unsigned long last_upd;
186 if(last_upd == 0) {
187 last_upd = msec;
188 }
189 float dt = (float)(msec - last_upd) / 1000.0;
191 float offs = 2.5 * dt;
192 float dx = 0, dy = 0;
194 // handle key input
195 if(keystate['w'] || keystate['W']) {
196 dy -= offs;
197 }
198 if(keystate['s'] || keystate['S']) {
199 dy += offs;
200 }
201 if(keystate['d'] || keystate['D']) {
202 dx += offs;
203 }
204 if(keystate['a'] || keystate['A']) {
205 dx -= offs;
206 }
208 cam.input_move(dx, 0, dy);
210 last_upd = msec;
211 }
213 void reshape(int x, int y)
214 {
215 glViewport(0, 0, x, y);
216 cfg.width = x;
217 cfg.height = y;
218 }
220 static bool stereo_shift_pressed;
222 void keyb(unsigned char key, int x, int y)
223 {
224 switch(key) {
225 case 27:
226 exit(0);
228 case 'z':
229 stereo_shift_pressed = true;
230 break;
232 case '\n':
233 case '\r':
234 {
235 static bool fullscr;
236 if(glutGetModifiers() & GLUT_ACTIVE_ALT) {
237 fullscr = !fullscr;
238 if(fullscr) {
239 glutFullScreen();
240 } else {
241 glutPositionWindow(20, 20);
242 }
243 }
244 }
245 break;
247 default:
248 break;
249 }
251 keystate[key] = true;
252 }
254 void key_release(unsigned char key, int x, int y)
255 {
256 switch(key) {
257 case 'z':
258 stereo_shift_pressed = false;
259 break;
261 default:
262 break;
263 }
265 keystate[key] = false;
266 }
268 static int prev_x, prev_y;
269 static bool bnstate[32];
271 void mouse(int bn, int state, int x, int y)
272 {
273 prev_x = x;
274 prev_y = y;
275 bnstate[bn - GLUT_LEFT_BUTTON] = state == GLUT_DOWN;
276 }
278 void motion(int x, int y)
279 {
280 int dx = x - prev_x;
281 int dy = y - prev_y;
282 prev_x = x;
283 prev_y = y;
285 if(stereo_shift_pressed) {
286 if(dy != 0) {
287 stereo_focus_dist += dy * 0.01;
288 stereo_eye_sep = stereo_focus_dist / 30.0;
289 printf("foc: %f, sep: %f\n", stereo_focus_dist, stereo_eye_sep);
290 glutPostRedisplay();
291 }
292 return;
293 }
295 if(bnstate[0]) {
296 cam.input_rotate(dy * 0.01, dx * 0.01, 0);
297 glutPostRedisplay();
298 }
299 if(bnstate[2]) {
300 cam.input_zoom(dy * 0.1);
301 glutPostRedisplay();
302 }
303 }