vrheights

annotate src/game.cc @ 6:608e03b688f8

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 30 Sep 2014 18:57:20 +0300
parents 053a52f0cb64
children 0eca023ed909
rev   line source
nuclear@2 1 #include <stdio.h>
nuclear@2 2 #include <assert.h>
nuclear@6 3 #include <vector>
nuclear@2 4 #include <algorithm>
nuclear@4 5 #include <vmath/vmath.h>
nuclear@2 6 #include "opengl.h"
nuclear@2 7 #include "game.h"
nuclear@2 8 #include "goatvr.h"
nuclear@3 9 #include "teapot.h"
nuclear@5 10 #include "console.h"
nuclear@5 11 #include "drawtext.h"
nuclear@2 12
nuclear@2 13 static void draw_scene();
nuclear@4 14 static void material(float r, float g, float b, float roughness);
nuclear@2 15 static void toggle_hmd_fullscr();
nuclear@2 16 static void create_rtarg(int x, int y);
nuclear@2 17 static int next_pow2(int x);
nuclear@6 18 static void con_handle(const char *cmd);
nuclear@2 19
nuclear@5 20 bool opt_separate_walk_look;
nuclear@5 21
nuclear@2 22 static int win_width, win_height;
nuclear@2 23 static unsigned int fb_tex;
nuclear@2 24 static unsigned int fbo, fb_depth;
nuclear@2 25 static int fb_xsz, fb_ysz;
nuclear@2 26 static int fb_tex_xsz, fb_tex_ysz;
nuclear@2 27
nuclear@5 28 static unsigned int chess_tex;
nuclear@5 29
nuclear@4 30 static float cam_theta, cam_phi;
nuclear@4 31 static Vector3 cam_pos;
nuclear@4 32 static bool keystate[256];
nuclear@4 33
nuclear@5 34 static Console con;
nuclear@5 35 static dtx_font *con_font;
nuclear@5 36
nuclear@2 37 bool game_init()
nuclear@2 38 {
nuclear@2 39 init_opengl();
nuclear@2 40
nuclear@2 41 if(vr_init() == -1) {
nuclear@2 42 return false;
nuclear@2 43 }
nuclear@2 44
nuclear@2 45 glEnable(GL_DEPTH_TEST);
nuclear@2 46 glEnable(GL_CULL_FACE);
nuclear@3 47 glEnable(GL_LIGHTING);
nuclear@2 48
nuclear@5 49 unsigned char chess_pixels[] = {
nuclear@5 50 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff,
nuclear@5 51 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff
nuclear@5 52 };
nuclear@5 53 glGenTextures(1, &chess_tex);
nuclear@5 54 glBindTexture(GL_TEXTURE_2D, chess_tex);
nuclear@5 55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
nuclear@5 56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
nuclear@5 57 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, chess_pixels);
nuclear@5 58
nuclear@5 59 if(!(con_font = dtx_open_font_glyphmap("data/mono14.glyphmap"))) {
nuclear@5 60 fprintf(stderr, "failed to open console font\n");
nuclear@5 61 return false;
nuclear@5 62 }
nuclear@5 63 con.set_font(con_font, 14);
nuclear@6 64 con.set_size(7, 40);
nuclear@5 65 con.set_position(0, 0, Console::CENTER);
nuclear@6 66 con.set_command_func(con_handle);
nuclear@5 67
nuclear@2 68 return true;
nuclear@2 69 }
nuclear@2 70
nuclear@2 71 void game_cleanup()
nuclear@2 72 {
nuclear@2 73 vr_shutdown();
nuclear@2 74 }
nuclear@2 75
nuclear@2 76 void game_update(long tm)
nuclear@2 77 {
nuclear@4 78 static long prev_upd;
nuclear@4 79 float dt = (tm - prev_upd) / 1000.0;
nuclear@4 80 prev_upd = tm;
nuclear@4 81
nuclear@5 82 if(con.is_visible()) {
nuclear@5 83 con.update();
nuclear@5 84 return;
nuclear@5 85 }
nuclear@5 86
nuclear@5 87 float offs = dt * 5.0;
nuclear@4 88 Vector3 dir;
nuclear@4 89
nuclear@4 90 if(keystate['d'] || keystate['D']) {
nuclear@4 91 dir += Vector3(offs, 0, 0);
nuclear@4 92 }
nuclear@4 93 if(keystate['a'] || keystate['A']) {
nuclear@4 94 dir += Vector3(-offs, 0, 0);
nuclear@4 95 }
nuclear@4 96 if(keystate['w'] || keystate['W']) {
nuclear@4 97 dir += Vector3(0, 0, -offs);
nuclear@4 98 }
nuclear@4 99 if(keystate['s'] || keystate['S']) {
nuclear@4 100 dir += Vector3(0, 0, offs);
nuclear@4 101 }
nuclear@4 102
nuclear@5 103 float cos_theta = cos(DEG_TO_RAD(cam_theta));
nuclear@5 104 float sin_theta = sin(DEG_TO_RAD(cam_theta));
nuclear@5 105 cam_pos.x += dir.x * cos_theta - dir.z * sin_theta;
nuclear@5 106 cam_pos.z += dir.x * sin_theta + dir.z * cos_theta;
nuclear@5 107
nuclear@5 108 if(!opt_separate_walk_look) {
nuclear@6 109 float rot[4];
nuclear@6 110 vr_view_rotation(0, rot);
nuclear@6 111 Quaternion q(rot[3], rot[0], rot[1], rot[2]);
nuclear@6 112
nuclear@6 113 cam_pos.transform(q);
nuclear@5 114 }
nuclear@2 115 }
nuclear@2 116
nuclear@2 117 void game_display()
nuclear@2 118 {
nuclear@2 119 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
nuclear@2 120 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@2 121
nuclear@2 122 for(int i=0; i<2; i++) {
nuclear@2 123 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
nuclear@2 124 vr_begin(i);
nuclear@2 125
nuclear@2 126 float proj[16];
nuclear@2 127 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
nuclear@2 128 glMatrixMode(GL_PROJECTION);
nuclear@2 129 glLoadMatrixf(proj);
nuclear@2 130 }
nuclear@2 131
nuclear@2 132 glMatrixMode(GL_MODELVIEW);
nuclear@2 133
nuclear@2 134 float view[16];
nuclear@2 135 vr_view_matrix(i, view);
nuclear@2 136 glLoadMatrixf(view);
nuclear@4 137 glRotatef(cam_phi, 1, 0, 0);
nuclear@4 138 glRotatef(cam_theta, 0, 1, 0);
nuclear@2 139 /* move the camera to the eye level of the user */
nuclear@2 140 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
nuclear@4 141 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
nuclear@2 142
nuclear@2 143 draw_scene();
nuclear@5 144 con.draw();
nuclear@2 145
nuclear@2 146 vr_end();
nuclear@2 147 }
nuclear@2 148
nuclear@2 149 glBindFramebuffer(GL_FRAMEBUFFER, 0);
nuclear@2 150 glViewport(0, 0, win_width, win_height);
nuclear@2 151
nuclear@2 152 vr_swap_buffers();
nuclear@2 153 assert(glGetError() == GL_NO_ERROR);
nuclear@2 154 }
nuclear@2 155
nuclear@2 156 void game_reshape(int x, int y)
nuclear@2 157 {
nuclear@2 158 win_width = x;
nuclear@2 159 win_height = y;
nuclear@2 160
nuclear@5 161 create_rtarg(vr_geti_def(VR_RENDER_XRES, x * 2), vr_geti_def(VR_RENDER_YRES, y));
nuclear@2 162 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
nuclear@2 163
nuclear@2 164 /* these might be overriden in VR mode (see game_display) */
nuclear@2 165 glViewport(0, 0, x, y);
nuclear@2 166 glMatrixMode(GL_PROJECTION);
nuclear@2 167 glLoadIdentity();
nuclear@2 168 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
nuclear@2 169 }
nuclear@2 170
nuclear@2 171 void game_keyboard(int key, bool pressed)
nuclear@2 172 {
nuclear@2 173 if(pressed) {
nuclear@5 174 if(con.is_visible()) {
nuclear@5 175 if(key == 27 || key == '`') {
nuclear@5 176 con.hide();
nuclear@5 177 } else {
nuclear@5 178 con.input_key(key);
nuclear@5 179 }
nuclear@5 180 } else {
nuclear@5 181 switch(key) {
nuclear@5 182 case 27:
nuclear@5 183 exit_game();
nuclear@5 184 break;
nuclear@2 185
nuclear@5 186 case 'f':
nuclear@5 187 toggle_hmd_fullscr();
nuclear@5 188 break;
nuclear@2 189
nuclear@5 190 case 'r':
nuclear@5 191 vr_recenter();
nuclear@5 192 break;
nuclear@2 193
nuclear@5 194 case '`':
nuclear@5 195 con.show();
nuclear@5 196 break;
nuclear@5 197
nuclear@5 198 default:
nuclear@5 199 break;
nuclear@5 200 }
nuclear@2 201 }
nuclear@2 202 }
nuclear@4 203
nuclear@4 204 if(key < 256) {
nuclear@4 205 keystate[key] = pressed;
nuclear@4 206 }
nuclear@2 207 }
nuclear@2 208
nuclear@4 209
nuclear@4 210 static int prev_x, prev_y;
nuclear@4 211 static bool bnstate[32];
nuclear@4 212
nuclear@2 213 void game_mouse_button(int bn, bool state, int x, int y)
nuclear@2 214 {
nuclear@4 215 bnstate[bn] = state;
nuclear@4 216 prev_x = x;
nuclear@4 217 prev_y = y;
nuclear@2 218 }
nuclear@2 219
nuclear@2 220 void game_mouse_motion(int x, int y)
nuclear@2 221 {
nuclear@4 222 int dx = x - prev_x;
nuclear@4 223 int dy = y - prev_y;
nuclear@4 224 prev_x = x;
nuclear@4 225 prev_y = y;
nuclear@2 226
nuclear@4 227 if(!dx && !dy) {
nuclear@4 228 return;
nuclear@4 229 }
nuclear@4 230 if(bnstate[0]) {
nuclear@4 231 cam_theta += dx * 0.5;
nuclear@4 232 cam_phi += dy * 0.5;
nuclear@3 233
nuclear@4 234 if(cam_phi < -90) cam_phi = -90;
nuclear@4 235 if(cam_phi > 90) cam_phi = 90;
nuclear@4 236 }
nuclear@3 237 }
nuclear@3 238
nuclear@2 239 static void draw_scene()
nuclear@2 240 {
nuclear@3 241 float lpos[][4] = {
nuclear@3 242 {-0.7, 0.7, 1, 0},
nuclear@3 243 {1, 0, 1, 0}
nuclear@3 244 };
nuclear@3 245 float lcol[][4] = {
nuclear@3 246 {0.9, 0.7, 0.6, 1},
nuclear@3 247 {0.3, 0.4, 0.75, 1}
nuclear@3 248 };
nuclear@3 249 for(int i=0; i<2; i++) {
nuclear@3 250 glEnable(GL_LIGHT0 + i);
nuclear@3 251 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
nuclear@3 252 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
nuclear@3 253 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
nuclear@3 254 }
nuclear@3 255
nuclear@3 256 glMatrixMode(GL_MODELVIEW);
nuclear@3 257
nuclear@5 258 glBindTexture(GL_TEXTURE_2D, chess_tex);
nuclear@5 259 glEnable(GL_TEXTURE_2D);
nuclear@3 260 material(1, 1, 1, 1);
nuclear@3 261 glBegin(GL_QUADS);
nuclear@3 262 glNormal3f(0, 1, 0);
nuclear@5 263 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
nuclear@5 264 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
nuclear@5 265 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
nuclear@5 266 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
nuclear@3 267 glEnd();
nuclear@5 268 glDisable(GL_TEXTURE_2D);
nuclear@3 269
nuclear@3 270 material(1, 1, 1, 0.4);
nuclear@3 271 glPushMatrix();
nuclear@3 272 glTranslatef(0, 1.3, -10);
nuclear@3 273 glFrontFace(GL_CW);
nuclear@3 274 bezier_teapot(2.0);
nuclear@3 275 glFrontFace(GL_CCW);
nuclear@3 276 glPopMatrix();
nuclear@2 277 }
nuclear@2 278
nuclear@4 279 static void material(float r, float g, float b, float roughness)
nuclear@4 280 {
nuclear@4 281 float gloss = 1.0 - roughness;
nuclear@4 282 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
nuclear@4 283 float specular[] = {gloss, gloss, gloss, 1.0};
nuclear@4 284 float shin = gloss * 128.0;
nuclear@4 285
nuclear@4 286 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
nuclear@4 287 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
nuclear@4 288 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
nuclear@4 289 }
nuclear@4 290
nuclear@4 291
nuclear@2 292 static void toggle_hmd_fullscr()
nuclear@2 293 {
nuclear@2 294 static bool fullscr;
nuclear@2 295 static int prev_x, prev_y;
nuclear@2 296 //static int prev_xsz, prev_ysz;
nuclear@2 297
nuclear@2 298 fullscr = !fullscr;
nuclear@2 299 if(fullscr) {
nuclear@2 300 /* entering fullscreen on the HMD */
nuclear@2 301 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
nuclear@2 302 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
nuclear@2 303 if(xoffs != -1) {
nuclear@2 304 get_window_pos(&prev_x, &prev_y);
nuclear@2 305 move_window(xoffs, yoffs);
nuclear@2 306 }
nuclear@2 307
nuclear@2 308 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
nuclear@2 309 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
nuclear@2 310 if(xsz != -1) {
nuclear@2 311 //prev_xsz = win_width;
nuclear@2 312 //prev_ysz = win_height;
nuclear@2 313 resize_window(xsz, ysz);
nuclear@2 314 }
nuclear@2 315 enter_fullscreen();
nuclear@2 316
nuclear@2 317 } else {
nuclear@2 318 /* leaving fullscreen */
nuclear@2 319 leave_fullscreen();
nuclear@2 320 /*move_window(prev_x, prev_y);
nuclear@2 321 resize_window(prev_xsz, prev_ysz);*/
nuclear@2 322 }
nuclear@2 323 }
nuclear@2 324
nuclear@2 325 static void create_rtarg(int x, int y)
nuclear@2 326 {
nuclear@2 327 if(x == fb_xsz && y == fb_ysz) {
nuclear@2 328 return; // nothing changed
nuclear@2 329 }
nuclear@2 330
nuclear@2 331 fb_xsz = x;
nuclear@2 332 fb_ysz = y;
nuclear@2 333 fb_tex_xsz = next_pow2(fb_xsz);
nuclear@2 334 fb_tex_ysz = next_pow2(fb_ysz);
nuclear@2 335
nuclear@5 336 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
nuclear@5 337
nuclear@2 338 if(!fbo) {
nuclear@2 339 glGenFramebuffers(1, &fbo);
nuclear@2 340
nuclear@2 341 glGenTextures(1, &fb_tex);
nuclear@2 342 glBindTexture(GL_TEXTURE_2D, fb_tex);
nuclear@2 343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@2 344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@2 345
nuclear@2 346 glGenRenderbuffers(1, &fb_depth);
nuclear@2 347 }
nuclear@2 348
nuclear@2 349 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
nuclear@2 350
nuclear@2 351 glBindTexture(GL_TEXTURE_2D, fb_tex);
nuclear@2 352 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
nuclear@2 353 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
nuclear@2 354
nuclear@2 355 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
nuclear@2 356 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
nuclear@2 357 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
nuclear@2 358
nuclear@2 359 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
nuclear@2 360 glBindFramebuffer(GL_FRAMEBUFFER, 0);
nuclear@2 361 }
nuclear@2 362
nuclear@2 363 static int next_pow2(int x)
nuclear@2 364 {
nuclear@2 365 x -= 1;
nuclear@2 366 x |= x >> 1;
nuclear@2 367 x |= x >> 2;
nuclear@2 368 x |= x >> 4;
nuclear@2 369 x |= x >> 8;
nuclear@2 370 x |= x >> 16;
nuclear@2 371 return x + 1;
nuclear@2 372 }
nuclear@6 373
nuclear@6 374 static const char *strip_spaces(const char *str)
nuclear@6 375 {
nuclear@6 376 while(*str && isspace(*str)) str++;
nuclear@6 377 return str;
nuclear@6 378 }
nuclear@6 379
nuclear@6 380 static void con_handle(const char *cmd)
nuclear@6 381 {
nuclear@6 382 std::vector<char*> argv;
nuclear@6 383 cmd = strip_spaces(cmd);
nuclear@6 384
nuclear@6 385 if(!*cmd || *cmd == '#') {
nuclear@6 386 return;
nuclear@6 387 }
nuclear@6 388
nuclear@6 389 char *line = (char*)alloca(strlen(cmd) + 1);
nuclear@6 390 strcpy(line, cmd);
nuclear@6 391
nuclear@6 392 char *tok = 0;
nuclear@6 393 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
nuclear@6 394 argv.push_back(tok);
nuclear@6 395 }
nuclear@6 396
nuclear@6 397 if(strcmp(argv[0], "set") == 0) {
nuclear@6 398 if(argv.size() < 3) {
nuclear@6 399 fprintf(stderr, "set must be followed by 2 arguments\n");
nuclear@6 400 return;
nuclear@6 401 }
nuclear@6 402 if(strcmp(argv[1], "separate-walk") == 0) {
nuclear@6 403 if(strcmp(argv[2], "true") == 0) {
nuclear@6 404 opt_separate_walk_look = true;
nuclear@6 405 } else if(strcmp(argv[2], "false") == 0) {
nuclear@6 406 opt_separate_walk_look = false;
nuclear@6 407 }
nuclear@6 408 } else {
nuclear@6 409 con.printf("unknown option: %s\n", argv[1]);
nuclear@6 410 }
nuclear@6 411 } else if(strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
nuclear@6 412 exit_game();
nuclear@6 413 } else {
nuclear@6 414 con.printf("invalid command: %s\n", argv[0]);
nuclear@6 415 }
nuclear@6 416 }