oculus1

view src/main.cc @ 9:b66b54a68dfd

tracking almost done
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 19 Sep 2013 06:36:48 +0300
parents 3265970a7315
children b2abb08c8f94
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"
9 static bool init();
10 static void cleanup();
11 static void disp();
12 static void draw_scene();
13 static void idle();
14 static void reshape(int x, int y);
15 static void keyb(unsigned char key, int x, int y);
16 static void keyup(unsigned char key, int x, int y);
17 static void mouse(int bn, int st, int x, int y);
18 static void motion(int x, int y);
19 static void passive(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 FpsCamera cam;
24 static int width, height;
25 static bool use_vr = false;
26 static bool mouselook = false;
28 static bool keystate[256];
31 int main(int argc, char **argv)
32 {
33 glutInitWindowSize(1280, 800);
34 glutInit(&argc, argv);
36 if(!parse_args(argc, argv)) {
37 return 1;
38 }
40 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
41 glutCreateWindow("oculus test 01");
43 glutDisplayFunc(disp);
44 glutIdleFunc(idle);
45 glutReshapeFunc(reshape);
46 glutKeyboardFunc(keyb);
47 glutKeyboardUpFunc(keyup);
48 glutMouseFunc(mouse);
49 glutMotionFunc(motion);
50 glutSpaceballRotateFunc(sball_rotate);
52 if(!init()) {
53 return 1;
54 }
55 atexit(cleanup);
57 glutMainLoop();
58 return 0;
59 }
61 static bool init()
62 {
63 glewInit(); // this must be first
65 if(GLEW_ARB_multisample) {
66 glEnable(GL_MULTISAMPLE);
67 }
69 glEnable(GL_DEPTH_TEST);
70 glEnable(GL_LIGHTING);
71 glEnable(GL_CULL_FACE);
73 glEnable(GL_LIGHT0);
74 glEnable(GL_LIGHTING);
76 cam.input_move(0, 1.75, 0);
78 if(vr_init(VR_INIT_OCULUS) == -1) {
79 return false;
80 }
81 return true;
82 }
84 static void cleanup()
85 {
86 vr_shutdown();
87 }
89 static void disp()
90 {
91 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
93 // test rift sensor
94 float quat[4], euler[3];
96 vr_get_rotation(quat);
97 vr_get_rotation_euler(euler);
99 Quaternion qrot(quat[3], quat[0], quat[1], quat[2]);
101 static unsigned int prev_print;
102 if(msec - prev_print > 1000) {
103 printf("q(%.3f + %.3fi + %.3fj + %.3fk)", quat[3], quat[0], quat[1], quat[2]);
104 printf(" - euler(%.3f %.3f %.3f)\n", euler[0], euler[1], euler[2]);
105 prev_print = msec;
106 }
108 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
110 glMatrixMode(GL_PROJECTION);
111 glLoadIdentity();
112 gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0);
114 glMatrixMode(GL_MODELVIEW);
115 glLoadIdentity();
117 Matrix4x4 mat = qrot.inverse().get_rotation_matrix();
118 load_matrix(mat);
120 cam.use_inverse();
122 draw_scene();
124 glutSwapBuffers();
125 assert(glGetError() == GL_NO_ERROR);
126 }
128 static void draw_teapot()
129 {
130 static int tealist;
132 if(!tealist) {
133 tealist = glGenLists(1);
134 glNewList(tealist, GL_COMPILE);
135 glutSolidTeapot(1.0);
136 glEndList();
137 }
139 glFrontFace(GL_CW);
140 glCallList(tealist);
141 glFrontFace(GL_CCW);
142 }
144 void draw_grid(float size, float spacing)
145 {
146 int num_lines = size / spacing;
147 float dist = size / 2.0;
149 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
150 glDisable(GL_LIGHTING);
152 glLineWidth(1.0);
154 glBegin(GL_LINES);
155 glColor3f(0.4, 0.4, 0.4);
157 float x = -dist;
158 for(int i=0; i<=num_lines; i++) {
159 if(i != num_lines / 2) {
160 glVertex3f(-dist, 0, x);
161 glVertex3f(dist, 0, x);
162 glVertex3f(x, 0, -dist);
163 glVertex3f(x, 0, dist);
164 }
165 x += spacing;
166 }
167 glEnd();
169 glLineWidth(2.0);
171 glBegin(GL_LINES);
172 glColor3f(1.0, 0, 0);
173 glVertex3f(-dist, 0, 0);
174 glVertex3f(dist, 0, 0);
175 glColor3f(0, 1.0, 0);
176 glVertex3f(0, 0, -dist);
177 glVertex3f(0, 0, dist);
178 glEnd();
180 glPopAttrib();
181 }
184 static void draw_scene()
185 {
186 float lpos[] = {0, 60, 0, 1};
187 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
189 static Vector2 teapos[] = {
190 Vector2(-8, 8), Vector2(8, 8), Vector2(8, -8), Vector2(-8, -8)
191 };
193 for(int i=0; i<4; i++) {
194 glPushMatrix();
195 glTranslatef(teapos[i].x, 0, teapos[i].y);
196 draw_teapot();
197 glPopMatrix();
198 }
200 draw_grid(100.0, 2.5);
201 }
203 static void idle()
204 {
205 glutPostRedisplay();
206 }
209 static void reshape(int x, int y)
210 {
211 width = x;
212 height = y;
213 }
215 static void keyb(unsigned char key, int x, int y)
216 {
217 switch(key) {
218 case 27:
219 exit(0);
221 case 'm':
222 mouselook = !mouselook;
223 if(mouselook) {
224 glutPassiveMotionFunc(passive);
225 glutSetCursor(GLUT_CURSOR_NONE);
226 glutWarpPointer(width / 2, height / 2);
227 } else {
228 glutPassiveMotionFunc(0);
229 glutSetCursor(GLUT_CURSOR_INHERIT);
230 }
231 break;
232 }
234 keystate[key] = true;
235 glutPostRedisplay();
236 }
238 static void keyup(unsigned char key, int x, int y)
239 {
240 keystate[key] = false;
241 glutPostRedisplay();
242 }
244 static bool bnstate[32];
245 static int prev_x, prev_y;
247 static void mouse(int bn, int st, int x, int y)
248 {
249 prev_x = x;
250 prev_y = y;
251 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
252 }
254 static void motion(int x, int y)
255 {
256 if(mouselook) {
257 // just call passive, it does what we need
258 passive(x, y);
259 }
260 }
262 static void passive(int x, int y)
263 {
264 // no need to test mouselook; this callback is only set when mouselook is enabled
265 int center_x = width / 2;
266 int center_y = height / 2;
268 int dx = x - center_x;
269 int dy = y - center_y;
271 if(!dx && !dy) {
272 return;
273 }
275 float dtheta_deg = dy * 0.1;
276 float dphi_deg = dx * 0.1;
278 cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0);
280 glutPostRedisplay();
281 glutWarpPointer(center_x, center_y);
282 }
284 static void sball_rotate(int rx, int ry, int rz)
285 {
286 }
288 static bool parse_args(int argc, char **argv)
289 {
290 for(int i=1; i<argc; i++) {
291 if(argv[i][0] == '-') {
292 if(strcmp(argv[i], "-vr") == 0) {
293 use_vr = true;
294 } else if(strcmp(argv[i], "-novr") == 0) {
295 use_vr = false;
296 } else {
297 fprintf(stderr, "invalid option: %s\n", argv[i]);
298 return false;
299 }
300 } else {
301 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
302 return false;
303 }
304 }
305 return true;
306 }