oculus1

diff src/main.cc @ 12:d797639e0234

moving on to the distortion... not correct yet
author John Tsiombikas <nuclear@member.fsf.org>
date Fri, 20 Sep 2013 10:14:29 +0300
parents 39ec672a5158
children 464e1d135d68
line diff
     1.1 --- a/src/main.cc	Fri Sep 20 07:00:18 2013 +0300
     1.2 +++ b/src/main.cc	Fri Sep 20 10:14:29 2013 +0300
     1.3 @@ -10,6 +10,8 @@
     1.4  static void cleanup();
     1.5  static void disp();
     1.6  static void draw_scene();
     1.7 +static void draw_teapot();
     1.8 +static void draw_grid(float size, float spacing);
     1.9  static void idle();
    1.10  static void reshape(int x, int y);
    1.11  static void keyb(unsigned char key, int x, int y);
    1.12 @@ -27,6 +29,9 @@
    1.13  
    1.14  static bool keystate[256];
    1.15  
    1.16 +static int rtarg_width, rtarg_height;
    1.17 +static unsigned int fbo, tex[2], zbuf;
    1.18 +
    1.19  
    1.20  int main(int argc, char **argv)
    1.21  {
    1.22 @@ -37,8 +42,11 @@
    1.23  		return 1;
    1.24  	}
    1.25  
    1.26 -	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE | GLUT_MULTISAMPLE);
    1.27 -	glutCreateWindow("oculus test 01");
    1.28 +	glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    1.29 +	glutCreateWindow("oculus vr test 01");
    1.30 +
    1.31 +	width = glutGet(GLUT_WINDOW_WIDTH);
    1.32 +	height = glutGet(GLUT_WINDOW_HEIGHT);
    1.33  
    1.34  	glutDisplayFunc(disp);
    1.35  	glutIdleFunc(idle);
    1.36 @@ -62,10 +70,6 @@
    1.37  {
    1.38  	glewInit(); // this must be first
    1.39  
    1.40 -	if(GLEW_ARB_multisample) {
    1.41 -		glEnable(GL_MULTISAMPLE);
    1.42 -	}
    1.43 -
    1.44  	glEnable(GL_DEPTH_TEST);
    1.45  	glEnable(GL_LIGHTING);
    1.46  	glEnable(GL_CULL_FACE);
    1.47 @@ -76,14 +80,59 @@
    1.48  	// y = height of neck
    1.49  	cam.input_move(0, 1.65, 0);
    1.50  
    1.51 -	if(vr_init(VR_INIT_OCULUS) == -1) {
    1.52 -		return false;
    1.53 +	if(use_vr) {
    1.54 +		if(vr_init(VR_INIT_OCULUS) == -1) {
    1.55 +			return false;
    1.56 +		}
    1.57 +
    1.58 +		// reshape to the size of the VR display
    1.59 +		int xsz = vr_get_width();
    1.60 +		int ysz = vr_get_height();
    1.61 +
    1.62 +		glutReshapeWindow(xsz, ysz);
    1.63 +
    1.64 +		rtarg_width = (xsz + xsz / 2) / 2;
    1.65 +		rtarg_height = ysz + ysz / 2;
    1.66 +	} else {
    1.67 +		rtarg_width = width;
    1.68 +		rtarg_height = height;
    1.69  	}
    1.70 +
    1.71 +	// create render targets for each eye
    1.72 +	GLenum wrap_mode = GL_CLAMP_TO_EDGE;
    1.73 +	if(!GLEW_SGIS_texture_edge_clamp) {
    1.74 +		wrap_mode = GL_CLAMP;
    1.75 +	}
    1.76 +
    1.77 +	glGenTextures(2, tex);
    1.78 +	for(int i=0; i<2; i++) {
    1.79 +		glBindTexture(GL_TEXTURE_2D, tex[i]);
    1.80 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    1.81 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    1.82 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, wrap_mode);
    1.83 +		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, wrap_mode);
    1.84 +		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, rtarg_width, rtarg_height, 0, GL_RGB, GL_UNSIGNED_BYTE, 0);
    1.85 +	}
    1.86 +
    1.87 +	// create the depth render buffer
    1.88 +	glGenRenderbuffers(1, &zbuf);
    1.89 +	glBindRenderbuffer(GL_RENDERBUFFER, zbuf);
    1.90 +	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, rtarg_width, rtarg_height);
    1.91 +
    1.92 +	// create the FBO
    1.93 +	glGenFramebuffers(1, &fbo);
    1.94 +	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
    1.95 +	glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[0], 0);
    1.96 +	glFramebufferRenderbuffer(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, zbuf);
    1.97 +
    1.98  	return true;
    1.99  }
   1.100  
   1.101  static void cleanup()
   1.102  {
   1.103 +	glDeleteTextures(2, tex);
   1.104 +	glDeleteRenderbuffers(1, &zbuf);
   1.105 +	glDeleteFramebuffers(1, &fbo);
   1.106  	vr_shutdown();
   1.107  }
   1.108  
   1.109 @@ -91,24 +140,75 @@
   1.110  {
   1.111  	unsigned int msec = glutGet(GLUT_ELAPSED_TIME);
   1.112  
   1.113 -	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.114 +	cam.track_vr();
   1.115  
   1.116  	glMatrixMode(GL_PROJECTION);
   1.117  	glLoadIdentity();
   1.118 -	gluPerspective(45.0, (float)width / (float)height, 0.5, 500.0);
   1.119 +	gluPerspective(RAD_TO_DEG(vr_get_fov()), (float)rtarg_width / (float)rtarg_height, 0.5, 500.0);
   1.120 +
   1.121 +	glViewport(0, 0, rtarg_width, rtarg_height);
   1.122 +
   1.123 +	glClearColor(0.1, 0.1, 0.1, 1.0);
   1.124 +
   1.125 +	// draw left view
   1.126 +	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
   1.127 +	glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[0], 0);
   1.128 +
   1.129 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.130  
   1.131  	glMatrixMode(GL_MODELVIEW);
   1.132  	glLoadIdentity();
   1.133 +	cam.use_inverse();
   1.134 +	glTranslatef(0.32, 0, 0);
   1.135 +	draw_scene();
   1.136  
   1.137 -	cam.track_vr();
   1.138 +
   1.139 +	// draw right view
   1.140 +	glBindFramebuffer(GL_FRAMEBUFFER, fbo);
   1.141 +	glFramebufferTexture(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, tex[1], 0);
   1.142 +
   1.143 +	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
   1.144 +
   1.145 +	glMatrixMode(GL_MODELVIEW);
   1.146 +	glLoadIdentity();
   1.147  	cam.use_inverse();
   1.148 +	glTranslatef(-0.32, 0, 0);
   1.149 +	draw_scene();
   1.150  
   1.151 -	draw_scene();
   1.152 +	// return to the regular window framebuffer
   1.153 +	glBindFramebuffer(GL_FRAMEBUFFER, 0);
   1.154 +	glViewport(0, 0, width, height);
   1.155 +
   1.156 +	glClearColor(0, 0, 0, 0);
   1.157 +	glClear(GL_COLOR_BUFFER_BIT);
   1.158 +
   1.159 +	vr_draw_eye(tex[0], VR_EYE_LEFT);
   1.160 +	vr_draw_eye(tex[1], VR_EYE_RIGHT);
   1.161  
   1.162  	glutSwapBuffers();
   1.163  	assert(glGetError() == GL_NO_ERROR);
   1.164  }
   1.165  
   1.166 +
   1.167 +static void draw_scene()
   1.168 +{
   1.169 +	float lpos[] = {0, 60, 0, 1};
   1.170 +	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
   1.171 +
   1.172 +	static Vector2 teapos[] = {
   1.173 +		Vector2(-8, 8), Vector2(8, 8), Vector2(8, -8), Vector2(-8, -8)
   1.174 +	};
   1.175 +
   1.176 +	for(int i=0; i<4; i++) {
   1.177 +		glPushMatrix();
   1.178 +		glTranslatef(teapos[i].x, 0, teapos[i].y);
   1.179 +		draw_teapot();
   1.180 +		glPopMatrix();
   1.181 +	}
   1.182 +
   1.183 +	draw_grid(100.0, 2.5);
   1.184 +}
   1.185 +
   1.186  static void draw_teapot()
   1.187  {
   1.188  	static int tealist;
   1.189 @@ -125,7 +225,7 @@
   1.190  	glFrontFace(GL_CCW);
   1.191  }
   1.192  
   1.193 -void draw_grid(float size, float spacing)
   1.194 +static void draw_grid(float size, float spacing)
   1.195  {
   1.196  	int num_lines = size / spacing;
   1.197  	float dist = size / 2.0;
   1.198 @@ -164,26 +264,6 @@
   1.199  	glPopAttrib();
   1.200  }
   1.201  
   1.202 -
   1.203 -static void draw_scene()
   1.204 -{
   1.205 -	float lpos[] = {0, 60, 0, 1};
   1.206 -	glLightfv(GL_LIGHT0, GL_POSITION, lpos);
   1.207 -
   1.208 -	static Vector2 teapos[] = {
   1.209 -		Vector2(-8, 8), Vector2(8, 8), Vector2(8, -8), Vector2(-8, -8)
   1.210 -	};
   1.211 -
   1.212 -	for(int i=0; i<4; i++) {
   1.213 -		glPushMatrix();
   1.214 -		glTranslatef(teapos[i].x, 0, teapos[i].y);
   1.215 -		draw_teapot();
   1.216 -		glPopMatrix();
   1.217 -	}
   1.218 -
   1.219 -	draw_grid(100.0, 2.5);
   1.220 -}
   1.221 -
   1.222  static void idle()
   1.223  {
   1.224  	glutPostRedisplay();
   1.225 @@ -194,6 +274,11 @@
   1.226  {
   1.227  	width = x;
   1.228  	height = y;
   1.229 +
   1.230 +	if(!use_vr) {
   1.231 +		rtarg_width = width;
   1.232 +		rtarg_height = height;
   1.233 +	}
   1.234  }
   1.235  
   1.236  static void keyb(unsigned char key, int x, int y)
   1.237 @@ -213,6 +298,24 @@
   1.238  			glutSetCursor(GLUT_CURSOR_INHERIT);
   1.239  		}
   1.240  		break;
   1.241 +
   1.242 +	case 'f':
   1.243 +		{
   1.244 +			static bool fullscreen;
   1.245 +			static int prev_xsz, prev_ysz;
   1.246 +
   1.247 +			fullscreen = !fullscreen;
   1.248 +
   1.249 +			if(fullscreen) {
   1.250 +				prev_xsz = width;
   1.251 +				prev_ysz = height;
   1.252 +				glutFullScreen();
   1.253 +			} else {
   1.254 +				glutReshapeWindow(prev_xsz, prev_ysz);
   1.255 +			}
   1.256 +			glutPostRedisplay();
   1.257 +		}
   1.258 +		break;
   1.259  	}
   1.260  
   1.261  	keystate[key] = true;