oculus1
diff 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 diff
1.1 --- a/src/main.cc Wed Sep 18 17:26:38 2013 +0300 1.2 +++ b/src/main.cc Wed Sep 18 22:15:04 2013 +0300 1.3 @@ -2,12 +2,7 @@ 1.4 #include <stdlib.h> 1.5 #include <string.h> 1.6 #include <assert.h> 1.7 -#include <GL/glew.h> 1.8 -#ifdef __APPLE__ 1.9 -#include <GLUT/glut.h> 1.10 -#else 1.11 -#include <GL/glut.h> 1.12 -#endif 1.13 +#include "opengl.h" 1.14 #include "vr.h" 1.15 #include "camera.h" 1.16 1.17 @@ -17,12 +12,20 @@ 1.18 static void idle(); 1.19 static void reshape(int x, int y); 1.20 static void keyb(unsigned char key, int x, int y); 1.21 +static void keyup(unsigned char key, int x, int y); 1.22 +static void mouse(int bn, int st, int x, int y); 1.23 +static void motion(int x, int y); 1.24 +static void passive(int x, int y); 1.25 static void sball_rotate(int rx, int ry, int rz); 1.26 static bool parse_args(int argc, char **argv); 1.27 1.28 static FlyCamera cam; 1.29 static int width, height; 1.30 static bool use_vr = false; 1.31 +static bool mouselook = false; 1.32 + 1.33 +static bool keystate[256]; 1.34 + 1.35 1.36 int main(int argc, char **argv) 1.37 { 1.38 @@ -40,10 +43,11 @@ 1.39 glutIdleFunc(idle); 1.40 glutReshapeFunc(reshape); 1.41 glutKeyboardFunc(keyb); 1.42 + glutKeyboardUpFunc(keyup); 1.43 + glutMouseFunc(mouse); 1.44 + glutMotionFunc(motion); 1.45 glutSpaceballRotateFunc(sball_rotate); 1.46 1.47 - glewInit(); 1.48 - 1.49 if(!init()) { 1.50 return 1; 1.51 } 1.52 @@ -55,6 +59,8 @@ 1.53 1.54 static bool init() 1.55 { 1.56 + init_opengl(); // this must be first 1.57 + 1.58 glEnable(GL_DEPTH_TEST); 1.59 glEnable(GL_LIGHTING); 1.60 glEnable(GL_CULL_FACE); 1.61 @@ -104,9 +110,17 @@ 1.62 float lpos[] = {0, 60, 0, 1}; 1.63 glLightfv(GL_LIGHT0, GL_POSITION, lpos); 1.64 1.65 - glFrontFace(GL_CW); 1.66 + /*glFrontFace(GL_CW); 1.67 glutSolidTeapot(1.0); 1.68 - glFrontFace(GL_CCW); 1.69 + glFrontFace(GL_CCW);*/ 1.70 + 1.71 + glBegin(GL_QUADS); 1.72 + glNormal3f(0, 1, 0); 1.73 + glVertex3f(-10, -1, 10); 1.74 + glVertex3f(10, -1, 10); 1.75 + glVertex3f(10, -1, -10); 1.76 + glVertex3f(-10, -1, -10); 1.77 + glEnd(); 1.78 1.79 glutSwapBuffers(); 1.80 assert(glGetError() == GL_NO_ERROR); 1.81 @@ -129,7 +143,65 @@ 1.82 switch(key) { 1.83 case 27: 1.84 exit(0); 1.85 + 1.86 + case 'm': 1.87 + mouselook = !mouselook; 1.88 + if(mouselook) { 1.89 + glutPassiveMotionFunc(passive); 1.90 + glutSetCursor(GLUT_CURSOR_NONE); 1.91 + glutWarpPointer(width / 2, height / 2); 1.92 + } else { 1.93 + glutPassiveMotionFunc(0); 1.94 + glutSetCursor(GLUT_CURSOR_INHERIT); 1.95 + } 1.96 + break; 1.97 } 1.98 + 1.99 + keystate[key] = true; 1.100 + glutPostRedisplay(); 1.101 +} 1.102 + 1.103 +static void keyup(unsigned char key, int x, int y) 1.104 +{ 1.105 + keystate[key] = false; 1.106 + glutPostRedisplay(); 1.107 +} 1.108 + 1.109 +static bool bnstate[32]; 1.110 +static int prev_x, prev_y; 1.111 + 1.112 +static void mouse(int bn, int st, int x, int y) 1.113 +{ 1.114 + prev_x = x; 1.115 + prev_y = y; 1.116 + bnstate[bn - GLUT_LEFT_BUTTON] = st == GLUT_DOWN; 1.117 +} 1.118 + 1.119 +static void motion(int x, int y) 1.120 +{ 1.121 + if(mouselook) { 1.122 + // just call passive, it does what we need 1.123 + passive(x, y); 1.124 + } 1.125 +} 1.126 + 1.127 +static void passive(int x, int y) 1.128 +{ 1.129 + // no need to test mouselook; this callback is only set when mouselook is enabled 1.130 + int center_x = width / 2; 1.131 + int center_y = height / 2; 1.132 + 1.133 + int dx = x - center_x; 1.134 + int dy = y - center_y; 1.135 + 1.136 + if(!dx && !dy) { 1.137 + return; 1.138 + } 1.139 + 1.140 + cam.input_rotate(dy * 0.1, dx * 0.1, 0.0); 1.141 + glutPostRedisplay(); 1.142 + 1.143 + glutWarpPointer(center_x, center_y); 1.144 } 1.145 1.146 static void sball_rotate(int rx, int ry, int rz)