//function to render scene given the combined modelview projection matrix 
//and a shader
void DrawScene(const glm::mat4& MVP, GLSLShader& shader) {
	//enable alpha blending with over compositing
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);

	//bind the cube vertex array object
	glBindVertexArray(cubeVAOID);
	//bind the shader
	shader.Use();
	//for all cubes
	for(int k=-1;k<=1;k++) {
		for(int j=-1;j<=1;j++) {
			int index =0;
			for(int i=-1;i<=1;i++) {
				GL_CHECK_ERRORS
				//set the modelling transformation and shader uniforms
				glm::mat4 T = glm::translate(glm::mat4(1), glm::vec3(i*2,j*2,k*2));
				glUniform4fv(shader("vColor"),1, &(box_colors[index++].x));
				glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP*R*T));
				//draw the cube
				glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
				GL_CHECK_ERRORS
			}
		}
	}
	//unbind shader
	shader.UnUse();
	//unbind vertex array object
	glBindVertexArray(0);
}
//display callback 
void OnRender() {
	//get the elapse time
	time = glutGet(GLUT_ELAPSED_TIME)/1000.0f * SPEED;

	//clear the colour and depth buffers
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//set teh camera viewing transformation
	glm::mat4 T		= glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, dist));
	glm::mat4 Rx	= glm::rotate(T,  rX, glm::vec3(1.0f, 0.0f, 0.0f));
	glm::mat4 MV	= glm::rotate(Rx, rY, glm::vec3(0.0f, 1.0f, 0.0f));
    glm::mat4 MVP	= P*MV;

	//bind the shader 
	shader.Use();
		//set the shader uniforms
		glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
		glUniform1f(shader("time"), time);
			//draw the mesh triangles
			glDrawElements(GL_TRIANGLES, TOTAL_INDICES, GL_UNSIGNED_SHORT, 0);

	//unbind the shader
	shader.UnUse();
	
	//swap front and back buffers to show the rendered result
	glutSwapBuffers();
}
//display function
void OnRender() {
	//clear colour and depth buffers
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//set the camera transform
	glm::mat4 T		= glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, dist));
	glm::mat4 Rx	= glm::rotate(T,  rX, glm::vec3(1.0f, 0.0f, 0.0f));
	glm::mat4 Ry	= glm::rotate(Rx, rY, glm::vec3(0.0f, 1.0f, 0.0f));
	glm::mat4 MV	= Ry;
    glm::mat4 MVP	= P*MV;

	//since we have kept the terrain vertex array object bound
	//it is still bound to the context so we can directly call draw element
	//which will draw vertices from the bound vertex array object
	//bind the terrain shader
	shader.Use();				
		//pass shader uniforms
		glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
			//draw terrain mesh
			glDrawElements(GL_TRIANGLES, TOTAL_INDICES, GL_UNSIGNED_INT, 0);
	//unbind shader
	shader.UnUse();
	
	//swap front and back buffers to show the rendered result
	glutSwapBuffers();
}
Beispiel #4
0
void onInit() {
	//tex = loadImage("../textures/texture.png");
	tex = loadImage("../textures/dots.png");
	tex2 = loadImage("../textures/dots.png");
	ctv = new CTextureViewer(0, "../shaders/textureViewer.vs", "../shaders/textureViewer.frag");
	ctv->setTexture(tex);
	ctv->setTexture2(tex2);

	hist.LoadFromFile(GL_VERTEX_SHADER, "../shaders/histogram.vs");
	hist.LoadFromFile(GL_FRAGMENT_SHADER, "../shaders/histogram.frag");
	hist.CreateAndLinkProgram();

	hist.Use();

	//Create uniforms and attributes (filled later)
	hist.AddAttribute("vPosition");
	hist.AddUniform("tex");
	hist.AddUniform("textureWidth");
	hist.AddUniform("textureHeight");

	hist.UnUse();

	initTex();
	initPointVBO();
	initHistogramFBO();
}
//renders sphere using the render shader
void DrawSphere(const glm::mat4& mvp) {
	renderShader.Use();
		glBindVertexArray(sphereVAOID);
		glUniformMatrix4fv(renderShader("MVP"), 1, GL_FALSE, glm::value_ptr(mvp));
			glDrawElements(GL_TRIANGLES, total_sphere_indices,GL_UNSIGNED_SHORT,0); 
	renderShader.UnUse();
}
void DrawCloth()
{ 
	renderShader.Use();
		glBindVertexArray(clothVAOID); 
		glUniformMatrix4fv(renderShader("MVP"), 1, GL_FALSE, glm::value_ptr(mMVP));						
			glDrawElements(GL_TRIANGLES, indices.size(),GL_UNSIGNED_SHORT,0);
		//glBindVertexArray(0);
	renderShader.UnUse();
}
void DrawGrid()
{ 
	renderShader.Use();
		glBindVertexArray(gridVAOID); 
		glUniformMatrix4fv(renderShader("MVP"), 1, GL_FALSE, glm::value_ptr(mMVP));						
			glDrawElements(GL_LINES, grid_indices.size(),GL_UNSIGNED_SHORT,0);
		glBindVertexArray(0);
	renderShader.UnUse();
}
Beispiel #8
0
void computeHistogram() {
	glViewport(0, 0, WIDTH, HEIGHT);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	glClearColor(0.0, 0.0, 0.0, 0.0);

	glDisable(GL_DEPTH_TEST);
	//Enable blending
	glEnable(GL_BLEND);
	glBlendFunc(GL_ONE, GL_ONE);
	//Additive
	glBlendEquation(GL_FUNC_ADD);
	
	hist.Use();

	glBindBuffer(GL_ARRAY_BUFFER, HistogramVBO);

	glVertexAttribPointer(hist["vPosition"], 2, GL_FLOAT, GL_FALSE, 2 * sizeof(GL_FLOAT), (void*)0);
	glEnableVertexAttribArray(hist["vPosition"]);

	glUniform1i(hist("tex"), 0);
	glUniform1f(hist("textureWidth"), (float)WIDTH);
	glUniform1f(hist("textureHeight"), (float)HEIGHT);

	glBindFramebuffer(GL_FRAMEBUFFER, FBO);
	//glClear(GL_COLOR_BUFFER_BIT);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	//Compute histogram
	glDrawArrays(GL_POINTS, 0, WIDTH*HEIGHT);


	glBindBuffer(GL_ARRAY_BUFFER, 0);
	hist.UnUse();
	//Disable blending
	glDisable(GL_BLEND);

	float hPixels[256];
	glReadPixels(0, 0, 256, 1, GL_BLUE, GL_FLOAT, hPixels);

	int sum = 0;
	if (printed != 0) 
	{
		cout << "\n\n-----------\n\n";
		for(int j = 0; j < 256; j++)
		{
			//if((j % 3) == 0)
				cout << j << ":" << hPixels[j] << endl;
				sum += hPixels[j];
		}
		cout << endl << sum << endl;
		printed--;
	}
	glBindFramebuffer(GL_FRAMEBUFFER, 0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
}
//display callback function
void OnRender() {

	GL_CHECK_ERRORS

	//clear colour and depth buffers
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//set the camera transform
	glm::mat4 T		= glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, dist));
	glm::mat4 Rx	= glm::rotate(T,  rX, glm::vec3(1.0f, 0.0f, 0.0f));
	glm::mat4 MV    = glm::rotate(Rx, rY, glm::vec3(0.0f, 1.0f, 0.0f));
	 
	//1) Render scene from the light's POV
	//enable rendering to FBO
 	glBindFramebuffer(GL_FRAMEBUFFER,fboID);
	//clear depth buffer
	glClear(GL_DEPTH_BUFFER_BIT);
	//reset viewport to the shadow map texture size
	glViewport(0,0,SHADOWMAP_WIDTH, SHADOWMAP_HEIGHT);
	
	//enable front face culling
	glCullFace(GL_FRONT);
		//draw scene from the point of view of light
		DrawScene(MV_L, P_L);
	//enable back face culling
	glCullFace(GL_BACK);

	//restore normal rendering path
	//unbind FBO, set the default back buffer and reset the viewport to screen size
	glBindFramebuffer(GL_FRAMEBUFFER,0);
	glDrawBuffer(GL_BACK_LEFT);
	glViewport(0,0,WIDTH, HEIGHT);

	//2) Render scene from point of view of eye
	DrawScene(MV, P, 0 );

	//bind light gizmo vertex array object
	glBindVertexArray(lightVAOID); {
		//set the flat shader
		flatshader.Use();
			//set the light's transform and render 3 lines
			glm::mat4 T = glm::translate(glm::mat4(1), lightPosOS);
			glUniformMatrix4fv(flatshader("MVP"), 1, GL_FALSE, glm::value_ptr(P*MV*T));
				glDrawArrays(GL_LINES, 0, 6);
		//unbind shader
		flatshader.UnUse();
	}
		
	//unbind the vertex array object
	glBindVertexArray(0);	

	//swap front and back buffers to show the rendered result
	glutSwapBuffers();
}
Beispiel #10
0
void OnRender() {
	glClear(GL_COLOR_BUFFER_BIT| GL_DEPTH_BUFFER_BIT);
	GL_CHECK_ERRORS

	shader.Use();

	drawDefaultBox();

	shader.UnUse();

	glutSwapBuffers();
}
Beispiel #11
0
void DrawClothPoints()
{ 
	particleShader.Use();
		//glBindVertexArray(clothVAOID); 
		glUniform1i(particleShader("selected_index"), selected_index);
		glUniformMatrix4fv(particleShader("MV"), 1, GL_FALSE, glm::value_ptr(mMV));	
		glUniformMatrix4fv(particleShader("MVP"), 1, GL_FALSE, glm::value_ptr(mMVP));										
			//draw the masses last						
			glDrawArrays(GL_POINTS, 0, total_points);					
		glBindVertexArray(0);
	particleShader.UnUse();	
}
//display function
void OnRender() {
	//clear the colour and depth buffers
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//bind shader
	shader.Use();
		//draw the full screen quad
		glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
	//unbind shader
	shader.UnUse();

	//swap front and back buffers to show the rendered result
	glutSwapBuffers();
}
Beispiel #13
0
void InitShaders(void)
{
	shader.LoadFromFile(GL_VERTEX_SHADER, "../CGE_solarsystem/shader.vert");
	shader.LoadFromFile(GL_FRAGMENT_SHADER, "../CGE_solarsystem/shader.frag");
	shader.CreateAndLinkProgram();
	shader.Use();
	shader.AddAttribute("vVertex");
	shader.AddAttribute("vUV");
	shader.AddUniform("MVP");
	shader.AddUniform("textureMap");
	glUniform1i(shader("textureMap"), 0);
	shader.UnUse();

	GL_CHECK_ERRORS
}
void OnRender() {
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
	glm::mat4 T		= glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, dist));
	glm::mat4 Rx	= glm::rotate(T,  rX, glm::vec3(1.0f, 0.0f, 0.0f));
	glm::mat4 Ry	= glm::rotate(Rx, rY, glm::vec3(0.0f, 1.0f, 0.0f));
	glm::mat4 MV	= Ry;
    glm::mat4 MVP	= P*MV;

	//glBindVertexArray(vaoID);
		shader.Use();				
			glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
				glDrawElements(GL_TRIANGLES, TOTAL_INDICES, GL_UNSIGNED_INT, 0);
		shader.UnUse();
	//glBindVertexArray(0);

	glutSwapBuffers();
}
//display callback function
void OnRender() {
	GL_CHECK_ERRORS
	//set the camera transform
	glm::mat4 Tr	= glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, dist));
	glm::mat4 Rx	= glm::rotate(Tr,  rX, glm::vec3(1.0f, 0.0f, 0.0f));
	glm::mat4 MV    = glm::rotate(Rx, rY, glm::vec3(0.0f, 1.0f, 0.0f));

	//clear the colour and depth buffers
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//get the combined modelview projection matrix
    glm::mat4 MVP	= P*MV;

	//render the grid object
	grid->Render(glm::value_ptr(MVP));

	//set the modelling transform to move the marhing result to origin
	glm::mat4 T = glm::translate(glm::mat4(1), glm::vec3(-0.5,-0.5,-0.5));

	//if rendering mode set to wireframe we set the front and back 
	//polygon mode to line
	if(bWireframe)
		glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	
	//set the volume marcher vertex array object
	glBindVertexArray(volumeMarcherVAO);
		//bind the shader
		shader.Use();
			//set the shader uniforms
			glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP*T));
				//render the triangles
				glDrawArrays(GL_TRIANGLES, 0, marcher->GetTotalVertices());
		//unbind the shader
		shader.UnUse();
	
	//restore the default polygon mode
	if(bWireframe)
		glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);

	//swap front and back buffers to show the rendered result
	glutSwapBuffers();
}
//display callback function
void OnRender() {
    GL_CHECK_ERRORS

    //set the camera transform
    glm::mat4 Tr	= glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, dist));
    glm::mat4 Rx	= glm::rotate(Tr,  rX, glm::vec3(1.0f, 0.0f, 0.0f));
    glm::mat4 MV    = glm::rotate(Rx, rY, glm::vec3(0.0f, 1.0f, 0.0f));

    //get the camera position
    glm::vec3 camPos = glm::vec3(glm::inverse(MV)*glm::vec4(0,0,0,1));

    //clear colour and depth buffer
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

    //get the combined modelview projection matrix
    glm::mat4 MVP	= P*MV;

    //render grid
    grid->Render(glm::value_ptr(MVP));

    //enable blending and bind the cube vertex array object
    glEnable(GL_BLEND);
    glBindVertexArray(cubeVAOID);
    //bind the raycasting shader
    shader.Use();
    //pass shader uniforms
    glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
    glUniform3fv(shader("camPos"), 1, &(camPos.x));
    //render the cube
    glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
    //unbind the raycasting shader
    shader.UnUse();
    //disable blending
    glDisable(GL_BLEND);

    //swap front and back buffers to show the rendered result
    glutSwapBuffers();
}
//display callback function
void OnRender() {
	//clear colour and depth buffer
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//set the camera transformation 
	glm::mat4 T	 = glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, dist));
	glm::mat4 Rx = glm::rotate(T,  rX, glm::vec3(1.0f, 0.0f, 0.0f));
	glm::mat4 V	 = glm::rotate(Rx, rY, glm::vec3(0.0f, 1.0f, 0.0f));
	glm::mat4 PV = P*V;

	//bind the shader
	shader.Use();
		//set the shader uniforms
		glUniformMatrix4fv(shader("PV"), 1, GL_FALSE, glm::value_ptr(PV));
		glUniform1i(shader("sub_divisions"), sub_divisions);
			//render instanced geometry
			glDrawElementsInstanced(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0, 4);
		//unbind shader
		shader.UnUse();

	//swap front and back buffers to show the rendered result
	glutSwapBuffers();
}
void OnInit() {
	GL_CHECK_ERRORS
	//setup shader
	shader.LoadFromFile(GL_VERTEX_SHADER, "shaders/shader.vert");
	shader.LoadFromFile(GL_GEOMETRY_SHADER, "shaders/shader.geom");
	shader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/shader.frag");
	shader.CreateAndLinkProgram();
	shader.Use();	
		shader.AddAttribute("vVertex"); 
		shader.AddUniform("heightMapTexture");
		shader.AddUniform("scale");
		shader.AddUniform("half_scale");
		shader.AddUniform("HALF_TERRAIN_SIZE");
		shader.AddUniform("MVP");

		glUniform1i(shader("heightMapTexture"), 0);
		glUniform2i(shader("HALF_TERRAIN_SIZE"), TERRAIN_WIDTH>>1, TERRAIN_DEPTH>>1);
		glUniform1f(shader("scale"), scale);
		glUniform1f(shader("half_scale"), half_scale);
	shader.UnUse();

	GL_CHECK_ERRORS

	//setup geometry
	

	//fill indices array
	GLuint* id=&indices[0];
	int i=0, j=0;
	
	//setup vertices 
	int count = 0;

	
	for( j=0;j<TERRAIN_DEPTH;j++) {		 
		for( i=0;i<TERRAIN_WIDTH;i++) {	 
			/*
			vertices[count] = glm::vec3( ( (float(i)/(TERRAIN_WIDTH-1))*2.0f-1)*TERRAIN_HALF_WIDTH, 
										 (pData[count]/255.0f)*scale-half_scale, 
										 ( (float(j)/(TERRAIN_DEPTH-1))*2.0-1)*TERRAIN_HALF_DEPTH);
										 */
			vertices[count] = glm::vec3( (float(i)/(TERRAIN_WIDTH-1)), 
										 0, 
										 (float(j)/(TERRAIN_DEPTH-1)));
			count++;
		}
	}
	 
	for (i = 0; i < TERRAIN_DEPTH-1; i++) {        
		for (j = 0; j < TERRAIN_WIDTH-1; j++) {			
			int i0 = j+ i*TERRAIN_WIDTH;
			int i1 = i0+1;
			int i2 = i0+TERRAIN_WIDTH;
			int i3 = i2+1;
			*id++ = i0; 
			*id++ = i2; 
			*id++ = i1; 
			*id++ = i1; 
			*id++ = i2; 
			*id++ = i3; 
		}    
	}

	GL_CHECK_ERRORS

	//setup vao and vbo stuff
	glGenVertexArrays(1, &vaoID);
	glGenBuffers(1, &vboVerticesID);
	glGenBuffers(1, &vboIndicesID);
	 
	glBindVertexArray(vaoID);	

		glBindBuffer (GL_ARRAY_BUFFER, vboVerticesID);
		glBufferData (GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS
		glEnableVertexAttribArray(shader["vVertex"]);
		glVertexAttribPointer(shader["vVertex"], 3, GL_FLOAT, GL_FALSE,0,0);
		GL_CHECK_ERRORS 
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS
						
			
	//load the heightmap texture using SOIL	
	int texture_width = 0, texture_height = 0, format=0;		 
	GLubyte* pData = SOIL_load_image(filename.c_str(), &texture_width, &texture_height, &format, SOIL_LOAD_L);
	
	//setup OpenGL texture
	glGenTextures(1, &heightMapTextureID);
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, heightMapTextureID);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RED, texture_width, texture_height, 0, GL_RED, GL_UNSIGNED_BYTE, pData);
	
	free(pData);
	
	GL_CHECK_ERRORS

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	GL_CHECK_ERRORS

	cout<<"Initialization successfull"<<endl;
}
//scene rendering function
void DrawScene(const glm::mat4& View, const glm::mat4& Proj, int isLightPass = 1) {

	GL_CHECK_ERRORS

	//bind the current shader
	shader.Use();
	//render plane first
	glBindVertexArray(planeVAOID); {
		//set the shader uniforms
		glUniform3fv(shader("light_position"),1, &(lightPosOS.x));
		glUniformMatrix4fv(shader("S"), 1, GL_FALSE, glm::value_ptr(S));
		glUniformMatrix4fv(shader("M"), 1, GL_FALSE, glm::value_ptr(glm::mat4(1)));
		glUniform1i(shader("bIsLightPass"), isLightPass);		
		glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(Proj*View));
		glUniformMatrix4fv(shader("MV"), 1, GL_FALSE, glm::value_ptr(View));
		glUniformMatrix3fv(shader("N"), 1, GL_FALSE, glm::value_ptr(glm::inverseTranspose(glm::mat3(View))));
		glUniform3f(shader("diffuse_color"), 1.0f,1.0f,1.0f);
			//draw plane triangles
			glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_SHORT, 0);
	}

	//render the cube 
	glBindVertexArray(cubeVAOID); {
		//set the cube's transform
		glm::mat4 T = glm::translate(glm::mat4(1),  glm::vec3(-1,1,0));
		glm::mat4 M = T;
		glm::mat4 MV = View*M;
		glm::mat4 MVP = Proj*MV;
		//pass shader uniforms
		glUniformMatrix4fv(shader("S"), 1, GL_FALSE, glm::value_ptr(S));
		glUniformMatrix4fv(shader("M"), 1, GL_FALSE, glm::value_ptr(M));
		glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
		glUniformMatrix4fv(shader("MV"), 1, GL_FALSE, glm::value_ptr(MV));
		glUniformMatrix3fv(shader("N"), 1, GL_FALSE, glm::value_ptr(glm::inverseTranspose(glm::mat3(MV))));
		glUniform3f(shader("diffuse_color"), 1.0f,0.0f,0.0f);
			//draw cube triangles
	 		glDrawElements(GL_TRIANGLES, 36, GL_UNSIGNED_SHORT, 0);
	}
	//render the sphere
	glBindVertexArray(sphereVAOID); {
		//set the sphere's transform
		glm::mat4 T = glm::translate(glm::mat4(1), glm::vec3(1,1,0));
		glm::mat4 M = T;
		glm::mat4 MV = View*M;
		glm::mat4 MVP = Proj*MV;
		//set the shader uniforms
		glUniformMatrix4fv(shader("S"), 1, GL_FALSE, glm::value_ptr(S));
		glUniformMatrix4fv(shader("M"), 1, GL_FALSE, glm::value_ptr(M));
		glUniformMatrix4fv(shader("MVP"), 1, GL_FALSE, glm::value_ptr(MVP));
		glUniformMatrix4fv(shader("MV"), 1, GL_FALSE, glm::value_ptr(MV));
		glUniformMatrix3fv(shader("N"), 1, GL_FALSE, glm::value_ptr(glm::inverseTranspose(glm::mat3(MV))));
		glUniform3f(shader("diffuse_color"), 0.0f, 0.0f, 1.0f);
			//draw sphere triangles
	 		glDrawElements(GL_TRIANGLES, totalSphereTriangles, GL_UNSIGNED_SHORT, 0);
	}

	//unbind shader
	shader.UnUse();

	GL_CHECK_ERRORS 
}
//OpenGL initialization
void OnInit() {

	//load the flat shader
	flatshader.LoadFromFile(GL_VERTEX_SHADER, "shaders/shader.vert");
	flatshader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/shader.frag");
	//compile and link shader
	flatshader.CreateAndLinkProgram();
	flatshader.Use();
		//add attributes and uniforms
		flatshader.AddAttribute("vVertex");
		flatshader.AddUniform("MVP");
	flatshader.UnUse();

	//load the shadow mapping shader
	shader.LoadFromFile(GL_VERTEX_SHADER, "shaders/PointLightShadowMapped.vert");
	shader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/PointLightShadowMapped.frag");
	//compile and link shader
	shader.CreateAndLinkProgram();
	shader.Use();
		//add attributes and uniforms
		shader.AddAttribute("vVertex");
		shader.AddAttribute("vNormal");
		shader.AddUniform("MVP");
		shader.AddUniform("MV");
		shader.AddUniform("M");
		shader.AddUniform("N");
		shader.AddUniform("S");
		shader.AddUniform("light_position");
		shader.AddUniform("diffuse_color");
		shader.AddUniform("bIsLightPass");
		shader.AddUniform("shadowMap");
		//pass value of constant uniforms at initialization
		glUniform1i(shader("shadowMap"),0);
	shader.UnUse();

	GL_CHECK_ERRORS

	//setup sphere geometry
	CreateSphere(1.0f,10,10, vertices, indices);

	//setup sphere vao and vbo stuff
	glGenVertexArrays(1, &sphereVAOID);
	glGenBuffers(1, &sphereVerticesVBO);
	glGenBuffers(1, &sphereIndicesVBO);
	glBindVertexArray(sphereVAOID);

		glBindBuffer (GL_ARRAY_BUFFER, sphereVerticesVBO);
		//pass vertices to the buffer object
		glBufferData (GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS
		//enable vertex attribute array for position
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex),0);
		GL_CHECK_ERRORS
		//enable vertex attribute array for normal
		glEnableVertexAttribArray(1);
		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex), (const GLvoid*)(offsetof(Vertex, normal)));
		GL_CHECK_ERRORS
		//pass sphere indices to element array buffer 
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, sphereIndicesVBO);
		glBufferData (GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(GLushort), &indices[0], GL_STATIC_DRAW);

	//store the total number of sphere triangles
	totalSphereTriangles = indices.size();

	//clear the vertices and indices vectors as we will reuse them
	//for cubes
	vertices.clear();
	indices.clear();

	//setup cube geometry
	CreateCube(2,vertices, indices);

	//setup cube vao and vbo stuff
	glGenVertexArrays(1, &cubeVAOID);
	glGenBuffers(1, &cubeVerticesVBO);
	glGenBuffers(1, &cubeIndicesVBO);
	glBindVertexArray(cubeVAOID);

		glBindBuffer (GL_ARRAY_BUFFER, cubeVerticesVBO);
		//pass vertices to the buffer object
		glBufferData (GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS
		//enable vertex attribute array for position
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex),0);
		GL_CHECK_ERRORS
		//enable vertex attribute array for normals
		glEnableVertexAttribArray(1);
		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex), (const GLvoid*)(offsetof(Vertex, normal)));
		GL_CHECK_ERRORS
		//pass cube indices to element array buffer
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, cubeIndicesVBO);
		glBufferData (GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(GLushort), &indices[0], GL_STATIC_DRAW);

	GL_CHECK_ERRORS

	//clear the vertices and indices vectors as we will reuse them
	//for plane
	vertices.clear();
	indices.clear();
	//create a plane object
	CreatePlane(100,100,vertices, indices);

	//setup plane VAO and VBO
	glGenVertexArrays(1, &planeVAOID);
	glGenBuffers(1, &planeVerticesVBO);
	glGenBuffers(1, &planeIndicesVBO);
	glBindVertexArray(planeVAOID);

		glBindBuffer (GL_ARRAY_BUFFER, planeVerticesVBO);
		//pass vertices to the buffer object
		glBufferData (GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS
		//enable vertex attribute array for position
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex),0);
		GL_CHECK_ERRORS
		//enable vertex attribute array for normals
		glEnableVertexAttribArray(1);
		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex), (const GLvoid*)(offsetof(Vertex, normal)));
		GL_CHECK_ERRORS
		//pass plane indices to element array buffer
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, planeIndicesVBO);
		glBufferData (GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(GLushort), &indices[0], GL_STATIC_DRAW);

	GL_CHECK_ERRORS

	//setup vao and vbo stuff for the light position crosshair
	glm::vec3 crossHairVertices[6];
	crossHairVertices[0] = glm::vec3(-0.5f,0,0);
	crossHairVertices[1] = glm::vec3(0.5f,0,0);
	crossHairVertices[2] = glm::vec3(0, -0.5f,0);
	crossHairVertices[3] = glm::vec3(0, 0.5f,0);
	crossHairVertices[4] = glm::vec3(0,0, -0.5f);
	crossHairVertices[5] = glm::vec3(0,0, 0.5f);

	//setup light gizmo vertex array and buffer object
	glGenVertexArrays(1, &lightVAOID);
	glGenBuffers(1, &lightVerticesVBO);
	glBindVertexArray(lightVAOID);

		glBindBuffer (GL_ARRAY_BUFFER, lightVerticesVBO);
		//pass light crosshair gizmo vertices to buffer object
		glBufferData (GL_ARRAY_BUFFER, sizeof(crossHairVertices), &(crossHairVertices[0].x), GL_STATIC_DRAW);
		GL_CHECK_ERRORS
		//enable vertex attribute array for position
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0,0);


	GL_CHECK_ERRORS

	//get light position from spherical coordinates
	lightPosOS.x = radius * cos(theta)*sin(phi);
	lightPosOS.y = radius * cos(phi);
	lightPosOS.z = radius * sin(theta)*sin(phi);

	//setup the shadowmap texture
	glGenTextures(1, &shadowMapTexID);
	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, shadowMapTexID);

	//set texture parameters
	GLfloat border[4]={1,0,0,0};
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_BORDER);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_MODE,GL_COMPARE_REF_TO_TEXTURE);
	glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_COMPARE_FUNC,GL_LEQUAL);
	glTexParameterfv(GL_TEXTURE_2D,GL_TEXTURE_BORDER_COLOR,border);
	glTexImage2D(GL_TEXTURE_2D,0,GL_DEPTH_COMPONENT24,SHADOWMAP_WIDTH,SHADOWMAP_HEIGHT,0,GL_DEPTH_COMPONENT,GL_UNSIGNED_BYTE,NULL);

	//set up FBO to get the depth component
	glGenFramebuffers(1,&fboID);
	glBindFramebuffer(GL_FRAMEBUFFER,fboID);
	glFramebufferTexture2D(GL_FRAMEBUFFER,GL_DEPTH_ATTACHMENT,GL_TEXTURE_2D,shadowMapTexID,0);

	//check framebuffer completeness status
	GLenum status = glCheckFramebufferStatus(GL_FRAMEBUFFER);
	if(status == GL_FRAMEBUFFER_COMPLETE) {
		cout<<"FBO setup successful."<<endl;
	} else {
		cout<<"Problem in FBO setup."<<endl;
	}

	//unbind FBO
	glBindFramebuffer(GL_FRAMEBUFFER,0);

	//set the light MV, P and bias matrices
	MV_L = glm::lookAt(lightPosOS,glm::vec3(0,0,0),glm::vec3(0,1,0));
	P_L  = glm::perspective(glm::radians(50.0f),1.0f,1.0f, 25.0f);
	B    = glm::scale(glm::translate(glm::mat4(1),glm::vec3(0.5,0.5,0.5)), glm::vec3(0.5,0.5,0.5));
	BP   = B*P_L;
	S    = BP*MV_L;

	//enable depth testing and culling
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	cout<<"Initialization successfull"<<endl;  
}
//OpenGL initialization
void OnInit() {

	//set the instance modeling matrix
	M[0] = glm::translate(glm::mat4(1), glm::vec3(-5,0,-5));
	M[1] = glm::translate(M[0], glm::vec3(10,0,0));
	M[2] = glm::translate(M[1], glm::vec3(0,0,10));
	M[3] = glm::translate(M[2], glm::vec3(-10,0,0));

	GL_CHECK_ERRORS

	//load the shader
	shader.LoadFromFile(GL_VERTEX_SHADER, "shaders/shader.vert");
	shader.LoadFromFile(GL_GEOMETRY_SHADER, "shaders/shader.geom");
	shader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/shader.frag");
	//create and link shader
	shader.CreateAndLinkProgram();
	shader.Use();
		//add attribute and uniform
		shader.AddAttribute("vVertex");
		shader.AddUniform("PV");
		shader.AddUniform("M");
		shader.AddUniform("sub_divisions");

		//set values of constant uniforms at initialization
		glUniform1i(shader("sub_divisions"), sub_divisions);
		glUniformMatrix4fv(shader("M"), 4, GL_FALSE, glm::value_ptr(M[0]));

	shader.UnUse();

	GL_CHECK_ERRORS

	//setup quad geometry
	//setup quad vertices
	vertices[0] = glm::vec3(-5,0,-5);
	vertices[1] = glm::vec3(-5,0,5);
	vertices[2] = glm::vec3(5,0,5);
	vertices[3] = glm::vec3(5,0,-5);

	//setup quad indices
	GLushort* id=&indices[0];
 	*id++ = 0;
	*id++ = 1;
	*id++ = 2;

	*id++ = 0;
	*id++ = 2;
	*id++ = 3;

	GL_CHECK_ERRORS

	//setup quad vao and vbo stuff
	glGenVertexArrays(1, &vaoID);
	glGenBuffers(1, &vboVerticesID);
	glGenBuffers(1, &vboIndicesID);

	glBindVertexArray(vaoID);

		glBindBuffer (GL_ARRAY_BUFFER, vboVerticesID);
		//pass the quad vertices to buffer object
		glBufferData (GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS
		//enable vertex attribute array for position
		glEnableVertexAttribArray(shader["vVertex"]);
		glVertexAttribPointer(shader["vVertex"], 3, GL_FLOAT, GL_FALSE,0,0);
		GL_CHECK_ERRORS
		//pass the quad indices to element array buffer
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS

	//set the polygon mode to render lines
	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);

	GL_CHECK_ERRORS
		
	cout<<"Initialization successfull"<<endl;
}
//display callback function
void OnRender() {
	GL_CHECK_ERRORS

	//camera transformation
	glm::mat4 Tr	= glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, dist));
	glm::mat4 Rx	= glm::rotate(Tr,  rX, glm::vec3(1.0f, 0.0f, 0.0f));
	glm::mat4 MV    = glm::rotate(Rx, rY, glm::vec3(0.0f, 1.0f, 0.0f));

	//clear colour and depth buffer
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//get the combined modelview projection matrix
    glm::mat4 MVP	= P*MV;

	//if we want to use depth peeling 
	if(bShowDepthPeeling) {
		//bind the colour blending FBO
		glBindFramebuffer(GL_FRAMEBUFFER, colorBlenderFBOID);
		//set the first colour attachment as the draw buffer
		glDrawBuffer(GL_COLOR_ATTACHMENT0);
		//clear the colour and depth buffer
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );

		// 1. In the first pass, we render normally with depth test enabled to get the nearest surface
		glEnable(GL_DEPTH_TEST);
		DrawScene(MVP, cubeShader);

		// 2. Depth peeling + blending pass
		int numLayers = (NUM_PASSES - 1) * 2;

		//for each pass
		for (int layer = 1; bUseOQ || layer < numLayers; layer++) {
			int currId = layer % 2;
			int prevId = 1 - currId;

			//bind the current FBO
			glBindFramebuffer(GL_FRAMEBUFFER, fbo[currId]);
			//set the first colour attachment as draw buffer
			glDrawBuffer(GL_COLOR_ATTACHMENT0);

			//set clear colour to black
			glClearColor(0, 0, 0, 0);
			//clear the colour and depth buffers
			glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

			//disbale blending and depth testing
			glDisable(GL_BLEND);
			glEnable(GL_DEPTH_TEST);

			//if we want to use occlusion query, we initiate it
			if (bUseOQ) {
				glBeginQuery(GL_SAMPLES_PASSED_ARB, queryId);
			}

			GL_CHECK_ERRORS

			//bind the depth texture from the previous step
			glBindTexture(GL_TEXTURE_RECTANGLE, depthTexID[prevId]);

			//render scene with the front to back peeling shader
	 		DrawScene(MVP, frontPeelShader);

			//if we initiated the occlusion query, we end it 
			if (bUseOQ) {
				glEndQuery(GL_SAMPLES_PASSED_ARB);
			}

			GL_CHECK_ERRORS

			//bind the colour blender FBO
			glBindFramebuffer(GL_FRAMEBUFFER, colorBlenderFBOID);
			//render to its first colour attachment 
			glDrawBuffer(GL_COLOR_ATTACHMENT0);

			//enable blending but disable depth testing
			glDisable(GL_DEPTH_TEST);
			glEnable(GL_BLEND);

			//change the blending equation to add
			glBlendEquation(GL_FUNC_ADD);
			//use separate blending function
			glBlendFuncSeparate(GL_DST_ALPHA, GL_ONE,
								GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);

			//bind the result from the previous iteration as texture
			glBindTexture(GL_TEXTURE_RECTANGLE, texID[currId]);
			//bind the blend shader and then draw a fullscreen quad
			blendShader.Use();
				 DrawFullScreenQuad();
			blendShader.UnUse();

			//disable blending
			glDisable(GL_BLEND);

			GL_CHECK_ERRORS

			//if we initiated the occlusion query, we get the query result
			//that is the total number of samples
			if (bUseOQ) {
				GLuint sample_count;
				glGetQueryObjectuiv(queryId, GL_QUERY_RESULT, &sample_count);
				if (sample_count == 0) {
					break;
				}
			}
		}

		GL_CHECK_ERRORS

		// 3. Final render pass
		//remove the FBO 
		glBindFramebuffer(GL_FRAMEBUFFER, 0);
		//restore the default back buffer
		glDrawBuffer(GL_BACK_LEFT);
		//disable depth testing and blending
		glDisable(GL_DEPTH_TEST);
		glDisable(GL_BLEND);

		//bind the colour blender texture
		glBindTexture(GL_TEXTURE_RECTANGLE, colorBlenderTexID);
		//bind the final shader
		finalShader.Use();
			//set shader uniforms
			glUniform4fv(finalShader("vBackgroundColor"), 1, &bg.x);
			//draw full screen quad
			DrawFullScreenQuad();
		finalShader.UnUse();

	} else {
//OpenGL initialization
void OnInit() {

    GL_CHECK_ERRORS

    //create a uniform grid of size 20x20 in XZ plane
    grid = new CGrid(20,20);

    GL_CHECK_ERRORS

    //Load the raycasting shader
    shader.LoadFromFile(GL_VERTEX_SHADER, "shaders/raycaster.vert");
    shader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/raycaster.frag");

    //compile and link the shader
    shader.CreateAndLinkProgram();
    shader.Use();
    //add attributes and uniforms
    shader.AddAttribute("vVertex");
    shader.AddUniform("MVP");
    shader.AddUniform("volume");
    shader.AddUniform("camPos");
    shader.AddUniform("step_size");

    //pass constant uniforms at initialization
    glUniform3f(shader("step_size"), 1.0f/XDIM, 1.0f/YDIM, 1.0f/ZDIM);
    glUniform1i(shader("volume"),0);
    shader.UnUse();

    GL_CHECK_ERRORS

    //load volume data
    if(LoadVolume()) {
        std::cout<<"Volume data loaded successfully."<<std::endl;
    } else {
        std::cout<<"Cannot load volume data."<<std::endl;
        exit(EXIT_FAILURE);
    }

    //set background colour
    glClearColor(bg.r, bg.g, bg.b, bg.a);

    //setup unit cube vertex array and vertex buffer objects
    glGenVertexArrays(1, &cubeVAOID);
    glGenBuffers(1, &cubeVBOID);
    glGenBuffers(1, &cubeIndicesID);

    //unit cube vertices
    glm::vec3 vertices[8]= {	glm::vec3(-0.5f,-0.5f,-0.5f),
                                glm::vec3( 0.5f,-0.5f,-0.5f),
                                glm::vec3( 0.5f, 0.5f,-0.5f),
                                glm::vec3(-0.5f, 0.5f,-0.5f),
                                glm::vec3(-0.5f,-0.5f, 0.5f),
                                glm::vec3( 0.5f,-0.5f, 0.5f),
                                glm::vec3( 0.5f, 0.5f, 0.5f),
                                glm::vec3(-0.5f, 0.5f, 0.5f)
                           };

    //unit cube indices
    GLushort cubeIndices[36]= {0,5,4,
                               5,0,1,
                               3,7,6,
                               3,6,2,
                               7,4,6,
                               6,4,5,
                               2,1,3,
                               3,1,0,
                               3,0,7,
                               7,0,4,
                               6,5,2,
                               2,5,1
                              };
    glBindVertexArray(cubeVAOID);
    glBindBuffer (GL_ARRAY_BUFFER, cubeVBOID);
    //pass cube vertices to buffer object memory
    glBufferData (GL_ARRAY_BUFFER, sizeof(vertices), &(vertices[0].x), GL_STATIC_DRAW);

    GL_CHECK_ERRORS

    //enable vertex attributre array for position
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,0,0);

    //pass indices to element array  buffer
    glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, cubeIndicesID);
    glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), &cubeIndices[0], GL_STATIC_DRAW);

    glBindVertexArray(0);

    //enable depth test
    glEnable(GL_DEPTH_TEST);

    //set the over blending function
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    cout<<"Initialization successfull"<<endl;
}
//OpenGL initialization function
void OnInit() {

	GL_CHECK_ERRORS

	//initialize FBO
	initFBO();

	//generate hardwre query
	glGenQueries(1, &queryId);

	//create a uniform grid of size 20x20 in XZ plane
	grid = new CGrid(20,20);

	GL_CHECK_ERRORS

	//generate the quad vertices
	glm::vec2 quadVerts[4];
	quadVerts[0] = glm::vec2(0,0);
	quadVerts[1] = glm::vec2(1,0);
	quadVerts[2] = glm::vec2(1,1);
	quadVerts[3] = glm::vec2(0,1);

	//generate quad indices
	GLushort quadIndices[]={ 0,1,2,0,2,3};

	//generate quad  vertex array and vertex buffer objects
	glGenVertexArrays(1, &quadVAOID);
	glGenBuffers(1, &quadVBOID);
	glGenBuffers(1, &quadIndicesID);

	glBindVertexArray(quadVAOID);
		glBindBuffer (GL_ARRAY_BUFFER, quadVBOID);
		//pass quad vertices to buffer object memory
		glBufferData (GL_ARRAY_BUFFER, sizeof(quadVerts), &quadVerts[0], GL_STATIC_DRAW);

		GL_CHECK_ERRORS

		//enable vertex attribute array for position
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE,0,0);

		//pass the quad indices to the element array buffer
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, quadIndicesID);
		glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(quadIndices), &quadIndices[0], GL_STATIC_DRAW);

	//setup unit cube vertex array and vertex buffer objects
	glGenVertexArrays(1, &cubeVAOID);
	glGenBuffers(1, &cubeVBOID);
	glGenBuffers(1, &cubeIndicesID);

	//unit cube vertices 
	glm::vec3 vertices[8]={	glm::vec3(-0.5f,-0.5f,-0.5f),
							glm::vec3( 0.5f,-0.5f,-0.5f),
							glm::vec3( 0.5f, 0.5f,-0.5f),
							glm::vec3(-0.5f, 0.5f,-0.5f),
							glm::vec3(-0.5f,-0.5f, 0.5f),
							glm::vec3( 0.5f,-0.5f, 0.5f),
							glm::vec3( 0.5f, 0.5f, 0.5f),
							glm::vec3(-0.5f, 0.5f, 0.5f)};

	//unit cube indices
	GLushort cubeIndices[36]={0,5,4,
							  5,0,1,
							  3,7,6,
							  3,6,2,
							  7,4,6,
							  6,4,5,
							  2,1,3,
							  3,1,0,
							  3,0,7,
							  7,0,4,
							  6,5,2,
							  2,5,1};
	glBindVertexArray(cubeVAOID);
		glBindBuffer (GL_ARRAY_BUFFER, cubeVBOID);
		//pass cube vertices to buffer object memory
		glBufferData (GL_ARRAY_BUFFER, sizeof(vertices), &(vertices[0].x), GL_STATIC_DRAW);

		GL_CHECK_ERRORS

		//enable vertex attributre array for position
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,0,0);

		//pass cube indices to element array  buffer
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, cubeIndicesID);
		glBufferData (GL_ELEMENT_ARRAY_BUFFER, sizeof(cubeIndices), &cubeIndices[0], GL_STATIC_DRAW);

	glBindVertexArray(0);

	//Load the cube shader
	cubeShader.LoadFromFile(GL_VERTEX_SHADER, "shaders/cube_shader.vert");
	cubeShader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/cube_shader.frag");

	//compile and link the shader
	cubeShader.CreateAndLinkProgram();
	cubeShader.Use();
		//add attributes and uniforms
		cubeShader.AddAttribute("vVertex");
		cubeShader.AddUniform("MVP");
		cubeShader.AddUniform("vColor");
	cubeShader.UnUse();

	//Load the front to back peeling shader
	frontPeelShader.LoadFromFile(GL_VERTEX_SHADER, "shaders/front_peel.vert");
	frontPeelShader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/front_peel.frag");
	//compile and link the shader
	frontPeelShader.CreateAndLinkProgram();
	frontPeelShader.Use();
		//add attributes and uniforms
		frontPeelShader.AddAttribute("vVertex");
		frontPeelShader.AddUniform("MVP");
		frontPeelShader.AddUniform("vColor");
		frontPeelShader.AddUniform("depthTexture");
		//pass constant uniforms at initialization
		glUniform1i(frontPeelShader("depthTexture"), 0);
	frontPeelShader.UnUse();

	//Load the blending shader
	blendShader.LoadFromFile(GL_VERTEX_SHADER, "shaders/blend.vert");
	blendShader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/blend.frag");
	//compile and link the shader
	blendShader.CreateAndLinkProgram();
	blendShader.Use();
		//add attributes and uniforms
		blendShader.AddAttribute("vVertex");
		blendShader.AddUniform("tempTexture");
		//pass constant uniforms at initialization
		glUniform1i(blendShader("tempTexture"), 0);
	blendShader.UnUse();

	//Load the final shader
	finalShader.LoadFromFile(GL_VERTEX_SHADER, "shaders/blend.vert");
	finalShader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/final.frag");
	//compile and link the shader
	finalShader.CreateAndLinkProgram();
	finalShader.Use();
		//add attributes and uniforms
		finalShader.AddAttribute("vVertex");
		finalShader.AddUniform("colorTexture");
		finalShader.AddUniform("vBackgroundColor");
		//pass constant uniforms at initialization
		glUniform1i(finalShader("colorTexture"), 0);
	finalShader.UnUse();
	cout<<"Initialization successfull"<<endl;
}
//OpenGL initialization
void OnInit() { 

	//load the cubemap shader
	cubemapShader.LoadFromFile(GL_VERTEX_SHADER, "shaders/cubemap.vert");
	cubemapShader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/cubemap.frag");
	//compile and link shader
	cubemapShader.CreateAndLinkProgram();
	cubemapShader.Use();
		//add shader attribute and uniforms
		cubemapShader.AddAttribute("vVertex");
		cubemapShader.AddAttribute("vNormal");
		cubemapShader.AddUniform("MVP");
		cubemapShader.AddUniform("eyePosition");
		cubemapShader.AddUniform("cubeMap");
		//set values of constant uniforms at initialization
		glUniform1i(cubemapShader("cubeMap"), 1);
	cubemapShader.UnUse();

	GL_CHECK_ERRORS

	//setup sphere geometry
	createSphere(1,10,10);

	GL_CHECK_ERRORS

	//setup sphere vao and vbo stuff
	glGenVertexArrays(1, &sphereVAOID);
	glGenBuffers(1, &sphereVerticesVBO);
	glGenBuffers(1, &sphereIndicesVBO);
	glBindVertexArray(sphereVAOID);

		glBindBuffer (GL_ARRAY_BUFFER, sphereVerticesVBO);
		//pass vertices to the buffer object
		glBufferData (GL_ARRAY_BUFFER, vertices.size()*sizeof(Vertex), &vertices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS
		//enable vertex attribute array for position
		glEnableVertexAttribArray(0);
		glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex),0);
		GL_CHECK_ERRORS
		//enable vertex attribute array for normal
		glEnableVertexAttribArray(1);
		glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex), (const GLvoid*)(offsetof(Vertex, normal)));
		GL_CHECK_ERRORS
		//pass sphere indices to element array buffer 
		glBindBuffer (GL_ELEMENT_ARRAY_BUFFER, sphereIndicesVBO);
		glBufferData (GL_ELEMENT_ARRAY_BUFFER, indices.size()*sizeof(GLushort), &indices[0], GL_STATIC_DRAW);
		 
	//generate the dynamic cubemap texture and bind to texture unit 1
	glGenTextures(1, &dynamicCubeMapID);
	glActiveTexture(GL_TEXTURE1);	
	glBindTexture(GL_TEXTURE_CUBE_MAP, dynamicCubeMapID);
	//set texture parameters
	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameterf(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_R, GL_CLAMP_TO_EDGE);
	//for all 6 cubemap faces
	for (int face = 0; face < 6; face++) {
		//allocate a different texture for each face and assign to the cubemap texture target
		glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + face, 0, GL_RGBA, CUBEMAP_SIZE, CUBEMAP_SIZE, 0, GL_RGBA, GL_FLOAT, NULL);
	}

	GL_CHECK_ERRORS

	//setup FBO
	glGenFramebuffers(1, &fboID);
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboID);

	//setup render buffer object (RBO)
	glGenRenderbuffers(1, &rboID);
	glBindRenderbuffer(GL_RENDERBUFFER, rboID);

	//set the renderbuffer storage to have the same dimensions as the cubemap texture
	//also set the renderbuffer as the depth attachment of the FBO
	glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT, CUBEMAP_SIZE, CUBEMAP_SIZE);
	glFramebufferRenderbuffer(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_RENDERBUFFER, fboID);

	//set the dynamic cubemap texture as the colour attachment of FBO
	glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, dynamicCubeMapID, 0);

	//check the framebuffer completeness status
	GLenum status = glCheckFramebufferStatus(GL_DRAW_FRAMEBUFFER);
	if(status != GL_FRAMEBUFFER_COMPLETE) {
		cerr<<"Frame buffer object setup error."<<endl;
		exit(EXIT_FAILURE);
	} else {
		cerr<<"FBO setup successfully."<<endl;
	}
	//unbind FBO
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);
	//unbind renderbuffer
	glBindRenderbuffer(GL_RENDERBUFFER, 0);

	GL_CHECK_ERRORS

	//create a grid object
	grid = new CGrid();

	//create a unit cube object
	cube = new CUnitCube(glm::vec3(1,0,0));

	GL_CHECK_ERRORS

	//enable depth testing and back face culling
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);

	cout<<"Initialization successfull"<<endl;
}
//OpenGL initialization function
void OnInit() {

	GL_CHECK_ERRORS

	//create a uniform grid of size 20x20 in XZ plane
	grid = new CGrid(20,20);

	GL_CHECK_ERRORS

	//create a new TetrahedraMarcher instance
	marcher = new TetrahedraMarcher();
	//set the volume dataset dimensions
	marcher->SetVolumeDimensions(256,256,256);
	//load the volume dataset
	marcher->LoadVolume(volume_file);
	//set the isosurface value
	marcher->SetIsosurfaceValue(48);
	//set the number of sampling voxels 
	marcher->SetNumSamplingVoxels(128,128,128);
	//begin tetrahedra marching
	marcher->MarchVolume();

	//setup the volume marcher vertex array object and vertex buffer object
	glGenVertexArrays(1, &volumeMarcherVAO);
	glGenBuffers(1, &volumeMarcherVBO);
	glBindVertexArray(volumeMarcherVAO);
	glBindBuffer (GL_ARRAY_BUFFER, volumeMarcherVBO);

	//pass the obtained vertices from the tetrahedra marcher and pass to the
	//buffer object memory
	glBufferData (GL_ARRAY_BUFFER, marcher->GetTotalVertices()*sizeof(Vertex), marcher->GetVertexPointer(), GL_STATIC_DRAW);

	//enable vertex attribute array for position
	glEnableVertexAttribArray(0);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex),0);

	//enable vertex attribute array for normals
	glEnableVertexAttribArray(1);
	glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE,sizeof(Vertex),(const GLvoid*)offsetof(Vertex, normal));

	GL_CHECK_ERRORS

	//load the shader 
	shader.LoadFromFile(GL_VERTEX_SHADER, "shaders/marcher.vert");
	shader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/marcher.frag");

	//compile and link the shader program
	shader.CreateAndLinkProgram();
	shader.Use();
		//add attribute and uniform
		shader.AddAttribute("vVertex");
		shader.AddAttribute("vNormal");
		shader.AddUniform("MVP");
	shader.UnUse();

	GL_CHECK_ERRORS

	//set the background colour
	glClearColor(bg.r, bg.g, bg.b, bg.a);

	//enable depth test and culling
	glEnable(GL_DEPTH_TEST);
	glEnable(GL_CULL_FACE);
	cout<<"Initialization successfull"<<endl;
}
//OpenGL initialization
void OnInit() {
	GL_CHECK_ERRORS

	//load shader
	shader.LoadFromFile(GL_VERTEX_SHADER, "shaders/shader.vert");
	shader.LoadFromFile(GL_FRAGMENT_SHADER, "shaders/shader.frag");
	//compile and link shader
	shader.CreateAndLinkProgram();
	shader.Use();
		//add attributes and uniforms
		shader.AddAttribute("vVertex");
		shader.AddUniform("textureMap");
		//pass values of constant uniforms at initialization
		glUniform1i(shader("textureMap"), 0);
	shader.UnUse();

	GL_CHECK_ERRORS

	//setup quad geometry
	//setup quad vertices
	vertices[0] = glm::vec2(0.0,0.0);
	vertices[1] = glm::vec2(1.0,0.0);
	vertices[2] = glm::vec2(1.0,1.0);
	vertices[3] = glm::vec2(0.0,1.0);

	//fill quad indices array
	GLushort* id=&indices[0];
	*id++ =0;
	*id++ =1;
	*id++ =2;
	*id++ =0;
	*id++ =2;
	*id++ =3;

	GL_CHECK_ERRORS

	//setup quad vao and vbo stuff
	glGenVertexArrays(1, &vaoID);
	glGenBuffers(1, &vboVerticesID);
	glGenBuffers(1, &vboIndicesID);

	glBindVertexArray(vaoID);

		glBindBuffer (GL_ARRAY_BUFFER, vboVerticesID);
		//pass quad vertices to buffer object
		glBufferData (GL_ARRAY_BUFFER, sizeof(vertices), &vertices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS
		//enable vertex attribute array for position
		glEnableVertexAttribArray(shader["vVertex"]);
		glVertexAttribPointer(shader["vVertex"], 2, GL_FLOAT, GL_FALSE,0,0);
		GL_CHECK_ERRORS
		//pass quad indices to element array buffer
		glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vboIndicesID);
		glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(indices), &indices[0], GL_STATIC_DRAW);
		GL_CHECK_ERRORS


	//load the image using SOIL
	int texture_width = 0, texture_height = 0, channels=0;
	GLubyte* pData = SOIL_load_image(filename.c_str(), &texture_width, &texture_height, &channels, SOIL_LOAD_AUTO);
	if(pData == NULL) {
		cerr<<"Cannot load image: "<<filename.c_str()<<endl;
		exit(EXIT_FAILURE);
	}
	//vertically flip the image on Y axis since it is inverted
	int i,j;
	for( j = 0; j*2 < texture_height; ++j )
	{
		int index1 = j * texture_width * channels;
		int index2 = (texture_height - 1 - j) * texture_width * channels;
		for( i = texture_width * channels; i > 0; --i )
		{
			GLubyte temp = pData[index1];
			pData[index1] = pData[index2];
			pData[index2] = temp;
			++index1;
			++index2;
		}
	}
	//setup OpenGL texture and bind to texture unit 0
	glGenTextures(1, &textureID);
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D, textureID);
		//set texture parameters
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);

		//allocate texture 
		glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, texture_width, texture_height, 0, GL_RGB, GL_UNSIGNED_BYTE, pData);

	//free SOIL image data
	SOIL_free_image_data(pData);

	GL_CHECK_ERRORS

	cout<<"Initialization successfull"<<endl;
}
Beispiel #28
0
void InitGL() { 
	glClearColor(1,1,1,1);
	glGenQueries(1, &query); 
	glGenQueries(1, &t_query); 

	texture_size_x =  numX+1;
	texture_size_y =  numY+1;

	CHECK_GL_ERRORS
	startTime = (float)glutGet(GLUT_ELAPSED_TIME);
	// get ticks per second
    QueryPerformanceFrequency(&frequency);

    // start timer
    QueryPerformanceCounter(&t1);

	 
	size_t i=0, j=0, count=0;
	int l1=0, l2=0; 
	int v = numY+1;
	int u = numX+1;

	printf("Total triangles: %3d\n",numX*numY*2);
	indices.resize( numX*numY*2*3);
	 
	
	X.resize(total_points);
	X_last.resize(total_points); 
	F.resize(total_points);
  
	//fill in positions
	for(int j=0;j<=numY;j++) {		 
		for(int i=0;i<=numX;i++) {	 
			X[count] = glm::vec4( ((float(i)/(u-1)) *2-1)* hsize, sizeX+1, ((float(j)/(v-1) )* sizeY),1);
			X_last[count] = X[count];
			count++;
		}
	} 

	//fill in indices
	GLushort* id=&indices[0];
	for (int i = 0; i < numY; i++) {        
		for (int j = 0; j < numX; j++) {            
			int i0 = i * (numX+1) + j;            
			int i1 = i0 + 1;            
			int i2 = i0 + (numX+1);            
			int i3 = i2 + 1;            
			if ((j+i)%2) {                
				*id++ = i0; *id++ = i2; *id++ = i1;                
				*id++ = i1; *id++ = i2; *id++ = i3;            
			} else {                
				*id++ = i0; *id++ = i2; *id++ = i3;                
				*id++ = i0; *id++ = i3; *id++ = i1;            
			}        
		}    
	}

	glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
	
	glEnable(GL_CULL_FACE);
	
	glEnable(GL_LINE_SMOOTH);
	glHint(GL_LINE_SMOOTH_HINT, GL_NICEST);
	glEnable(GL_BLEND);
	glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
	
	glCullFace(GL_BACK); 
	glEnable(GL_VERTEX_PROGRAM_POINT_SIZE);

	//Disable vsync
	wglSwapIntervalEXT(0);

	// Setup springs
	// Horizontal
	for (l1 = 0; l1 < v; l1++)	// v
		for (l2 = 0; l2 < (u - 1); l2++) {
			AddSpring((l1 * u) + l2,(l1 * u) + l2 + 1,KsStruct,KdStruct);
		}

	// Vertical
	for (l1 = 0; l1 < (u); l1++)	
		for (l2 = 0; l2 < (v - 1); l2++) {
			AddSpring((l2 * u) + l1,((l2 + 1) * u) + l1,KsStruct,KdStruct);
		}

	// Shearing Springs
	for (l1 = 0; l1 < (v - 1); l1++)	
		for (l2 = 0; l2 < (u - 1); l2++) {
			AddSpring((l1 * u) + l2,((l1 + 1) * u) + l2 + 1,KsShear,KdShear);
			AddSpring(((l1 + 1) * u) + l2,(l1 * u) + l2 + 1,KsShear,KdShear);
		}

	// Bend Springs
	for (l1 = 0; l1 < (v); l1++) {
		for (l2 = 0; l2 < (u - 2); l2++) {
			AddSpring((l1 * u) + l2,(l1 * u) + l2 + 2,KsBend,KdBend);
		}
		AddSpring((l1 * u) + (u - 3),(l1 * u) + (u - 1),KsBend,KdBend);
	}
	for (l1 = 0; l1 < (u); l1++) {
		for (l2 = 0; l2 < (v - 2); l2++) {
			AddSpring((l2 * u) + l1,((l2 + 2) * u) + l1,KsBend,KdBend);
		}
		AddSpring(((v - 3) * u) + l1,((v - 1) * u) + l1,KsBend,KdBend);
	}


	massSpringShader.LoadFromFile(GL_VERTEX_SHADER, "shaders/Spring.vp");
	particleShader.LoadFromFile(GL_VERTEX_SHADER,"shaders/Basic.vp");
	particleShader.LoadFromFile(GL_FRAGMENT_SHADER,"shaders/Basic.fp");
	renderShader.LoadFromFile(GL_VERTEX_SHADER,"shaders/Passthrough.vp");
	renderShader.LoadFromFile(GL_FRAGMENT_SHADER,"shaders/Passthrough.fp");
	massSpringShader.CreateAndLinkProgram();
	massSpringShader.Use();
		massSpringShader.AddAttribute("position_mass");
		massSpringShader.AddAttribute("prev_position");		 
		massSpringShader.AddUniform("tex_position_mass");
		massSpringShader.AddUniform("tex_pre_position_mass");
		massSpringShader.AddUniform("MVP");		
		massSpringShader.AddUniform("dt");
		massSpringShader.AddUniform("gravity");
		massSpringShader.AddUniform("ksStr");
		massSpringShader.AddUniform("ksShr");
		massSpringShader.AddUniform("ksBnd");
		massSpringShader.AddUniform("kdStr");
		massSpringShader.AddUniform("kdShr");
		massSpringShader.AddUniform("kdBnd");
		massSpringShader.AddUniform("DEFAULT_DAMPING"); 
		massSpringShader.AddUniform("texsize_x"); 
		massSpringShader.AddUniform("texsize_y"); 
		massSpringShader.AddUniform("step"); 
		massSpringShader.AddUniform("inv_cloth_size");
		massSpringShader.AddUniform("ellipsoid");

		glUniform1i(massSpringShader("tex_position_mass"), 0); 
		glUniform1i(massSpringShader("tex_pre_position_mass"), 1); 
		
	massSpringShader.UnUse();

	CHECK_GL_ERRORS

	
	particleShader.CreateAndLinkProgram();
	particleShader.Use();
		particleShader.AddAttribute("position_mass");
		particleShader.AddUniform("pointSize");
		particleShader.AddUniform("MV");
		particleShader.AddUniform("MVP");
		particleShader.AddUniform("vColor");
		particleShader.AddUniform("selected_index");
		glUniform1f(particleShader("pointSize"), pointSize);
		glUniform4fv(particleShader("vColor"),1, vRed);
	particleShader.UnUse();
 
	renderShader.CreateAndLinkProgram();
	renderShader.Use();
		renderShader.AddAttribute("position_mass");
		renderShader.AddUniform("MVP");
		renderShader.AddUniform("vColor");
		glUniform4fv(renderShader("vColor"),1, vGray);
	renderShader.UnUse();

	CHECK_GL_ERRORS

	//create vbo 
	createVBO(); 
 
	//setup transform feedback attributes	    	 
	glGenTransformFeedbacks(1, &tfID);
	glBindTransformFeedback(GL_TRANSFORM_FEEDBACK, tfID); 
	const char* varying_names[]={"out_position_mass", "out_prev_position"};	
	glTransformFeedbackVaryings(massSpringShader.GetProgram(), 2, varying_names, GL_SEPARATE_ATTRIBS);		
	glLinkProgram(massSpringShader.GetProgram());

  
}
//display callback function
void OnRender() {

	//increment the radius
	radius+=dx;

	//if radius is beyond limits, invert the movement direction
	if(radius<1 || radius>5)
		dx=-dx;

	GL_CHECK_ERRORS

	//clear colour buffer
	glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

	//set the camera transform
	glm::mat4 T		= glm::translate(glm::mat4(1.0f),glm::vec3(0.0f, 0.0f, dist));
	glm::mat4 Rx	= glm::rotate(T,  rX, glm::vec3(1.0f, 0.0f, 0.0f));
	glm::mat4 MV    = glm::rotate(Rx, rY, glm::vec3(0.0f, 1.0f, 0.0f));

	//get the eye position
	eyePos.x = -(MV[0][0] * MV[3][0] + MV[0][1] * MV[3][1] + MV[0][2] * MV[3][2]);
    eyePos.y = -(MV[1][0] * MV[3][0] + MV[1][1] * MV[3][1] + MV[1][2] * MV[3][2]);
    eyePos.z = -(MV[2][0] * MV[3][0] + MV[2][1] * MV[3][1] + MV[2][2] * MV[3][2]);

	//p is to translate the sphere to bring it to the ground level
	glm::vec3 p=glm::vec3(0,1,0);

	//when rendering to the CUBEMAP texture, we move all of cubes by opposite amount
	//so that the projection is clearly visible
	T = glm::translate(glm::mat4(1), -p);
	 
	//set the viewport to the size of the cube map texture
	glViewport(0,0,CUBEMAP_SIZE,CUBEMAP_SIZE);

	//bind the FBO
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboID);

		//set the GL_TEXTURE_CUBE_MAP_POSITIVE_X to the colour attachment of FBO
		glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_X, dynamicCubeMapID, 0);
		//clear the colour and depth buffers
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		//set the virtual viewrer at the reflective object center and render the scene
		//using the cube map projection matrix and appropriate viewing settings
		glm::mat4 MV1 = glm::lookAt(glm::vec3(0),glm::vec3(1,0,0), glm::vec3(0,-1,0));
 		DrawScene( MV1*T, Pcubemap);

		//set the GL_TEXTURE_CUBE_MAP_NEGATIVE_X to the colour attachment of FBO
		glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, dynamicCubeMapID, 0);
		//clear the colour and depth buffers
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		//set the virtual viewrer at the reflective object center and render the scene
		//using the cube map projection matrix and appropriate viewing settings
		glm::mat4 MV2 = glm::lookAt(glm::vec3(0),glm::vec3(-1,0,0), glm::vec3(0,-1,0));
		DrawScene( MV2*T, Pcubemap);
		
		//set the GL_TEXTURE_CUBE_MAP_POSITIVE_Y to the colour attachment of FBO		
		glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Y, dynamicCubeMapID, 0);
		//clear the colour and depth buffers 
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		//set the virtual viewrer at the reflective object center and render the scene
		//using the cube map projection matrix and appropriate viewing settings
		glm::mat4 MV3 = glm::lookAt(glm::vec3(0),glm::vec3(0,1,0), glm::vec3(1,0,0));
		DrawScene( MV3*T, Pcubemap);

		//set the GL_TEXTURE_CUBE_MAP_NEGATIVE_Y to the colour attachment of FBO		
		glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, dynamicCubeMapID, 0);
		//clear the colour and depth buffers
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		//set the virtual viewrer at the reflective object center and render the scene
		//using the cube map projection matrix and appropriate viewing settings
		glm::mat4 MV4 = glm::lookAt(glm::vec3(0),glm::vec3(0,-1,0), glm::vec3(1,0,0));
		DrawScene( MV4*T, Pcubemap);

		//set the GL_TEXTURE_CUBE_MAP_POSITIVE_Z to the colour attachment of FBO		
		glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, dynamicCubeMapID, 0);
		//clear the colour and depth buffers
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		//set the virtual viewrer at the reflective object center and render the scene
		//using the cube map projection matrix and appropriate viewing settings
		glm::mat4 MV5 = glm::lookAt(glm::vec3(0),glm::vec3(0,0,1), glm::vec3(0,-1,0));
		DrawScene(MV5*T, Pcubemap);

		//set the GL_TEXTURE_CUBE_MAP_NEGATIVE_Z to the colour attachment of FBO		
		glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, dynamicCubeMapID, 0);
		//clear the colour and depth buffers
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
		//set the virtual viewrer at the reflective object center and render the scene
		//using the cube map projection matrix and appropriate viewing settings
		glm::mat4 MV6 = glm::lookAt(glm::vec3(0),glm::vec3(0,0,-1), glm::vec3(0,-1,0));
		DrawScene( MV6*T, Pcubemap);

	//unbind the FBO
	glBindFramebuffer(GL_DRAW_FRAMEBUFFER, 0);

	//reset the default viewport 
	glViewport(0,0,WIDTH,HEIGHT);

	//render scene from the camera point of view and projection matrix
	DrawScene(MV, P);

	//bind the sphere vertex array object
	glBindVertexArray(sphereVAOID);

	//use the cubemap shader to render the reflective sphere
	cubemapShader.Use();
		//set the sphere transform
		T = glm::translate(glm::mat4(1), p);
		//set the shader uniforms
		glUniformMatrix4fv(cubemapShader("MVP"), 1, GL_FALSE, glm::value_ptr(P*(MV*T)));
		glUniform3fv(cubemapShader("eyePosition"), 1,  glm::value_ptr(eyePos));
			//draw the sphere triangles
			glDrawElements(GL_TRIANGLES,indices.size(),GL_UNSIGNED_SHORT,0);

	//unbind shader
	cubemapShader.UnUse();

	//swap front and back buffers to show the rendered result
	glutSwapBuffers();
}
Beispiel #30
0
void RenderGPU_TF() { 
	massSpringShader.Use();
		glUniformMatrix4fv(massSpringShader("MVP"), 1, GL_FALSE, glm::value_ptr(mMVP));					
		glUniform1f(massSpringShader("dt"),  timeStep);
		glUniform3fv(massSpringShader("gravity"),1,&gravity.x);
	 	glUniform1f(massSpringShader("ksStr"),  KsStruct); 
		glUniform1f(massSpringShader("ksShr"),  KsShear); 
		glUniform1f(massSpringShader("ksBnd"),  KsBend); 
		glUniform1f(massSpringShader("kdStr"),  KdStruct/1000.0f); 
		glUniform1f(massSpringShader("kdShr"),  KdShear/1000.0f); 
		glUniform1f(massSpringShader("kdBnd"),  KdBend/1000.0f); 
		glUniform1f(massSpringShader("DEFAULT_DAMPING"),  DEFAULT_DAMPING);  
		glUniform1i(massSpringShader("texsize_x"),texture_size_x);
		glUniform1i(massSpringShader("texsize_y"),texture_size_y);
		
		glUniform2f(massSpringShader("inv_cloth_size"),float(sizeX)/numX,float(sizeY)/numY);		
		glUniform2f(massSpringShader("step"),1.0f/(texture_size_x-1.0f),1.0f/(texture_size_y-1.0f));
	
		for(int i=0;i<NUM_ITER;i++) {
			glActiveTexture( GL_TEXTURE0);					
			glBindTexture( GL_TEXTURE_BUFFER, texPosID[writeID]);

			glActiveTexture( GL_TEXTURE1);					
			glBindTexture( GL_TEXTURE_BUFFER, texPrePosID[writeID]);
						
			glBindVertexArray( vaoUpdateID[writeID]);
				glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 0, vboID_Pos[readID]);
				glBindBufferBase(GL_TRANSFORM_FEEDBACK_BUFFER, 1, vboID_PrePos[readID]);
				glEnable(GL_RASTERIZER_DISCARD);    // disable rasterization
					glBeginQuery(GL_TIME_ELAPSED,t_query); 
						glBeginTransformFeedback(GL_POINTS); 							
							glDrawArrays(GL_POINTS, 0, total_points);							
						glEndTransformFeedback();
					glEndQuery(GL_TIME_ELAPSED); 
					glFlush();
				glDisable(GL_RASTERIZER_DISCARD);	

			int tmp = readID;
			readID=writeID;
			writeID = tmp;
		}
		// get the query result 
		glGetQueryObjectui64v(t_query, GL_QUERY_RESULT, &elapsed_time); 		
		delta_time = elapsed_time / 1000000.0f;	
	massSpringShader.UnUse();
				 
	CHECK_GL_ERRORS;

	glBindVertexArray(vaoRenderID[writeID]);
		glDisable(GL_DEPTH_TEST);
			renderShader.Use();
				glUniformMatrix4fv(renderShader("MVP"), 1, GL_FALSE, glm::value_ptr(mMVP));						
				glDrawElements(GL_TRIANGLES, indices.size(), GL_UNSIGNED_SHORT,0);								 
			renderShader.UnUse();
		glEnable(GL_DEPTH_TEST);
		 
		if(bDisplayMasses) {
			particleShader.Use();
				glUniform1i(particleShader("selected_index"), selected_index);
				glUniformMatrix4fv(particleShader("MV"), 1, GL_FALSE, glm::value_ptr(mMV));	
				glUniformMatrix4fv(particleShader("MVP"), 1, GL_FALSE, glm::value_ptr(mMVP));										
					//draw the masses last						
			  		glDrawArrays(GL_POINTS, 0, total_points);		
					//glDrawTransformFeedbackStream(GL_POINTS, tfID, 0); 
			particleShader.UnUse();		
		}
				
		glBindVertexArray( 0);	 
	
	CHECK_GL_ERRORS

	
}