vrheights

view src/game.cc @ 8:3f221bdc9bab

mesh loading walk polys
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 03 Oct 2014 04:16:16 +0300
parents 0eca023ed909
children df3a70664a7d
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include <vector>
4 #include <algorithm>
5 #include <vmath/vmath.h>
6 #include "opengl.h"
7 #include "game.h"
8 #include "goatvr.h"
9 #include "teapot.h"
10 #include "console.h"
11 #include "drawtext.h"
12 #include "game_var.h"
13 #include "scenefile.h"
15 static void draw_scene();
16 static void material(float r, float g, float b, float roughness);
17 static void toggle_hmd_fullscr();
18 static void create_rtarg(int x, int y);
19 static int next_pow2(int x);
20 static void con_handle(const char *cmd);
22 static int win_width, win_height;
23 static unsigned int fb_tex;
24 static unsigned int fbo, fb_depth;
25 static int fb_xsz, fb_ysz;
26 static int fb_tex_xsz, fb_tex_ysz;
28 static unsigned int chess_tex;
30 static float cam_theta = -90, cam_phi;
31 static Vector3 cam_pos = {15.4, 0, 0};
32 static bool keystate[256];
34 static Console con;
35 static dtx_font *con_font;
37 static SceneFile scn;
39 #define GVAR_LOOK_WALK "look-walk"
40 #define GVAR_MAX_STEP "max-step"
42 bool game_init()
43 {
44 init_opengl();
46 if(!(con_font = dtx_open_font_glyphmap("data/mono14.glyphmap"))) {
47 fprintf(stderr, "failed to open console font\n");
48 return false;
49 }
50 con.set_font(con_font, 14);
51 con.set_size(7, 52);
52 con.set_position(0, 0, Console::CENTER);
53 con.set_command_func(con_handle);
54 con.set_echo(false);
57 if(vr_init() == -1) {
58 return false;
59 }
61 glEnable(GL_DEPTH_TEST);
62 glEnable(GL_CULL_FACE);
63 glEnable(GL_LIGHTING);
65 unsigned char chess_pixels[] = {
66 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff,
67 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff
68 };
69 glGenTextures(1, &chess_tex);
70 glBindTexture(GL_TEXTURE_2D, chess_tex);
71 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
72 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
73 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, chess_pixels);
75 if(!scn.load("data/sibenik.goatsce")) {
76 fprintf(stderr, "failed to load scene file\n");
77 return false;
78 }
80 /* initialize all option variables */
81 set_gvar_bool(GVAR_LOOK_WALK, true);
82 set_gvar_num(GVAR_MAX_STEP, 0.5);
84 return true;
85 }
87 void game_cleanup()
88 {
89 vr_shutdown();
90 }
92 void game_update(long tm)
93 {
94 static long prev_upd;
95 float dt = (tm - prev_upd) / 1000.0;
96 prev_upd = tm;
98 if(con.is_visible()) {
99 con.update();
100 return;
101 }
103 float offs = dt * 5.0;
104 Vector3 dir;
106 if(keystate['d'] || keystate['D']) {
107 dir += Vector3(offs, 0, 0);
108 }
109 if(keystate['a'] || keystate['A']) {
110 dir += Vector3(-offs, 0, 0);
111 }
112 if(keystate['w'] || keystate['W']) {
113 dir += Vector3(0, 0, -offs);
114 }
115 if(keystate['s'] || keystate['S']) {
116 dir += Vector3(0, 0, offs);
117 }
119 if(get_gvar_bool(GVAR_LOOK_WALK)) {
120 float rot[4];
121 vr_view_rotation(0, rot);
122 Quaternion q(rot[3], rot[0], rot[1], rot[2]);
124 dir.transform(q);
125 }
127 float cos_theta = cos(DEG_TO_RAD(cam_theta));
128 float sin_theta = sin(DEG_TO_RAD(cam_theta));
129 cam_pos.x += dir.x * cos_theta - dir.z * sin_theta;
130 cam_pos.z += dir.x * sin_theta + dir.z * cos_theta;
132 float max_step = get_gvar_num(GVAR_MAX_STEP);
133 cam_pos = scn.find_walk_pos(cam_pos + Vector3(0, max_step, 0));
134 }
136 void game_display()
137 {
138 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
139 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
141 for(int i=0; i<2; i++) {
142 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
143 vr_begin(i);
145 float proj[16];
146 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
147 glMatrixMode(GL_PROJECTION);
148 glLoadMatrixf(proj);
149 }
151 glMatrixMode(GL_MODELVIEW);
153 float view[16];
154 vr_view_matrix(i, view);
155 glLoadMatrixf(view);
156 glRotatef(cam_phi, 1, 0, 0);
157 glRotatef(cam_theta, 0, 1, 0);
158 /* move the camera to the eye level of the user */
159 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
160 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
162 draw_scene();
163 con.draw();
165 vr_end();
166 }
168 glBindFramebuffer(GL_FRAMEBUFFER, 0);
169 glViewport(0, 0, win_width, win_height);
171 vr_swap_buffers();
172 assert(glGetError() == GL_NO_ERROR);
173 }
175 void game_reshape(int x, int y)
176 {
177 win_width = x;
178 win_height = y;
180 create_rtarg(vr_geti_def(VR_RENDER_XRES, x * 2), vr_geti_def(VR_RENDER_YRES, y));
181 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
183 /* these might be overriden in VR mode (see game_display) */
184 glViewport(0, 0, x, y);
185 glMatrixMode(GL_PROJECTION);
186 glLoadIdentity();
187 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
188 }
190 void game_keyboard(int key, bool pressed)
191 {
192 if(pressed) {
193 if(con.is_visible()) {
194 if(key == 27 || key == '`') {
195 con.hide();
196 } else {
197 con.input_key(key);
198 }
199 } else {
200 switch(key) {
201 case 27:
202 exit_game();
203 break;
205 case 'f':
206 toggle_hmd_fullscr();
207 break;
209 case 'r':
210 vr_recenter();
211 break;
213 case '`':
214 con.show();
215 break;
217 default:
218 break;
219 }
220 }
221 }
223 if(key < 256) {
224 keystate[key] = pressed;
225 }
226 }
229 static int prev_x, prev_y;
230 static bool bnstate[32];
232 void game_mouse_button(int bn, bool state, int x, int y)
233 {
234 bnstate[bn] = state;
235 prev_x = x;
236 prev_y = y;
237 }
239 void game_mouse_motion(int x, int y)
240 {
241 int dx = x - prev_x;
242 int dy = y - prev_y;
243 prev_x = x;
244 prev_y = y;
246 if(!dx && !dy) {
247 return;
248 }
249 if(bnstate[0]) {
250 cam_theta += dx * 0.5;
251 cam_phi += dy * 0.5;
253 if(cam_phi < -90) cam_phi = -90;
254 if(cam_phi > 90) cam_phi = 90;
255 }
256 }
258 MeshFace dbg_walk_face;
260 static void draw_scene()
261 {
262 float lpos[][4] = {
263 {-0.7, 0.7, 1, 0},
264 {1, 0, 1, 0}
265 };
266 float lcol[][4] = {
267 {0.9, 0.7, 0.6, 1},
268 {0.3, 0.4, 0.75, 1}
269 };
270 for(int i=0; i<2; i++) {
271 glEnable(GL_LIGHT0 + i);
272 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
273 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
274 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
275 }
277 glMatrixMode(GL_MODELVIEW);
279 /*
280 glBindTexture(GL_TEXTURE_2D, chess_tex);
281 glEnable(GL_TEXTURE_2D);
282 material(1, 1, 1, 1);
283 glBegin(GL_QUADS);
284 glNormal3f(0, 1, 0);
285 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
286 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
287 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
288 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
289 glEnd();
290 glDisable(GL_TEXTURE_2D);
291 */
293 /*material(1, 1, 1, 0.4);
294 glPushMatrix();
295 glTranslatef(0, 1.3, -10);
296 glFrontFace(GL_CW);
297 bezier_teapot(2.0);
298 glFrontFace(GL_CCW);
299 glPopMatrix();*/
301 for(size_t i=0; i<scn.obj.size(); i++) {
302 scn.obj[i]->draw();
303 }
305 glPushAttrib(GL_ENABLE_BIT);
306 glDisable(GL_DEPTH_TEST);
307 glDisable(GL_LIGHTING);
309 glBegin(GL_TRIANGLES);
310 glColor3f(0, 1, 0);
311 glVertex3fv(&dbg_walk_face.v[0].x);
312 glVertex3fv(&dbg_walk_face.v[1].x);
313 glVertex3fv(&dbg_walk_face.v[2].x);
314 glColor3f(1, 1, 1);
315 glEnd();
317 glPopAttrib();
318 }
320 static void material(float r, float g, float b, float roughness)
321 {
322 float gloss = 1.0 - roughness;
323 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
324 float specular[] = {gloss, gloss, gloss, 1.0};
325 float shin = gloss * 128.0;
327 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
328 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
329 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
330 }
333 static void toggle_hmd_fullscr()
334 {
335 static bool fullscr;
336 static int prev_x, prev_y;
337 //static int prev_xsz, prev_ysz;
339 fullscr = !fullscr;
340 if(fullscr) {
341 /* entering fullscreen on the HMD */
342 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
343 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
344 if(xoffs != -1) {
345 get_window_pos(&prev_x, &prev_y);
346 move_window(xoffs, yoffs);
347 }
349 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
350 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
351 if(xsz != -1) {
352 //prev_xsz = win_width;
353 //prev_ysz = win_height;
354 resize_window(xsz, ysz);
355 }
356 enter_fullscreen();
358 } else {
359 /* leaving fullscreen */
360 leave_fullscreen();
361 /*move_window(prev_x, prev_y);
362 resize_window(prev_xsz, prev_ysz);*/
363 }
364 }
366 static void create_rtarg(int x, int y)
367 {
368 if(x == fb_xsz && y == fb_ysz) {
369 return; // nothing changed
370 }
372 fb_xsz = x;
373 fb_ysz = y;
374 fb_tex_xsz = next_pow2(fb_xsz);
375 fb_tex_ysz = next_pow2(fb_ysz);
377 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
379 if(!fbo) {
380 glGenFramebuffers(1, &fbo);
382 glGenTextures(1, &fb_tex);
383 glBindTexture(GL_TEXTURE_2D, fb_tex);
384 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
385 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
387 glGenRenderbuffers(1, &fb_depth);
388 }
390 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
392 glBindTexture(GL_TEXTURE_2D, fb_tex);
393 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
394 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
396 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
397 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
398 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
400 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
401 glBindFramebuffer(GL_FRAMEBUFFER, 0);
402 }
404 static int next_pow2(int x)
405 {
406 x -= 1;
407 x |= x >> 1;
408 x |= x >> 2;
409 x |= x >> 4;
410 x |= x >> 8;
411 x |= x >> 16;
412 return x + 1;
413 }
415 static const char *strip_spaces(const char *str)
416 {
417 while(*str && isspace(*str)) str++;
418 return str;
419 }
421 struct Variable {
422 const char *var;
423 enum { BOOL, NUM } type;
424 bool bool_val;
425 float num_val;
426 };
428 static void con_handle(const char *cmd)
429 {
430 /* set debug values */
431 set_gvar_num("cam-pos-x", cam_pos[0]);
432 set_gvar_num("cam-pos-y", cam_pos[1]);
433 set_gvar_num("cam-pos-z", cam_pos[2]);
435 std::vector<char*> argv;
436 cmd = strip_spaces(cmd);
438 if(!*cmd || *cmd == '#') {
439 return;
440 }
442 char *line = (char*)alloca(strlen(cmd) + 1);
443 strcpy(line, cmd);
445 char *tok = 0;
446 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
447 argv.push_back(tok);
448 }
450 if(strcmp(argv[0], "set") == 0) {
451 if(argv.size() < 3) {
452 con.printf("set must be followed by 2 arguments\n");
453 return;
454 }
455 if(have_gvar(argv[1])) {
456 GameVariable &gv = get_gvar(argv[1]);
457 con.printf("%s->%s (prev: %s)\n", argv[1], argv[2], gv.to_str().c_str());
458 } else {
459 con.printf("%s->%s\n", argv[1], argv[2]);
460 }
461 set_gvar_parse(argv[1], argv[2]);
463 } else if(strcmp(argv[0], "get") == 0) {
464 if(argv.size() < 2) {
465 con.printf("get must be followed by the variable name\n");
466 return;
467 }
468 if(have_gvar(argv[1])) {
469 GameVariable &gv = get_gvar(argv[1]);
470 con.printf("%s: %s\n", argv[1], gv.to_str().c_str());
471 }
473 } else if(strcmp(argv[0], "vars") == 0) {
474 std::list<std::string> vars = get_gvar_list();
475 std::list<std::string>::const_iterator it = vars.begin();
476 while(it != vars.end()) {
477 con.printf(" %s\n", it++->c_str());
478 }
480 } else if(strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
481 exit_game();
483 } else if(strcmp(argv[0], "help") == 0) {
484 con.printf("available commands:\n");
485 con.printf(" set <var> <val> set value to a variable\n");
486 con.printf(" get <var> print the value of a variable\n");
487 con.printf(" vars print a list of all variables\n");
488 con.printf(" help print this help screen\n");
489 con.printf(" exit or quit quit the program\n");
491 } else {
492 con.printf("invalid command: %s\n", argv[0]);
493 }
494 }