oculus1

view src/main.cc @ 7:5a3ab7506c40

whatever
author John Tsiombikas <nuclear@member.fsf.org>
date Wed, 18 Sep 2013 17:26:38 +0300
parents d6dd6372add6
children 3265970a7315
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 FlyCamera 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 cam.use();
104 float lpos[] = {0, 60, 0, 1};
105 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
107 glFrontFace(GL_CW);
108 glutSolidTeapot(1.0);
109 glFrontFace(GL_CCW);
111 glutSwapBuffers();
112 assert(glGetError() == GL_NO_ERROR);
113 }
115 static void idle()
116 {
117 glutPostRedisplay();
118 }
121 static void reshape(int x, int y)
122 {
123 width = x;
124 height = y;
125 }
127 static void keyb(unsigned char key, int x, int y)
128 {
129 switch(key) {
130 case 27:
131 exit(0);
132 }
133 }
135 static void sball_rotate(int rx, int ry, int rz)
136 {
137 }
139 static bool parse_args(int argc, char **argv)
140 {
141 for(int i=1; i<argc; i++) {
142 if(argv[i][0] == '-') {
143 if(strcmp(argv[i], "-vr") == 0) {
144 use_vr = true;
145 } else if(strcmp(argv[i], "-novr") == 0) {
146 use_vr = false;
147 } else {
148 fprintf(stderr, "invalid option: %s\n", argv[i]);
149 return false;
150 }
151 } else {
152 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
153 return false;
154 }
155 }
156 return true;
157 }