Exemplo 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);
	}
}
Exemplo 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

}
// Initializes OpenGL features that will be used.
// lpParam - Pointer to anything you want.
void InitScene(LPVOID lpParam)
{
	//glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	//Новый цвет фона
	glClearColor(0.0f, 0.5f, 1.0f, 1.0f);

	// Setup triangle vertices
	fTriangle[0] = -0.9f; fTriangle[1] = 0.7f; fTriangle[2] = 0.0f;
	fTriangle[3] = -0.7f; fTriangle[4] = 0.9f; fTriangle[5] = 0.0f;
	fTriangle[6] = -0.5f; fTriangle[7] = 0.7f; fTriangle[8] = 0.0f;

	// Setup triangle color

	fTriangleColor[0] = 1.0f; fTriangleColor[1] = 0.0f; fTriangleColor[2] = 0.0f;
	fTriangleColor[3] = 0.0f; fTriangleColor[4] = 1.0f; fTriangleColor[5] = 0.0f;
	fTriangleColor[6] = 0.0f; fTriangleColor[7] = 0.0f; fTriangleColor[8] = 1.0f;
 
	// Setup quad vertices
 
	fQuad[0] = 0.7f; fQuad[1] = 0.9f; fQuad[2] = 0.0f;
	fQuad[3] = 0.7f; fQuad[4] = 0.6f; fQuad[5] = 0.0f;
	fQuad[6] = 0.9f; fQuad[7] = 0.9f; fQuad[8] = 0.0f;
	fQuad[9] = 0.9f; fQuad[10] = 0.6f; fQuad[11] = 0.0f;

	// Setup quad color

	fQuadColor[0] = 1.0f; fQuadColor[1] = 0.0f; fQuadColor[2] = 0.0f;
	fQuadColor[3] = 0.0f; fQuadColor[4] = 1.0f; fQuadColor[8] = 0.0f;
	fQuadColor[6] = 0.0f; fQuadColor[7] = 0.0f; fQuadColor[5] = 1.0f;
	fQuadColor[9] = 1.0f; fQuadColor[10] = 1.0f; fQuadColor[11] = 0.0f;

	// Setup duck vertices

	fDuck[0] = 0.0f; fDuck[1] = 0.8f;  fDuck[2] = 0.0f;
	fDuck[3] = -0.2f; fDuck[4] = 0.4f;  fDuck[5] = 0.0f;
	fDuck[6] = -0.2f; fDuck[7] = -0.3f;  fDuck[8] = 0.0f;
	fDuck[9] = -0.3f; fDuck[10] = -0.5f; fDuck[11] = 0.0f;
	fDuck[12] = -0.3f; fDuck[13] = -0.9f; fDuck[14] = 0.0f;
	fDuck[15] = -0.2f; fDuck[16] = -0.9f; fDuck[17] = 0.0f;
	fDuck[18] = -0.2f; fDuck[19] = -0.7f; fDuck[20] = 0.0f;
	fDuck[21] = -0.05f; fDuck[22] = -0.7f; fDuck[23] = 0.0f;
	fDuck[24] = -0.05f; fDuck[25] = -0.9f; fDuck[26] = 0.0f;
	fDuck[27] = 0.05f; fDuck[28] = -0.9f; fDuck[29] = 0.0f;
	fDuck[30] = 0.05f; fDuck[31] = -0.7f; fDuck[32] = 0.0f;
	fDuck[33] = 0.2f; fDuck[34] = -0.7f; fDuck[35] = 0.0f;
	fDuck[36] = 0.2f; fDuck[37] = -0.9f; fDuck[38] = 0.0f;
	fDuck[39] = 0.3f; fDuck[40] = -0.9f; fDuck[41] = 0.0f;
	fDuck[42] = 0.3f; fDuck[43] = -0.5f; fDuck[44] = 0.0f;
	fDuck[45] = 0.2f; fDuck[46] = -0.3f; fDuck[47] = 0.0f;
	fDuck[48] = 0.2f; fDuck[49] = 0.4f; fDuck[50] = 0.0f;
	fDuck[51] = 0.0f; fDuck[52] = 0.8f;  fDuck[53] = 0.0f;

	// Setup duck color

	fDuckColor[0] = 1.0f; fDuckColor[1] = 0.0f;  fDuckColor[2] = 0.0f;
	fDuckColor[3] = 1.0f; fDuckColor[4] = 1.0f;  fDuckColor[5] = 0.0f;
	fDuckColor[6] = 1.0f; fDuckColor[7] = 0.0f;  fDuckColor[8] = 1.0f;
	fDuckColor[9] = 1.0f; fDuckColor[10] = 0.0f; fDuckColor[11] = 0.0f;
	fDuckColor[12] = 1.0f; fDuckColor[13] = 1.0f; fDuckColor[14] = 0.0f;
	fDuckColor[15] = 1.0f; fDuckColor[16] = 0.0f; fDuckColor[17] = 1.0f;
	fDuckColor[18] = 1.0f; fDuckColor[19] = 0.0f; fDuckColor[20] = 0.0f;
	fDuckColor[21] = 1.0f; fDuckColor[22] = 1.0f; fDuckColor[23] = 0.0f;
	fDuckColor[24] = 1.0f; fDuckColor[25] = 0.0f; fDuckColor[26] = 1.0f;
	fDuckColor[27] = 1.0f; fDuckColor[28] = 0.0f; fDuckColor[29] = 0.0f;
	fDuckColor[30] = 1.0f; fDuckColor[31] = 1.0f; fDuckColor[32] = 0.0f;
	fDuckColor[33] = 0.2f; fDuckColor[34] = -0.7f; fDuckColor[35] = 0.0f;
	fDuckColor[36] = 0.2f; fDuckColor[37] = -0.9f; fDuckColor[38] = 0.0f;
	fDuckColor[39] = 0.3f; fDuckColor[40] = -0.9f; fDuckColor[41] = 0.0f;
	fDuckColor[42] = 0.3f; fDuckColor[43] = -0.5f; fDuckColor[44] = 0.0f;
	fDuckColor[45] = 0.2f; fDuckColor[46] = -0.3f; fDuckColor[47] = 0.0f;
	fDuckColor[48] = 0.2f; fDuckColor[49] = -0.3f; fDuckColor[50] = 0.0f;
	fDuckColor[51] = 1.0f; fDuckColor[52] = 0.0f;  fDuckColor[53] = 0.0f;

	// Setup duck vertices

	fMore[0] = 0.0f; fMore[1] = 0.0f;  fMore[2] = 0.0f;
	fMore[3] = 0.5f; fMore[4] = 0.0f;  fMore[5] = 0.0f;
	fMore[6] = 0.5f; fMore[7] = 0.5f;  fMore[8] = 0.0f;

	fMore[9] = 0.0f; fMore[10] = 0.0f; fMore[11] = 0.0f;
	fMore[12] = 0.0f; fMore[13] = 0.5f; fMore[14] = 0.0f;
	fMore[15] = -0.5f; fMore[16] = 0.5f; fMore[17] = 0.0f;

	fMore[18] = 0.0f; fMore[19] = 0.0f; fMore[20] = 0.0f;
	fMore[21] = -0.5f; fMore[22] = 0.0f; fMore[23] = 0.0f;
	fMore[24] = -0.5f; fMore[25] = -0.5f; fMore[26] = 0.0f;

	fMore[27] = 0.0f; fMore[28] = 0.0f; fMore[29] = 0.0f;
	fMore[30] = 0.0f; fMore[31] = -0.5f; fMore[32] = 0.0f;
	fMore[33] = 0.5f; fMore[34] = -0.5f; fMore[35] = 0.0f;

	for (int i = 0; i < 36; i++) {
		fMore[i] = fMore[i] * 0.4 - 0.6f;
	}

	// Setup duck color

	fMoreColor[0] = 1.0f; fMoreColor[1] = 1.0f;  fMoreColor[2] = 1.0f;
	fMoreColor[3] = 1.0f; fMoreColor[4] = 1.0f;  fMoreColor[5] = 1.0f;
	fMoreColor[6] = 1.0f; fMoreColor[7] = 1.0f;  fMoreColor[8] = 1.0f;
	fMoreColor[9] = 1.0f; fMoreColor[10] = 0.0f; fMoreColor[11] = 0.0f;
	fMoreColor[12] = 1.0f; fMoreColor[13] = 1.0f; fMoreColor[14] = 0.0f;
	fMoreColor[15] = 1.0f; fMoreColor[16] = 0.0f; fMoreColor[17] = 1.0f;
	fMoreColor[18] = 1.0f; fMoreColor[19] = 1.0f; fMoreColor[20] = 1.0f;
	fMoreColor[21] = 1.0f; fMoreColor[22] = 1.0f; fMoreColor[23] = 1.0f;
	fMoreColor[24] = 1.0f; fMoreColor[25] = 1.0f; fMoreColor[26] = 1.0f;
	fMoreColor[27] = 1.0f; fMoreColor[28] = 0.0f; fMoreColor[29] = 0.0f;
	fMoreColor[30] = 1.0f; fMoreColor[31] = 1.0f; fMoreColor[32] = 0.0f;
	fMoreColor[33] = 0.2f; fMoreColor[34] = -0.7f; fMoreColor[35] = 0.0f;

	glGenVertexArrays(4, uiVAO); // Generate two VAOs, one for triangle and one for quad
	glGenBuffers(8, uiVBO); // And four VBOs

	// Setup whole triangle
	glBindVertexArray(uiVAO[0]);

	glBindBuffer(GL_ARRAY_BUFFER, uiVBO[0]);
	glBufferData(GL_ARRAY_BUFFER, 9*sizeof(float), fTriangle, GL_STATIC_DRAW);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glBindBuffer(GL_ARRAY_BUFFER, uiVBO[1]);
	glBufferData(GL_ARRAY_BUFFER, 9*sizeof(float), fTriangleColor, GL_STATIC_DRAW);
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

	// Setup whole quad
	glBindVertexArray(uiVAO[1]);

	glBindBuffer(GL_ARRAY_BUFFER, uiVBO[2]);
	glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), fQuad, GL_STATIC_DRAW);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glBindBuffer(GL_ARRAY_BUFFER, uiVBO[3]);
	glBufferData(GL_ARRAY_BUFFER, 12*sizeof(float), fQuadColor, GL_STATIC_DRAW);
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

	// Setup whole quad
	glBindVertexArray(uiVAO[2]);

	glBindBuffer(GL_ARRAY_BUFFER, uiVBO[4]);
	glBufferData(GL_ARRAY_BUFFER, 54 * sizeof(float), fDuck, GL_STATIC_DRAW);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glBindBuffer(GL_ARRAY_BUFFER, uiVBO[5]);
	glBufferData(GL_ARRAY_BUFFER, 54 * sizeof(float), fDuckColor, GL_STATIC_DRAW);
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

	// Setup whole quad
	glBindVertexArray(uiVAO[3]);

	glBindBuffer(GL_ARRAY_BUFFER, uiVBO[6]);
	glBufferData(GL_ARRAY_BUFFER, 36 * sizeof(float), fMore, GL_STATIC_DRAW);
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, 0);

	glBindBuffer(GL_ARRAY_BUFFER, uiVBO[7]);
	glBufferData(GL_ARRAY_BUFFER, 36 * sizeof(float), fMoreColor, GL_STATIC_DRAW);
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 0, 0);

	// Load shaders and create shader program

	shVertex.LoadShader("data\\shaders\\shader.vert", GL_VERTEX_SHADER);
	shFragment.LoadShader("data\\shaders\\shader.frag", GL_FRAGMENT_SHADER);

	spMain.CreateProgram();
	spMain.AddShaderToProgram(&shVertex);
	spMain.AddShaderToProgram(&shFragment);

	spMain.LinkProgram();
	spMain.UseProgram();
}
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);
}
Exemplo n.º 6
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());		

}