⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 main.cpp

📁 it is a visual c++ file
💻 CPP
📖 第 1 页 / 共 2 页
字号:
			glVertexPointer(3, GL_FLOAT, sizeof(SHADOW_MODEL_VERTEX), &models[1].vertices[0].position);
			glNormalPointer(GL_FLOAT, sizeof(SHADOW_MODEL_VERTEX), &models[1].vertices[0].normal);
			glDrawElements(GL_TRIANGLES, models[1].numIndices, GL_UNSIGNED_INT, models[1].indices);
		}
		glPopMatrix();
	}

	glPopAttrib();




	//Draw shadow volumes
	glPushAttrib(GL_ALL_ATTRIB_BITS);
	glColorMask(0, 0, 0, 0);
	glShadeModel(GL_FLAT);
	glDepthMask(0);
	glDepthFunc(GL_LESS);
	glEnable(GL_STENCIL_TEST);

	for(int i=0; i<numTori; ++i)
	{
		//See which model is used for this torus
		int currentModel;
		if(i==0 || i==1)
			currentModel=0;
		else
			currentModel=1;

		//Calculate silhouette edges for this torus if not using vertex program volumes
		if(!useVPVolumes)
			models[currentModel].CalculateSilhouetteEdges(torus[i].objectSpaceLightPosition);

		//If using vertex program volumes, bind the program and
		//Set up vertex pointers to draw models
		if(useVPVolumes)
		{
			glBindProgramARB(GL_VERTEX_PROGRAM_ARB, shadowVolumeVP);

			//Pass light position as env parameter 0
			glProgramEnvParameter4fvARB(GL_VERTEX_PROGRAM_ARB, 0, VECTOR4D(torus[i].objectSpaceLightPosition));

			glEnable(GL_VERTEX_PROGRAM_ARB);

			glVertexPointer(3, GL_FLOAT, sizeof(SHADOW_MODEL_VERTEX), &models[currentModel].vertices[0].position);
			glNormalPointer(GL_FLOAT, sizeof(SHADOW_MODEL_VERTEX), &models[currentModel].vertices[0].normal);
		}
			
		glPushMatrix();
		glTranslatef(torus[i].position.x, torus[i].position.y, torus[i].position.z);
		glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
		glRotatef(torus[i].angle, 0.0f, 0.0f, 1.0f);


		
		//If not always using zFail, see if it is necessary for this particular torus
		bool needZFail=true;

		if(!alwaysUseZFail)
		{
			//If the bounding sphere of the model is completely behind any of the lightPlanes,
			//no need for z fail.
			for(int currentPlane=0; currentPlane<6; ++currentPlane)
			{
				if(	lightPlanes[currentPlane].normal.x*torus[i].position.x+
					lightPlanes[currentPlane].normal.y*torus[i].position.y+
					lightPlanes[currentPlane].normal.z*torus[i].position.z+
					lightPlanes[currentPlane].intercept<-modelRadii[currentModel])
				{
					needZFail=false;
					break;
				}
			}
		}

		

		//Draw shadow volumes WITHOUT two sided stencil
		if(!useTwoSidedStencil)
		{
			//Using zfail
			if(alwaysUseZFail || needZFail)
			{
				//Increment stencil buffer for back face depth fail
				glStencilFunc(GL_ALWAYS, 0, ~0);
				glStencilOp(GL_KEEP, GL_INCR, GL_KEEP);
				glCullFace(GL_FRONT);
				//Draw the shadow volume if not using vertex program
				if(!useVPVolumes)
					models[currentModel].DrawInfiniteShadowVolume(	torus[i].objectSpaceLightPosition,
																	true);
				//Draw the model if using vertex program
				if(useVPVolumes)
					glDrawElements(GL_TRIANGLES, models[currentModel].numIndices, GL_UNSIGNED_INT, models[currentModel].indices);

			
				//Decrement stencil buffer for front face depth fail
				glStencilOp(GL_KEEP, GL_DECR, GL_KEEP);
				glCullFace(GL_BACK);
				//Draw the shadow volume if not using vertex program
				if(!useVPVolumes)
					models[currentModel].DrawInfiniteShadowVolume(	torus[i].objectSpaceLightPosition,
																	true);
				//Draw the model if using vertex program
				if(useVPVolumes)
					glDrawElements(GL_TRIANGLES, models[currentModel].numIndices, GL_UNSIGNED_INT, models[currentModel].indices);

			}
			else	//using zpass
			{
				//Increment stencil buffer for front face depth pass
				glStencilFunc(GL_ALWAYS, 0, ~0);
				glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
				glCullFace(GL_BACK);
				//Draw the shadow volume if not using vertex program
				if(!useVPVolumes)
					models[currentModel].DrawInfiniteShadowVolume(	torus[i].objectSpaceLightPosition,
																	false);
				//Draw the model if using vertex program
				if(useVPVolumes)
					glDrawElements(GL_TRIANGLES, models[currentModel].numIndices, GL_UNSIGNED_INT, models[currentModel].indices);


				//Decrement stencil buffer for back face depth pass
				glStencilOp(GL_KEEP, GL_KEEP, GL_DECR);
				glCullFace(GL_FRONT);
				//Draw the shadow volume if not using vertex program
				if(!useVPVolumes)
					models[currentModel].DrawInfiniteShadowVolume(	torus[i].objectSpaceLightPosition,
																	false);
				//Draw the model if using vertex program
				if(useVPVolumes)
					glDrawElements(GL_TRIANGLES, models[currentModel].numIndices, GL_UNSIGNED_INT, models[currentModel].indices);
			}
		}

		//Draw shadow volumes WITH two sided stencil
		if(useTwoSidedStencil)
		{
			//Using zfail
			if(alwaysUseZFail || needZFail)
			{
				glDisable(GL_CULL_FACE);
				
				//Increment(with wrapping) for back face depth fail
				glActiveStencilFaceEXT(GL_BACK);
				glStencilFunc(GL_ALWAYS, 0, ~0);
				glStencilOp(GL_KEEP, GL_INCR_WRAP_EXT, GL_KEEP);

				//Decrement(with wrapping) for front face depth fail
				glActiveStencilFaceEXT(GL_FRONT);
				glStencilFunc(GL_ALWAYS, 0, ~0);
				glStencilOp(GL_KEEP, GL_DECR_WRAP_EXT, GL_KEEP);

				//Enable 2 sided stencil
				glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);

				//Draw the shadow volume if not using vertex program
				if(!useVPVolumes)
					models[currentModel].DrawInfiniteShadowVolume(	torus[i].objectSpaceLightPosition,
																	true);
				//Draw the model if using vertex program
				if(useVPVolumes)
					glDrawElements(GL_TRIANGLES, models[currentModel].numIndices, GL_UNSIGNED_INT, models[currentModel].indices);

			}
			else	//using zpass
			{
				glDisable(GL_CULL_FACE);

				//Increment(with wrapping) for front face depth pass
				glActiveStencilFaceEXT(GL_FRONT);
				glStencilFunc(GL_ALWAYS, 0, ~0);
				glStencilOp(GL_KEEP, GL_KEEP, GL_INCR_WRAP_EXT);

				//Decrement(with wrapping) for back face depth pass
				glActiveStencilFaceEXT(GL_BACK);
				glStencilFunc(GL_ALWAYS, 0, ~0);
				glStencilOp(GL_KEEP, GL_KEEP, GL_DECR_WRAP_EXT);

				//Enable 2 sided stencil
				glEnable(GL_STENCIL_TEST_TWO_SIDE_EXT);

				//Draw the shadow volume if not using vertex program
				if(!useVPVolumes)
					models[currentModel].DrawInfiniteShadowVolume(	torus[i].objectSpaceLightPosition,
																	false);
				//Draw the model if using vertex program
				if(useVPVolumes)
					glDrawElements(GL_TRIANGLES, models[currentModel].numIndices, GL_UNSIGNED_INT, models[currentModel].indices);
			}
		}

		glPopMatrix();
	}

	glPopAttrib();

	


	//Now draw lit where stencil==0
	glPushAttrib(GL_ENABLE_BIT);
	glEnable(GL_COLOR_MATERIAL);
	glLightfv(GL_LIGHT1, GL_POSITION, VECTOR4D(light.position)); 
	glLightfv(GL_LIGHT1, GL_AMBIENT, light.color/5);
	glLightfv(GL_LIGHT1, GL_DIFFUSE, light.color);
	glLightfv(GL_LIGHT1, GL_SPECULAR, white);
	glEnable(GL_LIGHT1);
	glEnable(GL_LIGHTING);
	glStencilFunc(GL_EQUAL, 0, ~0);
	glStencilOp(GL_KEEP, GL_KEEP, GL_KEEP);
	glEnable(GL_STENCIL_TEST);

	//Enable vertex program if using VP for volumes
	if(useVPVolumes)
	{
		glBindProgramARB(GL_VERTEX_PROGRAM_ARB, lightingVP);
		glEnable(GL_VERTEX_PROGRAM_ARB);
	}

	//Draw box
	glMaterialfv(GL_FRONT, GL_SPECULAR, black);
	glColor3f(0.2f, 0.2f, 1.0f);
	DrawBox();

	//Draw models
	glMaterialfv(GL_FRONT, GL_SPECULAR, white);
	glMaterialf(GL_FRONT, GL_SHININESS, 32.0f);
	for(int i=0; i<numTori; ++i)
	{
		glColor4fv(torus[i].color);
		glPushMatrix();
		glTranslatef(torus[i].position.x, torus[i].position.y, torus[i].position.z);
		glRotatef(90.0f, 1.0f, 0.0f, 0.0f);
		glRotatef(torus[i].angle, 0.0f, 0.0f, 1.0f);
		if(i==0 || i==1)
		{	
			glVertexPointer(3, GL_FLOAT, sizeof(SHADOW_MODEL_VERTEX), &models[0].vertices[0].position);
			glNormalPointer(GL_FLOAT, sizeof(SHADOW_MODEL_VERTEX), &models[0].vertices[0].normal);
			glDrawElements(GL_TRIANGLES, models[0].numIndices, GL_UNSIGNED_INT, models[0].indices);
		
		}
		else
		{
			glVertexPointer(3, GL_FLOAT, sizeof(SHADOW_MODEL_VERTEX), &models[1].vertices[0].position);
			glNormalPointer(GL_FLOAT, sizeof(SHADOW_MODEL_VERTEX), &models[1].vertices[0].normal);
			glDrawElements(GL_TRIANGLES, models[1].numIndices, GL_UNSIGNED_INT, models[1].indices);
		}
		glPopMatrix();
	}

	glPopAttrib();



	//Draw sphere at light's position
	glPushMatrix();
	glColor4fv(light.color);
	glTranslatef(light.position.x, light.position.y, light.position.z);
	gluSphere(sphere, 0.1, 24, 24);
	glPopMatrix();


	fpsCounter.Update();											//update frames per second counter
	glColor4f(1.0f, 1.0f, 0.0f, 1.0f);
	window.StartTextMode();
	window.Print(0, 28, "FPS: %.2f", fpsCounter.GetFps());			//print the fps
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
	window.Print(0, 48, "%s", alwaysUseZFail ?	"\"zFail\" algorithm with capped, infinite shadow volumes" :
												"\"zPass\" algorithm with non-capped volumes, where possible");
	glColor4f(0.0f, 1.0f, 0.0f, 1.0f);
	window.Print(0, 68, "%s", useVPVolumes ?	"Extruding shadow volumes in a vertex program" :
												"Standard CPU shadow volume calculations");
	glColor4f(1.0f, 0.0f, 0.0f, 1.0f);
	if(useTwoSidedStencil)
		window.Print(0, 88, "Using EXT_stencil_two_side");
	window.EndTextMode();
	glColor4f(1.0f, 1.0f, 1.0f, 1.0f);

	if(window.isKeyPressed(VK_F1))
	{
		window.SaveScreenshot();
		window.SetKeyReleased(VK_F1);
	}

	window.SwapBuffers();									//swap buffers

	//check for any opengl errors
	window.CheckGLError();

	//quit if necessary
	if(window.isKeyPressed(VK_ESCAPE))
		PostQuitMessage(0);
}

void DemoShutdown()
{
	window.Shutdown();										//Shutdown window
}

//ENTRY POINT FOR APPLICATION
//CALL WINDOW CREATION ROUTINE, DEAL WITH MESSAGES, WATCH FOR INTERACTION
int WINAPI WinMain(	HINSTANCE	hInstance,				//instance
					HINSTANCE	hPrevInstance,			//Previous Instance
					LPSTR		lpCmdLine,				//command line parameters
					int			nCmdShow)				//Window show state
{
	//Initiation
	errorLog.Init("Error Log.txt");

	//init variables etc, then GL
	if(!DemoInit())
	{
		errorLog.OutputError("Demo Initiation failed");
		return 0;
	}
	else
		errorLog.OutputSuccess("Demo Initiation Successful");

	if(!GLInit())
	{
		errorLog.OutputError("OpenGL Initiation failed");
		return 0;
	}
	else
		errorLog.OutputSuccess("OpenGL Initiation Successful");

	//Main Loop
	for(;;)
	{
		if(!(window.HandleMessages())) break;//handle windows messages, quit if returns false
		UpdateFrame();
		RenderFrame();
	}

	DemoShutdown();
	
	errorLog.OutputSuccess("Exiting...");
	return (window.msg.wParam);								//Exit The Program
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -