oculus1

view src/main.cc @ 5:681046a82ed2

foo
author John Tsiombikas <nuclear@member.fsf.org>
date Sun, 15 Sep 2013 05:01:38 +0300
parents bb81990dc7c8
children d6dd6372add6
line source
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <assert.h>
5 #include <GL/glew.h>
6 #ifdef __APPLE__
7 #include <GLUT/glut.h>
8 #else
9 #include <GL/glut.h>
10 #endif
11 #include "vr.h"
12 #include "camera.h"
14 static bool init();
15 static void cleanup();
16 static void disp();
17 static void idle();
18 static void reshape(int x, int y);
19 static void keyb(unsigned char key, int x, int y);
20 static void sball_rotate(int rx, int ry, int rz);
21 static bool parse_args(int argc, char **argv);
23 static Camera cam;
24 static int width, height;
25 static bool use_vr = false;
27 int main(int argc, char **argv)
28 {
29 glutInitWindowSize(1280, 800);
30 glutInit(&argc, argv);
32 if(!parse_args(argc, argv)) {
33 return 1;
34 }
36 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
37 glutCreateWindow("oculus test 01");
39 glutDisplayFunc(disp);
40 glutIdleFunc(idle);
41 glutReshapeFunc(reshape);
42 glutKeyboardFunc(keyb);
43 glutSpaceballRotateFunc(sball_rotate);
45 glewInit();
47 if(!init()) {
48 return 1;
49 }
50 atexit(cleanup);
52 glutMainLoop();
53 return 0;
54 }
56 static bool init()
57 {
58 glEnable(GL_DEPTH_TEST);
59 glEnable(GL_LIGHTING);
60 glEnable(GL_CULL_FACE);
62 glEnable(GL_LIGHT0);
63 glEnable(GL_LIGHTING);
65 if(vr_init(VR_INIT_OCULUS) == -1) {
66 return false;
67 }
68 return true;
69 }
71 static void cleanup()
72 {
73 vr_shutdown();
74 }
76 static void disp()
77 {
78 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
80 // test rift sensor
81 float quat[4], euler[3];
83 vr_get_rotation(quat);
84 vr_get_rotation_euler(euler);
86 Quaternion qrot(quat[3], quat[0], quat[1], quat[2]);
88 static unsigned int prev_print;
89 if(msec - prev_print > 1000) {
90 printf("q(%.3f + %.3fi + %.3fj %.3fk)", quat[3], quat[0], quat[1], quat[2]);
91 printf(" - euler(%.3f %.3f %.3f)\n", euler[0], euler[1], euler[2]);
92 prev_print = msec;
93 }
95 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
97 glMatrixMode(GL_PROJECTION);
98 glLoadIdentity();
99 gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0);
101 glMatrixMode(GL_MODELVIEW);
102 glLoadIdentity();
103 glTranslatef(0, 0, -8);
105 float lpos[] = {0, 60, 0, 1};
106 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
108 glFrontFace(GL_CW);
109 glutSolidTeapot(1.0);
110 glFrontFace(GL_CCW);
112 glutSwapBuffers();
113 assert(glGetError() == GL_NO_ERROR);
114 }
116 static void idle()
117 {
118 glutPostRedisplay();
119 }
122 static void reshape(int x, int y)
123 {
124 width = x;
125 height = y;
126 }
128 static void keyb(unsigned char key, int x, int y)
129 {
130 switch(key) {
131 case 27:
132 exit(0);
133 }
134 }
136 static void sball_rotate(int rx, int ry, int rz)
137 {
138 }
140 static bool parse_args(int argc, char **argv)
141 {
142 for(int i=1; i<argc; i++) {
143 if(argv[i][0] == '-') {
144 if(strcmp(argv[i], "-vr") == 0) {
145 use_vr = true;
146 } else if(strcmp(argv[i], "-novr") == 0) {
147 use_vr = false;
148 } else {
149 fprintf(stderr, "invalid option: %s\n", argv[i]);
150 return false;
151 }
152 } else {
153 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
154 return false;
155 }
156 }
157 return true;
158 }