示例#1
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
}
示例#2
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();
}
示例#3
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);
}
示例#4
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);	
}
示例#5
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();		

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

}
示例#7
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();
	}
}