vrchess

annotate src/game.cc @ 10:e3f0ca1d008a

added preliminary OpenHMD module
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 22 Aug 2014 20:11:15 +0300
parents 90abf4b93cc9
children 5dc4e2b8f6f5
rev   line source
nuclear@2 1 #include "game.h"
nuclear@2 2 #include "opengl.h"
nuclear@2 3 #include "camera.h"
nuclear@2 4 #include "texture.h"
nuclear@4 5 #include "vr/vr.h"
nuclear@2 6
nuclear@7 7 static void game_render_eye(int eye);
nuclear@2 8 static void draw_scene();
nuclear@6 9 static bool setup_rtarg(int x, int y);
nuclear@6 10
nuclear@7 11 static Texture *rtarg;
nuclear@6 12 static unsigned int fbo, rtarg_depth;
nuclear@7 13 static int rtwidth, rtheight;
nuclear@2 14
nuclear@2 15 static const float move_speed = 10.0f;
nuclear@2 16
nuclear@2 17 static int fb_width, fb_height;
nuclear@2 18 static FlyCamera cam;
nuclear@2 19 static Texture floor_tex;
nuclear@2 20 static bool keystate[256];
nuclear@2 21
nuclear@2 22 bool game_init()
nuclear@2 23 {
nuclear@4 24 vr_init();
nuclear@4 25
nuclear@2 26 glEnable(GL_DEPTH_TEST);
nuclear@2 27 glEnable(GL_CULL_FACE);
nuclear@2 28 glEnable(GL_LIGHTING);
nuclear@2 29 glEnable(GL_LIGHT0);
nuclear@2 30
nuclear@2 31 glClearColor(0.1, 0.1, 0.1, 1);
nuclear@2 32
nuclear@6 33
nuclear@2 34 if(!floor_tex.load("data/tiles.png")) {
nuclear@2 35 return false;
nuclear@2 36 }
nuclear@2 37
nuclear@2 38 cam.input_move(0, 0, 5);
nuclear@2 39 return true;
nuclear@2 40 }
nuclear@2 41
nuclear@2 42 void game_cleanup()
nuclear@2 43 {
nuclear@2 44 floor_tex.destroy();
nuclear@4 45 vr_shutdown();
nuclear@6 46
nuclear@6 47 if(fbo) {
nuclear@6 48 glDeleteFramebuffers(1, &fbo);
nuclear@6 49 glDeleteRenderbuffers(1, &rtarg_depth);
nuclear@7 50 delete rtarg;
nuclear@6 51 }
nuclear@2 52 }
nuclear@2 53
nuclear@2 54 void game_update(unsigned int msec)
nuclear@2 55 {
nuclear@2 56 static unsigned int prev_msec;
nuclear@2 57 float dt = (msec - prev_msec) / 1000.0f;
nuclear@2 58 float offs = dt * move_speed;
nuclear@2 59 prev_msec = msec;
nuclear@2 60
nuclear@2 61 Vector3 move;
nuclear@2 62 float roll = 0.0f;
nuclear@2 63
nuclear@2 64 if(keystate['d'] || keystate['D']) {
nuclear@2 65 move.x += offs;
nuclear@2 66 }
nuclear@2 67 if(keystate['a'] || keystate['A']) {
nuclear@2 68 move.x -= offs;
nuclear@2 69 }
nuclear@2 70 if(keystate['s'] || keystate['S']) {
nuclear@2 71 move.z += offs;
nuclear@2 72 }
nuclear@2 73 if(keystate['w'] || keystate['W']) {
nuclear@2 74 move.z -= offs;
nuclear@2 75 }
nuclear@2 76 if(keystate['e'] || keystate['E']) {
nuclear@2 77 roll += dt;
nuclear@2 78 }
nuclear@2 79 if(keystate['q'] || keystate['Q']) {
nuclear@2 80 roll -= dt;
nuclear@2 81 }
nuclear@2 82
nuclear@2 83 cam.input_move(move.x, move.y, move.z);
nuclear@2 84 cam.input_rotate(0, 0, roll);
nuclear@2 85 }
nuclear@2 86
nuclear@7 87 void game_render()
nuclear@2 88 {
nuclear@7 89 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
nuclear@7 90 glClearColor(1, 0, 0, 1);
nuclear@7 91 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@6 92
nuclear@7 93 glViewport(0, 0, rtwidth / 2.0, rtheight);
nuclear@7 94 vr_begin(VR_EYE_LEFT);
nuclear@7 95 game_render_eye(-1);
nuclear@7 96 vr_end();
nuclear@7 97
nuclear@7 98 glViewport(rtwidth / 2, 0, rtwidth / 2.0, rtheight);
nuclear@7 99 vr_begin(VR_EYE_RIGHT);
nuclear@7 100 game_render_eye(1);
nuclear@7 101 vr_end();
nuclear@7 102
nuclear@7 103 glBindFramebuffer(GL_FRAMEBUFFER, 0);
nuclear@8 104 glViewport(0, 0, fb_width, fb_height);
nuclear@8 105
nuclear@7 106 vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
nuclear@7 107 (float)rtheight / (float)rtarg->get_height());
nuclear@9 108
nuclear@9 109 vr_swap_buffers();
nuclear@7 110 }
nuclear@7 111
nuclear@7 112 static void game_render_eye(int eye)
nuclear@7 113 {
nuclear@4 114 float mat[16];
nuclear@2 115 Matrix4x4 view_matrix = cam.get_matrix().inverse();
nuclear@2 116
nuclear@2 117 glMatrixMode(GL_PROJECTION);
nuclear@2 118 glLoadIdentity();
nuclear@7 119 //if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
nuclear@4 120 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
nuclear@7 121 /*} else {
nuclear@7 122 glLoadMatrixf(mat);
nuclear@7 123 }*/
nuclear@2 124
nuclear@2 125 glMatrixMode(GL_MODELVIEW);
nuclear@9 126 //if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
nuclear@4 127 glLoadIdentity();
nuclear@9 128 /*} else {
nuclear@4 129 glLoadTransposeMatrixf(mat);
nuclear@9 130 }*/
nuclear@4 131 glMultTransposeMatrixf(view_matrix[0]);
nuclear@2 132
nuclear@2 133 draw_scene();
nuclear@2 134 }
nuclear@2 135
nuclear@2 136 void game_reshape(int x, int y)
nuclear@2 137 {
nuclear@2 138 glViewport(0, 0, x, y);
nuclear@2 139 fb_width = x;
nuclear@2 140 fb_height = y;
nuclear@6 141
nuclear@7 142 int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
nuclear@7 143 if(lxres) {
nuclear@7 144 int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
nuclear@7 145 int rxres = vr_get_opti(VR_OPT_REYE_XRES);
nuclear@7 146 int ryres = vr_get_opti(VR_OPT_REYE_YRES);
nuclear@7 147
nuclear@7 148 rtwidth = lxres + rxres;
nuclear@7 149 rtheight = lyres > ryres ? lyres : ryres;
nuclear@7 150 } else {
nuclear@7 151 rtwidth = x;
nuclear@7 152 rtheight = y;
nuclear@7 153 }
nuclear@7 154
nuclear@7 155 setup_rtarg(rtwidth, rtheight);
nuclear@2 156 }
nuclear@2 157
nuclear@2 158 void game_keyboard(int key, bool pressed, int x, int y)
nuclear@2 159 {
nuclear@2 160 if(pressed) {
nuclear@2 161 switch(key) {
nuclear@2 162 case 27:
nuclear@2 163 exit(0);
nuclear@9 164
nuclear@9 165 case ' ':
nuclear@9 166 vr_recenter();
nuclear@9 167 break;
nuclear@2 168 }
nuclear@2 169 }
nuclear@2 170
nuclear@2 171 if(key < 256) {
nuclear@2 172 keystate[key] = pressed;
nuclear@2 173 }
nuclear@2 174 }
nuclear@2 175
nuclear@2 176 static int prev_x, prev_y;
nuclear@2 177 static bool bnstate[32];
nuclear@2 178
nuclear@2 179 void game_mouse(int bn, bool pressed, int x, int y)
nuclear@2 180 {
nuclear@2 181 bnstate[bn] = pressed;
nuclear@2 182 prev_x = x;
nuclear@2 183 prev_y = y;
nuclear@2 184 }
nuclear@2 185
nuclear@2 186 void game_motion(int x, int y)
nuclear@2 187 {
nuclear@2 188 int dx = x - prev_x;
nuclear@2 189 int dy = y - prev_y;
nuclear@2 190 prev_x = x;
nuclear@2 191 prev_y = y;
nuclear@2 192
nuclear@2 193 if(!dx && !dy) return;
nuclear@2 194
nuclear@2 195 if(bnstate[0]) {
nuclear@2 196 float xrot = dy * 0.5;
nuclear@2 197 float yrot = dx * 0.5;
nuclear@2 198 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
nuclear@2 199 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
nuclear@2 200 }
nuclear@2 201 }
nuclear@2 202
nuclear@3 203 void game_mwheel(int dir)
nuclear@3 204 {
nuclear@3 205 cam.input_move(0, dir * 0.1, 0);
nuclear@3 206 }
nuclear@3 207
nuclear@2 208 void game_6dof_move(float x, float y, float z)
nuclear@2 209 {
nuclear@2 210 cam.input_move(x, y, z);
nuclear@2 211 }
nuclear@2 212
nuclear@2 213 void game_6dof_rotate(float x, float y, float z)
nuclear@2 214 {
nuclear@2 215 cam.input_rotate(x, y, z);
nuclear@2 216 }
nuclear@2 217
nuclear@2 218 static void draw_scene()
nuclear@2 219 {
nuclear@2 220 glMatrixMode(GL_MODELVIEW);
nuclear@2 221 glTranslatef(0, -1.5, 0);
nuclear@2 222
nuclear@2 223 float lpos[] = {-20, 30, 10, 1};
nuclear@2 224 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@2 225
nuclear@2 226 glEnable(GL_TEXTURE_2D);
nuclear@2 227 floor_tex.bind();
nuclear@2 228
nuclear@2 229 glMatrixMode(GL_TEXTURE);
nuclear@2 230 glScalef(8, 8, 8);
nuclear@2 231
nuclear@2 232 glBegin(GL_QUADS);
nuclear@2 233 glNormal3f(0, 1, 0);
nuclear@2 234 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
nuclear@2 235 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
nuclear@2 236 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
nuclear@2 237 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
nuclear@2 238 glEnd();
nuclear@2 239 glDisable(GL_TEXTURE_2D);
nuclear@2 240 glLoadIdentity();
nuclear@2 241
nuclear@2 242 glMatrixMode(GL_MODELVIEW);
nuclear@2 243 glPushMatrix();
nuclear@2 244 glTranslatef(0, 0.75, 0);
nuclear@2 245
nuclear@2 246 glFrontFace(GL_CW);
nuclear@2 247 glutSolidTeapot(1.0);
nuclear@2 248 glFrontFace(GL_CCW);
nuclear@2 249
nuclear@2 250 glPopMatrix();
nuclear@2 251 }
nuclear@6 252
nuclear@6 253 static bool setup_rtarg(int x, int y)
nuclear@6 254 {
nuclear@6 255 int tex_width = next_pow2(x);
nuclear@6 256 int tex_height = next_pow2(y);
nuclear@6 257
nuclear@6 258 /* create render targets for each eye */
nuclear@6 259 if(!fbo) {
nuclear@6 260 glGenFramebuffers(1, &fbo);
nuclear@6 261 glGenRenderbuffers(1, &rtarg_depth);
nuclear@7 262 rtarg = new Texture;
nuclear@6 263 }
nuclear@6 264
nuclear@6 265 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
nuclear@6 266
nuclear@6 267 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
nuclear@6 268 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
nuclear@6 269 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
nuclear@6 270
nuclear@7 271 rtarg->create2d(tex_width, tex_height);
nuclear@7 272 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
nuclear@7 273 rtarg->get_texture_id(), 0);
nuclear@6 274
nuclear@6 275 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
nuclear@6 276 fprintf(stderr, "incomplete framebuffer!\n");
nuclear@6 277 return false;
nuclear@6 278 }
nuclear@6 279 glBindFramebuffer(GL_FRAMEBUFFER, 0);
nuclear@7 280
nuclear@7 281 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
nuclear@6 282 return true;
nuclear@6 283 }