Esempio n. 1
0
void OnRender() 
{
	SetWindowTitle();

	std::string sTitle = g_sWindowTitle;
	glutSetWindowTitle(sTitle.c_str());	

	static GLfloat RedSurface[]   = { 1.0f, 0.0f, 0.0f, 1.0f};
	static GLfloat GreenSurface[] = { 0.0f, 1.0f, 0.0f, 1.0f};
	static GLfloat BlueSurface[]  = { 0.0f, 0.0f, 1.0f, 1.0f};
	static GLfloat YellowSurface[]  = { 1.0f, 1.0f, 0.0f, 1.0f};

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// clear screen and depth buffer
	glLoadIdentity();										// reset modelview matrix
	
	//---------------
	//Draw background
	//---------------
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_LIGHTING);
	glDisable(GL_DEPTH_TEST);
	glPushMatrix();
	glLoadIdentity();
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	
	glBegin(GL_QUADS);
		glColor3f(0.9F, 0.9F, 0.9F); glVertex2f( 1.0F,  1.0F);
		glColor3f(0.9F, 0.9F, 0.9F); glVertex2f(-1.0F,  1.0F);
		glColor3f(0.5F, 0.5F, 0.5F); glVertex2f(-1.0F, -1.0F);
		glColor3f(0.5F, 0.5F, 0.5F); glVertex2f( 1.0F, -1.0F);
	glEnd();
	
	glEnable(GL_DEPTH_TEST);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();
		
	//----------------
	// Applying camera
	//----------------
	hCamera.ApplyCamera();
	
	glCallList(btmPlate);

	glPushMatrix();
	glCallList(g_glLtAxis);
	glPopMatrix();

	// Lights
	GLfloat ambientLight[4] = {0.1f, 0.1f, 0.1f, 1.0f};
	GLfloat diffuseLight[4] = {0.7f, 0.7f, 0.7f, 1.0f};
	GLfloat lightPosition[4] = {200.0f, 200.0f, 100.0f, 0.0f};

	glEnable(GL_DEPTH_TEST);
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	glFrontFace(GL_CCW);
	
	glEnable(GL_LIGHTING);
	glLightfv(GL_LIGHT0, GL_AMBIENT, ambientLight);
	glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuseLight);
	glLightfv(GL_LIGHT0, GL_POSITION, lightPosition);
	glEnable(GL_LIGHT0);

	if ( !g_bPause )
	{
		if ( g_bOneStep )
		{
			g_bOneStep = false;
			g_bPause = true;
		}

		g_WorldSim.Update(g_dt);
		g_CurFrame++;
	}
	
	glEnable(GL_LIGHTING);
	glEnable(GL_LIGHT0);
	g_WorldSim.Render(g_bWireframe);	

	//------------
	// Draw texts
	//------------
	/* We are going to do some 2-D orthographic drawing. */
    glMatrixMode(GL_PROJECTION);
	glPushMatrix();
    glLoadIdentity();

	int w = g_Width;
    int h = g_Height;

    GLdouble size = (GLdouble)((w >= h) ? w : h) / 2.0;
    GLdouble aspect;

    if (w <= h) {
        aspect = (GLdouble)h/(GLdouble)w;
        glOrtho(-size, size, -size*aspect, size*aspect, -1000000.0, 1000000.0);
    }
    else {
        aspect = (GLdouble)w/(GLdouble)h;
        glOrtho(-size*aspect, size*aspect, -size, size, -1000000.0, 1000000.0);
    }

    /* Make the world and window coordinates coincide so that 1.0 in */
    /* model space equals one pixel in window space.                 */
    glScaled(aspect, aspect, 1.0);

	glDisable(GL_LIGHTING);

	glMatrixMode(GL_MODELVIEW);
	glPushMatrix();
	glLoadIdentity();	
	
	

	std::string sInfo = "Current Algorithm(press 'a' to change): ";
	sInfo.append(g_sWindowTitleInfo);

	int linePos = 0;
	
	DrawTextGlut(sInfo.c_str(), -g_Width/2 + 10, g_Height/2 - (linePos += 20));

	// frame
	char frame[10];
	itoa(g_CurFrame, frame, 10);
	sInfo = "Frame: ";
	sInfo.append(frame);
	DrawTextGlut(sInfo.c_str(), -g_Width/2 + 10, g_Height/2 - (linePos += 20));

	//// batch
	//char batch[5];
	//itoa(g_WorldSim.m_RenderBatchIndex, batch, 10);
	//sInfo = "Batch: ";
	//sInfo.append(batch);
	//DrawTextGlut(sInfo.c_str(), -g_Width/2 + 10, g_Height/2 - (linePos += 20));

	//// toggle CPU/GPU
	//if ( g_WorldSim.m_bGPU )
	//	sInfo = "GPU solver";
	//else
	//	sInfo = "CPU solver";

	//DrawTextGlut(sInfo.c_str(), -g_Width/2 + 10, g_Height/2 - (linePos += 20));

	// help for keys	
	DrawTextGlut("space: advance one step", -g_Width/2 + 10, g_Height/2 - (linePos += 20));
	DrawTextGlut("'s': start or stop", -g_Width/2 + 10, g_Height/2 - (linePos += 20));
	DrawTextGlut("'r': toggle rotating", -g_Width/2 + 10, g_Height/2 - (linePos += 20));
	//DrawTextGlut("'c': reset", -g_Width/2 + 10, g_Height/2 - (linePos += 20));
	//DrawTextGlut("'b' : next batch", -g_Width/2 + 10, g_Height/2 - (linePos += 20));
	//DrawTextGlut("'g' : toggle CPU/GPU solver", -g_Width/2 + 10, g_Height/2 - (linePos += 20));

	DrawTextGlut("===========================================", -g_Width/2 + 10, g_Height/2 - (linePos += 20));

	char str[100];
	sprintf(str, "Penetration depth:%11.9f", g_WorldSim.m_pNarrowPhase->GetPairs()[0].penetrationDepth);
	DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20));

	{
		const CTransform& trans = g_WorldSim.m_pNarrowPhase->GetPairs()[0].pObjA->GetTransform();
		sprintf(str, "ObjectA translation(%11.9f, %11.9f, %11.9f)", trans.GetTranslation().m_X, trans.GetTranslation().m_Y, trans.GetTranslation().m_Z);
		DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20));

		sprintf(str, "ObjectA rotation(%11.9f, %11.9f, %11.9f, %11.9f)", trans.GetRotation().m_X, trans.GetRotation().m_Y, trans.GetRotation().m_Z, trans.GetRotation().m_W);
		DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20));
	}

	{
		const CTransform& trans = g_WorldSim.m_pNarrowPhase->GetPairs()[0].pObjB->GetTransform();
		sprintf(str, "ObjectB translation(%11.9f, %11.9f, %11.9f)", trans.GetTranslation().m_X, trans.GetTranslation().m_Y, trans.GetTranslation().m_Z);
		DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20));

		sprintf(str, "ObjectB rotation(%11.9f, %11.9f, %11.9f, %11.9f)", trans.GetRotation().m_X, trans.GetRotation().m_Y, trans.GetRotation().m_Z, trans.GetRotation().m_W);
		DrawTextGlut(str, -g_Width/2 + 10, g_Height/2 - (linePos += 20));
	}

	glMatrixMode(GL_PROJECTION);	
	glPopMatrix();	

	glMatrixMode(GL_MODELVIEW);	
	glPopMatrix();
			
	glutSwapBuffers();
}
Esempio n. 2
0
//===================
void RenderFunc()
//===================
{	
	static GLfloat RedSurface[]   = { 1.0f, 0.0f, 0.0f, 1.0f};
	static GLfloat GreenSurface[] = { 0.0f, 1.0f, 0.0f, 1.0f};
	static GLfloat BlueSurface[]  = { 0.0f, 0.0f, 1.0f, 1.0f};
	static GLfloat YellowSurface[]  = { 1.0f, 1.0f, 0.0f, 1.0f};

	hCamera.Update();

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);		// clear screen and depth buffer
	glLoadIdentity();										// reset modelview matrix
	
	//---------------
	//Draw background
	//---------------
	glDisable(GL_TEXTURE_2D);
	glDisable(GL_LIGHTING);
	glDisable(GL_DEPTH_TEST);
	glPushMatrix();
	glLoadIdentity();
	glMatrixMode(GL_PROJECTION);
	glPushMatrix();
	glLoadIdentity();
	
	glBegin(GL_QUADS);
		glColor3f(0.9F, 0.9F, 0.9F); glVertex2f( 1.0F,  1.0F);
		glColor3f(0.9F, 0.9F, 0.9F); glVertex2f(-1.0F,  1.0F);
		glColor3f(0.2F, 0.2F, 0.2F); glVertex2f(-1.0F, -1.0F);
		glColor3f(0.2F, 0.2F, 0.2F); glVertex2f( 1.0F, -1.0F);
	glEnd();
	
	glEnable(GL_DEPTH_TEST);
	glPopMatrix();
	glMatrixMode(GL_MODELVIEW);
	glPopMatrix();
	
	//----------------
	// Applying camera
	//----------------
	hCamera.ApplyCamera();
	
	glCallList(btmPlate);

	//-------------------------------------------
	// Rotate GL coordinate so that Z axis is up.
	//-------------------------------------------

	/*

			Y                         		Z
			|						  		|
			|						  		|
			|				  ===>	  		|
			|________ X				  		|________ Y
			/						  		/
		   /  GL coordinate			  	   /  Modeler coordinate
		  /							      /
		 Z							     X
	*/

	//glRotatef(-90.0f, 0, 0, 1.0f);
	//glRotatef(-90.0f, 0, 1.0f, 0);
	
	//-------------
	// Draw an axis
	//-------------
	glPushMatrix();
	glCallList(g_glLtAxis);
	glPopMatrix();

	glEnable(GL_LIGHTING);
	
	if ( !g_bPause )
		g_BulletSim.Update();
		
	g_BulletSim.Render(2);
		 
	glDisable(GL_LIGHTING);
}