Exemple #1
0
Rasterizer::Rasterizer(const unsigned width, const unsigned height, const float degrees)
{
	int w = width;
	int h = height;
	SetFov(degrees);
	_leftUBounds = new float[h];
	_rightUBounds = new float[h];
	_leftVBounds = new float[h];
	_rightVBounds = new float[h];
	_leftColor = new Color[h];
	_rightColor = new Color[h];
	_leftZBounds = new float[h];
	_rightZBounds = new float[h];
	_leftBounds = new int[h];
	_rightBounds = new int[h];
	_leftWBounds = new float[h];
	_rightWBounds = new float[h];
	_pixels.resize(width * height);
	_width = w;
	_height = h;
	ResetBounds();
	_nearPlane = 2;
	_farPlane  = 100;
	_zBuff.resize(width * height);
	SetUpMatrices();
	ResetZBuff();
	_basisOrigin = Vertex(0.0f, 0.0f, 0.0f);
	_basisP = Vertex(Color(1.0f, 0.0f, 0.0f), 1.0f, 0.0f, 0.0f);
	_basisQ = Vertex(Color(0.0f, 1.0f, 0.0f), 0.0f, 1.0f, 0.0f);
	_basisR = Vertex(Color(0.0f, 0.0f, 1.0f), 0.0f, 0.0f, 1.0f);
}
Exemple #2
0
/*!****************************************************************************
 @Function		RenderScene
 @Return		bool		true if no error occured
 @Description	Main rendering loop function of the program. The shell will
				call this function every frame.
				eglSwapBuffers() will be performed by PVRShell automatically.
				PVRShell will also manage important OS events.
				Will also manage relevent OS events. The user has access to
				these events through an abstraction layer provided by PVRShell.
******************************************************************************/
bool OGLES2ShadowMapping::RenderScene()
{
	//rotate light position
	m_fLightAngle += 0.01f;
	m_vLightPosition.x = m_fLightDistance * (float) cos(m_fLightAngle);
	m_vLightPosition.z = m_fLightDistance * (float) sin(m_fLightAngle);
	m_vLightDirection.x = -m_vLightPosition.x;
	m_vLightDirection.z = -m_vLightPosition.z;

	SetUpMatrices();

	glEnable(GL_DEPTH_TEST);

	// Bind the frame buffer object
	glBindFramebuffer(GL_FRAMEBUFFER, m_uiFrameBufferObject);

	if(glCheckFramebufferStatus(GL_FRAMEBUFFER) == GL_FRAMEBUFFER_COMPLETE)
	{
		// Clear the screen and depth buffer so we can render from the light's view
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

		// Set the current viewport to our texture size
		glViewport(0, 0, m_ui32ShadowMapSize, m_ui32ShadowMapSize);

		// Since we don't care about colour when rendering the depth values to
		// the shadow-map texture, we disable color writing to increase speed.
		glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); 

		// Enable the simple shader for the light view pass. This render will not be shown to the user 
		// so only the simplest render needs to be implemented
		glUseProgram(m_SimpleShaderProgram.uiId);

		// Set the light projection matrix
		glUniformMatrix4fv(m_SimpleShaderProgram.uiProjectionMatrixLoc, 1, GL_FALSE, m_LightProjection.f);

		// Render the world according to the light's view
		DrawScene(m_LightView);

		// We can turn color writing back on since we already stored the depth values
		glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); 

		// Restore our normal viewport size to our screen width and height
		glViewport(0, 0,PVRShellGet(prefWidth),PVRShellGet(prefHeight));
	}

	glBindFramebuffer(GL_FRAMEBUFFER, 0);

	// Clear the colour and depth buffers, we are now going to render the scene again from scratch
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	
	// Load the shadow shader. This shader requires additional parameters; texProjMatrix for the depth buffer 
	// look up and the light direction for diffuse light (the effect is a lot nicer with the additon of the 
	// diffuse light).
	glUseProgram(m_ShadowShaderProgram.uiId);
	
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, m_uiShadowMapTexture);

	glUniformMatrix4fv(m_ShadowShaderProgram.uiProjectionMatrixLoc, 1, GL_FALSE, m_Projection.f);

	PVRTMat4 mViewInv, mTextureMatrix, mMatrix;
	mViewInv = m_View.inverse();

	// We need to calculate the texture projection matrix. This matrix takes the pixels from world space to previously rendered light projection space
	//where we can look up values from our saved depth buffer. The matrix is constructed from the light view and projection matrices as used for the previous render and 
	//then multiplied by the inverse of the current view matrix. 
	mTextureMatrix = m_BiasMatrix * m_LightProjection *  m_LightView * mViewInv;

	glUniformMatrix4fv(m_ShadowShaderProgram.uiTexProjMatrixLoc, 1, GL_FALSE, mTextureMatrix.f);

	DrawSceneWithShadow(m_View);

	// Re-enable the simple shader to draw the light source object
	glUseProgram(m_SimpleShaderProgram.uiId);

	SPODNode& Node = m_Scene.pNode[1];

	PVRTMat4 mWorld, mModelView;

	m_Scene.GetWorldMatrix(mWorld, Node);

	mWorld.f[12] = m_vLightPosition.x;
	mWorld.f[13] = m_vLightPosition.y;
	mWorld.f[14] = m_vLightPosition.z;

	mModelView = m_View * mWorld;

	glUniformMatrix4fv(m_SimpleShaderProgram.uiModelViewMatrixLoc, 1, GL_FALSE, mModelView.f);
	glUniformMatrix4fv(m_SimpleShaderProgram.uiProjectionMatrixLoc, 1, GL_FALSE, m_LightProjection.f);

	DrawMesh(1);

	m_Print3D.DisplayDefaultTitle("ShadowMap", "", ePVRTPrint3DSDKLogo);
	m_Print3D.Flush();

	return true;
}