oculus1

annotate 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
rev   line source
nuclear@0 1 #include <stdio.h>
nuclear@0 2 #include <stdlib.h>
nuclear@1 3 #include <string.h>
nuclear@0 4 #include <assert.h>
nuclear@0 5 #include <GL/glew.h>
nuclear@0 6 #ifdef __APPLE__
nuclear@0 7 #include <GLUT/glut.h>
nuclear@0 8 #else
nuclear@0 9 #include <GL/glut.h>
nuclear@0 10 #endif
nuclear@1 11 #include "vr.h"
nuclear@0 12 #include "camera.h"
nuclear@0 13
nuclear@0 14 static bool init();
nuclear@0 15 static void cleanup();
nuclear@0 16 static void disp();
nuclear@0 17 static void idle();
nuclear@0 18 static void reshape(int x, int y);
nuclear@0 19 static void keyb(unsigned char key, int x, int y);
nuclear@0 20 static void sball_rotate(int rx, int ry, int rz);
nuclear@1 21 static bool parse_args(int argc, char **argv);
nuclear@0 22
nuclear@6 23 static FlyCamera cam;
nuclear@0 24 static int width, height;
nuclear@1 25 static bool use_vr = false;
nuclear@0 26
nuclear@0 27 int main(int argc, char **argv)
nuclear@0 28 {
nuclear@3 29 glutInitWindowSize(1280, 800);
nuclear@0 30 glutInit(&argc, argv);
nuclear@1 31
nuclear@1 32 if(!parse_args(argc, argv)) {
nuclear@1 33 return 1;
nuclear@1 34 }
nuclear@1 35
nuclear@0 36 glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
nuclear@0 37 glutCreateWindow("oculus test 01");
nuclear@0 38
nuclear@0 39 glutDisplayFunc(disp);
nuclear@0 40 glutIdleFunc(idle);
nuclear@0 41 glutReshapeFunc(reshape);
nuclear@0 42 glutKeyboardFunc(keyb);
nuclear@0 43 glutSpaceballRotateFunc(sball_rotate);
nuclear@0 44
nuclear@0 45 glewInit();
nuclear@0 46
nuclear@0 47 if(!init()) {
nuclear@0 48 return 1;
nuclear@0 49 }
nuclear@0 50 atexit(cleanup);
nuclear@0 51
nuclear@0 52 glutMainLoop();
nuclear@0 53 return 0;
nuclear@0 54 }
nuclear@0 55
nuclear@0 56 static bool init()
nuclear@0 57 {
nuclear@0 58 glEnable(GL_DEPTH_TEST);
nuclear@0 59 glEnable(GL_LIGHTING);
nuclear@0 60 glEnable(GL_CULL_FACE);
nuclear@0 61
nuclear@0 62 glEnable(GL_LIGHT0);
nuclear@0 63 glEnable(GL_LIGHTING);
nuclear@0 64
nuclear@1 65 if(vr_init(VR_INIT_OCULUS) == -1) {
nuclear@5 66 return false;
nuclear@0 67 }
nuclear@0 68 return true;
nuclear@0 69 }
nuclear@0 70
nuclear@0 71 static void cleanup()
nuclear@0 72 {
nuclear@1 73 vr_shutdown();
nuclear@0 74 }
nuclear@0 75
nuclear@0 76 static void disp()
nuclear@0 77 {
nuclear@4 78 unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
nuclear@4 79
nuclear@3 80 // test rift sensor
nuclear@3 81 float quat[4], euler[3];
nuclear@3 82
nuclear@3 83 vr_get_rotation(quat);
nuclear@3 84 vr_get_rotation_euler(euler);
nuclear@3 85
nuclear@5 86 Quaternion qrot(quat[3], quat[0], quat[1], quat[2]);
nuclear@5 87
nuclear@4 88 static unsigned int prev_print;
nuclear@4 89 if(msec - prev_print > 1000) {
nuclear@4 90 printf("q(%.3f + %.3fi + %.3fj %.3fk)", quat[3], quat[0], quat[1], quat[2]);
nuclear@4 91 printf(" - euler(%.3f %.3f %.3f)\n", euler[0], euler[1], euler[2]);
nuclear@4 92 prev_print = msec;
nuclear@4 93 }
nuclear@3 94
nuclear@0 95 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
nuclear@0 96
nuclear@0 97 glMatrixMode(GL_PROJECTION);
nuclear@0 98 glLoadIdentity();
nuclear@0 99 gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0);
nuclear@0 100
nuclear@0 101 glMatrixMode(GL_MODELVIEW);
nuclear@7 102 cam.use();
nuclear@0 103
nuclear@0 104 float lpos[] = {0, 60, 0, 1};
nuclear@0 105 glLightfv(GL_LIGHT0, GL_POSITION, lpos);
nuclear@0 106
nuclear@0 107 glFrontFace(GL_CW);
nuclear@0 108 glutSolidTeapot(1.0);
nuclear@0 109 glFrontFace(GL_CCW);
nuclear@0 110
nuclear@0 111 glutSwapBuffers();
nuclear@0 112 assert(glGetError() == GL_NO_ERROR);
nuclear@0 113 }
nuclear@0 114
nuclear@0 115 static void idle()
nuclear@0 116 {
nuclear@0 117 glutPostRedisplay();
nuclear@0 118 }
nuclear@0 119
nuclear@0 120
nuclear@0 121 static void reshape(int x, int y)
nuclear@0 122 {
nuclear@0 123 width = x;
nuclear@0 124 height = y;
nuclear@0 125 }
nuclear@0 126
nuclear@0 127 static void keyb(unsigned char key, int x, int y)
nuclear@0 128 {
nuclear@0 129 switch(key) {
nuclear@0 130 case 27:
nuclear@0 131 exit(0);
nuclear@0 132 }
nuclear@0 133 }
nuclear@0 134
nuclear@0 135 static void sball_rotate(int rx, int ry, int rz)
nuclear@0 136 {
nuclear@0 137 }
nuclear@1 138
nuclear@1 139 static bool parse_args(int argc, char **argv)
nuclear@1 140 {
nuclear@1 141 for(int i=1; i<argc; i++) {
nuclear@1 142 if(argv[i][0] == '-') {
nuclear@1 143 if(strcmp(argv[i], "-vr") == 0) {
nuclear@1 144 use_vr = true;
nuclear@1 145 } else if(strcmp(argv[i], "-novr") == 0) {
nuclear@1 146 use_vr = false;
nuclear@1 147 } else {
nuclear@1 148 fprintf(stderr, "invalid option: %s\n", argv[i]);
nuclear@1 149 return false;
nuclear@1 150 }
nuclear@1 151 } else {
nuclear@1 152 fprintf(stderr, "unexpected argument: %s\n", argv[i]);
nuclear@1 153 return false;
nuclear@1 154 }
nuclear@1 155 }
nuclear@1 156 return true;
nuclear@1 157 }