Ejemplo n.º 1
0
void Game::DisplayFrameRate()
{


	CShaderProgram *fontProgram = (*m_pShaderPrograms)[1];

	RECT dimensions = m_gameWindow.GetDimensions();
	int height = dimensions.bottom - dimensions.top;

	// Increase the elapsed time and frame counter
	m_elapsedTime += m_dt;
	m_frameCount++;

	// Now we want to subtract the current time by the last time that was stored
	// to see if the time elapsed has been over a second, which means we found our FPS.
	if (m_elapsedTime > 1000)
    {
		m_elapsedTime = 0;
		m_framesPerSecond = m_frameCount;

		// Reset the frames per second
		m_frameCount = 0;
    }

	if (m_framesPerSecond > 0) {
		// Use the font shader program and render the text
		fontProgram->UseProgram();
		glDisable(GL_DEPTH_TEST);
		fontProgram->SetUniform("matrices.modelViewMatrix", glm::mat4(1));
		fontProgram->SetUniform("matrices.projMatrix", m_pCamera->GetOrthographicProjectionMatrix());
		fontProgram->SetUniform("vColour", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));
		m_pFtFont->Render(20, height - 20, 20, "FPS: %d", m_framesPerSecond);
	}
}
Ejemplo n.º 2
0
// Render method runs repeatedly in a loop
void Game::Render() 
{
	
	// Clear the buffers and enable depth testing (z-buffering)
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);

	// Set up a matrix stack
	glutil::MatrixStack modelViewMatrixStack;
	modelViewMatrixStack.SetIdentity();
	modelViewMatrixStack.LookAt(m_pCamera->GetPosition(), m_pCamera->GetView(), m_pCamera->GetUpVector());


	// Use the main shader program 
	CShaderProgram *pMainProgram = (*m_pShaderPrograms)[0];
	pMainProgram->UseProgram();

	// Set light and materials in main shader program
	glm::vec4 vPosition(-100, 100, 50, 1);
	glm::mat3 normalMatrix = m_pCamera->ComputeNormalMatrix(modelViewMatrixStack.Top());
	glm::vec4 vLightEye = modelViewMatrixStack.Top()*vPosition;
	pMainProgram->SetUniform("light1.position", vLightEye);		// Position of light source in eye coordinates
	pMainProgram->SetUniform("light1.La", glm::vec3(0.15f, 0.15f, 0.15f));
	pMainProgram->SetUniform("light1.Ld", glm::vec3(1.0f, 1.0f, 1.0f));
	pMainProgram->SetUniform("light1.Ls", glm::vec3(1.0f, 1.0f, 1.0f));
	pMainProgram->SetUniform("material1.Ma", glm::vec3(0.5f, 0.5f, 0.5f));
	pMainProgram->SetUniform("material1.Md", glm::vec3(0.5f, 0.5f, 0.5f));
	pMainProgram->SetUniform("material1.Ms", glm::vec3(0.5f, 0.5f, 0.5f));
	pMainProgram->SetUniform("material1.shininess", 15.0f);
	pMainProgram->SetUniform("bUseTexture", false);
	pMainProgram->SetUniform("sampler0", 0);


	// Render the grid
	modelViewMatrixStack.Push();
		pMainProgram->SetUniform("matrices.projMatrix", m_pCamera->GetPerspectiveProjectionMatrix());
		pMainProgram->SetUniform("matrices.modelViewMatrix", modelViewMatrixStack.Top());
		pMainProgram->SetUniform("matrices.normalMatrix", m_pCamera->ComputeNormalMatrix(modelViewMatrixStack.Top()));
		m_pGrid->Render();
	modelViewMatrixStack.Pop();

	
	// Switch to the sphere program
	CShaderProgram *pSphereProgram = (*m_pShaderPrograms)[2];
	pSphereProgram->UseProgram();

	// Set light and materials in sphere programme
	pSphereProgram->SetUniform("material1.Ma", glm::vec3(0.0f, 1.0f, 0.0f));
	pSphereProgram->SetUniform("material1.Md", glm::vec3(0.0f, 1.0f, 0.0f));
	pSphereProgram->SetUniform("material1.Ms", glm::vec3(1.0f, 1.0f, 1.0f));
	pSphereProgram->SetUniform("material1.shininess", 50.0f);
	pSphereProgram->SetUniform("light1.La", glm::vec3(0.15f, 0.15f, 0.15f));
	pSphereProgram->SetUniform("light1.Ld", glm::vec3(1.0f, 1.0f, 1.0f));
	pSphereProgram->SetUniform("light1.Ls", glm::vec3(1.0f, 1.0f, 1.0f));
	pSphereProgram->SetUniform("light1.position", vLightEye); 

	// Render the sphere
	modelViewMatrixStack.Push();
		modelViewMatrixStack.Translate(glm::vec3(0.0f, 5.0f, 50.0f));
		modelViewMatrixStack.Scale(5.0f);
		pSphereProgram->SetUniform("matrices.projMatrix", m_pCamera->GetPerspectiveProjectionMatrix());
		pSphereProgram->SetUniform("matrices.modelViewMatrix", modelViewMatrixStack.Top());
		pSphereProgram->SetUniform("matrices.normalMatrix", m_pCamera->ComputeNormalMatrix(modelViewMatrixStack.Top()));
		
		pSphereProgram->SetUniform("t",m_elapsedTime);
			
		pSphereProgram->SetUniform("levels",m_levels);

		glEnable(GL_BLEND);
		glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
		m_pSphere->Render();
	modelViewMatrixStack.Pop();

#ifdef __WIN32
    void swapBuffers();
#endif
#ifdef __APPLE__
    glSwapAPPLE();
#endif

}
void CSDLOpenGLWindow::RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glm::mat4 mModelView = cCamera.look();
	glm::mat4 mCurrent = mModelView;

	// First render textured objects

	glEnable(GL_TEXTURE_2D);
	spTextured.UseProgram();
	glBindVertexArray(uiVAOs[0]);

	spTextured.SetUniform("projectionMatrix", GetProjectionMatrix());
	spTextured.SetUniform("modelViewMatrix", mModelView);
	spTextured.SetUniform("vColor", glm::vec4(1, 1, 1, 1));

	// Render ground

	tBlueIce.BindTexture();
	glDrawArrays(GL_TRIANGLES, 36, 6);

	// Render 5 opaque boxes

	tBox.BindTexture();

	for (int i = 0; i < 5; i++)
	{
		float fSign = -1.0f + float(i % 2)*2.0f; // This just returns -1.0f or 1.0f (try to examine this)
		glm::vec3 vPos = glm::vec3(fSign*15.0f, 0.0f, 50.0f - float(i)*25.0f);
		mCurrent = glm::translate(mModelView, vPos);
		mCurrent = glm::scale(mCurrent, glm::vec3(8.0f, 8.0f, 8.0f));
		mCurrent = glm::rotate(mCurrent, fGlobalAngle + float(i)*50.0f* PIover180, glm::vec3(0.0f, 1.0f, 0.0f));
		spTextured.SetUniform("modelViewMatrix", mCurrent);
		glDrawArrays(GL_TRIANGLES, 0, 36);
	}

	// Now switch to only colored rendering

	glDisable(GL_TEXTURE_2D);
	spColored.UseProgram();
	glBindVertexArray(uiVAOs[1]);

	spColored.SetUniform("projectionMatrix", GetProjectionMatrix());
	
	// Render 5 transparent boxes

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	glDepthMask(0); // Disable writing to depth buffer

	for (int i = 0; i < 5; i++)
	{
		float fSign = 1.0f - float(i % 2)*2.0f; // Same case as before -  -1.0f or 1.0f
		glm::vec3 vPos = glm::vec3(fSign*15.0f, 0.0f, 50.0f - float(i)*25.0f);
		mCurrent = glm::translate(mModelView, vPos);
		mCurrent = glm::scale(mCurrent, glm::vec3(8.0f, 8.0f, 8.0f));
		mCurrent = glm::rotate(mCurrent, fGlobalAngle*0.8f + i*30.0f*PIover180, glm::vec3(1.0f, 0.0f, 0.0f)); // Just a variation of first rotating
		spColored.SetUniform("modelViewMatrix", mCurrent);
		spColored.SetUniform("vColor", vBoxColors[i]);
		glDrawArrays(GL_TRIANGLES, 0, 36);
	}
	glDisable(GL_BLEND);
	glDepthMask(1); // Re-enable writing to depth buffer

	fGlobalAngle += sof(100.0f*PIover180);
	cCamera.Update(this);
}
void CSDLOpenGLWindow::RenderScene()
{
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	glEnable(GL_TEXTURE_2D);
	spDirectionalLight.UseProgram();
	glBindVertexArray(uiVAOs[0]);

	// Set light properties

	float fSine = sin(fSunAngle*PIover180);
	glm::vec3 vSunPos(cos(fSunAngle*PIover180) * 70, sin(fSunAngle*PIover180) * 70, 0.0);

	// We'll change color of skies depending on sun's position
	glClearColor(0.0f, std::max(0.0f, 0.9f*fSine), std::max(0.0f, 0.9f*fSine), 1.0f);

	spDirectionalLight.SetUniform("sunLight.vColor", glm::vec3(1.0f, 1.0f, 1.0f));
	spDirectionalLight.SetUniform("sunLight.fAmbientIntensity", 0.25f);
	spDirectionalLight.SetUniform("sunLight.fStrength", 1.0f);
	spDirectionalLight.SetUniform("sunLight.vDirection", -glm::normalize(vSunPos));

	spDirectionalLight.SetUniform("projectionMatrix", GetProjectionMatrix());

	glm::mat4 mView = cCamera.look();
	glm::mat4 mModel(1.0);

	spDirectionalLight.SetUniform("gSampler", 0);

	spDirectionalLight.SetUniform("modelViewMatrix", mView*mModel);
	spDirectionalLight.SetUniform("normalMatrix", glm::transpose(glm::inverse(glm::mat3(mModel))));
	spDirectionalLight.SetUniform("vColor", glm::vec4(1.0f, 1.0f, 1.0f, 1.0f));

	// Render ground

	tTextures[0].BindTexture();
	glDrawArrays(GL_TRIANGLES, 36, 6);

	tTextures[1].BindTexture();

	for (int i = 0; i < 5; i++)
	{
		float fSign = -1.0f + float(i % 2)*2.0f; // This just returns -1.0f or 1.0f (try to examine this)
		glm::vec3 vPos = glm::vec3(fSign*15.0f, 0.0f, 50.0f - float(i)*25.0f);
		mModel = glm::translate(glm::mat4(1.0), vPos);
		mModel = glm::scale(mModel, glm::vec3(8.0f, 8.0f, 8.0f));
		// We need to trasnsform normals properly, it's done by transpose of inverse matrix of rotations and scales
		spDirectionalLight.SetUniform("normalMatrix", glm::transpose(glm::inverse(glm::mat3(mModel))));
		spDirectionalLight.SetUniform("modelViewMatrix", mView*mModel);
		glDrawArrays(GL_TRIANGLES, 0, 36);
	}

	// Render 5 tori (plural of torus :D)

	tTextures[2].BindTexture();

	for (int i = 0; i < 5; i++)
	{
		float fSign = 1.0f - float(i % 2)*2.0f; // This just returns -1.0f or 1.0f (try to examine this)
		glm::vec3 vPos = glm::vec3(fSign*15.0f, 0.0f, 50.0f - float(i)*25.0f);
		mModel = glm::translate(glm::mat4(1.0), vPos);
		mModel = glm::rotate(mModel, fGlobalAngle + i*30.0f, glm::vec3(0.0f, 1.0f, 0.0f));
		spDirectionalLight.SetUniform("normalMatrix", glm::transpose(glm::inverse(glm::mat3(mModel))));
		spDirectionalLight.SetUniform("modelViewMatrix", mView*mModel);
		glDrawArrays(GL_TRIANGLES, 42, iTorusFaces1 * 3);
	}

	tTextures[3].BindTexture();

	// Render "sun" :D

	mModel = glm::translate(glm::mat4(1.0), vSunPos);
	spDirectionalLight.SetUniform("modelViewMatrix", mView*mModel);
	spDirectionalLight.SetUniform("normalMatrix", glm::transpose(glm::inverse(glm::mat3(mModel))));

	// It must shine
	spDirectionalLight.SetUniform("sunLight.fAmbientIntensity", 0.8f);

	glDrawArrays(GL_TRIANGLES, 42 + iTorusFaces1 * 3, iTorusFaces2 * 3);

	fGlobalAngle += sof(100.0f*PIover180);
	cCamera.Update(this);

	if (Keys::Key(SDL_SCANCODE_LEFT))fSunAngle -= sof(45.0f);
	if (Keys::Key(SDL_SCANCODE_RIGHT))fSunAngle += sof(45.0f);
}
Ejemplo n.º 5
0
// Render method runs repeatedly in a loop
void Game::Render() 
{
	
	// Clear the buffers and enable depth testing (z-buffering)
	glClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glEnable(GL_DEPTH_TEST);

	// Set up a matrix stack
	glutil::MatrixStack modelViewMatrixStack;
	modelViewMatrixStack.SetIdentity();

	// Use the main shader program 
	CShaderProgram *pMainProgram = (*m_pShaderPrograms)[0];
	pMainProgram->UseProgram();
	pMainProgram->SetUniform("bUseTexture", false);
	pMainProgram->SetUniform("sampler0", 0);
	pMainProgram->SetUniform("CubeMapTex", 1);
	

	// Set the projection matrix
	pMainProgram->SetUniform("matrices.projMatrix", m_pCamera->GetPerspectiveProjectionMatrix());

	// Call LookAt to create the view matrix and put this on the modelViewMatrix stack. 
	// Store the view matrix and the normal matrix associated with the view matrix for later (they're useful for lighting -- since lighting is done in eye coordinates)
	modelViewMatrixStack.LookAt(m_pCamera->GetPosition(), m_pCamera->GetView(), m_pCamera->GetUpVector());
	glm::mat4 viewMatrix = modelViewMatrixStack.Top();
	glm::mat3 viewNormalMatrix = m_pCamera->ComputeNormalMatrix(viewMatrix);

	
	// Set light and materials in main shader program
	glm::vec4 lightPosition1 = glm::vec4(-100, 100, 50, 1); // Position of light source *in world coordinates*
	pMainProgram->SetUniform("light1.position", viewMatrix*lightPosition1); // Position of light source *in eye coordinates*
	pMainProgram->SetUniform("light1.La", glm::vec3(0.15f));		// Ambient colour of light
	pMainProgram->SetUniform("light1.Ld", glm::vec3(1.0f));		// Diffuse colour of light
	pMainProgram->SetUniform("light1.Ls", glm::vec3(1.0f));		// Specular colour of light
	pMainProgram->SetUniform("material1.Ma", glm::vec3(0.5f));	// Ambient material reflectance
	pMainProgram->SetUniform("material1.Md", glm::vec3(0.5f));	// Diffuse material reflectance
	pMainProgram->SetUniform("material1.Ms", glm::vec3(0.5f));	// Specular material reflectance
	pMainProgram->SetUniform("material1.shininess", 15.0f);		// Shininess material property
		


	// Render the grid
	modelViewMatrixStack.Push(); {
		pMainProgram->SetUniform("matrices.modelViewMatrix", modelViewMatrixStack.Top());
		pMainProgram->SetUniform("matrices.normalMatrix", m_pCamera->ComputeNormalMatrix(modelViewMatrixStack.Top()));
		m_pGrid->Render();
	} modelViewMatrixStack.Pop();

	
	// Switch to the sphere program
	CShaderProgram *pSphereProgram = (*m_pShaderPrograms)[2];
	pSphereProgram->UseProgram();

	// Set light and materials in sphere programme
	pSphereProgram->SetUniform("material1.Ma", glm::vec3(0.0f, 1.0f, 0.0f));
	pSphereProgram->SetUniform("material1.Md", glm::vec3(0.0f, 1.0f, 0.0f));
	pSphereProgram->SetUniform("material1.Ms", glm::vec3(1.0f, 1.0f, 1.0f));
	pSphereProgram->SetUniform("material1.shininess", 50.0f);
	pSphereProgram->SetUniform("light1.La", glm::vec3(0.15f, 0.15f, 0.15f));
	pSphereProgram->SetUniform("light1.Ld", glm::vec3(1.0f, 1.0f, 1.0f));
	pSphereProgram->SetUniform("light1.Ls", glm::vec3(1.0f, 1.0f, 1.0f));
	pSphereProgram->SetUniform("light1.position", viewMatrix*lightPosition1);
	pSphereProgram->SetUniform("t", m_t);
	pSphereProgram->SetUniform("iLevel", m_levels);

	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	// Render the sphere
	modelViewMatrixStack.Push(); {
		modelViewMatrixStack.Translate(glm::vec3(0.0f, 5.0f, 50.0f));
		modelViewMatrixStack.Scale(5.0f);
		pSphereProgram->SetUniform("matrices.projMatrix", m_pCamera->GetPerspectiveProjectionMatrix());
		pSphereProgram->SetUniform("matrices.modelViewMatrix", modelViewMatrixStack.Top());
		pSphereProgram->SetUniform("matrices.normalMatrix", m_pCamera->ComputeNormalMatrix(modelViewMatrixStack.Top()));
		m_pSphere->Render();
	} modelViewMatrixStack.Pop();

		
	// Draw the 2D graphics after the 3D graphics
	DisplayFrameRate();

	// Swap buffers to show the rendered image
	SwapBuffers(m_gameWindow.Hdc());		

}