4
\$\begingroup\$

I am using OpenGL 3.3 and am tryin to implement shadow mapping using cube maps. I have a framebuffer with a depth attachment and a cube map texture.

My question is how to design the shaders for the first pass, when creating the shadowmap.

This is my vertex shader:

in vec3 position;

uniform mat4 lightWVP;

void main()
{
    gl_Position = lightWVP * vec4(position, 1.0);
}

Now, do I even need a fragment shader in this shader pass? from what I understand after reading http://www.opengl.org/wiki/Fragment_Shader, by default gl_FragCoord.z is written to the currently attached depth component (to which my cubemap texture is bound to).

Thus I shouldnt even need a fragment shader for this pass and from what I understand, there is no other work to do in the fragment shader other than writing this value.

Is this correct?

\$\endgroup\$
4
\$\begingroup\$

Your problem is that you need to render the scene twice. One from the view of the Light and then the normal rendering pass from the camera point of view.

Now, do I even need a fragment shader in this shader pass?

Yes. According to your question you are using OpenGL 3.3 which makes it required to define a fragment shader to be able to render anything.

In your first pass (which will be rendered from the light point of view) the output need to be written to a RenderTarget whether it was the main frame buffer or your offscreen frame buffer.

What makes you write a fragment shader is not gl_FragCoord being explicitly written or not, but actually rendering the scene from two different point of views, your vertex shader where you transform the scene to the light view should be coupled with a fragment shader.

void main(){
    // Not really needed, OpenGL does it anyway
    fragmentdepth = gl_FragCoord.z;
}

The above code resembles the fragment shader for the first pass. Notice that it's really faster than other shaders that need to do alot of work like light calculation which is not really needed in your shadow pass.

\$\endgroup\$
  • \$\begingroup\$ Is there any advantage/disadvantage to using the short version TravisG posted? For example, if I provide an empty main() and have the depth buffer value writen implicitly, will the hardware be able to do any optimizations? \$\endgroup\$ – KaiserJohaan Nov 11 '13 at 13:32
  • \$\begingroup\$ no actually it's the same. Both shaders will do the same thing. But I prefer mentioning this in the shader. It is better for readability, what if someone read your shader doesn't know what openGL does by default? what if you want to experiment with the Z coord later, I don't see any harm in writing it explicitly. \$\endgroup\$ – concept3d Nov 11 '13 at 13:36
  • \$\begingroup\$ in case the shader was not empty, any branches will still write to depth buffer. If it was mentioned explicitly it will only assign it in the branch it was written in. \$\endgroup\$ – concept3d Nov 11 '13 at 13:39
3
\$\begingroup\$

You do need a fragment shader (it's required by the standard), but you can just provide an empty fragment shader.

shadowmap.frag:

void main() {}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.