vrheights

annotate src/game.cc @ 4:690ef7fa791f

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Sep 2014 13:19:22 +0300
parents 316ec8250af2
children 053a52f0cb64
rev   line source
nuclear@2 1 #include <stdio.h>
nuclear@2 2 #include <assert.h>
nuclear@2 3 #include <algorithm>
nuclear@4 4 #include <vmath/vmath.h>
nuclear@2 5 #include "opengl.h"
nuclear@2 6 #include "game.h"
nuclear@2 7 #include "goatvr.h"
nuclear@3 8 #include "teapot.h"
nuclear@2 9
nuclear@2 10 static void draw_scene();
nuclear@4 11 static void material(float r, float g, float b, float roughness);
nuclear@2 12 static void toggle_hmd_fullscr();
nuclear@2 13 static void create_rtarg(int x, int y);
nuclear@2 14 static int next_pow2(int x);
nuclear@2 15
nuclear@2 16 static int win_width, win_height;
nuclear@2 17 static unsigned int fb_tex;
nuclear@2 18 static unsigned int fbo, fb_depth;
nuclear@2 19 static int fb_xsz, fb_ysz;
nuclear@2 20 static int fb_tex_xsz, fb_tex_ysz;
nuclear@2 21
nuclear@4 22 static float cam_theta, cam_phi;
nuclear@4 23 static Vector3 cam_pos;
nuclear@4 24 static bool keystate[256];
nuclear@4 25
nuclear@2 26 bool game_init()
nuclear@2 27 {
nuclear@2 28 init_opengl();
nuclear@2 29
nuclear@2 30 if(vr_init() == -1) {
nuclear@2 31 return false;
nuclear@2 32 }
nuclear@2 33
nuclear@2 34 glEnable(GL_DEPTH_TEST);
nuclear@2 35 glEnable(GL_CULL_FACE);
nuclear@3 36 glEnable(GL_LIGHTING);
nuclear@2 37
nuclear@2 38 return true;
nuclear@2 39 }
nuclear@2 40
nuclear@2 41 void game_cleanup()
nuclear@2 42 {
nuclear@2 43 vr_shutdown();
nuclear@2 44 }
nuclear@2 45
nuclear@2 46 void game_update(long tm)
nuclear@2 47 {
nuclear@4 48 static long prev_upd;
nuclear@4 49 float dt = (tm - prev_upd) / 1000.0;
nuclear@4 50 prev_upd = tm;
nuclear@4 51
nuclear@4 52 float offs = dt * 1.0;
nuclear@4 53 Vector3 dir;
nuclear@4 54
nuclear@4 55 if(keystate['d'] || keystate['D']) {
nuclear@4 56 dir += Vector3(offs, 0, 0);
nuclear@4 57 }
nuclear@4 58 if(keystate['a'] || keystate['A']) {
nuclear@4 59 dir += Vector3(-offs, 0, 0);
nuclear@4 60 }
nuclear@4 61 if(keystate['w'] || keystate['W']) {
nuclear@4 62 dir += Vector3(0, 0, -offs);
nuclear@4 63 }
nuclear@4 64 if(keystate['s'] || keystate['S']) {
nuclear@4 65 dir += Vector3(0, 0, offs);
nuclear@4 66 }
nuclear@4 67
nuclear@4 68 cam_pos.x += dir.x * cos(cam_theta) - dir.y * sin(cam_theta);
nuclear@4 69 cam_pos.z += dir.x * sin(cam_theta) + dir.y * cos(cam_theta);
nuclear@2 70 }
nuclear@2 71
nuclear@2 72 void game_display()
nuclear@2 73 {
nuclear@2 74 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
nuclear@2 75 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@2 76
nuclear@2 77 for(int i=0; i<2; i++) {
nuclear@2 78 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
nuclear@2 79 vr_begin(i);
nuclear@2 80
nuclear@2 81 float proj[16];
nuclear@2 82 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
nuclear@2 83 glMatrixMode(GL_PROJECTION);
nuclear@2 84 glLoadMatrixf(proj);
nuclear@2 85 }
nuclear@2 86
nuclear@2 87 glMatrixMode(GL_MODELVIEW);
nuclear@2 88
nuclear@2 89 float view[16];
nuclear@2 90 vr_view_matrix(i, view);
nuclear@2 91 glLoadMatrixf(view);
nuclear@4 92 glRotatef(cam_phi, 1, 0, 0);
nuclear@4 93 glRotatef(cam_theta, 0, 1, 0);
nuclear@2 94 /* move the camera to the eye level of the user */
nuclear@2 95 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
nuclear@4 96 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
nuclear@2 97
nuclear@2 98 draw_scene();
nuclear@2 99
nuclear@2 100 vr_end();
nuclear@2 101 }
nuclear@2 102
nuclear@2 103 glBindFramebuffer(GL_FRAMEBUFFER, 0);
nuclear@2 104 glViewport(0, 0, win_width, win_height);
nuclear@2 105
nuclear@2 106 vr_swap_buffers();
nuclear@2 107 assert(glGetError() == GL_NO_ERROR);
nuclear@2 108 }
nuclear@2 109
nuclear@2 110 void game_reshape(int x, int y)
nuclear@2 111 {
nuclear@2 112 win_width = x;
nuclear@2 113 win_height = y;
nuclear@2 114
nuclear@2 115 create_rtarg(vr_geti_def(VR_RENDER_XRES, x), vr_geti_def(VR_RENDER_YRES, y));
nuclear@2 116 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
nuclear@2 117
nuclear@2 118 /* these might be overriden in VR mode (see game_display) */
nuclear@2 119 glViewport(0, 0, x, y);
nuclear@2 120 glMatrixMode(GL_PROJECTION);
nuclear@2 121 glLoadIdentity();
nuclear@2 122 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
nuclear@2 123 }
nuclear@2 124
nuclear@2 125 void game_keyboard(int key, bool pressed)
nuclear@2 126 {
nuclear@2 127 if(pressed) {
nuclear@2 128 switch(key) {
nuclear@2 129 case 27:
nuclear@2 130 exit_game();
nuclear@2 131 break;
nuclear@2 132
nuclear@2 133 case 'f':
nuclear@2 134 toggle_hmd_fullscr();
nuclear@2 135 break;
nuclear@2 136
nuclear@2 137 case 'r':
nuclear@2 138 vr_recenter();
nuclear@2 139 break;
nuclear@2 140
nuclear@2 141 default:
nuclear@2 142 break;
nuclear@2 143 }
nuclear@2 144 }
nuclear@4 145
nuclear@4 146 if(key < 256) {
nuclear@4 147 keystate[key] = pressed;
nuclear@4 148 }
nuclear@2 149 }
nuclear@2 150
nuclear@4 151
nuclear@4 152 static int prev_x, prev_y;
nuclear@4 153 static bool bnstate[32];
nuclear@4 154
nuclear@2 155 void game_mouse_button(int bn, bool state, int x, int y)
nuclear@2 156 {
nuclear@4 157 bnstate[bn] = state;
nuclear@4 158 prev_x = x;
nuclear@4 159 prev_y = y;
nuclear@2 160 }
nuclear@2 161
nuclear@2 162 void game_mouse_motion(int x, int y)
nuclear@2 163 {
nuclear@4 164 int dx = x - prev_x;
nuclear@4 165 int dy = y - prev_y;
nuclear@4 166 prev_x = x;
nuclear@4 167 prev_y = y;
nuclear@2 168
nuclear@4 169 if(!dx && !dy) {
nuclear@4 170 return;
nuclear@4 171 }
nuclear@4 172 if(bnstate[0]) {
nuclear@4 173 cam_theta += dx * 0.5;
nuclear@4 174 cam_phi += dy * 0.5;
nuclear@3 175
nuclear@4 176 if(cam_phi < -90) cam_phi = -90;
nuclear@4 177 if(cam_phi > 90) cam_phi = 90;
nuclear@4 178 }
nuclear@3 179 }
nuclear@3 180
nuclear@2 181 static void draw_scene()
nuclear@2 182 {
nuclear@3 183 float lpos[][4] = {
nuclear@3 184 {-0.7, 0.7, 1, 0},
nuclear@3 185 {1, 0, 1, 0}
nuclear@3 186 };
nuclear@3 187 float lcol[][4] = {
nuclear@3 188 {0.9, 0.7, 0.6, 1},
nuclear@3 189 {0.3, 0.4, 0.75, 1}
nuclear@3 190 };
nuclear@3 191 for(int i=0; i<2; i++) {
nuclear@3 192 glEnable(GL_LIGHT0 + i);
nuclear@3 193 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
nuclear@3 194 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
nuclear@3 195 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
nuclear@3 196 }
nuclear@3 197
nuclear@3 198 glMatrixMode(GL_MODELVIEW);
nuclear@3 199
nuclear@3 200 material(1, 1, 1, 1);
nuclear@3 201 glBegin(GL_QUADS);
nuclear@3 202 glNormal3f(0, 1, 0);
nuclear@3 203 glVertex3f(-10, 0, 10);
nuclear@3 204 glVertex3f(10, 0, 10);
nuclear@3 205 glVertex3f(10, 0, -10);
nuclear@3 206 glVertex3f(-10, 0, -10);
nuclear@3 207 glEnd();
nuclear@3 208
nuclear@3 209 material(1, 1, 1, 0.4);
nuclear@3 210 glPushMatrix();
nuclear@3 211 glTranslatef(0, 1.3, -10);
nuclear@3 212 glFrontFace(GL_CW);
nuclear@3 213 bezier_teapot(2.0);
nuclear@3 214 glFrontFace(GL_CCW);
nuclear@3 215 glPopMatrix();
nuclear@2 216 }
nuclear@2 217
nuclear@4 218 static void material(float r, float g, float b, float roughness)
nuclear@4 219 {
nuclear@4 220 float gloss = 1.0 - roughness;
nuclear@4 221 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
nuclear@4 222 float specular[] = {gloss, gloss, gloss, 1.0};
nuclear@4 223 float shin = gloss * 128.0;
nuclear@4 224
nuclear@4 225 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
nuclear@4 226 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
nuclear@4 227 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
nuclear@4 228 }
nuclear@4 229
nuclear@4 230
nuclear@2 231 static void toggle_hmd_fullscr()
nuclear@2 232 {
nuclear@2 233 static bool fullscr;
nuclear@2 234 static int prev_x, prev_y;
nuclear@2 235 //static int prev_xsz, prev_ysz;
nuclear@2 236
nuclear@2 237 fullscr = !fullscr;
nuclear@2 238 if(fullscr) {
nuclear@2 239 /* entering fullscreen on the HMD */
nuclear@2 240 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
nuclear@2 241 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
nuclear@2 242 if(xoffs != -1) {
nuclear@2 243 get_window_pos(&prev_x, &prev_y);
nuclear@2 244 move_window(xoffs, yoffs);
nuclear@2 245 }
nuclear@2 246
nuclear@2 247 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
nuclear@2 248 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
nuclear@2 249 if(xsz != -1) {
nuclear@2 250 //prev_xsz = win_width;
nuclear@2 251 //prev_ysz = win_height;
nuclear@2 252 resize_window(xsz, ysz);
nuclear@2 253 }
nuclear@2 254 enter_fullscreen();
nuclear@2 255
nuclear@2 256 } else {
nuclear@2 257 /* leaving fullscreen */
nuclear@2 258 leave_fullscreen();
nuclear@2 259 /*move_window(prev_x, prev_y);
nuclear@2 260 resize_window(prev_xsz, prev_ysz);*/
nuclear@2 261 }
nuclear@2 262 }
nuclear@2 263
nuclear@2 264 static void create_rtarg(int x, int y)
nuclear@2 265 {
nuclear@2 266 if(x == fb_xsz && y == fb_ysz) {
nuclear@2 267 return; // nothing changed
nuclear@2 268 }
nuclear@2 269
nuclear@2 270 fb_xsz = x;
nuclear@2 271 fb_ysz = y;
nuclear@2 272 fb_tex_xsz = next_pow2(fb_xsz);
nuclear@2 273 fb_tex_ysz = next_pow2(fb_ysz);
nuclear@2 274
nuclear@2 275 if(!fbo) {
nuclear@2 276 glGenFramebuffers(1, &fbo);
nuclear@2 277
nuclear@2 278 glGenTextures(1, &fb_tex);
nuclear@2 279 glBindTexture(GL_TEXTURE_2D, fb_tex);
nuclear@2 280 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
nuclear@2 281 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
nuclear@2 282
nuclear@2 283 glGenRenderbuffers(1, &fb_depth);
nuclear@2 284 }
nuclear@2 285
nuclear@2 286 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
nuclear@2 287
nuclear@2 288 glBindTexture(GL_TEXTURE_2D, fb_tex);
nuclear@2 289 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
nuclear@2 290 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
nuclear@2 291
nuclear@2 292 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
nuclear@2 293 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
nuclear@2 294 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
nuclear@2 295
nuclear@2 296 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
nuclear@2 297 glBindFramebuffer(GL_FRAMEBUFFER, 0);
nuclear@2 298 }
nuclear@2 299
nuclear@2 300 static int next_pow2(int x)
nuclear@2 301 {
nuclear@2 302 x -= 1;
nuclear@2 303 x |= x >> 1;
nuclear@2 304 x |= x >> 2;
nuclear@2 305 x |= x >> 4;
nuclear@2 306 x |= x >> 8;
nuclear@2 307 x |= x >> 16;
nuclear@2 308 return x + 1;
nuclear@2 309 }