Deffered Shading

I am suppose to implement a shader in GLSL using deferred shading.

As I understand I can write to the textures like this.

gl_FragData[0] = gl_Color;
gl_FragData[1] = vec4(vNormal, pixelDepth);
gl_FragData[2] = vec4(worldPosition, 1.0);
gl_FragData[3] = vec4(diff_coeff, phong_coeff, two_sided, 1.0);

What I don’t understand is how to use the textures in the final rendering. Should this be done in fragment shader?
How is this suppose to work?

Best regards

The code uses MRT for baking camera space data into 4 textures.

In the light application stage you unproject the pixel into the world space, compute lighting based on position,normal & coefficients (sampling from the textures you have) and contribute to the pixel color. This, of course is done in a fragment shader.

I am not asking you to write the shader but i would be very helpful if you could demonstrate what you mean.

How can I access the textures the in fragment shader? Could you show me how to access texture 1(of 4) in my fragment shader.

Thanks for your help.

I don’t get it, isato. Why are you asking about Deferred Shading in GLSL here if you don’t know the basics: sampling from the texture? Draw a full-screen textured quad first, at least.

#version 130

uniform sampler2D unit_normal;

void main(){
 vec2 tc = gl_FragCoord.xy;
 vec4 normal = texture(unit_normal,tc);

The reason I am asking is that its an assignment. I am a student.

Thanks for your code I think I understand the shading code. The uniforms are variables accessible from both shaders and given bye the main program.

I guess I am asking for the basics then. I have been trying to understand. I recently read 

Look at this example

#version 110

void main()
	gl_FragData[0] = vec4(0.0, 1.0, 0.0);
	gl_FragData[1] = vec4(0.0, 0.0, 1.0);

There is no uniforms declared. How do I know witch textures is on witch index.

uniform sampler2D unit_normal;// I guess this is gl_FragData[1]? 

gl_FragData[0] = gl_Color;
gl_FragData[1] = vec4(vNormal, pixelDepth);
gl_FragData[2] = vec4(worldPosition, 1.0);
gl_FragData[3] = vec4(diff_coeff, phong_coeff, two_sided, 1.0)


The baking of the G-buffer and its application are two different stages.

For the baking, you attach destination textures to FBO & draw to them at once (MRT) like in the code provided.

For the application, you are just using this 4 textures.
Don’t ask the basics here, read about them on OpenGL wiki.

For shader reference I can point you here:


Is deferred shading the same as deferred lightning?

I am asking because when googling deferred lightning first hit is wiki deferred shading.

No, I believe Deferred Lighting is also referred to as Light Pre-pass. Google it, possibly in combination with Wolfgang Engel. Some good stuff on it in SIGGRAPH 2009 IIRC and also on his blog.

Deferred Shading: store material attributes on framebuffer, and then come back with shader lights and light them.

Deferred Lighting: store light attributes on the framebuffer, and then come back with shader materials and light them.

I am trying here to build my G-Buffer. This is an example i found but i don’t understand the color is stored in gl_FragData[2]. This example does not work without a texture on each geometry?

varying vec3 position, normal;

void main(void)
	position = vec3(gl_ModelViewMatrix * gl_Vertex);
	normal = gl_NormalMatrix * gl_Normal;
	gl_Position = ftransform();
	gl_TexCoord[0] = gl_MultiTexCoord0;
#extension GL_ARB_draw_buffers : enable

varying vec3 position, normal;
uniform sampler2D Tex0;

void main(void)
	gl_FragData[0] = vec4(, 0.0);
	gl_FragData[1] = vec4(normalize(, 0.0);
	gl_FragData[2] = texture2D(Tex0, gl_TexCoord[0].st);

Not sure I understand your question. If you don’t have a textures, stuff black, white, or whatever makes sense given your lighting equation in there.

I was thinking if my geometry was colored using for glColor.
Pretend that I have a red quad without any texture, just colored with glColor. That will not go in this G-buffer?

IIRC, you usually store your material albedo(s) per texel in the G-buffer. Whether it comes from the vertex color, 1 or more textures, or both, makes no difference to the technique. It’s all in how you write the shader and populate the gl_FragData[i] outputs.