oculus1

view src/main.cc @ 8:3265970a7315

added xcode project
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 18 Sep 2013 22:15:04 +0300
parents 5a3ab7506c40
children b66b54a68dfd
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 idle();
13 static void reshape(int x, int y);
14 static void keyb(unsigned char key, int x, int y);
15 static void keyup(unsigned char key, int x, int y);
16 static void mouse(int bn, int st, int x, int y);
17 static void motion(int x, int y);
18 static void passive(int x, int y);
19 static void sball_rotate(int rx, int ry, int rz);
20 static bool parse_args(int argc, char **argv);
22 static FlyCamera cam;
23 static int width, height;
24 static bool use_vr = false;
25 static bool mouselook = false;
27 static bool keystate[256];
30 int main(int argc, char **argv)
31 {
32 glutInitWindowSize(1280, 800);
33 glutInit(&argc, argv);
35 if(!parse_args(argc, argv)) {
36 return 1;
37 }
39 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
40 glutCreateWindow("oculus test 01");
42 glutDisplayFunc(disp);
43 glutIdleFunc(idle);
44 glutReshapeFunc(reshape);
45 glutKeyboardFunc(keyb);
46 glutKeyboardUpFunc(keyup);
47 glutMouseFunc(mouse);
48 glutMotionFunc(motion);
49 glutSpaceballRotateFunc(sball_rotate);
51 if(!init()) {
52 return 1;
53 }
54 atexit(cleanup);
56 glutMainLoop();
57 return 0;
58 }
60 static bool init()
61 {
62 init_opengl(); // this must be first
64 glEnable(GL_DEPTH_TEST);
65 glEnable(GL_LIGHTING);
66 glEnable(GL_CULL_FACE);
68 glEnable(GL_LIGHT0);
69 glEnable(GL_LIGHTING);
71 if(vr_init(VR_INIT_OCULUS) == -1) {
72 return false;
73 }
74 return true;
75 }
77 static void cleanup()
78 {
79 vr_shutdown();
80 }
82 static void disp()
83 {
84 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
86 // test rift sensor
87 float quat[4], euler[3];
89 vr_get_rotation(quat);
90 vr_get_rotation_euler(euler);
92 Quaternion qrot(quat[3], quat[0], quat[1], quat[2]);
94 static unsigned int prev_print;
95 if(msec - prev_print > 1000) {
96 printf("q(%.3f + %.3fi + %.3fj %.3fk)", quat[3], quat[0], quat[1], quat[2]);
97 printf(" - euler(%.3f %.3f %.3f)\n", euler[0], euler[1], euler[2]);
98 prev_print = msec;
99 }
101 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
103 glMatrixMode(GL_PROJECTION);
104 glLoadIdentity();
105 gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0);
107 glMatrixMode(GL_MODELVIEW);
108 cam.use();
110 float lpos[] = {0, 60, 0, 1};
111 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
113 /*glFrontFace(GL_CW);
114 glutSolidTeapot(1.0);
115 glFrontFace(GL_CCW);*/
117 glBegin(GL_QUADS);
118 glNormal3f(0, 1, 0);
119 glVertex3f(-10, -1, 10);
120 glVertex3f(10, -1, 10);
121 glVertex3f(10, -1, -10);
122 glVertex3f(-10, -1, -10);
123 glEnd();
125 glutSwapBuffers();
126 assert(glGetError() == GL_NO_ERROR);
127 }
129 static void idle()
130 {
131 glutPostRedisplay();
132 }
135 static void reshape(int x, int y)
136 {
137 width = x;
138 height = y;
139 }
141 static void keyb(unsigned char key, int x, int y)
142 {
143 switch(key) {
144 case 27:
145 exit(0);
147 case 'm':
148 mouselook = !mouselook;
149 if(mouselook) {
150 glutPassiveMotionFunc(passive);
151 glutSetCursor(GLUT_CURSOR_NONE);
152 glutWarpPointer(width / 2, height / 2);
153 } else {
154 glutPassiveMotionFunc(0);
155 glutSetCursor(GLUT_CURSOR_INHERIT);
156 }
157 break;
158 }
160 keystate[key] = true;
161 glutPostRedisplay();
162 }
164 static void keyup(unsigned char key, int x, int y)
165 {
166 keystate[key] = false;
167 glutPostRedisplay();
168 }
170 static bool bnstate[32];
171 static int prev_x, prev_y;
173 static void mouse(int bn, int st, int x, int y)
174 {
175 prev_x = x;
176 prev_y = y;
177 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
178 }
180 static void motion(int x, int y)
181 {
182 if(mouselook) {
183 // just call passive, it does what we need
184 passive(x, y);
185 }
186 }
188 static void passive(int x, int y)
189 {
190 // no need to test mouselook; this callback is only set when mouselook is enabled
191 int center_x = width / 2;
192 int center_y = height / 2;
194 int dx = x - center_x;
195 int dy = y - center_y;
197 if(!dx && !dy) {
198 return;
199 }
201 cam.input_rotate(dy * 0.1, dx * 0.1, 0.0);
202 glutPostRedisplay();
204 glutWarpPointer(center_x, center_y);
205 }
207 static void sball_rotate(int rx, int ry, int rz)
208 {
209 }
211 static bool parse_args(int argc, char **argv)
212 {
213 for(int i=1; i<argc; i++) {
214 if(argv[i][0] == '-') {
215 if(strcmp(argv[i], "-vr") == 0) {
216 use_vr = true;
217 } else if(strcmp(argv[i], "-novr") == 0) {
218 use_vr = false;
219 } else {
220 fprintf(stderr, "invalid option: %s\n", argv[i]);
221 return false;
222 }
223 } else {
224 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
225 return false;
226 }
227 }
228 return true;
229 }