oculus1

view src/main.cc @ 22:251bc9e2e4a1

made mouselook only affect horizontal movement in VR mode
author John Tsiombikas <nuclear@member.fsf.org>
date Sat, 28 Sep 2013 04:13:09 +0300
parents f3672317e5c2
children 0c76f70fb7e9
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include "opengl.h"
6 #include "vr.h"
7 #include "camera.h"
8 #include "sdr.h"
10 static bool init();
11 static void cleanup();
12 static void disp();
13 static void disp_vr();
14 static void draw_scene();
15 static void draw_teapot(float size);
16 static void draw_squares();
17 static void draw_grid(float size, float spacing);
18 static void toggle_mouselook();
19 static void toggle_fullscreen();
20 static void idle();
21 static void reshape(int x, int y);
22 static void keyb(unsigned char key, int x, int y);
23 static void keyup(unsigned char key, int x, int y);
24 static void mouse(int bn, int st, int x, int y);
25 static void motion(int x, int y);
26 static void passive(int x, int y);
27 static void sball_rotate(int rx, int ry, int rz);
28 static bool parse_args(int argc, char **argv);
30 static VRFpsCamera cam;
31 static int width, height;
32 static bool use_vr = false;
34 static bool keystate[256];
36 static int rtarg_width, rtarg_height;
37 static unsigned int fbo, tex[2], zbuf;
39 static unsigned int teapot_sdr;
41 static bool fullscreen_pending;
43 int main(int argc, char **argv)
44 {
45 glutInitWindowSize(1280, 800);
46 glutInit(&argc, argv);
48 if(!parse_args(argc, argv)) {
49 return 1;
50 }
52 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
53 glutCreateWindow("oculus vr test 01");
55 width = glutGet(GLUT_WINDOW_WIDTH);
56 height = glutGet(GLUT_WINDOW_HEIGHT);
58 glutDisplayFunc(use_vr ? disp_vr : disp);
59 glutIdleFunc(idle);
60 glutReshapeFunc(reshape);
61 glutKeyboardFunc(keyb);
62 glutKeyboardUpFunc(keyup);
63 glutMouseFunc(mouse);
64 glutMotionFunc(motion);
65 glutSpaceballRotateFunc(sball_rotate);
67 if(!init()) {
68 return 1;
69 }
70 atexit(cleanup);
72 glutMainLoop();
73 return 0;
74 }
76 static bool init()
77 {
78 glewInit(); // this must be first
80 glEnable(GL_DEPTH_TEST);
81 glEnable(GL_LIGHTING);
82 glEnable(GL_CULL_FACE);
84 glEnable(GL_LIGHT0);
85 glEnable(GL_LIGHTING);
86 glEnable(GL_NORMALIZE);
88 // y = height of neck
89 cam.input_move(0, 1.65, 0);
91 if(use_vr) {
92 if(vr_init(VR_INIT_OCULUS) == -1) {
93 return false;
94 }
96 // reshape to the size of the VR display
97 int xsz = vr_get_width();
98 int ysz = vr_get_height();
100 glutReshapeWindow(xsz, ysz);
102 rtarg_width = (xsz + xsz / 2) / 2;
103 rtarg_height = ysz + ysz / 2;
105 printf("render target: %dx%d\n", rtarg_width, rtarg_height);
107 // create render targets for each eye
108 GLenum wrap_mode = GL_CLAMP_TO_EDGE;
109 if(!GLEW_SGIS_texture_edge_clamp) {
110 wrap_mode = GL_CLAMP;
111 }
113 glGenTextures(2, tex);
114 for(int i=0; i<2; i++) {
115 glBindTexture(GL_TEXTURE_2D, tex[i]);
116 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
117 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
118 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_mode);
119 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_mode);
120 glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rtarg_width, rtarg_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
121 }
123 // create the depth render buffer
124 glGenRenderbuffers(1, &zbuf);
125 glBindRenderbuffer(GL_RENDERBUFFER, zbuf);
126 glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, rtarg_width, rtarg_height);
128 // create the FBO
129 glGenFramebuffers(1, &fbo);
130 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
131 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[0], 0);
132 glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, zbuf);
133 }
135 teapot_sdr = create_program_load("sdr/phong.v.glsl", "sdr/phong.p.glsl");
137 return true;
138 }
140 static void cleanup()
141 {
142 glDeleteTextures(2, tex);
143 glDeleteRenderbuffers(1, &zbuf);
144 glDeleteFramebuffers(1, &fbo);
145 vr_shutdown();
146 }
148 static void handle_input(float dt)
149 {
150 Vector3 inpv;
151 float offs = dt * 2.0;
153 if(keystate['w'] || keystate['W']) {
154 inpv.z -= offs;
155 }
156 if(keystate['s'] || keystate['S']) {
157 inpv.z += offs;
158 }
159 if(keystate['d'] || keystate['D']) {
160 inpv.x += offs;
161 }
162 if(keystate['a'] || keystate['A']) {
163 inpv.x -= offs;
164 }
166 cam.input_move(inpv.x, inpv.y, inpv.z);
167 }
169 // display function used in regular mode
170 static void disp()
171 {
172 static long prev_msec;
173 long msec = glutGet(GLUT_ELAPSED_TIME);
174 float dt = (msec - prev_msec) / 1000.0;
175 prev_msec = msec;
177 handle_input(dt);
179 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
181 glMatrixMode(GL_PROJECTION);
182 glLoadIdentity();
183 gluPerspective(45, (float)width / (float)height, 0.25, 500.0);
185 glMatrixMode(GL_MODELVIEW);
186 glLoadIdentity();
187 cam.use_inverse();
188 draw_scene();
190 glutSwapBuffers();
191 assert(glGetError() == GL_NO_ERROR);
192 }
194 // display function used in VR mode
195 static void disp_vr()
196 {
197 static long prev_msec;
198 long msec = glutGet(GLUT_ELAPSED_TIME);
199 float dt = (msec - prev_msec) / 1000.0;
200 prev_msec = msec;
202 handle_input(dt);
203 cam.track_vr();
205 float proj_matrix[16];
207 float eye_dist = vr_get_eyedist();
209 glViewport(0, 0, rtarg_width, rtarg_height);
211 glClearColor(0.1, 0.1, 0.1, 1.0);
213 // draw left view
214 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
215 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[0], 0);
217 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
219 glMatrixMode(GL_PROJECTION);
220 vr_get_proj_matrix(proj_matrix, VR_EYE_LEFT);
221 glLoadTransposeMatrixf(proj_matrix);
223 glMatrixMode(GL_MODELVIEW);
224 glLoadIdentity();
225 glTranslatef(eye_dist / 2.0, 0, 0);
226 cam.use_inverse();
227 draw_scene();
230 // draw right view
231 glBindFramebuffer(GL_FRAMEBUFFER, fbo);
232 glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[1], 0);
234 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
236 glMatrixMode(GL_PROJECTION);
237 vr_get_proj_matrix(proj_matrix, VR_EYE_RIGHT);
238 glLoadTransposeMatrixf(proj_matrix);
240 glMatrixMode(GL_MODELVIEW);
241 glLoadIdentity();
242 glTranslatef(-eye_dist / 2.0, 0, 0);
243 cam.use_inverse();
244 draw_scene();
246 // return to the regular window framebuffer
247 glBindFramebuffer(GL_FRAMEBUFFER, 0);
248 glViewport(0, 0, width, height);
250 glClearColor(0, 0, 0, 0);
251 glClear(GL_COLOR_BUFFER_BIT);
253 vr_draw_eye(tex[0], VR_EYE_LEFT);
254 vr_draw_eye(tex[1], VR_EYE_RIGHT);
256 glutSwapBuffers();
257 assert(glGetError() == GL_NO_ERROR);
259 glFinish();
260 }
262 static void draw_scene()
263 {
264 float lpos[] = {0, 60, 0, 1};
265 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
267 draw_grid(50.0, 2.5);
269 static const Vector2 teapos[] = {
270 Vector2(-8, -8), Vector2(8, -8), Vector2(8, 8), Vector2(-8, 8)
271 };
272 static const float teasize[] = { 1.0, 2.0, 1.7, 1.4 };
273 static const float teacolor[][4] = {
274 {1.0, 0.4, 0.2, 1.0}, {0.2, 0.35, 1.0, 1.0}, {1.0, 0.9, 0.3, 1.0}, {0.3, 1.0, 0.4, 1.0}
275 };
276 static const float spec[] = {0.8, 0.8, 0.8, 1.0};
278 glUseProgram(teapot_sdr);
280 for(int i=0; i<4; i++) {
281 glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT_AND_DIFFUSE, teacolor[i]);
282 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, spec);
283 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 70.0);
285 glPushMatrix();
286 glTranslatef(teapos[i].x, 0, teapos[i].y);
287 draw_teapot(teasize[i]);
288 glPopMatrix();
289 }
290 glUseProgram(0);
292 draw_squares();
293 }
295 static void draw_teapot(float size)
296 {
297 static int tealist;
299 if(!tealist) {
300 tealist = glGenLists(1);
301 glNewList(tealist, GL_COMPILE);
302 glutSolidTeapot(1.0);
303 glEndList();
304 }
306 glMatrixMode(GL_MODELVIEW);
307 glPushMatrix();
308 glScalef(size, size, size);
309 glTranslatef(0, 0.73, 0);
311 glFrontFace(GL_CW);
312 glCallList(tealist);
313 glFrontFace(GL_CCW);
315 glPopMatrix();
316 }
318 static void draw_squares()
319 {
320 static const int num_sq = 8;
322 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
323 glDisable(GL_LIGHTING);
325 glMatrixMode(GL_MODELVIEW);
326 glPushMatrix();
327 glTranslatef(0, 1, 0);
330 glLineWidth(2.0);
331 glColor3f(1.0, 0.7, 0.2);
333 float zdist = 2.0;
334 for(int i=0; i<num_sq; i++) {
335 glBegin(GL_LINE_LOOP);
336 glVertex3f(-1, -1, -zdist);
337 glVertex3f(1, -1, -zdist);
338 glVertex3f(1, 1, -zdist);
339 glVertex3f(-1, 1, -zdist);
340 glEnd();
342 zdist += 2.0;
343 }
345 glPopMatrix();
346 glPopAttrib();
347 }
349 static void draw_grid(float size, float spacing)
350 {
351 int num_lines = size / spacing;
352 float dist = size / 2.0;
354 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
355 glDisable(GL_LIGHTING);
357 glLineWidth(1.0);
359 glBegin(GL_LINES);
360 glColor3f(0.4, 0.4, 0.4);
362 float x = -dist;
363 for(int i=0; i<=num_lines; i++) {
364 if(i != num_lines / 2) {
365 glVertex3f(-dist, 0, x);
366 glVertex3f(dist, 0, x);
367 glVertex3f(x, 0, -dist);
368 glVertex3f(x, 0, dist);
369 }
370 x += spacing;
371 }
372 glEnd();
374 glLineWidth(2.0);
376 glBegin(GL_LINES);
377 glColor3f(1.0, 0, 0);
378 glVertex3f(-dist, 0, 0);
379 glVertex3f(dist, 0, 0);
380 glColor3f(0, 1.0, 0);
381 glVertex3f(0, 0, -dist);
382 glVertex3f(0, 0, dist);
383 glEnd();
385 glPopAttrib();
386 }
388 static bool mouselook;
390 static void toggle_mouselook()
391 {
392 mouselook = !mouselook;
393 if(mouselook) {
394 glutPassiveMotionFunc(passive);
395 glutSetCursor(GLUT_CURSOR_NONE);
396 glutWarpPointer(width / 2, height / 2);
397 } else {
398 glutPassiveMotionFunc(0);
399 glutSetCursor(GLUT_CURSOR_INHERIT);
400 }
401 }
403 static bool fullscreen;
404 static void toggle_fullscreen()
405 {
406 static int prev_x, prev_y;
407 static int prev_xsz, prev_ysz;
409 fullscreen = !fullscreen;
411 if(fullscreen) {
412 prev_x = glutGet(GLUT_WINDOW_X);
413 prev_y = glutGet(GLUT_WINDOW_Y);
414 prev_xsz = width;
415 prev_ysz = height;
417 if(use_vr) {
418 // go fullscreen to the correct monitor
419 int x, y;
420 vr_get_display_pos(&x, &y);
421 glutPositionWindow(x, y);
423 // also warp the mouse and enable mouselook
424 glutWarpPointer(x + width / 2, y + height / 2);
426 /* Ok this next line needs some explanation:
427 * glutPositionWindow, doesn't necessarilly get executed directly.
428 * GLUT might defer it for the next round of event processing, just
429 * so that it may coalesce multiple positioning requests.
430 * However that means that if we just called glutFullScreen right
431 * here, the window manager would have no idea that the window will
432 * move to another monitor, thus making it the size of the monitor
433 * it occupied before the move.
434 * So I'm setting a flag here, and execute the glutFullScreen call
435 * at the next idle invocation. (would display be a safer place?).
436 */
437 fullscreen_pending = true;
438 } else {
439 glutFullScreen();
440 toggle_mouselook();
441 }
442 } else {
443 glutReshapeWindow(prev_xsz, prev_ysz);
444 glutPositionWindow(prev_x, prev_y);
446 if(mouselook) {
447 toggle_mouselook();
448 }
449 }
450 glutPostRedisplay();
451 }
454 static void idle()
455 {
456 if(fullscreen_pending) {
457 glutFullScreen();
459 if(!mouselook) {
460 toggle_mouselook();
461 }
462 fullscreen_pending = false;
463 }
464 glutPostRedisplay();
465 }
468 static void reshape(int x, int y)
469 {
470 width = x;
471 height = y;
473 if(!use_vr) {
474 rtarg_width = width;
475 rtarg_height = height;
476 }
477 }
479 static void keyb(unsigned char key, int x, int y)
480 {
481 switch(key) {
482 case 27:
483 exit(0);
485 case 'm':
486 toggle_mouselook();
487 break;
489 case 'f':
490 toggle_fullscreen();
491 break;
492 }
494 keystate[key] = true;
495 glutPostRedisplay();
496 }
498 static void keyup(unsigned char key, int x, int y)
499 {
500 keystate[key] = false;
501 glutPostRedisplay();
502 }
504 static bool bnstate[32];
505 static int prev_x, prev_y;
507 static void mouse(int bn, int st, int x, int y)
508 {
509 prev_x = x;
510 prev_y = y;
511 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
512 }
514 static void motion(int x, int y)
515 {
516 if(mouselook) {
517 // just call passive, it does what we need
518 passive(x, y);
519 }
520 }
522 static void passive(int x, int y)
523 {
524 // no need to test mouselook; this callback is only set when mouselook is enabled
525 int center_x = width / 2;
526 int center_y = height / 2;
528 int dx = x - center_x;
529 int dy = y - center_y;
531 if(use_vr && fullscreen) {
532 dy = 0;
533 }
535 if(!dx && !dy) {
536 return;
537 }
539 float dtheta_deg = dy * 0.1;
540 float dphi_deg = dx * 0.1;
542 cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0);
544 glutPostRedisplay();
545 glutWarpPointer(center_x, center_y);
546 }
548 static void sball_rotate(int rx, int ry, int rz)
549 {
550 }
552 static bool parse_args(int argc, char **argv)
553 {
554 for(int i=1; i<argc; i++) {
555 if(argv[i][0] == '-') {
556 if(strcmp(argv[i], "-vr") == 0) {
557 use_vr = true;
558 } else if(strcmp(argv[i], "-novr") == 0) {
559 use_vr = false;
560 } else {
561 fprintf(stderr, "invalid option: %s\n", argv[i]);
562 return false;
563 }
564 } else {
565 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
566 return false;
567 }
568 }
569 return true;
570 }