1
votes

I'm curently learning to create shadown using GLSL but I have some troubles here:
1. In GLSL 3.3, we can use this statement in fragment shader:

layout(location = 0) out float fragmentdepth;

to write only depth 16bit out to texture (setted as GL_DEPTH_COMPONENT16 before) but how can I do some thing like that in OpenGL 2.1 (GLSL 1.20)?

  1. As far as I know, for rendering depth buffer, we only need change the camera position to light position and camera direction to light direction and changed back if we are drawing real scene, Is it right?
2
I think you should attach depth buffer to an fbo, and render to gl_FragDepth; I'm not sure if you won't need an extension for that.Bartek Banachewicz
@BartekBanachewicz: I have used glew in my projects, thanks you, i'll try gl_FragDepth :)Bình Nguyên

2 Answers

3
votes

In GLSL 3.3, we can use this statement in fragment shader:

to write only depth 16bit out to texture (setted as GL_DEPTH_COMPONENT16 before)

No, you can't.

That will set fragmentdepth to write to a color buffer. And you cannot attach an image with the GL_DEPTH_COMPONENT16 image format to a GL_COLOR_ATTACHMENTi attachment point of an FBO. Attempting to do so will give you a GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT error.

In all versions of GLSL, the only way to write to the attached depth buffer is to write to gl_FragDepth. And in all versions of GLSL, you can only have one depth buffer attached to the FBO. Though Image Load/Store does allow you to work around that, but you lose depth testing and such.

1
votes

As stated here you need to use FBO extension (EXT) if targeting OpenGL 2.1.Also why on earth are you still using fixed pipeline?It is deprecated .If your hardware allows you - use OpenGL 3.3+ and then leverage (core) FBOs with texture attachments (or renderbuffers) to which you can draw depth buffer data.Yes,you can still do the same with the deprecated profile but well,the modern OpenGL made a huge step forward since then.

Anyways , here is what you need for your version.