vrheights

view src/game.cc @ 6:608e03b688f8

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 30 Sep 2014 18:57:20 +0300
parents 053a52f0cb64
children 0eca023ed909
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"
13 static void draw_scene();
14 static void material(float r, float g, float b, float roughness);
15 static void toggle_hmd_fullscr();
16 static void create_rtarg(int x, int y);
17 static int next_pow2(int x);
18 static void con_handle(const char *cmd);
20 bool opt_separate_walk_look;
22 static int win_width, win_height;
23 static unsigned int fb_tex;
24 static unsigned int fbo, fb_depth;
25 static int fb_xsz, fb_ysz;
26 static int fb_tex_xsz, fb_tex_ysz;
28 static unsigned int chess_tex;
30 static float cam_theta, cam_phi;
31 static Vector3 cam_pos;
32 static bool keystate[256];
34 static Console con;
35 static dtx_font *con_font;
37 bool game_init()
38 {
39 init_opengl();
41 if(vr_init() == -1) {
42 return false;
43 }
45 glEnable(GL_DEPTH_TEST);
46 glEnable(GL_CULL_FACE);
47 glEnable(GL_LIGHTING);
49 unsigned char chess_pixels[] = {
50 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff,
51 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff
52 };
53 glGenTextures(1, &chess_tex);
54 glBindTexture(GL_TEXTURE_2D, chess_tex);
55 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
56 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
57 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, chess_pixels);
59 if(!(con_font = dtx_open_font_glyphmap("data/mono14.glyphmap"))) {
60 fprintf(stderr, "failed to open console font\n");
61 return false;
62 }
63 con.set_font(con_font, 14);
64 con.set_size(7, 40);
65 con.set_position(0, 0, Console::CENTER);
66 con.set_command_func(con_handle);
68 return true;
69 }
71 void game_cleanup()
72 {
73 vr_shutdown();
74 }
76 void game_update(long tm)
77 {
78 static long prev_upd;
79 float dt = (tm - prev_upd) / 1000.0;
80 prev_upd = tm;
82 if(con.is_visible()) {
83 con.update();
84 return;
85 }
87 float offs = dt * 5.0;
88 Vector3 dir;
90 if(keystate['d'] || keystate['D']) {
91 dir += Vector3(offs, 0, 0);
92 }
93 if(keystate['a'] || keystate['A']) {
94 dir += Vector3(-offs, 0, 0);
95 }
96 if(keystate['w'] || keystate['W']) {
97 dir += Vector3(0, 0, -offs);
98 }
99 if(keystate['s'] || keystate['S']) {
100 dir += Vector3(0, 0, offs);
101 }
103 float cos_theta = cos(DEG_TO_RAD(cam_theta));
104 float sin_theta = sin(DEG_TO_RAD(cam_theta));
105 cam_pos.x += dir.x * cos_theta - dir.z * sin_theta;
106 cam_pos.z += dir.x * sin_theta + dir.z * cos_theta;
108 if(!opt_separate_walk_look) {
109 float rot[4];
110 vr_view_rotation(0, rot);
111 Quaternion q(rot[3], rot[0], rot[1], rot[2]);
113 cam_pos.transform(q);
114 }
115 }
117 void game_display()
118 {
119 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
120 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
122 for(int i=0; i<2; i++) {
123 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
124 vr_begin(i);
126 float proj[16];
127 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
128 glMatrixMode(GL_PROJECTION);
129 glLoadMatrixf(proj);
130 }
132 glMatrixMode(GL_MODELVIEW);
134 float view[16];
135 vr_view_matrix(i, view);
136 glLoadMatrixf(view);
137 glRotatef(cam_phi, 1, 0, 0);
138 glRotatef(cam_theta, 0, 1, 0);
139 /* move the camera to the eye level of the user */
140 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
141 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
143 draw_scene();
144 con.draw();
146 vr_end();
147 }
149 glBindFramebuffer(GL_FRAMEBUFFER, 0);
150 glViewport(0, 0, win_width, win_height);
152 vr_swap_buffers();
153 assert(glGetError() == GL_NO_ERROR);
154 }
156 void game_reshape(int x, int y)
157 {
158 win_width = x;
159 win_height = y;
161 create_rtarg(vr_geti_def(VR_RENDER_XRES, x * 2), vr_geti_def(VR_RENDER_YRES, y));
162 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
164 /* these might be overriden in VR mode (see game_display) */
165 glViewport(0, 0, x, y);
166 glMatrixMode(GL_PROJECTION);
167 glLoadIdentity();
168 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
169 }
171 void game_keyboard(int key, bool pressed)
172 {
173 if(pressed) {
174 if(con.is_visible()) {
175 if(key == 27 || key == '`') {
176 con.hide();
177 } else {
178 con.input_key(key);
179 }
180 } else {
181 switch(key) {
182 case 27:
183 exit_game();
184 break;
186 case 'f':
187 toggle_hmd_fullscr();
188 break;
190 case 'r':
191 vr_recenter();
192 break;
194 case '`':
195 con.show();
196 break;
198 default:
199 break;
200 }
201 }
202 }
204 if(key < 256) {
205 keystate[key] = pressed;
206 }
207 }
210 static int prev_x, prev_y;
211 static bool bnstate[32];
213 void game_mouse_button(int bn, bool state, int x, int y)
214 {
215 bnstate[bn] = state;
216 prev_x = x;
217 prev_y = y;
218 }
220 void game_mouse_motion(int x, int y)
221 {
222 int dx = x - prev_x;
223 int dy = y - prev_y;
224 prev_x = x;
225 prev_y = y;
227 if(!dx && !dy) {
228 return;
229 }
230 if(bnstate[0]) {
231 cam_theta += dx * 0.5;
232 cam_phi += dy * 0.5;
234 if(cam_phi < -90) cam_phi = -90;
235 if(cam_phi > 90) cam_phi = 90;
236 }
237 }
239 static void draw_scene()
240 {
241 float lpos[][4] = {
242 {-0.7, 0.7, 1, 0},
243 {1, 0, 1, 0}
244 };
245 float lcol[][4] = {
246 {0.9, 0.7, 0.6, 1},
247 {0.3, 0.4, 0.75, 1}
248 };
249 for(int i=0; i<2; i++) {
250 glEnable(GL_LIGHT0 + i);
251 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
252 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
253 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
254 }
256 glMatrixMode(GL_MODELVIEW);
258 glBindTexture(GL_TEXTURE_2D, chess_tex);
259 glEnable(GL_TEXTURE_2D);
260 material(1, 1, 1, 1);
261 glBegin(GL_QUADS);
262 glNormal3f(0, 1, 0);
263 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
264 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
265 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
266 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
267 glEnd();
268 glDisable(GL_TEXTURE_2D);
270 material(1, 1, 1, 0.4);
271 glPushMatrix();
272 glTranslatef(0, 1.3, -10);
273 glFrontFace(GL_CW);
274 bezier_teapot(2.0);
275 glFrontFace(GL_CCW);
276 glPopMatrix();
277 }
279 static void material(float r, float g, float b, float roughness)
280 {
281 float gloss = 1.0 - roughness;
282 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
283 float specular[] = {gloss, gloss, gloss, 1.0};
284 float shin = gloss * 128.0;
286 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
287 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
288 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
289 }
292 static void toggle_hmd_fullscr()
293 {
294 static bool fullscr;
295 static int prev_x, prev_y;
296 //static int prev_xsz, prev_ysz;
298 fullscr = !fullscr;
299 if(fullscr) {
300 /* entering fullscreen on the HMD */
301 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
302 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
303 if(xoffs != -1) {
304 get_window_pos(&prev_x, &prev_y);
305 move_window(xoffs, yoffs);
306 }
308 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
309 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
310 if(xsz != -1) {
311 //prev_xsz = win_width;
312 //prev_ysz = win_height;
313 resize_window(xsz, ysz);
314 }
315 enter_fullscreen();
317 } else {
318 /* leaving fullscreen */
319 leave_fullscreen();
320 /*move_window(prev_x, prev_y);
321 resize_window(prev_xsz, prev_ysz);*/
322 }
323 }
325 static void create_rtarg(int x, int y)
326 {
327 if(x == fb_xsz && y == fb_ysz) {
328 return; // nothing changed
329 }
331 fb_xsz = x;
332 fb_ysz = y;
333 fb_tex_xsz = next_pow2(fb_xsz);
334 fb_tex_ysz = next_pow2(fb_ysz);
336 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
338 if(!fbo) {
339 glGenFramebuffers(1, &fbo);
341 glGenTextures(1, &fb_tex);
342 glBindTexture(GL_TEXTURE_2D, fb_tex);
343 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
344 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
346 glGenRenderbuffers(1, &fb_depth);
347 }
349 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
351 glBindTexture(GL_TEXTURE_2D, fb_tex);
352 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
353 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
355 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
356 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
357 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
359 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
360 glBindFramebuffer(GL_FRAMEBUFFER, 0);
361 }
363 static int next_pow2(int x)
364 {
365 x -= 1;
366 x |= x >> 1;
367 x |= x >> 2;
368 x |= x >> 4;
369 x |= x >> 8;
370 x |= x >> 16;
371 return x + 1;
372 }
374 static const char *strip_spaces(const char *str)
375 {
376 while(*str && isspace(*str)) str++;
377 return str;
378 }
380 static void con_handle(const char *cmd)
381 {
382 std::vector<char*> argv;
383 cmd = strip_spaces(cmd);
385 if(!*cmd || *cmd == '#') {
386 return;
387 }
389 char *line = (char*)alloca(strlen(cmd) + 1);
390 strcpy(line, cmd);
392 char *tok = 0;
393 while((tok = strtok(tok ? 0 : line, " \t\v\n\r"))) {
394 argv.push_back(tok);
395 }
397 if(strcmp(argv[0], "set") == 0) {
398 if(argv.size() < 3) {
399 fprintf(stderr, "set must be followed by 2 arguments\n");
400 return;
401 }
402 if(strcmp(argv[1], "separate-walk") == 0) {
403 if(strcmp(argv[2], "true") == 0) {
404 opt_separate_walk_look = true;
405 } else if(strcmp(argv[2], "false") == 0) {
406 opt_separate_walk_look = false;
407 }
408 } else {
409 con.printf("unknown option: %s\n", argv[1]);
410 }
411 } else if(strcmp(argv[0], "exit") == 0 || strcmp(argv[0], "quit") == 0) {
412 exit_game();
413 } else {
414 con.printf("invalid command: %s\n", argv[0]);
415 }
416 }