Exemple #1
0
//Called when a key is pressed
void Keyboard(unsigned char key, int x, int y)
{
	//If escape is pressed, exit
	if(key==27)
		exit(0);

	//Use P to pause the animation and U to unpause
	if(key=='P' || key=='p')
		timer.Pause();

	if(key=='U' || key=='u')
		timer.Unpause();
}
Exemple #2
0
//Perform per frame updates
void UpdateFrame()
{
	window.Update();
	camera.Update();

	//Change anisotropy level
	if(	window.isKeyPressed(VK_UP) && EXT_texture_filter_anisotropic_supported &&
		currentAnisotropy<maxAnisotropy)
	{
		window.MakeCurrent();
		currentAnisotropy*=2;
		glBindTexture(GL_TEXTURE_2D, pbufferTexture);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, currentAnisotropy);
		window.SetKeyReleased(VK_UP);
	}
	
	if(	window.isKeyPressed(VK_DOWN) && EXT_texture_filter_anisotropic_supported &&
		currentAnisotropy>1)
	{
		window.MakeCurrent();
		currentAnisotropy/=2;
		glBindTexture(GL_TEXTURE_2D, pbufferTexture);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, currentAnisotropy);
		window.SetKeyReleased(VK_DOWN);
	}

	//toggle mipmaps
	if( window.isKeyPressed('M') && useMipmapFilter==false && SGIS_generate_mipmap_supported)
	{
		window.MakeCurrent();
		glBindTexture(GL_TEXTURE_2D, pbufferTexture);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, true);
		useMipmapFilter=true;
	}

	if( window.isKeyPressed('L') && useMipmapFilter==true && SGIS_generate_mipmap_supported)
	{
		window.MakeCurrent();
		glBindTexture(GL_TEXTURE_2D, pbufferTexture);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP_SGIS, false);
		useMipmapFilter=false;
	}

	//Pause/unpause
	if(window.isKeyPressed('P'))
		timer.Pause();

	if(window.isKeyPressed('U'))
		timer.Unpause();

	//Swap between scenes in the pbuffer
	if(window.isKeyPressed('1') && drawTextured)
	{
		//Draw wire tori
		drawTextured=false;
	}

	if(window.isKeyPressed('2') && !drawTextured)
	{
		//draw textured sphere
		drawTextured=true;
	}
}
Exemple #3
0
//Perform per frame updates
void UpdateFrame()
{
	window.Update();

	//update balls' position
	float c = 2.0f*(float)cos(timer.GetTime()/600);
  
	metaballs[0].position.x=-4.0f*(float)cos(timer.GetTime()/700) - c;
	metaballs[0].position.y=4.0f*(float)sin(timer.GetTime()/600) - c;

	metaballs[1].position.x=5.0f*(float)sin(timer.GetTime()/400) + c;
	metaballs[1].position.y=5.0f*(float)cos(timer.GetTime()/400) - c;

	metaballs[2].position.x=-5.0f*(float)cos(timer.GetTime()/400) - 0.2f*(float)sin(timer.GetTime()/600);
	metaballs[2].position.y=5.0f*(float)sin(timer.GetTime()/500) - 0.2f*(float)sin(timer.GetTime()/400);
	
	//increase or decrease density
	if(window.isKeyPressed(VK_UP) && gridSize<maxGridSize)
	{
		gridSize++;
		cubeGrid.Init(gridSize);
	}

	if(window.isKeyPressed(VK_DOWN) && gridSize>minGridSize)
	{
		gridSize--;
		cubeGrid.Init(gridSize);
	}

	//pause & unpause
	if(window.isKeyPressed('P'))
	{
		timer.Pause();
	}

	if(window.isKeyPressed('U'))
	{
		timer.Unpause();
	}

	//clear the field
	for(int i=0; i<cubeGrid.numVertices; i++)
	{
		cubeGrid.vertices[i].value=0.0f;
		cubeGrid.vertices[i].normal.LoadZero();
	}
	
	//evaluate the scalar field at each point
	VECTOR3D ballToPoint;
	float squaredRadius;
	VECTOR3D ballPosition;
	float normalScale;
	for(int i=0; i<numMetaballs; i++)
	{
		squaredRadius=metaballs[i].squaredRadius;
		ballPosition=metaballs[i].position;

		//VC++6 standard does not inline functions
		//by inlining these maually, in this performance-critical area,
		//almost a 100% increase in speed is found
		for(int j=0; j<cubeGrid.numVertices; j++)
		{
			//ballToPoint=cubeGrid.vertices[j].position-ballPosition;
			ballToPoint.x=cubeGrid.vertices[j].position.x-ballPosition.x;
			ballToPoint.y=cubeGrid.vertices[j].position.y-ballPosition.y;
			ballToPoint.z=cubeGrid.vertices[j].position.z-ballPosition.z;
			
			//get squared distance from ball to point
			//float squaredDistance=ballToPoint.GetSquaredLength();
			float squaredDistance=	ballToPoint.x*ballToPoint.x +
									ballToPoint.y*ballToPoint.y +
									ballToPoint.z*ballToPoint.z;
			if(squaredDistance==0.0f)
				squaredDistance=0.0001f;

			//value = r^2/d^2
			cubeGrid.vertices[j].value+=squaredRadius/squaredDistance;

			//normal = (r^2 * v)/d^4
			normalScale=squaredRadius/(squaredDistance*squaredDistance);
			//cubeGrid.vertices[j].normal+=ballToPoint*normalScale;
			cubeGrid.vertices[j].normal.x+=ballToPoint.x*normalScale;
			cubeGrid.vertices[j].normal.y+=ballToPoint.y*normalScale;
			cubeGrid.vertices[j].normal.z+=ballToPoint.z*normalScale;
		}
	}

	//toggle wireframe
	if(window.isKeyPressed('W'))
	{
		glPolygonMode(GL_FRONT, GL_LINE);
	}

	if(window.isKeyPressed('F'))
	{
		glPolygonMode(GL_FRONT, GL_FILL);
	}

	//Change color
	if(window.isKeyPressed(VK_SPACE))
	{
		currentDiffuseColor++;
		if(currentDiffuseColor==numDiffuseColors)
			currentDiffuseColor=0;
		glLightfv(GL_LIGHT1, GL_DIFFUSE, diffuseColors[currentDiffuseColor]);

		window.SetKeyReleased(VK_SPACE);
	}
}