Shadow's in OpenGL Core

I would like to try and implement some shadows in my application but I can’t seem to find any tutorials on using OpenGL Core specification. As I understand it this is how to do shadow mapping in a nutshell and would like some clarification on whether this approach seems correct. If anyone knows some tutorials for OpenGL Core and not using glut shortcuts I could really use those as well, or book recommendations.

  1. Create a FBO and set as target to render to
  2. Create texture matrix:

texMatrix = (projectionMatrix * modelViewMatrix) * inverse(modelViewMatrix);

  1. Apply with shaders, Vertex:

uniform mat4 texMatrix; //calculated texture matrix
uniform mat4 mvMatrix;  //model view matrix
varying vec4 shadowTexCoord;
void main () {
 shadowTexCoord = texMatrix * mvMatrix * glVertex;
}

Fragment:


uniform sampler2Dshadow shadowMap;
varying vec4 shadowTexCoord;
void main() {
 gl_FragColor = shadow2Dproj(shadowMap , shadowTexCoord).r * glColor;
}

These shaders are just “in a nutshell” and don’t include all the in/out/uniforms for everything. I got this information from http://wiki.vrmedia.it/index.php?title=Shadow_Mapping

Follow the link in my signature. I have written simple shadow mapping sample. Note, also that in core profile you should not use varying keyword or built-in variables like gl_FragColor.

Yes, thank you. I don’t plan to use varying or gl_Fragcolor I only used those to be clear on the sample shaders. I will look at your shadow mapping sample.

I can’t seem to figure this out, it is very difficult to track what I am doing wrong here. To start, I am simply trying to render depth information to a texture. In the shader I tried outputting FragCoord.z directly and I always get pure white. I tried dividing by my screen depth and that resulted in pure black. I set glDepthRange(0,1); to make sure I was getting normalized depth values but it appears as though I’m not even getting any depth values. I set up my context with:


glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);

It would also seem my FBO isn’t being set up properly, every time I check it’s status I get a value of 0 which is not the value of GL_FRAMEBUFFER_COMPLETE. Here is how I set up my FBO:


glActiveTexture(GL_TEXTURE1);
glGenTextures(1, &texShadow);
glBindTexture(GL_TEXTURE_2D, texShadow);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_REF_TO_TEXTURE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC, GL_LEQUAL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, gameVars.screen.w, gameVars.screen.h, 0, GL_RGBA, GL_FLOAT, NULL);

glGenFramebuffers(1,  &fboShadow);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fboShadow);
glDrawBuffer(GL_NONE);
glReadBuffer(GL_NONE);
glFramebufferTexture2D(GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, texShadow, 0);

I can render the proper view from my camera position, but I just can’t figure out how to get the depth information into the texture to make use of it. I am new to FBO’s and am having a hard time finding up to date examples of it’s use. Any help is greatly appreciated, I have been struggling with this for 2 days with no luck.

I’ve made a little progress, I realized I had made a stupid mistake to make the FBO not initialize. I had the init() function before the context creation so I fixed that and now the FBO is being created successfully, however it seems the depth values still aren’t being written to the texture.

If I go into my shaders and set the color value to:


vFragcolor = vec4(gl_FragCoord.z, gl_FragCoord.z, gl_FragCoord.z, 1 );

All I get is pure white, shouldn’t this give me a greyscale image of the fragment depths?

I realize I’m pretty much on my own here but I would still like to post updates. I have my depth buffer functioning finally. I had to switch to orthographic view and set a specific near and far range to get the depth values correct. I am down to 2 problems at this point:

  1. I apparently still cannot get the depth data written to a FBO texture. The texture is always pure white.
  2. Create a proper texture matrix to apply the shadow texture

I am stuck on the FBO problem, I have looked through many different setups and can’t figure out why mine isn’t working.

Camera View overlayed over standard view showing depth buffer:

The texture is always pure white.

How do you know that?

Only because if I try to render it with the routine for applying the shadow to the scene it does nothing, when rendered to a quad it is white. The texture matrix I have is currently wrong but it will still apply the malformed texture to the scene.

However this is my first time using depth textures so I could be mistaken in it’s use. However I got the code from randalls sample code and most of my code is identical to that. Also the superbible 5e doesn’t talk about using depth textures.

EDIT: I don’t know exactly how I did it, but the texture is suddenly working now. I think it might have been because the texture got bound and used before the FBO wrote to it.

Well I finally got everything working, other than some buffer fighting it’s working rather well. Just need to fine tune the normals. My problem with the texture matrix I think was the order of operations which I had to solve by sending the data into the shader and calculating it there once I had all of the data. I can probably streamline that to function faster though.