Esempio n. 1
0
/** Get the view matrix from an orientation sensor. */
static void viewmat_get_orient_sensor(float viewmatrix[16], int viewportNum)
{
	float quaternion[4];
	orient_sensor_get(&viewmat_orientsense, quaternion);
	
	float cyclopsViewMatrix[16];
	mat4f_rotateQuatVec_new(cyclopsViewMatrix, quaternion);
	viewmat_fix_rotation(cyclopsViewMatrix);
	
	/* set a default camera position */
	float pos[4] = { 0, 1.5, 0, 1 };
	mat4f_setColumn(cyclopsViewMatrix, pos, 3);
	mat4f_invert(cyclopsViewMatrix);

	viewmat_get_generic(viewmatrix, cyclopsViewMatrix, viewportNum);
}
Esempio n. 2
0
/* Called by GLUT whenever the window needs to be redrawn. This
 * function should not be called directly by the programmer. Instead,
 * we can call glutPostRedisplay() to request that GLUT call display()
 * at some point. */
void display()
{
	/* If we are using DGR, send or receive data to keep multiple
	 * processes/computers synchronized. */
	dgr_update();

	/* Render the scene once for each viewport. Frequently one
	 * viewport will fill the entire screen. However, this loop will
	 * run twice for HMDs (once for the left eye and once for the
	 * right. */
	viewmat_begin_frame();
	for(int viewportID=0; viewportID<viewmat_num_viewports(); viewportID++)
	{
		viewmat_begin_eye(viewportID);

		/* Where is the viewport that we are drawing onto and what is its size? */
		int viewport[4]; // x,y of lower left corner, width, height
		viewmat_get_viewport(viewport, viewportID);
		glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);

		/* Clear the current viewport. Without glScissor(), glClear()
		 * clears the entire screen. We could call glClear() before
		 * this viewport loop---but on order for all variations of
		 * this code to work (Oculus support, etc), we can only draw
		 * after viewmat_begin_eye(). */
		glScissor(viewport[0], viewport[1], viewport[2], viewport[3]);
		glEnable(GL_SCISSOR_TEST);
		glClearColor(.2,.2,.2,0); // set clear color to grey
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		glDisable(GL_SCISSOR_TEST);
		glEnable(GL_DEPTH_TEST); // turn on depth testing
		kuhl_errorcheck();

		/* Turn on blending (note, if you are using transparent textures,
		   the transparency may not look correct unless you draw further
		   items before closer items. This program always draws the
		   geometry in the same order.). */
		glEnable(GL_BLEND);
		glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
		glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ZERO);

		/* Get the view or camera matrix; update the frustum values if needed. */
		float viewMat[16], perspective[16];
		viewmat_eye eye = viewmat_get(viewMat, perspective, viewportID);

		/* Always put camera at origin (no translation, no
		 * interpupillary distance). */
		float noTranslation[4] = {0,0,0,1};
		mat4f_setColumn(viewMat, noTranslation, 3); // last column

		/* Create a scale matrix. */
		float scaleMatrix[16];
		mat4f_scale_new(scaleMatrix, 20, 20, 20);

		// Modelview = (viewMatrix * scaleMatrix) * rotationMatrix
		float modelview[16];
		mat4f_mult_mat4f_new(modelview, viewMat, scaleMatrix);

		kuhl_errorcheck();
		glUseProgram(program);
		kuhl_errorcheck();
		/* Send the perspective projection matrix to the vertex program. */
		glUniformMatrix4fv(kuhl_get_uniform("Projection"),
		                   1, // number of 4x4 float matrices
		                   0, // transpose
		                   perspective); // value
		/* Send the modelview matrix to the vertex program. */
		glUniformMatrix4fv(kuhl_get_uniform("ModelView"),
		                   1, // number of 4x4 float matrices
		                   0, // transpose
		                   modelview); // value
		kuhl_errorcheck();

		if(texIdLeft != 0 && texIdRight != 0) // cylinder
		{
			/* Draw the cylinder with the appropriate texture */
			if(eye == VIEWMAT_EYE_RIGHT)
				kuhl_geometry_texture(&cylinder, texIdRight, "tex", KG_WARN);
			else
				kuhl_geometry_texture(&cylinder, texIdLeft, "tex", KG_WARN);
			kuhl_geometry_draw(&cylinder);
		}
		else // cubemap
		{
			if(eye == VIEWMAT_EYE_RIGHT)
				setupCubemap(cubemapRightTex, quad, modelview);
			else
				setupCubemap(cubemapLeftTex, quad, modelview);
		}
	} // finish viewport loop
	viewmat_end_frame();

	/* Check for errors. If there are errors, consider adding more
	 * calls to kuhl_errorcheck() in your code. */
	kuhl_errorcheck();

	/* Ask GLUT to call display() again. We shouldn't call display()
	 * ourselves recursively because it will not leave time for GLUT
	 * to call other callback functions for when a key is pressed, the
	 * window is resized, etc. */
	glutPostRedisplay();

	static int fps_state_init = 0;
	static kuhl_fps_state fps_state;
	if(fps_state_init == 0)
	{
		kuhl_getfps_init(&fps_state);
		fps_state_init = 1;
	}
	float fps = kuhl_getfps(&fps_state);
	if(fps_state.frame == 0)
		msg(INFO, "fps: %6.1f\n", fps);
}