Beispiel #1
0
void RenderManager::Render( const Simulation &gameSim, const EngineConfig &engineCfg ) const
{
    glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); //clear the screen buffer

	//render scene to fbo with active camera
    glUseProgram( geometryPassShader.GetProgramHandler() );

		glDepthMask( GL_TRUE ); //enable writing to the depth buffer
		glEnable( GL_DEPTH_TEST );

		BindDeferredFbo();

			glEnable( GL_CULL_FACE );
			glCullFace( GL_BACK );

			//bind the surface texture and pass it to the shader
			glActiveTexture( GL_TEXTURE0 );
			glUniform1i( surfaceTextureId, 0 );

			//render the simulation
			ProjectionState cameraProjection = gameSim.RenderLit();
			//ProjectionState cameraProjection = gameSim.RenderShadowCasters( gameSim.GetStaticLights()[2].GetCameras()[5] ); //for testing light cameras

			int viewportMatrix[4];
			float perspectiveMatrix[16];
			cameraProjection.CopyViewportMatrix( viewportMatrix );
			cameraProjection.CopyPerspectiveMatrix( perspectiveMatrix );

		UnbindDeferredFbo();

	glUseProgram( 0 );

	//render depth buffer to fullscreen quad
	glClear( GL_DEPTH_BUFFER_BIT );
	OrthographicCamera orthoCamera( frmr::Vec3f(), frmr::Vec2f(), engineCfg.GetActiveWidth(), engineCfg.GetActiveHeight() );
	orthoCamera.ApplyTransformation();

    glUseProgram( depthTransferShader.GetProgramHandler() );
		glActiveTexture( GL_TEXTURE0 );
		glBindTexture( GL_TEXTURE_2D, depthTexture );
		glUniform1i( depthId, 0 );
		glCallList( fullscreenQuad );
	glUseProgram( 0 );

    glDepthMask( GL_FALSE ); //disable writing to the depth buffer
    glDisable( GL_DEPTH_TEST );

    //send all the textures, the viewport parameters and the perspective matrix to the deferred rendering shader
    glUseProgram( lightPassShader.GetProgramHandler() );
		glUniform1i( normalsId, 0 );
		glUniform1i( diffuseId, 1 );
		glUniform1i( depthId, 2 );
		glUniform4iv( viewportParamsId, 4, viewportMatrix );
		glUniformMatrix4fv( perspectiveMatrixId, 16, false, perspectiveMatrix );
    glUseProgram( 0 );

    glColor4f( 1.0f, 1.0f, 1.0f, 1.0f );

    glEnable( GL_STENCIL_TEST );
    glClearStencil( 0 );

    //const vector<Light> staticLights;// = gameSim.GetStaticLights();
    vector<Light> staticLights = gameSim.GetStaticLights();

	PerspectiveCamera activeCamera = gameSim.GetActiveCamera();

    glDisable( GL_BLEND );

    for ( auto lightIt : staticLights )
    {
    	//if light casts shadows
    	//for each face
    	//render face to fbo

    	if ( lightIt.CastsShadows() )
		{
			glBindFramebuffer( GL_FRAMEBUFFER, shadowFbo );
			glViewport( 0, 0, 1024, 1024 );
			glCullFace( GL_FRONT );

			glUseProgram( shadowPassShader.GetProgramHandler() );

				int cameraIndex = 0;
				for ( auto camIt : lightIt.GetCameras() )
				{
					//if camera frustum intersects activeCamera frustum
					glFramebufferTexture2D( GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_CUBE_MAP_POSITIVE_X + cameraIndex++, shadowMap, 0 );
					glClear( GL_DEPTH_BUFFER_BIT );

					gameSim.RenderShadowCasters( camIt );
				}

			glUseProgram( 0 );
			glBindFramebuffer( GL_FRAMEBUFFER, 0 );
		}

        glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE ); //disable writing to the color buffer
        glStencilMask( 0xFF ); //enable writing to the stencil buffer

        glClear( GL_STENCIL_BUFFER_BIT );

        glStencilFunc( GL_NEVER, 1, 0xFF ); // never pass stencil test
        glStencilOp( GL_REPLACE, GL_KEEP, GL_KEEP );  // replace stencil buffer values to ref=1

        activeCamera.ApplyTransformation();
		ResetViewport( engineCfg );

		//render icosphere
        glPushMatrix();
            glTranslatef( lightIt.GetPosition().GetX(), lightIt.GetPosition().GetY(), lightIt.GetPosition().GetZ() );
            glScalef( lightIt.GetRadius(), lightIt.GetRadius(), lightIt.GetRadius() );
            glDisable( GL_CULL_FACE );
            glDisable( GL_DEPTH_TEST ); //TODO: use the stencil buffer with depth fail and cull front face
            glBindTexture( GL_TEXTURE_2D, 0 );
            glCallList( icosphere );
        glPopMatrix();

        glDisable( GL_DEPTH_TEST );

        //light only where the stencil buffer is equal to 1
        glStencilFunc( GL_EQUAL, 1, 0xFF );

        glColorMask( GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE ); //enable writing to the color buffer
        glStencilMask( 0x00 ); //disable writing to the stencil buffer

		orthoCamera.ApplyTransformation();


		glDisable( GL_STENCIL_TEST );

        glUseProgram( lightPassShader.GetProgramHandler() );

			//pass the light's attributes to the shader
			glUniform3f( lightPositionId, lightIt.GetPosition().GetX(), lightIt.GetPosition().GetY(), lightIt.GetPosition().GetZ() );
			glUniform3f( lightColorId, lightIt.GetColor().GetX(), lightIt.GetColor().GetY(), lightIt.GetColor().GetZ() );
			glUniform1f( lightLinearAttenuationId, lightIt.GetLinearAttenuation() );
			glUniform1f( lightQuadraticAttenuationId, lightIt.GetQuadraticAttenuation() );

			glUniform1i( shadowId, shadowMap );
			glEnable( GL_TEXTURE_CUBE_MAP );
			glBindTexture( GL_TEXTURE_CUBE_MAP, shadowMap );

			glDisable( GL_CULL_FACE );

			//enable blending so that each new quad adds to whatever's in the render buffer
			glEnable( GL_BLEND );
			glBlendFunc( GL_ONE, GL_ONE );

			//bind the diffuse, normal and depth maps before rendering fullscreen quad
			glActiveTexture( GL_TEXTURE0 );
			glBindTexture( GL_TEXTURE_2D, normalsTexture );
			glActiveTexture( GL_TEXTURE1 );
			glBindTexture( GL_TEXTURE_2D, diffuseTexture );
			glActiveTexture( GL_TEXTURE2 );
			glBindTexture( GL_TEXTURE_2D, depthTexture );

			glCallList( fullscreenQuad );

			glDisable( GL_TEXTURE_CUBE_MAP );
			glDisable( GL_BLEND );

		glUseProgram( 0 );
    }

    glDisable(GL_STENCIL_TEST );

    // Reset OpenGL state
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, 0);

	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, 0);

	glActiveTexture(GL_TEXTURE2);
	glBindTexture(GL_TEXTURE_2D, 0);

    //change to perspective projection
	//enable depth test
	//enable depth mask
	//draw projectiles

    //change to orthogonal projection
	//disable depth test
	//disable depth mask
	//draw HUD
	//draw UI
}