vrheights

view src/game.cc @ 5:053a52f0cb64

console
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 26 Sep 2014 18:40:15 +0300
parents 690ef7fa791f
children 608e03b688f8
line source
1 #include <stdio.h>
2 #include <assert.h>
3 #include <algorithm>
4 #include <vmath/vmath.h>
5 #include "opengl.h"
6 #include "game.h"
7 #include "goatvr.h"
8 #include "teapot.h"
9 #include "console.h"
10 #include "drawtext.h"
12 static void draw_scene();
13 static void material(float r, float g, float b, float roughness);
14 static void toggle_hmd_fullscr();
15 static void create_rtarg(int x, int y);
16 static int next_pow2(int x);
18 bool opt_separate_walk_look;
20 static int win_width, win_height;
21 static unsigned int fb_tex;
22 static unsigned int fbo, fb_depth;
23 static int fb_xsz, fb_ysz;
24 static int fb_tex_xsz, fb_tex_ysz;
26 static unsigned int chess_tex;
28 static float cam_theta, cam_phi;
29 static Vector3 cam_pos;
30 static bool keystate[256];
32 static Console con;
33 static dtx_font *con_font;
35 bool game_init()
36 {
37 init_opengl();
39 if(vr_init() == -1) {
40 return false;
41 }
43 glEnable(GL_DEPTH_TEST);
44 glEnable(GL_CULL_FACE);
45 glEnable(GL_LIGHTING);
47 unsigned char chess_pixels[] = {
48 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff,
49 0xff, 0x00, 0xff, 0x00, 0x00, 0xff, 0x00, 0xff
50 };
51 glGenTextures(1, &chess_tex);
52 glBindTexture(GL_TEXTURE_2D, chess_tex);
53 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
54 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
55 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 4, 4, 0, GL_LUMINANCE, GL_UNSIGNED_BYTE, chess_pixels);
57 if(!(con_font = dtx_open_font_glyphmap("data/mono14.glyphmap"))) {
58 fprintf(stderr, "failed to open console font\n");
59 return false;
60 }
61 con.set_size(6, 40);
62 con.set_font(con_font, 14);
63 con.set_position(0, 0, Console::CENTER);
65 return true;
66 }
68 void game_cleanup()
69 {
70 vr_shutdown();
71 }
73 void game_update(long tm)
74 {
75 static long prev_upd;
76 float dt = (tm - prev_upd) / 1000.0;
77 prev_upd = tm;
79 if(con.is_visible()) {
80 con.update();
81 return;
82 }
84 float offs = dt * 5.0;
85 Vector3 dir;
87 if(keystate['d'] || keystate['D']) {
88 dir += Vector3(offs, 0, 0);
89 }
90 if(keystate['a'] || keystate['A']) {
91 dir += Vector3(-offs, 0, 0);
92 }
93 if(keystate['w'] || keystate['W']) {
94 dir += Vector3(0, 0, -offs);
95 }
96 if(keystate['s'] || keystate['S']) {
97 dir += Vector3(0, 0, offs);
98 }
100 float cos_theta = cos(DEG_TO_RAD(cam_theta));
101 float sin_theta = sin(DEG_TO_RAD(cam_theta));
102 cam_pos.x += dir.x * cos_theta - dir.z * sin_theta;
103 cam_pos.z += dir.x * sin_theta + dir.z * cos_theta;
105 if(!opt_separate_walk_look) {
106 }
107 }
109 void game_display()
110 {
111 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
112 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
114 for(int i=0; i<2; i++) {
115 glViewport(i == 0 ? 0 : fb_xsz / 2, 0, fb_xsz / 2, fb_ysz);
116 vr_begin(i);
118 float proj[16];
119 if(vr_proj_matrix(i, 0.5, 500.0, proj)) {
120 glMatrixMode(GL_PROJECTION);
121 glLoadMatrixf(proj);
122 }
124 glMatrixMode(GL_MODELVIEW);
126 float view[16];
127 vr_view_matrix(i, view);
128 glLoadMatrixf(view);
129 glRotatef(cam_phi, 1, 0, 0);
130 glRotatef(cam_theta, 0, 1, 0);
131 /* move the camera to the eye level of the user */
132 glTranslatef(0, -vr_getf_def(VR_EYE_HEIGHT, 1.65), 0);
133 glTranslatef(-cam_pos.x, -cam_pos.y, -cam_pos.z);
135 draw_scene();
136 con.draw();
138 vr_end();
139 }
141 glBindFramebuffer(GL_FRAMEBUFFER, 0);
142 glViewport(0, 0, win_width, win_height);
144 vr_swap_buffers();
145 assert(glGetError() == GL_NO_ERROR);
146 }
148 void game_reshape(int x, int y)
149 {
150 win_width = x;
151 win_height = y;
153 create_rtarg(vr_geti_def(VR_RENDER_XRES, x * 2), vr_geti_def(VR_RENDER_YRES, y));
154 vr_output_texture(fb_tex, 0, 0, (float)fb_xsz / (float)fb_tex_xsz, (float)fb_ysz / (float)fb_tex_ysz);
156 /* these might be overriden in VR mode (see game_display) */
157 glViewport(0, 0, x, y);
158 glMatrixMode(GL_PROJECTION);
159 glLoadIdentity();
160 gluPerspective(50.0, (float)x / (float)y, 0.5, 500.0);
161 }
163 void game_keyboard(int key, bool pressed)
164 {
165 if(pressed) {
166 if(con.is_visible()) {
167 if(key == 27 || key == '`') {
168 con.hide();
169 } else {
170 con.input_key(key);
171 }
172 } else {
173 switch(key) {
174 case 27:
175 exit_game();
176 break;
178 case 'f':
179 toggle_hmd_fullscr();
180 break;
182 case 'r':
183 vr_recenter();
184 break;
186 case '`':
187 con.show();
188 break;
190 default:
191 break;
192 }
193 }
194 }
196 if(key < 256) {
197 keystate[key] = pressed;
198 }
199 }
202 static int prev_x, prev_y;
203 static bool bnstate[32];
205 void game_mouse_button(int bn, bool state, int x, int y)
206 {
207 bnstate[bn] = state;
208 prev_x = x;
209 prev_y = y;
210 }
212 void game_mouse_motion(int x, int y)
213 {
214 int dx = x - prev_x;
215 int dy = y - prev_y;
216 prev_x = x;
217 prev_y = y;
219 if(!dx && !dy) {
220 return;
221 }
222 if(bnstate[0]) {
223 cam_theta += dx * 0.5;
224 cam_phi += dy * 0.5;
226 if(cam_phi < -90) cam_phi = -90;
227 if(cam_phi > 90) cam_phi = 90;
228 }
229 }
231 static void draw_scene()
232 {
233 float lpos[][4] = {
234 {-0.7, 0.7, 1, 0},
235 {1, 0, 1, 0}
236 };
237 float lcol[][4] = {
238 {0.9, 0.7, 0.6, 1},
239 {0.3, 0.4, 0.75, 1}
240 };
241 for(int i=0; i<2; i++) {
242 glEnable(GL_LIGHT0 + i);
243 glLightfv(GL_LIGHT0 + i, GL_POSITION, lpos[i]);
244 glLightfv(GL_LIGHT0 + i, GL_DIFFUSE, lcol[i]);
245 glLightfv(GL_LIGHT0 + i, GL_SPECULAR, lcol[i]);
246 }
248 glMatrixMode(GL_MODELVIEW);
250 glBindTexture(GL_TEXTURE_2D, chess_tex);
251 glEnable(GL_TEXTURE_2D);
252 material(1, 1, 1, 1);
253 glBegin(GL_QUADS);
254 glNormal3f(0, 1, 0);
255 glTexCoord2f(0, 0); glVertex3f(-10, 0, 10);
256 glTexCoord2f(1, 0); glVertex3f(10, 0, 10);
257 glTexCoord2f(1, 1); glVertex3f(10, 0, -10);
258 glTexCoord2f(0, 1); glVertex3f(-10, 0, -10);
259 glEnd();
260 glDisable(GL_TEXTURE_2D);
262 material(1, 1, 1, 0.4);
263 glPushMatrix();
264 glTranslatef(0, 1.3, -10);
265 glFrontFace(GL_CW);
266 bezier_teapot(2.0);
267 glFrontFace(GL_CCW);
268 glPopMatrix();
269 }
271 static void material(float r, float g, float b, float roughness)
272 {
273 float gloss = 1.0 - roughness;
274 float diffuse[] = {r * roughness, g * roughness, b * roughness, 1.0};
275 float specular[] = {gloss, gloss, gloss, 1.0};
276 float shin = gloss * 128.0;
278 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, diffuse);
279 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, specular);
280 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shin);
281 }
284 static void toggle_hmd_fullscr()
285 {
286 static bool fullscr;
287 static int prev_x, prev_y;
288 //static int prev_xsz, prev_ysz;
290 fullscr = !fullscr;
291 if(fullscr) {
292 /* entering fullscreen on the HMD */
293 int xoffs = vr_geti_def(VR_WIN_XOFFS, -1);
294 int yoffs = vr_geti_def(VR_WIN_YOFFS, -1);
295 if(xoffs != -1) {
296 get_window_pos(&prev_x, &prev_y);
297 move_window(xoffs, yoffs);
298 }
300 int xsz = vr_geti_def(VR_DISPLAY_WIDTH, -1);
301 int ysz = vr_geti_def(VR_DISPLAY_HEIGHT, -1);
302 if(xsz != -1) {
303 //prev_xsz = win_width;
304 //prev_ysz = win_height;
305 resize_window(xsz, ysz);
306 }
307 enter_fullscreen();
309 } else {
310 /* leaving fullscreen */
311 leave_fullscreen();
312 /*move_window(prev_x, prev_y);
313 resize_window(prev_xsz, prev_ysz);*/
314 }
315 }
317 static void create_rtarg(int x, int y)
318 {
319 if(x == fb_xsz && y == fb_ysz) {
320 return; // nothing changed
321 }
323 fb_xsz = x;
324 fb_ysz = y;
325 fb_tex_xsz = next_pow2(fb_xsz);
326 fb_tex_ysz = next_pow2(fb_ysz);
328 printf("creating %dx%d render target (tex size: %dx%d)\n", fb_xsz, fb_ysz, fb_tex_xsz, fb_tex_ysz);
330 if(!fbo) {
331 glGenFramebuffers(1, &fbo);
333 glGenTextures(1, &fb_tex);
334 glBindTexture(GL_TEXTURE_2D, fb_tex);
335 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
336 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
338 glGenRenderbuffers(1, &fb_depth);
339 }
341 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
343 glBindTexture(GL_TEXTURE_2D, fb_tex);
344 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, fb_tex_xsz, fb_tex_ysz, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
345 glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, fb_tex, 0);
347 glBindRenderbuffer(GL_RENDERBUFFER, fb_depth);
348 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, fb_tex_xsz, fb_tex_ysz);
349 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fb_depth);
351 assert(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE);
352 glBindFramebuffer(GL_FRAMEBUFFER, 0);
353 }
355 static int next_pow2(int x)
356 {
357 x -= 1;
358 x |= x >> 1;
359 x |= x >> 2;
360 x |= x >> 4;
361 x |= x >> 8;
362 x |= x >> 16;
363 return x + 1;
364 }