Jump to content
  • Advertisement
Sign in to follow this  
Enalis

OpenGL Light Accumulation with Deferred Shading with GLSL Woes

This topic is 4324 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have been working on a deferred shading implementation for glsl in opengl. I have a function that should in theory draw a quad and store the info for that quad in three different buffers, and then I have shaders that make use of that info for light accumulation and then later blending and hdr effects. The problem right now is in the light accum process. My function is currently using two color buffers from an fbo and flipping back and forth. The shader pulls info from color buffer 3 and renders to 4 combining the new light with the values from buffer 3, next light it flips back. Will this work, how would I just use an accumulation buffer either with the Frame Buffer Object or without? How is this normally stored? Here is some code.
	// setup our aux buffers
	GLint currentDrawBuf;
	glGetIntegerv(GL_DRAW_BUFFER, &currentDrawBuf); // Save the current Draw buffer

	// do initial info gathering pass
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, renderer.FBO);
	glReadBuffer(GL_NONE);
	glDrawBuffer(GL_NONE);
	deferredShader.TurnOn();

	GLenum buffers[] = {GL_COLOR_ATTACHMENT0_EXT, GL_COLOR_ATTACHMENT1_EXT, GL_COLOR_ATTACHMENT2_EXT};
	glDrawBuffers(3, buffers);

	glPushMatrix();
	glViewport(0, 0, width, height);

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

	
	deferredShader.SetFloat3("cameraPos", GLSL_UNIFORM, camera.xPos, camera.yPos, camera.zPos);
	deferredShader.SetFloat3("cameraDir", GLSL_UNIFORM, camera.xView - camera.xPos, camera.yView - camera.yPos, camera.zView - camera.zPos);
	deferredShader.SetFloat("depthNear", GLSL_UNIFORM, 0.1f);
	deferredShader.SetFloat("depthFar", GLSL_UNIFORM, 5050.0f);
	deferredShader.SetFloat("specular", GLSL_UNIFORM, 0.75f);	// get from object material
	deferredShader.SetInt("decalMap", 0);						// object material
	deferredShader.SetInt("normalMap", 1);						// object material
	deferredShader.SetInt("heightMap", 2);						// object material

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D, parallaxTestTextures[DECAL_TEXTURE]);
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, parallaxTestTextures[NORMAL_TEXTURE]);
	glActiveTexture(GL_TEXTURE2);
	glBindTexture(GL_TEXTURE_2D, parallaxTestTextures[HEIGHT_TEXTURE]);

//	DrawSomeObjects(0.0f, 0.0f, 0.0f, 1.0f);
	// Draw our wall.  Keep in mind we are sending the s tangent through glColor3f().
	glBegin(GL_QUADS);
//		parallaxShader.SetFloat3("tangent", GLSL_ATTRIBUTE, 1.0f, 0.0f, 0.0f);
//		parallaxShader.SetFloat3("binormal", GLSL_ATTRIBUTE, 0.0f, 1.0f, 0.0f);
		glTexCoord2f(0.0f, 0.0f);	glNormal3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-10.0f, -10.0f, 0.0f);
//		parallaxShader.SetFloat3("tangent", GLSL_ATTRIBUTE, 1.0f, 0.0f, 0.0f);
//		parallaxShader.SetFloat3("binormal", GLSL_ATTRIBUTE, 0.0f, 1.0f, 0.0f);
		glTexCoord2f(1.0f, 0.0f);	glNormal3f(0.0f, 0.0f, 1.0f);
		glVertex3f(10.0f, -10.0f, 0.0f);
//		parallaxShader.SetFloat3("tangent", GLSL_ATTRIBUTE, 1.0f, 0.0f, 0.0f);
//		parallaxShader.SetFloat3("binormal", GLSL_ATTRIBUTE, 0.0f, 1.0f, 0.0f);
		glTexCoord2f(1.0f, 1.0f);	glNormal3f(0.0f, 0.0f, 1.0f);
		glVertex3f(10.0f, 10.0f, 0.0f);
//		parallaxShader.SetFloat3("tangent", GLSL_ATTRIBUTE, 1.0f, 0.0f, 0.0f);
//		parallaxShader.SetFloat3("binormal", GLSL_ATTRIBUTE, 0.0f, 1.0f, 0.0f);
		glTexCoord2f(0.0f, 1.0f);	glNormal3f(0.0f, 0.0f, 1.0f);
		glVertex3f(-10.0f, 10.0f, 0.0f);
	glEnd();

	glPopMatrix();

	deferredShader.TurnOff();
	// end initial info gathering pass

	//now do all shadow passes

	// end shadow passes

	// do all lighting passes on accumulation buffer
	glDrawBuffer(GL_COLOR_ATTACHMENT3_EXT);		// treat this like an accum buffer
	glViewport(0, 0, width, height);
//	glDrawBuffer(GL_COLOR_ATTACHMENT4_EXT);

	glPushMatrix();
	orthoMode(0, 16, 0, 12);

	lightAccumShader.TurnOn();
	bool flip = true;
	for (int i = 0; i < renderer.numOfLights; i++){
		if (flip){	// if flip = true draw to buffer 4 and read from 3
			glDrawBuffer(GL_COLOR_ATTACHMENT4_EXT);
		}else{		// vice-verca
			glDrawBuffer(GL_COLOR_ATTACHMENT3_EXT);
		}
		glActiveTexture(GL_TEXTURE0);
		glBindTexture(GL_TEXTURE_2D,renderer.FBOTextures[0]);			// Bind the Decals and specular
		glActiveTexture(GL_TEXTURE1);
		glBindTexture(GL_TEXTURE_2D,renderer.FBOTextures[1]);			// Bind the normals and depth
		glActiveTexture(GL_TEXTURE2);
		glBindTexture(GL_TEXTURE_2D,renderer.FBOTextures[2]);			// Bind the Position and free
		glActiveTexture(GL_TEXTURE3);
		if (flip){	// if flip = true read from buffer 3 and write to 4
			glBindTexture(GL_TEXTURE_2D,renderer.FBOTextures[3]);
		}else{		// vice-verca
			glBindTexture(GL_TEXTURE_2D,renderer.FBOTextures[4]);
		}
		glActiveTexture(GL_TEXTURE0);

		lightAccumShader.SetInt("input0", 0);
		lightAccumShader.SetInt("input1", 1);
		lightAccumShader.SetInt("input2", 2);
		lightAccumShader.SetInt("input3", 3);

		lightAccumShader.SetInt("lightType", 0);
		lightAccumShader.SetFloat3("lightDiffuse", GLSL_UNIFORM, renderer.lights->diffuse.x, renderer.lights->diffuse.y, renderer.lights->diffuse.z);
		lightAccumShader.SetFloat3("lightAmbient", GLSL_UNIFORM, renderer.lights->ambient.x, renderer.lights->ambient.y, renderer.lights->ambient.z);
		lightAccumShader.SetFloat3("lightAmbient", GLSL_UNIFORM, renderer.lights->specular.x, renderer.lights->specular.y, renderer.lights->specular.z);

		lightAccumShader.SetFloat("lightConstantAttenuation", GLSL_UNIFORM, renderer.lights->constantAttenuation);
		lightAccumShader.SetFloat("lightLinearAttenuation", GLSL_UNIFORM, renderer.lights->linearAttenuation);
		lightAccumShader.SetFloat("lightQuadraticAttenuation", GLSL_UNIFORM, renderer.lights->quadraticAttenuation);

		lightAccumShader.SetFloat3("lightPosition", GLSL_UNIFORM, renderer.lights->position.x, renderer.lights->position.y, renderer.lights->position.z);
		lightAccumShader.SetFloat3("lightPosition", GLSL_UNIFORM, renderer.lights->direction.x, renderer.lights->direction.y, renderer.lights->direction.z);
		lightAccumShader.SetFloat("lightPosition", GLSL_UNIFORM, renderer.lights->angle);
		lightAccumShader.SetFloat("spotExponet", GLSL_UNIFORM, renderer.lights->spotExponet);
		lightAccumShader.SetFloat("spotCutoff", GLSL_UNIFORM, renderer.lights->spotCutoff);
		lightAccumShader.SetFloat("spotCosCutoff", GLSL_UNIFORM, renderer.lights->spotCutoff);

		lightAccumShader.SetFloat3("cameraPosition", GLSL_UNIFORM, camera.xPos, camera.yPos, camera.zPos);
		lightAccumShader.SetFloat3("cameraDirection", GLSL_UNIFORM, camera.xView - camera.xPos, camera.yView - camera.yPos, camera.zView - camera.zPos);
		lightAccumShader.SetFloat("depthNear", GLSL_UNIFORM, 0.1f);
		lightAccumShader.SetFloat("depthFar", GLSL_UNIFORM, 5050.0f);

		glBegin(GL_QUADS);
		glTexCoord2f(0.0f, 0.0f);	glVertex2i(0, 0);
		glTexCoord2f(1.0f, 0.0f);	glVertex2i(16, 0);
		glTexCoord2f(1.0f, 1.0f);	glVertex2i(16, 12);
		glTexCoord2f(0.0f, 1.0f);	glVertex2i(0, 12);
		glEnd();

		glActiveTexture(GL_TEXTURE0);
		if (flip){ flip = false;
		}else{ flip = true; }
	}
	perspectiveMode();
	glPopMatrix();

	lightAccumShader.TurnOff();
	// end lighting passes

	// do final blending pass
	glBindFramebufferEXT(GL_FRAMEBUFFER_EXT, 0);
	glDrawBuffer(currentDrawBuf);
	glViewport(0, 0, width, height);

	glActiveTexture(GL_TEXTURE0);
	glBindTexture(GL_TEXTURE_2D,renderer.FBOTextures[0]);			// Bind To The Blur Texture
	glActiveTexture(GL_TEXTURE1);
	glBindTexture(GL_TEXTURE_2D, renderer.FBOTextures[1]);
	glActiveTexture(GL_TEXTURE3);
	if (flip){	// if flip = true read from buffer 3 and write to 4
		glBindTexture(GL_TEXTURE_2D,renderer.FBOTextures[4]);
	}else{		// vice-verca
		glBindTexture(GL_TEXTURE_2D,renderer.FBOTextures[3]);
	}
	glActiveTexture(GL_TEXTURE0);

	blendHDRShader.TurnOn();

	blendHDRShader.SetInt("input0", 0);
	blendHDRShader.SetInt("input1", 1);
	blendHDRShader.SetInt("input3", 3);

	glPushMatrix();
	orthoMode(0, 16, 0, 12);

	glBegin(GL_QUADS);
	glTexCoord2f(0.0f, 0.0f);	glVertex2i(0, 0);
	glTexCoord2f(1.0f, 0.0f);	glVertex2i(16, 0);
	glTexCoord2f(1.0f, 1.0f);	glVertex2i(16, 12);
	glTexCoord2f(0.0f, 1.0f);	glVertex2i(0, 12);
	glEnd();

	glActiveTexture(GL_TEXTURE0);

	perspectiveMode();
	glPopMatrix();

	blendHDRShader.TurnOff();
	// end final blending pass

Share this post


Link to post
Share on other sites
Advertisement
I think by just reading the doc I figured it out, so you write with the draw and read buffer set to the same, after you're done drawing you cal glAccum with GL_ACCUM as the first option and an n value. But my only question is what should I be setting the draw and read buffers to and what do I set them back to when I'm done, like what are they set to by default?

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!