oculus1

diff src/main.cc @ 9:b66b54a68dfd

tracking almost done
author John Tsiombikas <nuclear@member.fsf.org>
date Thu, 19 Sep 2013 06:36:48 +0300
parents 3265970a7315
children b2abb08c8f94
line diff
     1.1 --- a/src/main.cc	Wed Sep 18 22:15:04 2013 +0300
     1.2 +++ b/src/main.cc	Thu Sep 19 06:36:48 2013 +0300
     1.3 @@ -9,6 +9,7 @@
     1.4  static bool init();
     1.5  static void cleanup();
     1.6  static void disp();
     1.7 +static void draw_scene();
     1.8  static void idle();
     1.9  static void reshape(int x, int y);
    1.10  static void keyb(unsigned char key, int x, int y);
    1.11 @@ -19,7 +20,7 @@
    1.12  static void sball_rotate(int rx, int ry, int rz);
    1.13  static bool parse_args(int argc, char **argv);
    1.14  
    1.15 -static FlyCamera cam;
    1.16 +static FpsCamera cam;
    1.17  static int width, height;
    1.18  static bool use_vr = false;
    1.19  static bool mouselook = false;
    1.20 @@ -36,7 +37,7 @@
    1.21  		return 1;
    1.22  	}
    1.23  
    1.24 -	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.25 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
    1.26  	glutCreateWindow("oculus test 01");
    1.27  
    1.28  	glutDisplayFunc(disp);
    1.29 @@ -59,7 +60,11 @@
    1.30  
    1.31  static bool init()
    1.32  {
    1.33 -	init_opengl(); // this must be first
    1.34 +	glewInit(); // this must be first
    1.35 +
    1.36 +	if(GLEW_ARB_multisample) {
    1.37 +		glEnable(GL_MULTISAMPLE);
    1.38 +	}
    1.39  
    1.40  	glEnable(GL_DEPTH_TEST);
    1.41  	glEnable(GL_LIGHTING);
    1.42 @@ -68,6 +73,8 @@
    1.43  	glEnable(GL_LIGHT0);
    1.44  	glEnable(GL_LIGHTING);
    1.45  
    1.46 +	cam.input_move(0, 1.75, 0);
    1.47 +
    1.48  	if(vr_init(VR_INIT_OCULUS) == -1) {
    1.49  		return false;
    1.50  	}
    1.51 @@ -93,7 +100,7 @@
    1.52  
    1.53  	static unsigned int prev_print;
    1.54  	if(msec - prev_print > 1000) {
    1.55 -		printf("q(%.3f + %.3fi + %.3fj %.3fk)", quat[3], quat[0], quat[1], quat[2]);
    1.56 +		printf("q(%.3f + %.3fi + %.3fj + %.3fk)", quat[3], quat[0], quat[1], quat[2]);
    1.57  		printf(" - euler(%.3f %.3f %.3f)\n", euler[0], euler[1], euler[2]);
    1.58  		prev_print = msec;
    1.59  	}
    1.60 @@ -105,25 +112,92 @@
    1.61  	gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0);
    1.62  
    1.63  	glMatrixMode(GL_MODELVIEW);
    1.64 -	cam.use();
    1.65 +	glLoadIdentity();
    1.66  
    1.67 +	Matrix4x4 mat = qrot.inverse().get_rotation_matrix();
    1.68 +	load_matrix(mat);
    1.69 +
    1.70 +	cam.use_inverse();
    1.71 +
    1.72 +	draw_scene();
    1.73 +
    1.74 +	glutSwapBuffers();
    1.75 +	assert(glGetError() == GL_NO_ERROR);
    1.76 +}
    1.77 +
    1.78 +static void draw_teapot()
    1.79 +{
    1.80 +	static int tealist;
    1.81 +
    1.82 +	if(!tealist) {
    1.83 +		tealist = glGenLists(1);
    1.84 +		glNewList(tealist, GL_COMPILE);
    1.85 +		glutSolidTeapot(1.0);
    1.86 +		glEndList();
    1.87 +	}
    1.88 +
    1.89 +	glFrontFace(GL_CW);
    1.90 +	glCallList(tealist);
    1.91 +	glFrontFace(GL_CCW);
    1.92 +}
    1.93 +
    1.94 +void draw_grid(float size, float spacing)
    1.95 +{
    1.96 +	int num_lines = size / spacing;
    1.97 +	float dist = size / 2.0;
    1.98 +
    1.99 +	glPushAttrib(GL_ENABLE_BIT | GL_LINE_BIT);
   1.100 +	glDisable(GL_LIGHTING);
   1.101 +
   1.102 +	glLineWidth(1.0);
   1.103 +
   1.104 +	glBegin(GL_LINES);
   1.105 +	glColor3f(0.4, 0.4, 0.4);
   1.106 +
   1.107 +	float x = -dist;
   1.108 +	for(int i=0; i<=num_lines; i++) {
   1.109 +		if(i != num_lines / 2) {
   1.110 +			glVertex3f(-dist, 0, x);
   1.111 +			glVertex3f(dist, 0, x);
   1.112 +			glVertex3f(x, 0, -dist);
   1.113 +			glVertex3f(x, 0, dist);
   1.114 +		}
   1.115 +		x += spacing;
   1.116 +	}
   1.117 +	glEnd();
   1.118 +
   1.119 +	glLineWidth(2.0);
   1.120 +
   1.121 +	glBegin(GL_LINES);
   1.122 +	glColor3f(1.0, 0, 0);
   1.123 +	glVertex3f(-dist, 0, 0);
   1.124 +	glVertex3f(dist, 0, 0);
   1.125 +	glColor3f(0, 1.0, 0);
   1.126 +	glVertex3f(0, 0, -dist);
   1.127 +	glVertex3f(0, 0, dist);
   1.128 +	glEnd();
   1.129 +
   1.130 +	glPopAttrib();
   1.131 +}
   1.132 +
   1.133 +
   1.134 +static void draw_scene()
   1.135 +{
   1.136  	float lpos[] = {0, 60, 0, 1};
   1.137  	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
   1.138  
   1.139 -	/*glFrontFace(GL_CW);
   1.140 -	glutSolidTeapot(1.0);
   1.141 -	glFrontFace(GL_CCW);*/
   1.142 +	static Vector2 teapos[] = {
   1.143 +		Vector2(-8, 8), Vector2(8, 8), Vector2(8, -8), Vector2(-8, -8)
   1.144 +	};
   1.145  
   1.146 -	glBegin(GL_QUADS);
   1.147 -	glNormal3f(0, 1, 0);
   1.148 -	glVertex3f(-10, -1, 10);
   1.149 -	glVertex3f(10, -1, 10);
   1.150 -	glVertex3f(10, -1, -10);
   1.151 -	glVertex3f(-10, -1, -10);
   1.152 -	glEnd();
   1.153 +	for(int i=0; i<4; i++) {
   1.154 +		glPushMatrix();
   1.155 +		glTranslatef(teapos[i].x, 0, teapos[i].y);
   1.156 +		draw_teapot();
   1.157 +		glPopMatrix();
   1.158 +	}
   1.159  
   1.160 -	glutSwapBuffers();
   1.161 -	assert(glGetError() == GL_NO_ERROR);
   1.162 +	draw_grid(100.0, 2.5);
   1.163  }
   1.164  
   1.165  static void idle()
   1.166 @@ -198,9 +272,12 @@
   1.167  		return;
   1.168  	}
   1.169  
   1.170 -	cam.input_rotate(dy * 0.1, dx * 0.1, 0.0);
   1.171 +	float dtheta_deg = dy * 0.1;
   1.172 +	float dphi_deg = dx * 0.1;
   1.173 +
   1.174 +	cam.input_rotate(DEG_TO_RAD(dtheta_deg), DEG_TO_RAD(dphi_deg), 0);
   1.175 +
   1.176  	glutPostRedisplay();
   1.177 -
   1.178  	glutWarpPointer(center_x, center_y);
   1.179  }
   1.180