예제 #1
0
int main()
{
	// Create and initialize window.
	setErrorCallbackAndInit(error_callback);
	GraphicsWindow* window = new GraphicsWindow(1200,800,"Test Window",NULL,NULL,key_callback);
	window->makeContextCurrent();

	// Initalize glew.
	initGlew();

	// Create and initialize Camera
	FreeCamera* camera = new FreeCamera(45.0f, 16.0f/9.0f, 
					0.0f, 0.0f, 
					0.1f, 10000.0f, 
					0.0004f, 3.0f,
					glm::vec3(0,0,-50), 
					glm::vec3(0,1,0), 
					glm::vec3(0,0,0), 
					true);

	camera->setViewport(window->getWindowWidth(),window->getWindowHeight(),0.5f,0.5f);

	// Load accumulation shader for accumulating all the transparent matrials, as well as their alphas.
	GLSLProgram* defaultShader = new GLSLProgram();
	defaultShader->initShaderProgram("Vert.glsl","","","","Frag.glsl");

	// Load screen filling quad shader.
	GLSLProgram* screenFillingQuadShader = new GLSLProgram();
	screenFillingQuadShader->initShaderProgram("screenFillingQuadVert.glsl","","","","screenFillingQuadFrag.glsl");

	// Load accumulation shader for accumulating all the transparent matrials, as well as their alphas.
	GLSLProgram* accumTransparencyRevealageShader = new GLSLProgram();
	accumTransparencyRevealageShader->initShaderProgram("AccumTransparencyRevealageVert.glsl","","","","AccumTransparencyRevealageFrag.glsl");

	// Load Weighted Average shader, which will be used for final compositing (a variation of screen filling quad shader using multiple textures).
	GLSLProgram* newOITCoverageShader = new GLSLProgram();
	newOITCoverageShader->initShaderProgram("NewOITCoverageVert.glsl","","","","NewOITCoverageFrag.glsl");

	// Create screen filling quad.
	Quad* screenFillingQuad = new Quad(glm::vec3(-1.0,1.0,0), glm::vec3(-1.0,-1.0,0), glm::vec3(1.0,-1.0,0), glm::vec3(1.0,1.0,0), glm::vec3(0), 0, 0, 0);
	
	screenFillingQuad->initQuad();
	screenFillingQuad->setGLSLProgram(*newOITCoverageShader);

	TextureManager::GetInstance().loadTexture("../Content/Textures/Particle_Smoke/smoke_particle_red_base.png");
	TextureManager::GetInstance().loadTexture("../Content/Textures/Particle_Smoke/smoke_particle_grey_base_2.png");
	TextureManager::GetInstance().loadTexture("../Content/Textures/Particle_Smoke/smoke_particle_grey_base_3.png");

	Quad* quad1 = new Quad(glm::vec3(100+100,100+100,-1), glm::vec3(100+100,-100+100,-1), glm::vec3(-100+100,-100+100,-1), glm::vec3(-100+100,100+100,-1), glm::vec3(0), 10, 10, 0);
	quad1->initQuad();
	quad1->initDefaultTexture(255,0,0,89);
	quad1->setTexture(TextureManager::GetInstance().getTextureHandle("../Content/Textures/Particle_Smoke/smoke_particle_grey_base_3.png"));

	Quad* quad2 = new Quad(glm::vec3(100+100,100+100,-20), glm::vec3(100+100,-100+100,-20), glm::vec3(-100+100,-100+100,-20), glm::vec3(-100+100,100+100,-20), glm::vec3(0), 10, 10, 0);
	quad2->initQuad();
	quad2->initDefaultTexture(0,255,0,89);
	quad2->setTexture(TextureManager::GetInstance().getTextureHandle("../Content/Textures/Particle_Smoke/smoke_particle_grey_base_2.png"));

	Quad* quad3 = new Quad(glm::vec3(100+100,100+100,20), glm::vec3(100+100,-100+100,20), glm::vec3(-100+100,-100+100,20), glm::vec3(-100+100,100+100,20), glm::vec3(0), 10, 10, 0);
	quad3->initQuad();
	quad3->initDefaultTexture(0,0,255,89);
	quad3->setTexture(TextureManager::GetInstance().getTextureHandle("../Content/Textures/Particle_Smoke/smoke_particle_red_base.png"));

	Quad* quad4 = new Quad(glm::vec3(100+100,100+100,-40), glm::vec3(100+100,-100+100,-40), glm::vec3(-100+100,-100+100,-40), glm::vec3(-100+100,100+100,-40), glm::vec3(0), 10, 10, 0);
	quad4->initQuad();
	quad4->initDefaultTexture(255,0,127,50);

	Quad* quad5 = new Quad(glm::vec3(100+100,100+100,40), glm::vec3(100+100,-100+100,40), glm::vec3(-100+100,-100+100,40), glm::vec3(-100+100,100+100,40), glm::vec3(0), 10, 10, 0);
	quad5->initQuad();
	quad5->initDefaultTexture(255,127,0,50);

	ModelLoader* modelImporter = new ModelLoader();
	modelImporter->importModel("../Content/Models/crytek-sponza/sponza.obj", processFlagsOnModelImport);

	// Create and initialize model
	Model* model = new Model(glm::vec3(0,0,0), "../Content/Models/crytek-sponza/");
	model->loadModel(modelImporter->getScene());

	// Create framebuffers
	std::vector<unsigned int> activeColorAttachmentsOpaque;
	std::vector<unsigned int> activeColorAttachmentsTransparent;

	Framebuffer* opaqueFrameBuffer = new Framebuffer(window->getWindowWidth(),window->getWindowHeight());
	
	activeColorAttachmentsOpaque.push_back(0);

	opaqueFrameBuffer->setColorAttachment(0);

	//frameBuffer->setDepthAttachment();
	opaqueFrameBuffer->setDepthStencilTexture();

	opaqueFrameBuffer->unbind();

	Framebuffer* accumFrameBuffer = new Framebuffer(window->getWindowWidth(),window->getWindowHeight());
	
	activeColorAttachmentsTransparent.push_back(0);
	activeColorAttachmentsTransparent.push_back(1);

	accumFrameBuffer->setColorAttachment(0);
	accumFrameBuffer->setColorAttachment(1);

	//frameBuffer->setDepthAttachment();
	accumFrameBuffer->setDepthStencilTexture();

	accumFrameBuffer->unbind();

	// Additional textures to pass for the second transparency render pass.
	std::vector<GLuint> additionalTextureHandles;
	additionalTextureHandles.push_back(accumFrameBuffer->getColorAttachment(0));
	additionalTextureHandles.push_back(accumFrameBuffer->getColorAttachment(1));

	std::vector<GLuint> opaqueTextureHandle;
	opaqueTextureHandle.push_back(opaqueFrameBuffer->getColorAttachment(0));

	// Move this to "GraphicsWindow" 
	glfwSetCursorPos(window->getWindowHandle(), (double) (window->getWindowWidth()/2.0), (double) (window->getWindowHeight()/2.0));
	
	// Move this to "Camera"
	//glClearColor(0.4f,0.6f,0.94f,0.0f);
	glClearColor(0.0f,0.0f,0.0f,0.0f);

	const float clearColorWhite = 1.0f;
	const float clearColorBlack = 0.0f;

	// Sampler
	GLuint sampler = 0;
	glGenSamplers(1, &sampler);

	glSamplerParameteri(sampler, GL_TEXTURE_WRAP_S, GL_REPEAT);  
	glSamplerParameteri(sampler, GL_TEXTURE_WRAP_T, GL_REPEAT);
	glSamplerParameteri(sampler, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
	glSamplerParameteri(sampler, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

	check_gl_error();

	// Render Loop
	while(!window->shouldClose())
	{
		//glClearColor(0.0f,0.0f,0.0f,0.0f);
		glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);

		glEnable(GL_DEPTH_TEST);
		glDepthFunc(GL_LEQUAL);
		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);

		// Update camera
		camera->camControll(window->getWindowHandle());
		camera->update();
		
		// Update shader uniforms
		model->getCurrentGLSLProgram()->use();
		model->getCurrentGLSLProgram()->setUniform("lightPosition", camera->getCamPos());
		model->getCurrentGLSLProgram()->setUniform("camPosition", camera->getCamPos());
		model->getCurrentGLSLProgram()->setUniform("viewMatrix", camera->getVMatrix());
		model->getCurrentGLSLProgram()->setUniform("normalMatrix", camera->getTranspInvMVMatrix()); // Change this!
		model->getCurrentGLSLProgram()->setUniform("VPMatrix", camera->getVPMatrix());

		opaqueFrameBuffer->clean();
		opaqueFrameBuffer->bind();
		opaqueFrameBuffer->bindForRenderPass(activeColorAttachmentsOpaque);

		model->renderOpaque();
		//quad4->render();
		//quad5->render();

		//opaqueFrameBuffer->unbind();

		//// Blitting the opaque scene depth to propperly depth test the transparen against it.
		//opaqueFrameBuffer->bindForReading();

		//accumFrameBuffer->bindForWriting();
		//glBlitFramebuffer(0, 0, window->getWindowWidth(), window->getWindowHeight(), 0, 0, window->getWindowWidth(), window->getWindowHeight(), 
		//				  GL_DEPTH_BUFFER_BIT, GL_NEAREST);

		//opaqueFrameBuffer->unbind();

		//_______________________________________________________________________________________________________________________________________________________________________________
		// Acuumulation pass
		accumTransparencyRevealageShader->use();
		accumTransparencyRevealageShader->setUniform("VPMatrix", camera->getVPMatrix());

		model->setGLSLProgram(accumTransparencyRevealageShader);
		quad1->setGLSLProgram(*accumTransparencyRevealageShader);
		quad2->setGLSLProgram(*accumTransparencyRevealageShader);
		quad3->setGLSLProgram(*accumTransparencyRevealageShader);
		quad4->setGLSLProgram(*accumTransparencyRevealageShader);
		quad5->setGLSLProgram(*accumTransparencyRevealageShader);

		accumFrameBuffer->clean();
		accumFrameBuffer->bind();
		accumFrameBuffer->bindForRenderPass(activeColorAttachmentsTransparent);
		accumFrameBuffer->cleanColorAttachment(1,clearColorWhite);

		opaqueFrameBuffer->unbind();

		// Blitting the opaque scene depth to propperly depth test the transparen against it.
		opaqueFrameBuffer->bindForReading();

		accumFrameBuffer->bindForWriting();
		glBlitFramebuffer(0, 0, window->getWindowWidth(), window->getWindowHeight(), 0, 0, window->getWindowWidth(), window->getWindowHeight(), 
						  GL_DEPTH_BUFFER_BIT, GL_NEAREST);

		opaqueFrameBuffer->unbind();

		accumFrameBuffer->bind();
		accumFrameBuffer->bindForRenderPass(activeColorAttachmentsTransparent);

		glEnable(GL_BLEND);
		glBlendFunci(0, GL_ONE, GL_ONE);
		glBlendFunci(1, GL_ZERO, GL_ONE_MINUS_SRC_ALPHA);
		//glEnable(GL_DEPTH_TEST);
		glDepthMask(GL_FALSE);
		glDisable(GL_CULL_FACE);

		model->renderTransparent();
		quad1->render();
		quad2->render();
		quad3->render();
		//quad4->render();
		//quad5->render();

		accumFrameBuffer->unbind();

		model->setGLSLProgram(defaultShader);
		quad1->setGLSLProgram(*defaultShader);
		quad2->setGLSLProgram(*defaultShader);
		quad3->setGLSLProgram(*defaultShader);
		quad4->setGLSLProgram(*defaultShader);
		quad5->setGLSLProgram(*defaultShader);

		glDisable(GL_BLEND);
		//glEnable(GL_DEPTH_TEST);
		glDepthMask(GL_TRUE);
		glEnable(GL_CULL_FACE);
		glCullFace(GL_BACK);

		//_______________________________________________________________________________________________________________________________________________________________________________
		// Final compositing pass

		//screenFillingQuad->setGLSLProgram(*screenFillingQuadShader);
		//screenFillingQuad->getCurrentShaderProgram()->use();
		//screenFillingQuad->renderWithAdditionalTextures(opaqueTextureHandle,sampler);

		newOITCoverageShader->use();

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

		screenFillingQuad->renderWithAdditionalTextures(additionalTextureHandles,sampler);

		glDepthMask(GL_TRUE);
		glDisable(GL_BLEND);
		//_______________________________________________________________________________________________________________________________________________________________________________

		// For debug!
		//screenFillingQuad->setGLSLProgram(*screenFillingQuadShader);
		//screenFillingQuad->getCurrentShaderProgram()->use();
		//screenFillingQuad->setTexture(opaqueFrameBuffer->getColorAttachment(0));
		//screenFillingQuad->render();

		accumFrameBuffer->clean();

		window->swapBuffers();

		glfwPollEvents();

		check_gl_error();
	}

	glfwTerminate();
	window->release();
	
	return 0;
}