vrheights

view src/game.cc @ 7:0eca023ed909

- fixed the hmd-tracking / mouselook interaction - added game variable system
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 01 Oct 2014 01:06:55 +0300
parents 608e03b688f8
children 3f221bdc9bab
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include <vector>
4 #include <algorithm>
5 #include <vmath/vmath.h>
6 #include "opengl.h"
7 #include "game.h"
8 #include "goatvr.h"
9 #include "teapot.h"
10 #include "console.h"
11 #include "drawtext.h"
12 #include "game_var.h"
14 static void draw_scene();
15 static void material(float r, float g, float b, float roughness);
16 static void toggle_hmd_fullscr();
17 static void create_rtarg(int x, int y);
18 static int next_pow2(int x);
19 static void con_handle(const char *cmd);
21 static int win_width, win_height;
22 static unsigned int fb_tex;
23 static unsigned int fbo, fb_depth;
24 static int fb_xsz, fb_ysz;
25 static int fb_tex_xsz, fb_tex_ysz;
27 static unsigned int chess_tex;
29 static float cam_theta, cam_phi;
30 static Vector3 cam_pos;
31 static bool keystate[256];
33 static Console con;
34 static dtx_font *con_font;
36 #define OPT_LOOK_WALK "look-walk"
38 bool game_init()
39 {
40 init_opengl();
42 if(vr_init() == -1) {
43 return false;
44 }
46 glEnable(GL_DEPTH_TEST);
47 glEnable(GL_CULL_FACE);
48 glEnable(GL_LIGHTING);
50 unsigned char chess_pixels[] = {
51 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff,
52 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff
53 };
54 glGenTextures(1, &chess_tex);
55 glBindTexture(GL_TEXTURE_2D, chess_tex);
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
57 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
58 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, chess_pixels);
60 if(!(con_font = dtx_open_font_glyphmap("data/mono14.glyphmap"))) {
61 fprintf(stderr, "failed to open console font\n");
62 return false;
63 }
64 con.set_font(con_font, 14);
65 con.set_size(7, 52);
66 con.set_position(0, 0, Console::CENTER);
67 con.set_command_func(con_handle);
68 con.set_echo(false);
70 /* initialize all option variables */
71 set_gvar_bool(OPT_LOOK_WALK, true);
73 return true;
74 }
76 void game_cleanup()
77 {
78 vr_shutdown();
79 }
81 void game_update(long tm)
82 {
83 static long prev_upd;
84 float dt = (tm - prev_upd) / 1000.0;
85 prev_upd = tm;
87 if(con.is_visible()) {
88 con.update();
89 return;
90 }
92 float offs = dt * 5.0;
93 Vector3 dir;
95 if(keystate['d'] || keystate['D']) {
96 dir += Vector3(offs, 0, 0);
97 }
98 if(keystate['a'] || keystate['A']) {
99 dir += Vector3(-offs, 0, 0);
100 }
101 if(keystate['w'] || keystate['W']) {
102 dir += Vector3(0, 0, -offs);
103 }
104 if(keystate['s'] || keystate['S']) {
105 dir += Vector3(0, 0, offs);
106 }
108 if(get_gvar_bool(OPT_LOOK_WALK)) {
109 float rot[4];
110 vr_view_rotation(0, rot);
111 Quaternion q(rot[3], rot[0], rot[1], rot[2]);
113 dir.transform(q);
114 }
116 float cos_theta = cos(DEG_TO_RAD(cam_theta));
117 float sin_theta = sin(DEG_TO_RAD(cam_theta));
118 cam_pos.x += dir.x * cos_theta - dir.z * sin_theta;
119 cam_pos.z += dir.x * sin_theta + dir.z * cos_theta;
121 }
123 void game_display()
124 {
125 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
126 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
128 for(int i=0; i<2; i++) {
129 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
130 vr_begin(i);
132 float proj[16];
133 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
134 glMatrixMode(GL_PROJECTION);
135 glLoadMatrixf(proj);
136 }
138 glMatrixMode(GL_MODELVIEW);
140 float view[16];
141 vr_view_matrix(i, view);
142 glLoadMatrixf(view);
143 glRotatef(cam_phi, 1, 0, 0);
144 glRotatef(cam_theta, 0, 1, 0);
145 /* move the camera to the eye level of the user */
146 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
147 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
149 draw_scene();
150 con.draw();
152 vr_end();
153 }
155 glBindFramebuffer(GL_FRAMEBUFFER, 0);
156 glViewport(0, 0, win_width, win_height);
158 vr_swap_buffers();
159 assert(glGetError() == GL_NO_ERROR);
160 }
162 void game_reshape(int x, int y)
163 {
164 win_width = x;
165 win_height = y;
167 create_rtarg(vr_geti_def(VR_RENDER_XRES, x * 2), vr_geti_def(VR_RENDER_YRES, y));
168 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
170 /* these might be overriden in VR mode (see game_display) */
171 glViewport(0, 0, x, y);
172 glMatrixMode(GL_PROJECTION);
173 glLoadIdentity();
174 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
175 }
177 void game_keyboard(int key, bool pressed)
178 {
179 if(pressed) {
180 if(con.is_visible()) {
181 if(key == 27 || key == '`') {
182 con.hide();
183 } else {
184 con.input_key(key);
185 }
186 } else {
187 switch(key) {
188 case 27:
189 exit_game();
190 break;
192 case 'f':
193 toggle_hmd_fullscr();
194 break;
196 case 'r':
197 vr_recenter();
198 break;
200 case '`':
201 con.show();
202 break;
204 default:
205 break;
206 }
207 }
208 }
210 if(key < 256) {
211 keystate[key] = pressed;
212 }
213 }
216 static int prev_x, prev_y;
217 static bool bnstate[32];
219 void game_mouse_button(int bn, bool state, int x, int y)
220 {
221 bnstate[bn] = state;
222 prev_x = x;
223 prev_y = y;
224 }
226 void game_mouse_motion(int x, int y)
227 {
228 int dx = x - prev_x;
229 int dy = y - prev_y;
230 prev_x = x;
231 prev_y = y;
233 if(!dx && !dy) {
234 return;
235 }
236 if(bnstate[0]) {
237 cam_theta += dx * 0.5;
238 cam_phi += dy * 0.5;
240 if(cam_phi < -90) cam_phi = -90;
241 if(cam_phi > 90) cam_phi = 90;
242 }
243 }
245 static void draw_scene()
246 {
247 float lpos[][4] = {
248 {-0.7, 0.7, 1, 0},
249 {1, 0, 1, 0}
250 };
251 float lcol[][4] = {
252 {0.9, 0.7, 0.6, 1},
253 {0.3, 0.4, 0.75, 1}
254 };
255 for(int i=0; i<2; i++) {
256 glEnable(GL_LIGHT0 + i);
257 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
258 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
259 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
260 }
262 glMatrixMode(GL_MODELVIEW);
264 glBindTexture(GL_TEXTURE_2D, chess_tex);
265 glEnable(GL_TEXTURE_2D);
266 material(1, 1, 1, 1);
267 glBegin(GL_QUADS);
268 glNormal3f(0, 1, 0);
269 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
270 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
271 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
272 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
273 glEnd();
274 glDisable(GL_TEXTURE_2D);
276 material(1, 1, 1, 0.4);
277 glPushMatrix();
278 glTranslatef(0, 1.3, -10);
279 glFrontFace(GL_CW);
280 bezier_teapot(2.0);
281 glFrontFace(GL_CCW);
282 glPopMatrix();
283 }
285 static void material(float r, float g, float b, float roughness)
286 {
287 float gloss = 1.0 - roughness;
288 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
289 float specular[] = {gloss, gloss, gloss, 1.0};
290 float shin = gloss * 128.0;
292 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
293 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
294 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
295 }
298 static void toggle_hmd_fullscr()
299 {
300 static bool fullscr;
301 static int prev_x, prev_y;
302 //static int prev_xsz, prev_ysz;
304 fullscr = !fullscr;
305 if(fullscr) {
306 /* entering fullscreen on the HMD */
307 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
308 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
309 if(xoffs != -1) {
310 get_window_pos(&prev_x, &prev_y);
311 move_window(xoffs, yoffs);
312 }
314 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
315 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
316 if(xsz != -1) {
317 //prev_xsz = win_width;
318 //prev_ysz = win_height;
319 resize_window(xsz, ysz);
320 }
321 enter_fullscreen();
323 } else {
324 /* leaving fullscreen */
325 leave_fullscreen();
326 /*move_window(prev_x, prev_y);
327 resize_window(prev_xsz, prev_ysz);*/
328 }
329 }
331 static void create_rtarg(int x, int y)
332 {
333 if(x == fb_xsz && y == fb_ysz) {
334 return; // nothing changed
335 }
337 fb_xsz = x;
338 fb_ysz = y;
339 fb_tex_xsz = next_pow2(fb_xsz);
340 fb_tex_ysz = next_pow2(fb_ysz);
342 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
344 if(!fbo) {
345 glGenFramebuffers(1, &fbo);
347 glGenTextures(1, &fb_tex);
348 glBindTexture(GL_TEXTURE_2D, fb_tex);
349 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
350 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
352 glGenRenderbuffers(1, &fb_depth);
353 }
355 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
357 glBindTexture(GL_TEXTURE_2D, fb_tex);
358 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
359 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
361 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
362 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
363 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
365 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
366 glBindFramebuffer(GL_FRAMEBUFFER, 0);
367 }
369 static int next_pow2(int x)
370 {
371 x -= 1;
372 x |= x >> 1;
373 x |= x >> 2;
374 x |= x >> 4;
375 x |= x >> 8;
376 x |= x >> 16;
377 return x + 1;
378 }
380 static const char *strip_spaces(const char *str)
381 {
382 while(*str && isspace(*str)) str++;
383 return str;
384 }
386 struct Variable {
387 const char *var;
388 enum { BOOL, NUM } type;
389 bool bool_val;
390 float num_val;
391 };
393 static void con_handle(const char *cmd)
394 {
395 std::vector<char*> argv;
396 cmd = strip_spaces(cmd);
398 if(!*cmd || *cmd == '#') {
399 return;
400 }
402 char *line = (char*)alloca(strlen(cmd) + 1);
403 strcpy(line, cmd);
405 char *tok = 0;
406 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
407 argv.push_back(tok);
408 }
410 if(strcmp(argv[0], "set") == 0) {
411 if(argv.size() < 3) {
412 con.printf("set must be followed by 2 arguments\n");
413 return;
414 }
415 if(have_gvar(argv[1])) {
416 GameVariable &gv = get_gvar(argv[1]);
417 con.printf("%s->%s (prev: %s)\n", argv[1], argv[2], gv.to_str().c_str());
418 } else {
419 con.printf("%s->%s\n", argv[1], argv[2]);
420 }
421 set_gvar_parse(argv[1], argv[2]);
423 } else if(strcmp(argv[0], "get") == 0) {
424 if(argv.size() < 2) {
425 con.printf("get must be followed by the variable name\n");
426 return;
427 }
428 if(have_gvar(argv[1])) {
429 GameVariable &gv = get_gvar(argv[1]);
430 con.printf("%s: %s\n", argv[1], gv.to_str().c_str());
431 }
433 } else if(strcmp(argv[0], "vars") == 0) {
434 std::list<std::string> vars = get_gvar_list();
435 std::list<std::string>::const_iterator it = vars.begin();
436 while(it != vars.end()) {
437 con.printf(" %s\n", it++->c_str());
438 }
440 } else if(strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
441 exit_game();
443 } else if(strcmp(argv[0], "help") == 0) {
444 con.printf("available commands:\n");
445 con.printf(" set <var> <val> set value to a variable\n");
446 con.printf(" get <var> print the value of a variable\n");
447 con.printf(" vars print a list of all variables\n");
448 con.printf(" help print this help screen\n");
449 con.printf(" exit or quit quit the program\n");
451 } else {
452 con.printf("invalid command: %s\n", argv[0]);
453 }
454 }