oculus1

view src/main.cc @ 11:39ec672a5158

added simple head model to the VR camera
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Sep 2013 07:00:18 +0300
parents b2abb08c8f94
children d797639e0234
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 // y = height of neck
77 cam.input_move(0, 1.65, 0);
79 if(vr_init(VR_INIT_OCULUS) == -1) {
80 return false;
81 }
82 return true;
83 }
85 static void cleanup()
86 {
87 vr_shutdown();
88 }
90 static void disp()
91 {
92 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
94 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
96 glMatrixMode(GL_PROJECTION);
97 glLoadIdentity();
98 gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0);
100 glMatrixMode(GL_MODELVIEW);
101 glLoadIdentity();
103 cam.track_vr();
104 cam.use_inverse();
106 draw_scene();
108 glutSwapBuffers();
109 assert(glGetError() == GL_NO_ERROR);
110 }
112 static void draw_teapot()
113 {
114 static int tealist;
116 if(!tealist) {
117 tealist = glGenLists(1);
118 glNewList(tealist, GL_COMPILE);
119 glutSolidTeapot(1.0);
120 glEndList();
121 }
123 glFrontFace(GL_CW);
124 glCallList(tealist);
125 glFrontFace(GL_CCW);
126 }
128 void draw_grid(float size, float spacing)
129 {
130 int num_lines = size / spacing;
131 float dist = size / 2.0;
133 glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
134 glDisable(GL_LIGHTING);
136 glLineWidth(1.0);
138 glBegin(GL_LINES);
139 glColor3f(0.4, 0.4, 0.4);
141 float x = -dist;
142 for(int i=0; i<=num_lines; i++) {
143 if(i != num_lines / 2) {
144 glVertex3f(-dist, 0, x);
145 glVertex3f(dist, 0, x);
146 glVertex3f(x, 0, -dist);
147 glVertex3f(x, 0, dist);
148 }
149 x += spacing;
150 }
151 glEnd();
153 glLineWidth(2.0);
155 glBegin(GL_LINES);
156 glColor3f(1.0, 0, 0);
157 glVertex3f(-dist, 0, 0);
158 glVertex3f(dist, 0, 0);
159 glColor3f(0, 1.0, 0);
160 glVertex3f(0, 0, -dist);
161 glVertex3f(0, 0, dist);
162 glEnd();
164 glPopAttrib();
165 }
168 static void draw_scene()
169 {
170 float lpos[] = {0, 60, 0, 1};
171 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
173 static Vector2 teapos[] = {
174 Vector2(-8, 8), Vector2(8, 8), Vector2(8, -8), Vector2(-8, -8)
175 };
177 for(int i=0; i<4; i++) {
178 glPushMatrix();
179 glTranslatef(teapos[i].x, 0, teapos[i].y);
180 draw_teapot();
181 glPopMatrix();
182 }
184 draw_grid(100.0, 2.5);
185 }
187 static void idle()
188 {
189 glutPostRedisplay();
190 }
193 static void reshape(int x, int y)
194 {
195 width = x;
196 height = y;
197 }
199 static void keyb(unsigned char key, int x, int y)
200 {
201 switch(key) {
202 case 27:
203 exit(0);
205 case 'm':
206 mouselook = !mouselook;
207 if(mouselook) {
208 glutPassiveMotionFunc(passive);
209 glutSetCursor(GLUT_CURSOR_NONE);
210 glutWarpPointer(width / 2, height / 2);
211 } else {
212 glutPassiveMotionFunc(0);
213 glutSetCursor(GLUT_CURSOR_INHERIT);
214 }
215 break;
216 }
218 keystate[key] = true;
219 glutPostRedisplay();
220 }
222 static void keyup(unsigned char key, int x, int y)
223 {
224 keystate[key] = false;
225 glutPostRedisplay();
226 }
228 static bool bnstate[32];
229 static int prev_x, prev_y;
231 static void mouse(int bn, int st, int x, int y)
232 {
233 prev_x = x;
234 prev_y = y;
235 bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN;
236 }
238 static void motion(int x, int y)
239 {
240 if(mouselook) {
241 // just call passive, it does what we need
242 passive(x, y);
243 }
244 }
246 static void passive(int x, int y)
247 {
248 // no need to test mouselook; this callback is only set when mouselook is enabled
249 int center_x = width / 2;
250 int center_y = height / 2;
252 int dx = x - center_x;
253 int dy = y - center_y;
255 if(!dx && !dy) {
256 return;
257 }
259 float dtheta_deg = dy * 0.1;
260 float dphi_deg = dx * 0.1;
262 cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0);
264 glutPostRedisplay();
265 glutWarpPointer(center_x, center_y);
266 }
268 static void sball_rotate(int rx, int ry, int rz)
269 {
270 }
272 static bool parse_args(int argc, char **argv)
273 {
274 for(int i=1; i<argc; i++) {
275 if(argv[i][0] == '-') {
276 if(strcmp(argv[i], "-vr") == 0) {
277 use_vr = true;
278 } else if(strcmp(argv[i], "-novr") == 0) {
279 use_vr = false;
280 } else {
281 fprintf(stderr, "invalid option: %s\n", argv[i]);
282 return false;
283 }
284 } else {
285 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
286 return false;
287 }
288 }
289 return true;
290 }