Question

I am confused about shadow mapping. Here's what I've understood (folowing steps are not working:) )

How to get profit (please don't get confused about the code, it is roughly because I write on Java):

1. Create empty depthTexture (mine is 1024x1024) with parameters

glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE, GL_COMPARE_R_TO_TEXTURE) and

glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT, width, height, 0, GL_DEPTH_COMPONENT, GL_FLOAT, GL_NULL)

2. Create FBO and attach that texture to it

glFramebufferTexture(GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, depthTexture, 0)

3. Setup new projection and view matrix for lighting camera

I used the same as my main camera just with another coords, because scene looks better in it (I tried it out, so there's no problem... I guess ..).

4. Create new little shader to determinate gl_Position for FBO and modify the main shader with fancy stuff (bias matrix * lightCamera matrix * vertex, sampler2Dshadow and more)

5. Of course uniform everything and do something.

And now RENDER LOOP

1. Of course uniform everything and do something(again).

2. Bind FBO, bind little shader, glViewport(0, 0, 1024, 1024), colorMask to false, clear depth buffer, glCullFace(GL_FRONT), glBindTexture(GL_TEXTURE_2D, 0)

3. Render everything using only vertex position attribs completely for nothing

4. Unbind FBO, rebind program to main shader (that one with fancy stuff), glViewport, enable colorMask, glCullFace(GL_BACK)

5. Render the scene normally without even thinking about "How the hell main shader gonna get sampler2DShadow because we dont bind it, dont uniform it, and don't even touch it"

6. Watch countless glitches, bugs and pixel orgy

Actually I tried to uniform depthTexture to sampler, but I've got black screen only. And even if I dont render FBO, I get the same picture when I do that.

Can someone explain, what am I missing?

I feel like shader uses diffuse texture twice: as a diffuse texture and as a depthTexture, but I don't know how to give it that depthTexture.

Was it helpful?

Solution

here is a good link to read: http://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/

and here: http://www.paulsprojects.net/tutorials/smt/smt.html (although the second link uses old fixed function opengl)

in general:

  1. create one depth texture
  2. attach this texture to FBO
  3. bind FBO, setup proper viewport
  4. render scene from light pos, save only depth values to your texture
  5. unbind FBO and setup final scene vieport and camera position
  6. render scene normally using shadow test (sampler2DShadow)

you can render your depth map always (in render loop) or only when light pos changes.

I do not know why you are rendering you scene normally twice... are you using Z-prepass, or something? just try the basic version I think.

my old code with shadow maps:

void RenderShadowMap() {
    currDepth->Bind();
    glClear(GL_DEPTH_BUFFER_BIT);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gLightCam.SetProjectionMatrix();
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
    gLightCam.SetViewMatrix();

    glColorMask(false, false, false, false);
    glUseProgram(0);   // draw without any shaders... just default depth
    glEnable(GL_POLYGON_OFFSET_FILL);
    glPolygonOffset(offFactor, offUnits);
    SimpleScene(false); // floor does not cast shadow so do not render it
    glDisable(GL_POLYGON_OFFSET_FILL);
    glColorMask(true, true, true, true);
}

render scene:

// compose shadow matrix:
MATRIX4X4 bias(0.5f, 0.0f, 0.0f, 0.0f, 
               0.0f, 0.5f, 0.0f, 0.0f,
               0.0f, 0.0f, 0.5f, 0.0f,
               0.5f, 0.5f, 0.5f, 1.0f);
MATRIX4X4 *invCam = gSphericalCam.GetInvViewMatrix();
MATRIX4X4 smMat = (*gLightCam.GetViewProjMatrix()) * (*invCam);

gShaderProgramManager->GetProgram("shadow")->Use();
gShaderProgramManager->GetProgram("shadow")->SetMatrix("shadowMat", &smMat);
gShaderProgramManager->GetProgram("shadow")->SetBool("useShadow", currCam != &gLightCam && useShadow);
gShaderProgramManager->GetProgram("shadow")->SetFloat("shadowMapSize", (float)currDepth->GetWidth());
glActiveTexture(GL_TEXTURE0);
glEnable(GL_TEXTURE_2D);
glColor3f(1.0f, 1.0f, 1.0f);
gTextureManager->Bind("default");
glActiveTexture(GL_TEXTURE1);
currDepth->BindDepthAsTexture();
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_MODE_ARB, GL_COMPARE_R_TO_TEXTURE_ARB);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_COMPARE_FUNC_ARB, GL_LEQUAL);

SimpleScene();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top