conworlds

annotate src/game.cc @ 7:bd8202d6d28d

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