conworlds
view src/game.cc @ 18:e4257df067a1
oh yeah, now the tracking is solid. Wasn't rendering the correct part of the texture before.
author | John Tsiombikas <nuclear@member.fsf.org> |
---|---|
date | Tue, 26 Aug 2014 12:59:15 +0300 |
parents | 9b0db7dbde6e |
children | 782ff06817fb |
line source
1 #include <math.h>
2 #include "game.h"
3 #include "gameopt.h"
4 #include "opengl.h"
5 #include "camera.h"
6 #include "texture.h"
7 #include "vr/vr.h"
9 static void game_render_eye(int eye);
10 static void draw_scene();
11 static bool setup_rtarg(int x, int y);
12 static void draw_box(float sz);
13 static void draw_pyramid(float basesz, float height);
16 static Texture *rtarg;
17 static unsigned int fbo, rtarg_depth;
18 static int rtwidth, rtheight;
20 static const float move_speed = 10.0f;
22 static int fb_width, fb_height;
23 static FlyCamera cam;
24 static Texture floor_tex;
25 static bool keystate[256];
26 static float player_height = 1.68;
28 bool game_init()
29 {
30 if(opt.vr) {
31 vr_init();
32 if(opt.vr_module) {
33 vr_use_module_named(opt.vr_module);
34 }
36 player_height = vr_get_optf(VR_OPT_EYE_HEIGHT);
37 }
39 glEnable(GL_DEPTH_TEST);
40 glEnable(GL_CULL_FACE);
41 glEnable(GL_LIGHTING);
42 glEnable(GL_LIGHT0);
43 glEnable(GL_NORMALIZE);
45 glClearColor(0.1, 0.1, 0.1, 1);
48 if(!floor_tex.load("data/tiles.png")) {
49 return false;
50 }
52 cam.input_move(0, player_height, 0);
53 return true;
54 }
56 void game_cleanup()
57 {
58 floor_tex.destroy();
59 if(opt.vr) {
60 vr_shutdown();
61 }
63 if(fbo) {
64 glDeleteFramebuffers(1, &fbo);
65 glDeleteRenderbuffers(1, &rtarg_depth);
66 delete rtarg;
67 }
68 }
70 void game_update(unsigned int msec)
71 {
72 static unsigned int prev_msec;
73 float dt = (msec - prev_msec) / 1000.0f;
74 float offs = dt * move_speed;
75 prev_msec = msec;
77 Vector3 move;
78 float roll = 0.0f;
80 if(keystate['d'] || keystate['D']) {
81 move.x += offs;
82 }
83 if(keystate['a'] || keystate['A']) {
84 move.x -= offs;
85 }
86 if(keystate['s'] || keystate['S']) {
87 move.z += offs;
88 }
89 if(keystate['w'] || keystate['W']) {
90 move.z -= offs;
91 }
92 if(keystate['e'] || keystate['E']) {
93 roll += dt;
94 }
95 if(keystate['q'] || keystate['Q']) {
96 roll -= dt;
97 }
99 cam.input_move(move.x, move.y, move.z);
100 cam.input_rotate(0, 0, roll);
101 }
103 void game_render()
104 {
105 if(opt.vr) {
106 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
107 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
109 glViewport(0, 0, rtwidth / 2.0, rtheight);
110 vr_begin(VR_EYE_LEFT);
111 game_render_eye(-1);
112 vr_end();
114 glViewport(rtwidth / 2, 0, rtwidth / 2.0, rtheight);
115 vr_begin(VR_EYE_RIGHT);
116 game_render_eye(1);
117 vr_end();
119 glBindFramebuffer(GL_FRAMEBUFFER, 0);
120 glViewport(0, 0, fb_width, fb_height);
122 vr_output_texture(rtarg->get_texture_id(), 0, 0, (float)rtwidth / (float)rtarg->get_width(),
123 (float)rtheight / (float)rtarg->get_height());
124 } else {
125 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
127 game_render_eye(0);
128 }
130 vr_swap_buffers();
131 }
133 static void game_render_eye(int eye)
134 {
135 float mat[16];
136 Matrix4x4 view_matrix = cam.get_matrix().inverse();
138 glMatrixMode(GL_PROJECTION);
139 glLoadIdentity();
140 if(eye == 0 || !vr_proj_matrix(eye < 0 ? 0 : 1, 0.5, 500.0, mat)) {
141 gluPerspective(60.0, (float)fb_width / (float)fb_height, 0.5, 500.0);
142 } else {
143 glLoadMatrixf(mat);
144 }
146 glMatrixMode(GL_MODELVIEW);
147 if(eye == 0 || !vr_view_matrix(eye < 0 ? 0 : 1, mat)) {
148 glLoadIdentity();
149 } else {
150 glLoadMatrixf(mat);
151 }
152 glMultTransposeMatrixf(view_matrix[0]);
154 draw_scene();
155 }
157 void game_reshape(int x, int y)
158 {
159 glViewport(0, 0, x, y);
160 fb_width = x;
161 fb_height = y;
163 int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
164 if(lxres) {
165 int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
166 int rxres = vr_get_opti(VR_OPT_REYE_XRES);
167 int ryres = vr_get_opti(VR_OPT_REYE_YRES);
169 rtwidth = lxres + rxres;
170 rtheight = lyres > ryres ? lyres : ryres;
171 } else {
172 rtwidth = x;
173 rtheight = y;
174 }
176 setup_rtarg(rtwidth, rtheight);
177 }
179 void game_keyboard(int key, bool pressed, int x, int y)
180 {
181 if(pressed) {
182 switch(key) {
183 case 27:
184 exit(0);
186 case ' ':
187 vr_recenter();
188 break;
189 }
190 }
192 if(key < 256) {
193 keystate[key] = pressed;
194 }
195 }
197 static int prev_x, prev_y;
198 static bool bnstate[32];
200 void game_mouse(int bn, bool pressed, int x, int y)
201 {
202 bnstate[bn] = pressed;
203 prev_x = x;
204 prev_y = y;
205 }
207 void game_motion(int x, int y)
208 {
209 int dx = x - prev_x;
210 int dy = y - prev_y;
211 prev_x = x;
212 prev_y = y;
214 if(!dx && !dy) return;
216 if(bnstate[0]) {
217 float xrot = dy * 0.5;
218 float yrot = dx * 0.5;
219 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
220 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
221 }
222 }
224 void game_mwheel(int dir)
225 {
226 cam.input_move(0, dir * 0.1, 0);
227 }
229 void game_6dof_move(float x, float y, float z)
230 {
231 cam.input_move(x, y, z);
232 }
234 void game_6dof_rotate(float x, float y, float z)
235 {
236 cam.input_rotate(x, y, z);
237 }
239 static void draw_scene()
240 {
241 float lpos[] = {-20, 30, 10, 1};
242 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
244 glEnable(GL_TEXTURE_2D);
245 bind_texture(&floor_tex);
247 glMatrixMode(GL_TEXTURE);
248 glScalef(8, 8, 8);
250 glBegin(GL_QUADS);
251 glNormal3f(0, 1, 0);
252 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
253 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
254 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
255 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
256 glEnd();
257 glDisable(GL_TEXTURE_2D);
258 glLoadIdentity();
260 glMatrixMode(GL_MODELVIEW);
262 for(int i=0; i<4; i++) {
263 glPushMatrix();
264 glTranslatef(i & 1 ? -10 : 10, 0, i & 2 ? -10 : 10);
265 draw_pyramid(2.0, 2.0);
266 glPopMatrix();
267 }
268 }
270 static bool setup_rtarg(int x, int y)
271 {
272 int tex_width = next_pow2(x);
273 int tex_height = next_pow2(y);
275 /* create render targets for each eye */
276 if(!fbo) {
277 glGenFramebuffers(1, &fbo);
278 glGenRenderbuffers(1, &rtarg_depth);
279 rtarg = new Texture;
280 }
282 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
284 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
285 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
286 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
288 rtarg->create2d(tex_width, tex_height);
289 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
290 rtarg->get_texture_id(), 0);
292 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
293 fprintf(stderr, "incomplete framebuffer!\n");
294 return false;
295 }
296 glBindFramebuffer(GL_FRAMEBUFFER, 0);
298 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
299 return true;
300 }
302 static void draw_box(float sz)
303 {
304 float hsz = sz / 2.0;
306 glBegin(GL_QUADS);
307 glNormal3f(0, 0, 1);
308 glVertex3f(-hsz, -hsz, hsz);
309 glVertex3f(hsz, -hsz, hsz);
310 glVertex3f(hsz, hsz, hsz);
311 glVertex3f(-hsz, hsz, hsz);
312 glNormal3f(1, 0, 0);
313 glVertex3f(hsz, -hsz, hsz);
314 glVertex3f(hsz, -hsz, -hsz);
315 glVertex3f(hsz, hsz, -hsz);
316 glVertex3f(hsz, hsz, hsz);
317 glNormal3f(0, 0, -1);
318 glVertex3f(hsz, -hsz, -hsz);
319 glVertex3f(-hsz, -hsz, -hsz);
320 glVertex3f(-hsz, hsz, -hsz);
321 glVertex3f(hsz, hsz, -hsz);
322 glNormal3f(-1, 0, 0);
323 glVertex3f(-hsz, -hsz, -hsz);
324 glVertex3f(-hsz, -hsz, hsz);
325 glVertex3f(-hsz, hsz, hsz);
326 glVertex3f(-hsz, hsz, -hsz);
327 glNormal3f(0, 1, 0);
328 glVertex3f(-hsz, hsz, hsz);
329 glVertex3f(hsz, hsz, hsz);
330 glVertex3f(hsz, hsz, -hsz);
331 glVertex3f(-hsz, hsz, -hsz);
332 glNormal3f(0, -1, 0);
333 glVertex3f(-hsz, -hsz, -hsz);
334 glVertex3f(hsz, -hsz, -hsz);
335 glVertex3f(hsz, -hsz, hsz);
336 glVertex3f(-hsz, -hsz, hsz);
337 glEnd();
338 }
340 static void draw_pyramid(float basesz, float height)
341 {
342 float hsz = basesz / 2.0;
343 float theta = atan(hsz / height);
344 float nx = cos(theta);
345 float ny = sin(theta);
347 glBegin(GL_TRIANGLES);
348 glNormal3f(0, ny, nx);
349 glVertex3f(-hsz, 0, hsz);
350 glVertex3f(hsz, 0, hsz);
351 glVertex3f(0, height, 0);
352 glNormal3f(nx, ny, 0);
353 glVertex3f(hsz, 0, hsz);
354 glVertex3f(hsz, 0, -hsz);
355 glVertex3f(0, height, 0);
356 glNormal3f(0, ny, -nx);
357 glVertex3f(hsz, 0, -hsz);
358 glVertex3f(-hsz, 0, -hsz);
359 glVertex3f(0, height, 0);
360 glNormal3f(-nx, ny, 0);
361 glVertex3f(-hsz, 0, -hsz);
362 glVertex3f(-hsz, 0, hsz);
363 glVertex3f(0, height, 0);
364 glEnd();
365 }