Example #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);
	}
}
void LoadShaders()
{
	// Load shaders and create shader programs

	shShaders[0].LoadShader("data\\shaders\\tut09\\shader.vert", GL_VERTEX_SHADER);
	shShaders[1].LoadShader("data\\shaders\\tut09\\shader.frag", GL_FRAGMENT_SHADER);
	shShaders[2].LoadShader("data\\shaders\\common\\directionalLight.frag", GL_FRAGMENT_SHADER);
	shShaders[3].LoadShader("data\\shaders\\tut09\\ortho2D.vert", GL_VERTEX_SHADER);
	shShaders[4].LoadShader("data\\shaders\\tut09\\ortho2D.frag", GL_FRAGMENT_SHADER);
	shShaders[5].LoadShader("data\\shaders\\tut09\\font2D.frag", GL_FRAGMENT_SHADER);

	spDirectionalLight.CreateProgram();
		spDirectionalLight.AddShaderToProgram(&shShaders[0]);
		spDirectionalLight.AddShaderToProgram(&shShaders[1]);
		spDirectionalLight.AddShaderToProgram(&shShaders[2]);
	spDirectionalLight.LinkProgram();

	spOrtho2D.CreateProgram();
		spOrtho2D.AddShaderToProgram(&shShaders[3]);
		spOrtho2D.AddShaderToProgram(&shShaders[4]);
	spOrtho2D.LinkProgram();

	spFont2D.CreateProgram();
		spFont2D.AddShaderToProgram(&shShaders[3]);
		spFont2D.AddShaderToProgram(&shShaders[5]);
	spFont2D.LinkProgram();
}
void MainLoop(CPlatform * const  pPlatform)
{
	static float degrees[] = {0,0};
	CMatrix44 rot;	
	Transform transform(10);	

	//update the main application
	pPlatform->Tick();	
	WRender::ClearScreenBuffer(COLOR_BIT | DEPTH_BIT); //not really needed 
	transform.Push();
		program.SetMtx44(mt,transform.GetCurrentMatrix().data);
		WRender::Draw(WRender::TRIANGLE_STRIP, WRender::U_BYTE, sizeof(indices)/sizeof(unsigned char), 0);

		transform.Translate(0.2f,0.0,0.0);
		program.SetMtx44(mt,transform.GetCurrentMatrix().data);
		WRender::Draw(WRender::TRIANGLE_STRIP, WRender::U_BYTE, sizeof(indices)/sizeof(unsigned char), 0);
			

		transform.Push();				
			transform.Translate(-0.4f,0.0,0.0);
			rot.Identity();
			rot.Rotate(degrees[0],0,0,1);
			transform.ApplyTransform(rot);
			program.SetMtx44(mt,transform.GetCurrentMatrix().data);
			WRender::Draw(WRender::TRIANGLE_STRIP, WRender::U_BYTE, sizeof(indices)/sizeof(unsigned char), 0);

			transform.Push();
				transform.Translate(0.0,0.3f,0);
				program.SetMtx44(mt,transform.GetCurrentMatrix().data);
				WRender::Draw(WRender::TRIANGLE_STRIP, WRender::U_BYTE, sizeof(indices)/sizeof(unsigned char), 0);
			transform.Pop();
		transform.Pop();

		transform.Push();
			rot.Identity();
			rot.Rotate(degrees[1],0,0,1);
			transform.ApplyTransform(rot);
			transform.Translate(0.0,-0.3f,0.0);
			transform.ApplyTransform(rot);
			program.SetMtx44(mt,transform.GetCurrentMatrix().data);
			WRender::Draw(WRender::TRIANGLE_STRIP, WRender::U_BYTE, sizeof(indices)/sizeof(unsigned char), 0);
		transform.Pop();
	transform.Pop();
	//swap buffers
	pPlatform->UpdateBuffers();

	degrees[0] -= 360.0f*pPlatform->GetDT();
	degrees[1] += 180.0f*pPlatform->GetDT();
	if(degrees[0] < 0.0f)
		degrees[0] += 360.0f;
	if(degrees[1] > 360.0f)
		degrees[1] -= 360.0f;
}
void CSDLOpenGLWindow::ReleaseScene()
{
	spTextured.DeleteProgram();
	spColored.DeleteProgram();

	shFragmentTex.DeleteShader();
	shFragmentCol.DeleteShader();
	shVertexTex.DeleteShader();
	shVertexCol.DeleteShader();

	glDeleteVertexArrays(2, uiVAOs);
	vboSceneObjects.DeleteVBO();
	tBox.DeleteTexture();
	tBlueIce.DeleteTexture();
}
// Releases OpenGL scene.
// lpParam - Pointer to anything you want.
void ReleaseScene(LPVOID lpParam)
{
	spMain.DeleteProgram();

	shVertex.DeleteShader();
	shFragment.DeleteShader();
}
Example #6
0
void DrawGeometry(bool shadowPass)
{
	CMatrix44 rot;
	rot.Rotate(45,1,0,0);

	transform.Push();
	if(shadowPass == false)
		shadow_transform.Push();

	transforms.mvp = transforms.proj * transform.GetCurrentMatrix();
	WRender::UpdateBuffer(WRender::UNIFORM, WRender::DYNAMIC, ubo, sizeof(Transforms), (void*)&transforms, 0);				
	if(shadowPass == false)
		program.SetMtx44(uShadowMtx, shadow_transform.GetCurrentMatrix().data);
	//Plane
	WRender::BindVertexArrayObject(vaoPlane);
	WRender::DrawArray(WRender::TRIANGLES, 4*3, 0);

	transform.Pop();
	if(shadowPass == false)
		shadow_transform.Pop();
	
	transform.Push();
	if(shadowPass == false)
		shadow_transform.Push();
	
	transform.ApplyTransform(rot);
	transform.Translate(0, 5.0f, 0);
	if(shadowPass == false)
	{
		shadow_transform.ApplyTransform(rot);
		shadow_transform.Translate(0, 5.0f, 0);
	}
	transforms.mvp = transforms.proj * transform.GetCurrentMatrix();
	WRender::UpdateBuffer(WRender::UNIFORM, WRender::DYNAMIC, ubo, sizeof(Transforms), (void*)&transforms, 0);				
	if(shadowPass == false)
		program.SetMtx44(uShadowMtx, shadow_transform.GetCurrentMatrix().data);
	//Torus
	WRender::BindVertexArrayObject(vaoTorus);
	WRender::DrawArray(WRender::TRIANGLES, nTorusVertices, 0);

	transform.Pop();
	if(shadowPass == false)
		shadow_transform.Pop();
}
SharePtr<CMaterial> CComponentGraphic_GL::CreateMaterial(const TString &textureFileName)
{
    SharePtr<CMaterial> material = new CMaterial(true);
    material->GetRenderState()->SetBoolState(GL_DEPTH_TEST, true);
    material->GetRenderState()->SetBoolState(GL_ALPHA_TEST, true);
    material->GetRenderState()->SetAlphaFunc(GL_GREATER);
    material->GetRenderState()->SetAlphaRef(0);
    material->GetRenderState()->SetBoolState(GL_BLEND, true);
    material->GetRenderState()->SetBlendFuncSrcFactor(GL_SRC_ALPHA);
    material->GetRenderState()->SetBlendFuncTargetFactor(GL_ONE_MINUS_SRC_ALPHA);
    SharePtr<CShader> pVS = CResourceManager::GetInstance()->GetResource<CShader>(_T("PointTexShader.vs"), false);
    SharePtr<CShader> pPS = CResourceManager::GetInstance()->GetResource<CShader>(_T("PointTexShader.ps"), false);
    BEATS_ASSERT(pVS && pPS, _T("Load Shader Failed!"));
    CShaderProgram *program = CRenderManager::GetInstance()->GetShaderProgram(pVS->ID(), pPS->ID());
    material->GetRenderState()->SetShaderProgram(program->ID());
    SharePtr<CTexture> texture = CResourceManager::GetInstance()->GetResource<CTexture>(textureFileName, false);
    BEATS_ASSERT(texture, _T("Create Component Texture Failed!"));
    material->SetTexture(0, texture);
    return material;
}
Example #8
0
void CleanUp(void)
{	
	WRender::DeleteBuffer(ubo);
	DestroyShape(sphere);
	DestroyShape(cube);
	DestroyShape(box);
	WRender::DeleteTexture( cubemap_tex );	

	program_cube.CleanUp();

	glswShutdown();
}
Example #9
0
void Setup(CPlatform * const  pPlatform)
{	
	CShader vertexShader(CShader::VERT, &pVertexShader, 1);
	CShader fragmentShader(CShader::FRAG, &pFragmentShader, 1);
	CShaderProgram program;

	//setup the shaders
	program.Initialise();
	program.AddShader(&vertexShader);
	program.AddShader(&fragmentShader);
	program.Link();

	glGenVertexArrays(1,&vao);	//vertex array object
	glGenBuffers(1, &ab);		//array buffer

	//fill buffers
	glBindBuffer(GL_ARRAY_BUFFER, ab);
	glBufferData(GL_ARRAY_BUFFER, sizeof(vertices), vertices, GL_STATIC_DRAW);

	//bind vao and ste it's data
	glBindVertexArray(vao);
	//glBindBuffer(GL_ARRAY_BUFFER, ab);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float)*6, BUFFER_OFFSET(0)); //P
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float)*6, BUFFER_OFFSET(sizeof(float)*3)); //C	
	glEnableVertexAttribArray(0);
	glEnableVertexAttribArray(1);	

	//set rendering states
	glEnable(GL_CULL_FACE);
	glClearColor(0,0,0,0);
	program.Start(); //even when glDeleteProgram is called the program won't be deleted untilit7s out of use
}
Example #10
0
void Setup(CPlatform * const  pPlatform)
{
	CShader vertexShader(CShader::VERT, &pVertexShader, 1);
	CShader fragmentShader(CShader::FRAG, &pFragmentShader, 1);	

	// - - - - - - - - - - 
	//setup the shaders	
	program.Initialise();
	program.AddShader(&vertexShader);
	program.AddShader(&fragmentShader);
	program.Link();
	mt = program.GetUniformLocation("mt");
	
	// - - - - - - - - - - 
	// setup vertex buffers etc
	vao = WRender::CreateVertexArrayObject();
	eab = WRender::CreateBuffer(WRender::ELEMENTS, WRender::STATIC, sizeof(indices), indices);
	ab = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(vertices), vertices);
	
	WRender::BindVertexArrayObject(vao);
	WRender::BindBuffer(WRender::ELEMENTS, eab);
	WRender::VertexAttribute va[3] = {
		{ab, 0, 3, WRender::FLOAT, 0, sizeof(float)*6, 0, 0},
		{ab, 1, 3, WRender::FLOAT, 0, sizeof(float)*6, sizeof(float)*3, 0},
	};
	WRender::SetAttributeFormat( va, 2, 0);
	program.Start();
}
void CSDLOpenGLWindow::ReleaseScene()
{
	for (int i = 0; i < 4; i++)
		tTextures[i].DeleteTexture();

	spDirectionalLight.DeleteProgram();

	shFragment.DeleteShader();
	shVertex.DeleteShader();

	glDeleteVertexArrays(1, uiVAOs);
	vboSceneObjects.DeleteVBO();
}
void MainLoop(CPlatform * const  pPlatform)
{
	pPlatform->Tick();	
	if(pPlatform->GetKeyboard().keys[KB_SPACE].IsToggledPress())
	{
		if(++activeTexture >= MAX_TEXTURES)
			activeTexture = 0;
		WRender::BindTexture(tex[activeTexture]);
		d_printf("Texture Index %d, id %d\n",activeTexture,tex[activeTexture].handle);			
	}
	static float layer = 0.0f;
	program.SetFloat( "layer", layer );
	layer += 0.01f;
	if (layer > 1.0){
		layer -= 1.0f;
	}

	WRender::ClearScreenBuffer(COLOR_BIT | DEPTH_BIT); //not really needed 
	WRender::Draw(WRender::TRIANGLE_STRIP, WRender::U_BYTE, sizeof(indices)/sizeof(unsigned char), 0);		
	//swap buffers
	pPlatform->UpdateBuffers();
}
Example #13
0
void Setup(CPlatform * const  pPlatform)
{
	unsigned int got = 0;
	const char *pVertStr[2] = {0,0}, *pFragStr[2] = {0,0};
	float *pFrustumVertices = 0;
	float colour[] = {1.0f,0.0f,0.0f, 0.0f,1.0f,0.0f, 0.0f,0.0f,1.0f};
	int nFacets = 0;

	glswInit();
	glswSetPath( "../resources/", ".glsl" );
	WRender::SetClearColour(0,0,0,0);	
	WRender::EnableCulling(true);
	
	// - - - - - - - - - - - - - - - - - - - -
	// - - - - - - - - - - - - - - - - - - - -
	glswGetShadersAlt( "shaders.Shared+shaders.Skymap.Vertex", pVertStr, 2);
	glswGetShadersAlt( "shaders.Shared+shaders.Skymap.Fragment", pFragStr, 2);
	CShader vertexShader(CShader::VERT, pVertStr, 2);
	CShader fragmentShader(CShader::FRAG, pFragStr, 2);	
	//setup the shaders
	program_cube.Initialise();
	program_cube.AddShader(&vertexShader);
	program_cube.AddShader(&fragmentShader);	
	program_cube.Link();

	program_cube.Start();
	program_cube.SetTextureUnit("cube_map",0);

	// - - - - - - - - - - 
	//set up shapes
	box = CreateShapeCube();
	sphere = CreateShapeSphereNormals();	
	//sphere = CreateShapeSphere();	
	//sphere = CreateShapeCylinder();
	//sphere = CreateShapeCone();
	//sphere = CreateShapeTorus();

	// - - - - - - - - - -
	// set up cubemap
	CreateCubeMapTextures();

	//ubo for cameras etc
	ubo = WRender::CreateBuffer(WRender::UNIFORM, WRender::DYNAMIC, sizeof(Transforms), &transform);
	WRender::BindBufferToIndex(WRender::UNIFORM, ubo, 1);
	Transform::CreateProjectionMatrix(transforms.proj, -0.1f, 0.1f, -0.1f, 0.1f, 0.1f, 50.0f);
}
void CSDLOpenGLWindow::InitScene()
{
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	vboSceneObjects.CreateVBO();
	glGenVertexArrays(1, uiVAOs); // Create one VAO
	glBindVertexArray(uiVAOs[0]);

	vboSceneObjects.BindVBO();

	// Add cube to VBO

	for (int i = 0; i < 36; i++)
	{
		vboSceneObjects.AddData(&vCubeVertices[i], sizeof(glm::vec3));
		vboSceneObjects.AddData(&vCubeTexCoords[i % 6], sizeof(glm::vec2));
		vboSceneObjects.AddData(&vCubeNormals[i / 6], sizeof(glm::vec3));
	}

	// Add ground to VBO

	for (int i = 0; i < 6; i++)
	{
		vboSceneObjects.AddData(&vGround[i], sizeof(glm::vec3));
		vCubeTexCoords[i] *= 10.0f;
		vboSceneObjects.AddData(&vCubeTexCoords[i % 6], sizeof(glm::vec2));
		glm::vec3 vGroundNormal(0.0f, 1.0f, 0.0f);
		vboSceneObjects.AddData(&vGroundNormal, sizeof(glm::vec3));
	}

	// Add torus to VBO and remember number of faces (triangles) of this torus
	iTorusFaces1 = GenerateTorus(vboSceneObjects, 7.0f, 2.0f, 20, 20);
	// Add sun torus to VBO
	iTorusFaces2 = GenerateTorus(vboSceneObjects, 10.0f, 6.0f, 10, 10);

	vboSceneObjects.UploadDataToGPU(GL_STATIC_DRAW);

	// Vertex positions
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(glm::vec3) + sizeof(glm::vec2), 0);
	// Texture coordinates
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 2 * sizeof(glm::vec3) + sizeof(glm::vec2), (void*)sizeof(glm::vec3));
	// Normal vectors
	glEnableVertexAttribArray(2);
	glVertexAttribPointer(2, 3, GL_FLOAT, GL_FALSE, 2 * sizeof(glm::vec3) + sizeof(glm::vec2), (void*)(sizeof(glm::vec3) + sizeof(glm::vec2)));

	// Load shaders and create shader programs

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

	spDirectionalLight.CreateProgram();
		spDirectionalLight.AddShaderToProgram(&shVertex);
		spDirectionalLight.AddShaderToProgram(&shFragment);
		spDirectionalLight.AddShaderToProgram(&shDirLight);
	spDirectionalLight.LinkProgram();

	// Load textures

	std::string sTextureNames[] = { "ground.jpg", "box.jpg", "rust.jpg", "sun.jpg" };

	for (int i = 0; i < 4; i++)
	{
		tTextures[i].LoadTexture2D("data\\textures\\" + sTextureNames[i], true);
		tTextures[i].SetFiltering(TEXTURE_FILTER_MAG_BILINEAR, TEXTURE_FILTER_MIN_BILINEAR_MIPMAP);
	}

	glEnable(GL_DEPTH_TEST);
	glClearDepth(1.0);

	cCamera = CWalkingCamera(glm::vec3(0.0f, 3.0f, -20.0f), glm::vec3(0.0f, 3.0f, -19.0f), glm::vec3(0.0f, 1.0f, 0.0f), 35.0f);
}
Example #15
0
void MainLoop(CPlatform * const  pPlatform)
{	//update the main application
	CVec3df cameraPosition( 0.0f, 0.0f, distance );

	pPlatform->Tick();	
	WRender::ClearScreenBuffer(COLOR_BIT | DEPTH_BIT);
	WRender::EnableDepthTest(true);

	WRender::ActiveTexture(WRender::Texture::UNIT_0);
	WRender::BindTexture(cubemap_tex);

	transform.Push();			
	{
		CMatrix44 rotLat,rotLong,inverse_camera, model_matrix;

		rotLat.Rotate(latitude, 1, 0, 0);
		rotLong.Rotate(longitude, 0, 1, 0);

		transform.Translate(cameraPosition);
		transform.ApplyTransform(rotLat);
		transform.ApplyTransform(rotLong);			
		
		transform.Push();					
		{
			transform.Scale(10.0f,10.0f,10.0f);
			transforms.mv = transform.GetCurrentMatrix();
			transforms.mvp = transforms.proj * transforms.mv;
			CMatrix33 normal;

			normal = transforms.mv;
			normal = normal.Invert();
			normal = normal.Transpose();			
			transforms.nrm[ 0] = normal.x.x;			transforms.nrm[ 1] = normal.x.y;			transforms.nrm[ 2] = normal.x.z;
			transforms.nrm[ 4] = normal.y.x;			transforms.nrm[ 5] = normal.y.y;			transforms.nrm[ 6] = normal.y.z;
			transforms.nrm[ 8] = normal.z.x;			transforms.nrm[ 9] = normal.z.y;			transforms.nrm[10] = normal.z.z;

			WRender::UpdateBuffer(WRender::UNIFORM, WRender::DYNAMIC, ubo, sizeof(Transforms), (void*)&transforms, 0);				
			WRender::EnableCulling(true);
			WRender::CullMode(WRender::FRONT_FACE);
			program_cube.Start();			
			DrawShape(box);
		}
		transform.Pop();


		transform.Push();					
		{
			transform.Translate(0.0f, 0, 0);
			transforms.mv = transform.GetCurrentMatrix();
			transforms.mvp = transforms.proj * transforms.mv;
			CMatrix33 normal;

			normal = transforms.mv;
			normal = normal.Invert();
			normal = normal.Transpose();			
			transforms.nrm[ 0] = normal.x.x;			transforms.nrm[ 1] = normal.x.y;			transforms.nrm[ 2] = normal.x.z;
			transforms.nrm[ 4] = normal.y.x;			transforms.nrm[ 5] = normal.y.y;			transforms.nrm[ 6] = normal.y.z;
			transforms.nrm[ 8] = normal.z.x;			transforms.nrm[ 9] = normal.z.y;			transforms.nrm[10] = normal.z.z;

			WRender::UpdateBuffer(WRender::UNIFORM, WRender::DYNAMIC, ubo, sizeof(Transforms), (void*)&transforms, 0);				
			WRender::EnableCulling(true);			
			WRender::CullMode(WRender::BACK_FACE);
			program_cube.Start();
			DrawShape(sphere);
		}
		transform.Pop();		
		
	}
	transform.Pop();		

	pPlatform->UpdateBuffers();
	if (pPlatform->GetKeyboard().keys[KB_LEFTSHIFT].IsPressed()){
		if(pPlatform->GetKeyboard().keys[KB_UP].IsPressed())
			distance += 1.0f * pPlatform->GetDT();
		if(pPlatform->GetKeyboard().keys[KB_DOWN].IsPressed())
			distance -= 1.0f * pPlatform->GetDT();
	}else{
		if(pPlatform->GetKeyboard().keys[KB_UP].IsPressed())
			latitude += 90.0f * pPlatform->GetDT();
		if(pPlatform->GetKeyboard().keys[KB_DOWN].IsPressed())
			latitude -= 90.0f * pPlatform->GetDT();
		
		if(pPlatform->GetKeyboard().keys[KB_LEFT].IsPressed())//l
			longitude += 90.0f * pPlatform->GetDT();
		if(pPlatform->GetKeyboard().keys[KB_RIGHT].IsPressed())//r
			longitude -= 90.0f * pPlatform->GetDT();
	}
}
Example #16
0
void Setup(CPlatform * const  pPlatform)
{
	//for the shapes
	unsigned int nTorusFloats;
	float *pTorusVertices = 0;
	float *pPlaneVertices = 0;	
	float colour[] = {1.0f,0.0f,0.0f, 0.0f,1.0f,0.0f, 0.0f,0.0f,1.0f};
	unsigned int torusSegments = 36, torusTubeSegments = 36;

	//for the shaders
	const char *pVertStr[3] = {0,0,0}, *pFragStr = 0;
	const char *pFragTexStr = 0, *pVertTexStr = 0;

	//fbo stuff
	WRender::Texture::SDescriptor descDepth = {WRender::Texture::TEX_2D, WRender::Texture::DEPTH_COMPONENT, TEX_DIMENSIONS, TEX_DIMENSIONS, 0, 0, WRender::Texture::DONT_GEN_MIPMAP};
	WRender::Texture::SParam param[] ={	
		{ WRender::Texture::MIN_FILTER, WRender::Texture::LINEAR},
		{ WRender::Texture::MAG_FILTER, WRender::Texture::LINEAR},
		{ WRender::Texture::WRAP_S, WRender::Texture::CLAMP_TO_EDGE},
		{ WRender::Texture::WRAP_T, WRender::Texture::CLAMP_TO_EDGE},
	};	
	
	glswInit();
	glswSetPath("../resources/", ".glsl");
	WRender::SetClearColour(0,0,0,0);	
	WRender::EnableCulling(true);
	// - - - - - - - - - - 
	//setup the shaders
	// - - - - - - - - - - 

	//normal shader
	glswGetShadersAlt("shaders.Version+shaders.Shared+shaders.SingleShadow.Vertex", pVertStr, 3);
	pFragStr = glswGetShaders("shaders.Version+shaders.SingleShadow.Fragment");
	CShader vertexShader(CShader::VERT, pVertStr, 3);
	CShader fragmentShader(CShader::FRAG, &pFragStr, 1);
	
	program.Initialise();
	program.AddShader(&vertexShader);
	program.AddShader(&fragmentShader);
	program.Link();
	uShadowMtx = program.GetUniformLocation("shadowMtx");
	program.Start();
	program.SetTextureUnit("shadowMap", 0);
	program.Stop();

	//debug shader for textures in screen space
	pVertTexStr = glswGetShaders("shaders.Version+shaders.Dbg.ScreenSpaceTexture.Vertex");
	pFragTexStr = glswGetShaders("shaders.Version+shaders.Dbg.ScreenSpaceTexture.Fragment");
	CShader vTexShader(CShader::VERT, &pVertTexStr, 1);
	CShader fTexShader(CShader::FRAG, &pFragTexStr, 1);	

	textureShader.Initialise();
	textureShader.AddShader(&vTexShader);
	textureShader.AddShader(&fTexShader);
	textureShader.Link();
	textureShader.Start();
	textureShader.SetTextureUnit("texture",0);
	textureShader.Stop();

	// - - - - - - - - - - 
	//set up shapes
	// - - - - - - - - - - 
	//shared colours
	abColour = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(colour), colour);

	//Torus
	nTorusVertices = 2*torusTubeSegments*torusSegments*3;
	nTorusFloats = nTorusVertices*3;
	pTorusVertices = new float[nTorusFloats];
	CreateTorus(pTorusVertices, torusSegments, 3.0f, torusTubeSegments, 1.0f);	
	vaoTorus = WRender::CreateVertexArrayObject();
	abTorus = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(float)*nTorusFloats, pTorusVertices);	

	WRender::BindVertexArrayObject(vaoTorus);
	WRender::VertexAttribute vaTorus[2] = {
		{abTorus, 0, 3, WRender::FLOAT, 0, sizeof(float)*3, 0, 0},
		{abColour, 1, 3, WRender::FLOAT, 0, sizeof(float)*9, sizeof(float)*6, 1},
	};
	WRender::SetAttributeFormat( vaTorus, 2, 0);
	delete[] pTorusVertices;

	//Plane
	pPlaneVertices = new float[4*3*3];
	CreatePlane(pPlaneVertices, 20.0f, 20.0f);
	vaoPlane = WRender::CreateVertexArrayObject();
	abPlane = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(float)*4*3*3, pPlaneVertices);	

	WRender::BindVertexArrayObject(vaoPlane);
	WRender::VertexAttribute vaFrustum[2] = {
		{abPlane, 0, 3, WRender::FLOAT, 0, sizeof(float)*3, 0, 0},
		{abColour, 1, 3, WRender::FLOAT, 0, sizeof(float)*9, sizeof(float)*3, 1},
	};
	WRender::SetAttributeFormat( vaFrustum, 2, 0);
	delete[] pPlaneVertices;

	//for screen aligned texture
	sqVao = WRender::CreateVertexArrayObject();	
	sqEab = WRender::CreateBuffer(WRender::ELEMENTS, WRender::STATIC, sizeof(sqIndices), sqIndices);
	sqAb = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(sqVertices), sqVertices);
	WRender::BindVertexArrayObject(sqVao);
	WRender::BindBuffer(WRender::ELEMENTS, sqEab);
	WRender::VertexAttribute sqVa[2] = {
		{sqAb, 0, 3, WRender::FLOAT, 0, sizeof(float)*5, 0, 0},				//vertices
		{sqAb, 1, 2, WRender::FLOAT, 0, sizeof(float)*5, sizeof(float)*3, 0},	//texture coordinates
	};
	WRender::SetAttributeFormat( sqVa, 2, 0);
	WRender::UnbindVertexArrayObject();

	// - - - - - - - - - - 
	//ubo for cameras etc
	// - - - - - - - - - - 
	ubo = WRender::CreateBuffer(WRender::UNIFORM, WRender::DYNAMIC, sizeof(Transforms), &transform);
	WRender::BindBufferToIndex(WRender::UNIFORM, ubo, 1);

	//create the texture and FBO for rendering to when drawing 
	//during shadow stage
	WRender::CreateBaseTexture(depthTexture, descDepth);	
	WRender::SetTextureParams(depthTexture,param,4);

	WRender::CreateFrameBuffer(fbo);
	WRender::AddTextureRenderBuffer(fbo, depthTexture, WRender::ATT_DEPTH, 0);
	WRender::BindFrameBuffer(WRender::FrameBuffer::DRAW, fbo);
	WRender::SetDrawBuffer(WRender::DB_NONE);
	WRender::CheckFrameBuffer(fbo);

	//set the projection matrix
	Transform::CreateProjectionMatrix(transforms.proj, -1.0f, 1.0f, -1.0f, 1.0f, 1.0f, 50.0f);
	WRender::BindTexture(depthTexture,WRender::Texture::UNIT_0);	
}
Example #17
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());		

}
Example #18
0
void MainLoop(CPlatform * const  pPlatform)
{
	float tranD[3] = {0.0f, -1.0f, 0};				
	//update the main application
	pPlatform->Tick();	
	WRender::ClearScreenBuffer(COLOR_BIT | DEPTH_BIT);
	WRender::EnableDepthTest(true);		
	program.Start();

	//Draw from light perspective
	//glPolygonOffset( 2.5f, 25.0f );
	//glEnable( GL_POLYGON_OFFSET_FILL);
	transform.Push();			
	{
		WRender::SetupViewport(0, 0, TEX_DIMENSIONS, TEX_DIMENSIONS);	//adjust viewport
		WRender::BindFrameBuffer(WRender::FrameBuffer::DRAW, fbo);		//set fbo 
		WRender::ClearScreenBuffer(COLOR_BIT | DEPTH_BIT);				//clear draw buffers (must be called after we set the fbo)

		rotLat.Identity();	rotLat.Rotate(lightLatitude, 1, 0, 0);
		rotLong.Identity();	rotLong.Rotate(lightLongitude, 0, 1, 0);

		transform.Translate(cameraPosition);			
		transform.ApplyTransform(rotLat);
		transform.ApplyTransform(rotLong);

		//setup shadow mvp
			
		shadow_transform.Identity();
		shadow_transform.Translate(0.5f, 0.5f, 0.5f);
		shadow_transform.Scale(0.5f,0.5f,0.5f);
		shadow_transform.ApplyTransform(transforms.proj);
		shadow_transform.ApplyTransform(transform.GetCurrentMatrix());
		DrawGeometry(true);

		WRender::UnbindFrameBuffer(WRender::FrameBuffer::DRAW); //reset draw buffer
		WRender::SetupViewport(0, 0, 640, 640);	//reset viewport			
	}
	//glDisable( GL_POLYGON_OFFSET_FILL);
	transform.Pop();		

	//draw scene
	transform.Push();			
	{
		rotLat.Identity();	rotLat.Rotate(latitude, 1, 0, 0);
		rotLong.Identity();	rotLong.Rotate(longitude, 0, 1, 0);

		transform.Translate(cameraPosition);
		transform.ApplyTransform(rotLat);
		transform.ApplyTransform(rotLong);
		DrawGeometry(false);			
	}
	transform.Pop();		

	//render depth texture to screen
	textureShader.Start();
	WRender::BindVertexArrayObject(sqVao);					
	textureShader.SetVec3("translate",tranD);		
	WRender::Draw(WRender::TRIANGLE_STRIP, WRender::U_BYTE, sizeof(sqIndices)/sizeof(unsigned char), 0);		
		
	//update Keyboard
	pPlatform->UpdateBuffers();
	if(!pPlatform->GetKeyboard().keys[KB_LEFTSHIFT].IsPressed())
	{	pLat = &latitude; pLong = &longitude;}
	else
	{	pLat = &lightLatitude; pLong = &lightLongitude;}

	if(pPlatform->GetKeyboard().keys[KB_UP].IsPressed())
		*pLat += 90.0f * pPlatform->GetDT();
	if(pPlatform->GetKeyboard().keys[KB_DOWN].IsPressed())
		*pLat -= 90.0f * pPlatform->GetDT();
	if(pPlatform->GetKeyboard().keys[KB_LEFT].IsPressed())//l
		*pLong += 90.0f * pPlatform->GetDT();
	if(pPlatform->GetKeyboard().keys[KB_RIGHT].IsPressed())//r
		*pLong -= 90.0f * pPlatform->GetDT();		

}
////////////////////////////////////////////////////////////////////////////////
///
/// \brief Main function
///
/// This is the entrance point for program startup.
///
/// \param  argc number of given arguments
/// \param  argv array, storing the arguments
/// \return exit code
///
///////////////////////////////////////////////////////////////////////////////
int main(int argc, char *argv[])
{
    if (argc != 2)
    {
        usage();
        return EXIT_FAILURE;
    }
    std::string strDataPath = argv[1];
    
    Log.setColourScheme(LOG_COLOUR_SCHEME_ONBLACK);
    
    //--- Init Graphics ------------------------------------------------------//
    CGraphics& Graphics = CGraphics::getInstance();
    WindowHandleType* pWindow = new WindowHandleType(sf::VideoMode(Graphics.getWidthScr(), Graphics.getHeightScr()),
                                                    "Planeworld - GL Font Rendering Test", sf::Style::Default,
                                                    sf::ContextSettings(24,8,4,4,5,sf::ContextSettings::Core));
    MEM_ALLOC("WindowHandleType")
    
    Graphics.setWindow(pWindow);
    
    //--- Init ---------------------------------------------------------------//
    CShader VertexShaderFont;
    CShader FragmentShaderFont;
    CShaderProgram ShaderProgramFont;
    CRenderMode RenderModeFont;
    
    VertexShaderFont.load(strDataPath+"/shader/font.vert", GL_VERTEX_SHADER);
    FragmentShaderFont.load(strDataPath+"/shader/font.frag", GL_FRAGMENT_SHADER);
    
    ShaderProgramFont.create(VertexShaderFont, FragmentShaderFont);
    RenderModeFont.setShaderProgram(&ShaderProgramFont);
    RenderModeFont.setRenderModeType(RenderModeType::VERT3COL4TEX2);
    
    Graphics.registerRenderMode("font", &RenderModeFont);
    
    RenderModeFont.use();

    CFontManager FontManager;
        
    FontManager.addFont("anka_c87_r", strDataPath+"/fonts/AnkaCoder-C87-r.ttf");
    FontManager.setFont("anka_c87_r");
    
    Graphics.init();
    Graphics.setupScreenSpace();
    
    const std::vector<float> vecUVs =
    {
        0, 1,
        1, 1,
        0, 0,
        1, 0
    };
    
    //--- Main Event Loop ----------------------------------------------------//
    CTimer TimerMain; TimerMain.start();
    bool bDone = false;
    sf::Event Event;
    
    Graphics.setColor({{1.0, 1.0, 1.0, 1.0}});
    
    while (!bDone)
    {
        while (pWindow->pollEvent(Event))
        {
            switch (Event.type)
            {
                case sf::Event::KeyPressed:
                    if (Event.key.code == sf::Keyboard::Escape)
                        bDone = true;
                    break;
                case sf::Event::Closed:
                {
                    bDone = true;
                    break;
                }
                default:
                    break;
            }
        }
        
        Graphics.beginRenderBatch("font");
            FontManager.setFont("anka_c87_r");
            Graphics.setColor({{0.0, 0.0, 1.0, 1.0}});
            Graphics.texturedRect(Vector2d(0.0, FONT_MGR_ATLAS_SIZE_DEFAULT*FONT_MGR_SCALE),
                                    Vector2d(FONT_MGR_ATLAS_SIZE_DEFAULT*FONT_MGR_SCALE, 0.0),
                                    &vecUVs);
            Graphics.setColor({{0.0, 1.0, 0.0, 1.0}});
            FontManager.drawText("You should see a blue texture atlas above (ASCII characters).", 300.0f, 200.0f);
            FontManager.drawText("This notice is green, also regular font face.", 300.0f, 220.0f);
        Graphics.endRenderBatch();
        
        Graphics.swapBuffers();
        TimerMain.sleepRemaining(60.0);
    }

    //--- Clean up -----------------------------------------------------------//
    if (pWindow != nullptr)
    {
        delete pWindow;
        MEM_FREED("WindowHandleType")
        pWindow = nullptr;
    }
    return EXIT_SUCCESS;
}
// 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();
}
Example #21
0
// Initialisation:  This method only runs once at startup
void Game::Initialise() 
{
	// Set the clear colour and depth
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClearDepth(1.0f);

	/// Create objects
	m_pCamera = new CCamera;
	m_pSkybox = new CSkybox;
	m_pShaderPrograms = new vector <CShaderProgram *>;
	m_pPlanarTerrain = new CPlane;
	m_pFtFont = new CFreeTypeFont;
	m_pBarrelMesh = new COpenAssetImportMesh;
	m_pHorseMesh = new COpenAssetImportMesh;
	m_pSphere = new CSphere;
	m_pAudio = new CAudio;
	m_pGrid = new CGrid;

	RECT dimensions = m_gameWindow.GetDimensions();

	int width = dimensions.right - dimensions.left;
	int height = dimensions.bottom - dimensions.top;

	// Set the orthographic and perspective projection matrices based on the image size
	m_pCamera->SetOrthographicProjectionMatrix(width, height); 
	m_pCamera->SetPerspectiveProjectionMatrix(45.0f, (float) width / (float) height, 0.5f, 5000.0f);

	// Load shaders
	vector<CShader> shShaders;
	vector<string> sShaderFileNames;
	sShaderFileNames.push_back("mainShader.vert");
	sShaderFileNames.push_back("mainShader.frag");
	sShaderFileNames.push_back("textShader.vert");
	sShaderFileNames.push_back("textShader.frag");
	sShaderFileNames.push_back("sphereShader.vert");
	sShaderFileNames.push_back("sphereShader.frag");


	for (int i = 0; i < (int) sShaderFileNames.size(); i++) {
		string sExt = sShaderFileNames[i].substr((int) sShaderFileNames[i].size()-4, 4);
		int iShaderType;
		if (sExt == "vert") iShaderType = GL_VERTEX_SHADER;
		else if (sExt == "frag") iShaderType = GL_FRAGMENT_SHADER;
		else if (sExt == "geom") iShaderType = GL_GEOMETRY_SHADER;
		else if (sExt == "tcnl") iShaderType = GL_TESS_CONTROL_SHADER;
		else iShaderType = GL_TESS_EVALUATION_SHADER;
		CShader shader;
		shader.LoadShader("resources\\shaders\\"+sShaderFileNames[i], iShaderType);
		shShaders.push_back(shader);
	}

	// Create the main shader program
	CShaderProgram *pMainProgram = new CShaderProgram;
	pMainProgram->CreateProgram();
	pMainProgram->AddShaderToProgram(&shShaders[0]);
	pMainProgram->AddShaderToProgram(&shShaders[1]);
	pMainProgram->LinkProgram();
	m_pShaderPrograms->push_back(pMainProgram);

	// Create a shader program for fonts
	CShaderProgram *pFontProgram = new CShaderProgram;
	pFontProgram->CreateProgram();
	pFontProgram->AddShaderToProgram(&shShaders[2]);
	pFontProgram->AddShaderToProgram(&shShaders[3]);
	pFontProgram->LinkProgram();
	m_pShaderPrograms->push_back(pFontProgram);

	// Create the sphere shader program
	CShaderProgram *pSphereProgram = new CShaderProgram;
	pSphereProgram->CreateProgram();
	pSphereProgram->AddShaderToProgram(&shShaders[4]);
	pSphereProgram->AddShaderToProgram(&shShaders[5]);
	pSphereProgram->LinkProgram();
	m_pShaderPrograms->push_back(pSphereProgram);


	// Create the skybox
	// Skybox downloaded from http://www.akimbo.in/forum/viewtopic.php?f=10&t=9
	m_pSkybox->Create(2500.0f);
	
	// Create the planar terrain
	m_pPlanarTerrain->Create("resources\\textures\\", "grassfloor01.jpg", 2000.0f, 2000.0f, 50.0f); // Texture downloaded from http://www.psionicgames.com/?page_id=26 on 24 Jan 2013

	m_pFtFont->LoadSystemFont("arial.ttf", 32);
	m_pFtFont->SetShaderProgram(pFontProgram);

	// Load some meshes in OBJ format
	m_pBarrelMesh->Load("resources\\models\\Barrel\\Barrel02.obj");  // Downloaded from http://www.psionicgames.com/?page_id=24 on 24 Jan 2013
	m_pHorseMesh->Load("resources\\models\\Horse\\Horse2.obj");  // Downloaded from http://opengameart.org/content/horse-lowpoly on 24 Jan 2013

	// Create a sphere
	m_pSphere->Create("resources\\textures\\", "dirtpile01.jpg", 25, 25);  // Texture downloaded from http://www.psionicgames.com/?page_id=26 on 24 Jan 2013
	glEnable(GL_CULL_FACE);

	// Initialise audio and play background music
	m_pAudio->Initialise();
	m_pAudio->LoadEventSound("Resources\\Audio\\Boing.wav");					// Royalty free sound from freesound.org
	m_pAudio->LoadMusicStream("Resources\\Audio\\DST-Garote.mp3");	// Royalty free music from http://www.nosoapradio.us/
	m_pAudio->PlayMusicStream();

	// Create the grid
	m_pGrid->Create(500, 500, 15);
}
void CSDLOpenGLWindow::InitScene()
{
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

	vboSceneObjects.CreateVBO();
	glGenVertexArrays(2, uiVAOs); // Create one VAO
	glBindVertexArray(uiVAOs[0]);

	vboSceneObjects.BindVBO();

	// Add cube to VBO

	for (int i = 0; i < 36; i++)
	{
		vboSceneObjects.AddData(&vCubeVertices[i], sizeof(glm::vec3));
		vboSceneObjects.AddData(&vCubeTexCoords[i % 6], sizeof(glm::vec2));
	}

	// Add ground to VBO

	for (int i = 0; i < 6; i++)
	{
		vboSceneObjects.AddData(&vGround[i], sizeof(glm::vec3));
		vCubeTexCoords[i] *= 10.0f;
		vboSceneObjects.AddData(&vCubeTexCoords[i % 6], sizeof(glm::vec2));
	}

	vboSceneObjects.UploadDataToGPU(GL_STATIC_DRAW);

	// Vertex positions
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3) + sizeof(glm::vec2), 0);
	// Texture coordinates
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, sizeof(glm::vec3) + sizeof(glm::vec2), (void*)sizeof(glm::vec3));

	// Setup another VAO for untextured objects

	glBindVertexArray(uiVAOs[1]);
	// Here enable only first vertex attribute - position (no texture coordinates)
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3) + sizeof(glm::vec2), 0);

	// Load shaders and create shader programs

	shVertexTex.LoadShader("data\\shaders\\tut07\\shaderTex.vert", GL_VERTEX_SHADER);
	shFragmentTex.LoadShader("data\\shaders\\tut07\\shaderTex.frag", GL_FRAGMENT_SHADER);
	shVertexCol.LoadShader("data\\shaders\\tut07\\shaderCol.vert", GL_VERTEX_SHADER);
	shFragmentCol.LoadShader("data\\shaders\\tut07\\shaderCol.frag", GL_FRAGMENT_SHADER);

	spTextured.CreateProgram();
		spTextured.AddShaderToProgram(&shVertexTex);
		spTextured.AddShaderToProgram(&shFragmentTex);
	spTextured.LinkProgram();

	spColored.CreateProgram();
		spColored.AddShaderToProgram(&shVertexCol);
		spColored.AddShaderToProgram(&shFragmentCol);
	spColored.LinkProgram();

	tBlueIce.LoadTexture2D("data\\textures\\blueice.jpg", true);
	tBlueIce.SetFiltering(TEXTURE_FILTER_MAG_BILINEAR, TEXTURE_FILTER_MIN_BILINEAR_MIPMAP);

	tBox.LoadTexture2D("data\\textures\\box.jpg", true);
	tBox.SetFiltering(TEXTURE_FILTER_MAG_BILINEAR, TEXTURE_FILTER_MIN_BILINEAR_MIPMAP);

	glEnable(GL_DEPTH_TEST);
	glClearDepth(1.0);
	glClearColor(0.2f, 0.32f, 0.5f, 1.0f);

	cCamera = CWalkingCamera(glm::vec3(0.0f, 0.0f, -20.0f), glm::vec3(0.0f, 0.0f, -19.0f), glm::vec3(0.0f, 1.0f, 0.0f), 15.0f);
}
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);
}
Example #24
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);

	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);
}
void Setup(CPlatform * const  pPlatform)
{	
	int utex = 0;
	unsigned char * pTexture = 0;
	WRender::Texture::SDescriptor desc = {WRender::Texture::TEX_3D, WRender::Texture::RGB8, WIDTH, HEIGHT, DEPTH, 0, WRender::Texture::DONT_GEN_MIPMAP};
	WRender::Texture::SParam param[] ={
		//tex 0
		{ WRender::Texture::MIN_FILTER, WRender::Texture::NEAREST},
		{ WRender::Texture::MAG_FILTER, WRender::Texture::NEAREST},
		{ WRender::Texture::WRAP_S, WRender::Texture::REPEAT},
		{ WRender::Texture::WRAP_T, WRender::Texture::REPEAT},
		// tex 1
		{ WRender::Texture::MIN_FILTER, WRender::Texture::LINEAR},
		{ WRender::Texture::MAG_FILTER, WRender::Texture::LINEAR},
		{ WRender::Texture::WRAP_S, WRender::Texture::REPEAT},
		{ WRender::Texture::WRAP_T, WRender::Texture::REPEAT},
		// tex 2
		{ WRender::Texture::MIN_FILTER, WRender::Texture::LINEAR},
		{ WRender::Texture::MAG_FILTER, WRender::Texture::LINEAR},
		{ WRender::Texture::WRAP_S, WRender::Texture::REPEAT},
		{ WRender::Texture::WRAP_T, WRender::Texture::CLAMP_TO_EDGE},
		// tex 3
		{ WRender::Texture::MIN_FILTER, WRender::Texture::LINEAR},
		{ WRender::Texture::MAG_FILTER, WRender::Texture::LINEAR},
		{ WRender::Texture::WRAP_S, WRender::Texture::CLAMP_TO_EDGE},
		{ WRender::Texture::WRAP_T, WRender::Texture::CLAMP_TO_EDGE},
		// tex 4
		{ WRender::Texture::MIN_FILTER, WRender::Texture::NEAREST},
		{ WRender::Texture::MAG_FILTER, WRender::Texture::NEAREST},
		{ WRender::Texture::WRAP_S, WRender::Texture::CLAMP_TO_EDGE},
		{ WRender::Texture::WRAP_T, WRender::Texture::CLAMP_TO_EDGE},
	};
	CShader vertexShader(CShader::VERT, &pVertexShader, 1);
	CShader fragmentShader(CShader::FRAG, &pFragmentShader, 1);	
	WRender::Pixel::Data pixelData = {WRender::Pixel::RGB, WRender::Pixel::UCHAR, 0, 0};

	// - - - - - - - - - - 
	// create textures
	pTexture = new unsigned char[WIDTH * HEIGHT * DEPTH * 3];
	for(unsigned int d=0;d<DEPTH;d++)
		for(unsigned int h=0;h<HEIGHT;h++)
			for(unsigned int w=0;w<WIDTH;w++)			
			{
				int depth_index = d*HEIGHT*WIDTH*3;
				int height_index = h*WIDTH*3;
				int width_index = w*3;

				pTexture[depth_index + height_index + width_index + 0] = (255*w)/(WIDTH);			//R
				pTexture[depth_index + height_index + width_index + 1] = (255*h)/(HEIGHT);			//G
				pTexture[depth_index + height_index + width_index + 2] = (255*d)/(DEPTH);//(255*(w+h))/(WIDTH+HEIGHT);	//B
			}	
	pixelData.pData = (void*)pTexture;
	
	WRender::ActiveTexture(WRender::Texture::UNIT_4);
	//tex 0
	if(WRender::CreateBaseTexture(tex[0], desc))
	{		
		WRender::UpdateTextureData(tex[0], pixelData);
		WRender::SetTextureParams(tex[0], param, 4);		
	}
	//tex 1
	if(WRender::CreateBaseTextureData(tex[1], desc, pixelData))
		WRender::SetTextureParams(tex[1], param+4, 4);		
	//tex 2
	if(WRender::CreateBaseTextureData(tex[2], desc, pixelData))
		WRender::SetTextureParams(tex[2], param+8, 4);		

	//tex 3
	if(WRender::CreateBaseTextureData(tex[3], desc, pixelData))
		WRender::SetTextureParams(tex[3], param+12, 4);		

	//tex 4
	if(WRender::CreateBaseTextureData(tex[4], desc, pixelData))
		WRender::SetTextureParams(tex[4], param+16, 4);		
	delete [] pTexture;	

	// - - - - - - - - - - 
	//setup the shaders
	program.Initialise();
	program.AddShader(&vertexShader);
	program.AddShader(&fragmentShader);
	program.Link();
	utex = program.GetUniformLocation("tex");

	// - - - - - - - - - - 
	// setup vertex buffers etc
	vao = WRender::CreateVertexArrayObject();
	eab = WRender::CreateBuffer(WRender::ELEMENTS, WRender::STATIC, sizeof(indices), indices);
	ab = WRender::CreateBuffer(WRender::ARRAY, WRender::STATIC, sizeof(vertices), vertices);
	
	WRender::BindVertexArrayObject(vao);
	WRender::BindBuffer(WRender::ELEMENTS, eab);
	WRender::VertexAttribute va[] = {
		{ab, 0, 3, WRender::FLOAT, 0, sizeof(float)*5, 0, 0},
		{ab, 3, 2, WRender::FLOAT, 0, sizeof(float)*5, sizeof(float)*3, 0}
	};
	WRender::SetAttributeFormat( va, 2, 0);
	program.Start();
	//program.SetTextureUnit(utex, 4);
	glEnable(GL_CULL_FACE);
	WRender::BindTexture(tex[activeTexture]);	

}
Example #27
0
// Initialisation:  This method only runs once at startup
void Game::Initialise() 
{
	// Set the clear colour and depth
	glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
	glClearDepth(1.0f);

	/// Create objects
	m_pCamera = new CCamera;
	m_pSkybox = new CSkybox;
	m_pShaderPrograms = new vector <CShaderProgram *>;
    m_pSphere = new CSphere;
	m_pGrid = new CGrid;
#ifdef _WIN32
	m_pFtFont = new CFreeTypeFont;
	RECT dimensions = m_gameWindow.GetDimensions();
	m_width = dimensions.right - dimensions.left;
	m_height = dimensions.bottom - dimensions.top;
#endif

	// Set the orthographic and perspective projection matrices based on the image size
	m_pCamera->SetOrthographicProjectionMatrix(m_width, m_height);
	m_pCamera->SetPerspectiveProjectionMatrix(45.0f, (float) m_width / (float) m_height, 0.5f, 5000.0f);

	// Load shaders
	vector<CShader> shShaders;
	vector<string> sShaderFileNames;
	sShaderFileNames.push_back("mainShader.vert");
	sShaderFileNames.push_back("mainShader.frag");
	sShaderFileNames.push_back("textShader.vert");
	sShaderFileNames.push_back("textShader.frag");
	sShaderFileNames.push_back("sphereShader.vert");
	sShaderFileNames.push_back("sphereShader.frag");

	for (int i = 0; i < (int) sShaderFileNames.size(); i++) {
		string sExt = sShaderFileNames[i].substr((int) sShaderFileNames[i].size()-4, 4);
		int iShaderType;
		if (sExt == "vert") iShaderType = GL_VERTEX_SHADER;
		else if (sExt == "frag") iShaderType = GL_FRAGMENT_SHADER;
		else if (sExt == "geom") iShaderType = GL_GEOMETRY_SHADER;
		else if (sExt == "tcnl") iShaderType = GL_TESS_CONTROL_SHADER;
		else iShaderType = GL_TESS_EVALUATION_SHADER;
		CShader shader;
        std::string shaderFile = resourcePath(sShaderFileNames[i], "shaders", "");
		shader.LoadShader(shaderFile, iShaderType);
		shShaders.push_back(shader);
	}

	// Create the main shader program
	CShaderProgram *pMainProgram = new CShaderProgram;
	pMainProgram->CreateProgram();
	pMainProgram->AddShaderToProgram(&shShaders[0]);
	pMainProgram->AddShaderToProgram(&shShaders[1]);
	pMainProgram->LinkProgram();
	m_pShaderPrograms->push_back(pMainProgram);
	// Create a shader program for fonts
	CShaderProgram *pFontProgram = new CShaderProgram;
	pFontProgram->CreateProgram();
	pFontProgram->AddShaderToProgram(&shShaders[2]);
	pFontProgram->AddShaderToProgram(&shShaders[3]);
	pFontProgram->LinkProgram();
	m_pShaderPrograms->push_back(pFontProgram);
#ifdef _WIN32

    m_pFtFont->LoadSystemFont("arial.ttf", 32);
	m_pFtFont->SetShaderProgram(pFontProgram);
#endif
	// Create the sphere shader program
	CShaderProgram *pSphereProgram = new CShaderProgram;
	pSphereProgram->CreateProgram();
	pSphereProgram->AddShaderToProgram(&shShaders[4]);
	pSphereProgram->AddShaderToProgram(&shShaders[5]);
	pSphereProgram->LinkProgram();
	m_pShaderPrograms->push_back(pSphereProgram);

	// Create a sphere
    std::string textureDir = resourcePath("", "textures", "");

	m_pSphere->Create(textureDir, "dirtpile01.jpg", 35, 35);  // Texture downloaded from http://www.psionicgames.com/?page_id=26 on 24 Jan 2013
	glEnable(GL_CULL_FACE);
	
	// Create the grid
	m_pGrid->Create(500, 500, 15);

}
Example #28
0
 void CShaderProgram::operator delete(void* pProgram)
 {
     CShaderProgram* pThis = static_cast< CShaderProgram* >( pProgram );
     VKE_ASSERT( pThis, "Can not delete null pointer." );
     pThis->Release();
 }