oculus1

view src/main.cc @ 10:b2abb08c8f94

proper FPS-style vr tracking
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Sep 2013 06:49:39 +0300
parents b66b54a68dfd
children 39ec672a5158
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 VRFpsCamera 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 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
95 glMatrixMode(GL_PROJECTION);
96 glLoadIdentity();
97 gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0);
99 glMatrixMode(GL_MODELVIEW);
100 glLoadIdentity();
102 cam.track_vr();
103 cam.use_inverse();
105 draw_scene();
107 glutSwapBuffers();
108 assert(glGetError() == GL_NO_ERROR);
109 }
111 static void draw_teapot()
112 {
113 static int tealist;
115 if(!tealist) {
116 tealist = glGenLists(1);
117 glNewList(tealist, GL_COMPILE);
118 glutSolidTeapot(1.0);
119 glEndList();
120 }
122 glFrontFace(GL_CW);
123 glCallList(tealist);
124 glFrontFace(GL_CCW);
125 }
127 void draw_grid(float size, float spacing)
128 {
129 int num_lines = size / spacing;
130 float dist = size / 2.0;
132 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
133 glDisable(GL_LIGHTING);
135 glLineWidth(1.0);
137 glBegin(GL_LINES);
138 glColor3f(0.4, 0.4, 0.4);
140 float x = -dist;
141 for(int i=0; i<=num_lines; i++) {
142 if(i != num_lines / 2) {
143 glVertex3f(-dist, 0, x);
144 glVertex3f(dist, 0, x);
145 glVertex3f(x, 0, -dist);
146 glVertex3f(x, 0, dist);
147 }
148 x += spacing;
149 }
150 glEnd();
152 glLineWidth(2.0);
154 glBegin(GL_LINES);
155 glColor3f(1.0, 0, 0);
156 glVertex3f(-dist, 0, 0);
157 glVertex3f(dist, 0, 0);
158 glColor3f(0, 1.0, 0);
159 glVertex3f(0, 0, -dist);
160 glVertex3f(0, 0, dist);
161 glEnd();
163 glPopAttrib();
164 }
167 static void draw_scene()
168 {
169 float lpos[] = {0, 60, 0, 1};
170 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
172 static Vector2 teapos[] = {
173 Vector2(-8, 8), Vector2(8, 8), Vector2(8, -8), Vector2(-8, -8)
174 };
176 for(int i=0; i<4; i++) {
177 glPushMatrix();
178 glTranslatef(teapos[i].x, 0, teapos[i].y);
179 draw_teapot();
180 glPopMatrix();
181 }
183 draw_grid(100.0, 2.5);
184 }
186 static void idle()
187 {
188 glutPostRedisplay();
189 }
192 static void reshape(int x, int y)
193 {
194 width = x;
195 height = y;
196 }
198 static void keyb(unsigned char key, int x, int y)
199 {
200 switch(key) {
201 case 27:
202 exit(0);
204 case 'm':
205 mouselook = !mouselook;
206 if(mouselook) {
207 glutPassiveMotionFunc(passive);
208 glutSetCursor(GLUT_CURSOR_NONE);
209 glutWarpPointer(width / 2, height / 2);
210 } else {
211 glutPassiveMotionFunc(0);
212 glutSetCursor(GLUT_CURSOR_INHERIT);
213 }
214 break;
215 }
217 keystate[key] = true;
218 glutPostRedisplay();
219 }
221 static void keyup(unsigned char key, int x, int y)
222 {
223 keystate[key] = false;
224 glutPostRedisplay();
225 }
227 static bool bnstate[32];
228 static int prev_x, prev_y;
230 static void mouse(int bn, int st, int x, int y)
231 {
232 prev_x = x;
233 prev_y = y;
234 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
235 }
237 static void motion(int x, int y)
238 {
239 if(mouselook) {
240 // just call passive, it does what we need
241 passive(x, y);
242 }
243 }
245 static void passive(int x, int y)
246 {
247 // no need to test mouselook; this callback is only set when mouselook is enabled
248 int center_x = width / 2;
249 int center_y = height / 2;
251 int dx = x - center_x;
252 int dy = y - center_y;
254 if(!dx && !dy) {
255 return;
256 }
258 float dtheta_deg = dy * 0.1;
259 float dphi_deg = dx * 0.1;
261 cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0);
263 glutPostRedisplay();
264 glutWarpPointer(center_x, center_y);
265 }
267 static void sball_rotate(int rx, int ry, int rz)
268 {
269 }
271 static bool parse_args(int argc, char **argv)
272 {
273 for(int i=1; i<argc; i++) {
274 if(argv[i][0] == '-') {
275 if(strcmp(argv[i], "-vr") == 0) {
276 use_vr = true;
277 } else if(strcmp(argv[i], "-novr") == 0) {
278 use_vr = false;
279 } else {
280 fprintf(stderr, "invalid option: %s\n", argv[i]);
281 return false;
282 }
283 } else {
284 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
285 return false;
286 }
287 }
288 return true;
289 }