vrchess

annotate src/game.cc @ 6:3c36bc28c6c2

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