conworlds

view src/game.cc @ 17:c814f77d177e

moved to SDL2
author John Tsiombikas <nuclear@member.fsf.org>
date Mon, 25 Aug 2014 22:02:08 +0300
parents 7a2041ddb7e7
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 glLoadIdentity();
151 //glLoadMatrixf(mat);
152 }
153 glMultTransposeMatrixf(view_matrix[0]);
154 glMultMatrixf(mat);
156 draw_scene();
157 }
159 void game_reshape(int x, int y)
160 {
161 glViewport(0, 0, x, y);
162 fb_width = x;
163 fb_height = y;
165 int lxres = vr_get_opti(VR_OPT_LEYE_XRES);
166 if(lxres) {
167 int lyres = vr_get_opti(VR_OPT_LEYE_YRES);
168 int rxres = vr_get_opti(VR_OPT_REYE_XRES);
169 int ryres = vr_get_opti(VR_OPT_REYE_YRES);
171 rtwidth = lxres + rxres;
172 rtheight = lyres > ryres ? lyres : ryres;
173 } else {
174 rtwidth = x;
175 rtheight = y;
176 }
178 setup_rtarg(rtwidth, rtheight);
179 }
181 void game_keyboard(int key, bool pressed)
182 {
183 if(pressed) {
184 switch(key) {
185 case 27:
186 exit(0);
188 case ' ':
189 vr_recenter();
190 break;
191 }
192 }
194 if(key < 256) {
195 keystate[key] = pressed;
196 }
197 }
199 static int prev_x, prev_y;
200 static bool bnstate[32];
202 void game_mouse(int bn, bool pressed, int x, int y)
203 {
204 bnstate[bn] = pressed;
205 prev_x = x;
206 prev_y = y;
207 }
209 void game_motion(int x, int y)
210 {
211 int dx = x - prev_x;
212 int dy = y - prev_y;
213 prev_x = x;
214 prev_y = y;
216 if(!dx && !dy) return;
218 if(bnstate[0]) {
219 float xrot = dy * 0.5;
220 float yrot = dx * 0.5;
221 cam.input_rotate(DEG_TO_RAD(xrot), 0, 0);
222 cam.input_rotate(0, DEG_TO_RAD(yrot), 0);
223 }
224 }
226 void game_mwheel(int dir)
227 {
228 cam.input_move(0, dir * 0.1, 0);
229 }
231 void game_6dof_move(float x, float y, float z)
232 {
233 cam.input_move(x, y, z);
234 }
236 void game_6dof_rotate(float x, float y, float z)
237 {
238 cam.input_rotate(x, y, z);
239 }
241 static void draw_scene()
242 {
243 float lpos[] = {-20, 30, 10, 1};
244 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
246 glEnable(GL_TEXTURE_2D);
247 bind_texture(&floor_tex);
249 glMatrixMode(GL_TEXTURE);
250 glScalef(8, 8, 8);
252 glBegin(GL_QUADS);
253 glNormal3f(0, 1, 0);
254 glTexCoord2f(0, 0); glVertex3f(-25, 0, 25);
255 glTexCoord2f(1, 0); glVertex3f(25, 0, 25);
256 glTexCoord2f(1, 1); glVertex3f(25, 0, -25);
257 glTexCoord2f(0, 1); glVertex3f(-25, 0, -25);
258 glEnd();
259 glDisable(GL_TEXTURE_2D);
260 glLoadIdentity();
262 glMatrixMode(GL_MODELVIEW);
264 for(int i=0; i<4; i++) {
265 glPushMatrix();
266 glTranslatef(i & 1 ? -10 : 10, 0, i & 2 ? -10 : 10);
267 draw_pyramid(2.0, 2.0);
268 glPopMatrix();
269 }
270 }
272 static bool setup_rtarg(int x, int y)
273 {
274 int tex_width = next_pow2(x);
275 int tex_height = next_pow2(y);
277 /* create render targets for each eye */
278 if(!fbo) {
279 glGenFramebuffers(1, &fbo);
280 glGenRenderbuffers(1, &rtarg_depth);
281 rtarg = new Texture;
282 }
284 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
286 glBindRenderbuffer(GL_RENDERBUFFER, rtarg_depth);
287 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, tex_width, tex_height);
288 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, rtarg_depth);
290 rtarg->create2d(tex_width, tex_height);
291 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
292 rtarg->get_texture_id(), 0);
294 if(glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
295 fprintf(stderr, "incomplete framebuffer!\n");
296 return false;
297 }
298 glBindFramebuffer(GL_FRAMEBUFFER, 0);
300 printf("created render target %dx%d (texture: %dx%d)\n", x, y, tex_width, tex_height);
301 return true;
302 }
304 static void draw_box(float sz)
305 {
306 float hsz = sz / 2.0;
308 glBegin(GL_QUADS);
309 glNormal3f(0, 0, 1);
310 glVertex3f(-hsz, -hsz, hsz);
311 glVertex3f(hsz, -hsz, hsz);
312 glVertex3f(hsz, hsz, hsz);
313 glVertex3f(-hsz, hsz, hsz);
314 glNormal3f(1, 0, 0);
315 glVertex3f(hsz, -hsz, hsz);
316 glVertex3f(hsz, -hsz, -hsz);
317 glVertex3f(hsz, hsz, -hsz);
318 glVertex3f(hsz, hsz, hsz);
319 glNormal3f(0, 0, -1);
320 glVertex3f(hsz, -hsz, -hsz);
321 glVertex3f(-hsz, -hsz, -hsz);
322 glVertex3f(-hsz, hsz, -hsz);
323 glVertex3f(hsz, hsz, -hsz);
324 glNormal3f(-1, 0, 0);
325 glVertex3f(-hsz, -hsz, -hsz);
326 glVertex3f(-hsz, -hsz, hsz);
327 glVertex3f(-hsz, hsz, hsz);
328 glVertex3f(-hsz, hsz, -hsz);
329 glNormal3f(0, 1, 0);
330 glVertex3f(-hsz, hsz, hsz);
331 glVertex3f(hsz, hsz, hsz);
332 glVertex3f(hsz, hsz, -hsz);
333 glVertex3f(-hsz, hsz, -hsz);
334 glNormal3f(0, -1, 0);
335 glVertex3f(-hsz, -hsz, -hsz);
336 glVertex3f(hsz, -hsz, -hsz);
337 glVertex3f(hsz, -hsz, hsz);
338 glVertex3f(-hsz, -hsz, hsz);
339 glEnd();
340 }
342 static void draw_pyramid(float basesz, float height)
343 {
344 float hsz = basesz / 2.0;
345 float theta = atan(hsz / height);
346 float nx = cos(theta);
347 float ny = sin(theta);
349 glBegin(GL_TRIANGLES);
350 glNormal3f(0, ny, nx);
351 glVertex3f(-hsz, 0, hsz);
352 glVertex3f(hsz, 0, hsz);
353 glVertex3f(0, height, 0);
354 glNormal3f(nx, ny, 0);
355 glVertex3f(hsz, 0, hsz);
356 glVertex3f(hsz, 0, -hsz);
357 glVertex3f(0, height, 0);
358 glNormal3f(0, ny, -nx);
359 glVertex3f(hsz, 0, -hsz);
360 glVertex3f(-hsz, 0, -hsz);
361 glVertex3f(0, height, 0);
362 glNormal3f(-nx, ny, 0);
363 glVertex3f(-hsz, 0, -hsz);
364 glVertex3f(-hsz, 0, hsz);
365 glVertex3f(0, height, 0);
366 glEnd();
367 }