Shadow2DProj - No impact?

Started by MattCa, December 14, 2012, 19:37:37

Previous topic - Next topic

MattCa

Hey Guys,
I've been working on this for nearly a day now, and I've yet to find any way to fix this.

I'm using shadow2DProj to create Shadows in my scene, but it is having no impact, and no shadows are created.

However, when I change shadow2DProj to shadow2D, it appears to draw the actual depth map to the cube I am drawing.

I'm pretty sure it's just me not setting a GL property, but I'm close to giving up!

Below is my the code I am using to create the FBO and set up the depth map:
    private void setupDepthRendering() {
        depthTexture = GL11.glGenTextures();
        GL11.glBindTexture(GL11.GL_TEXTURE_2D, depthTexture);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameterf(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D,  GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);

        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_DEPTH_COMPONENT, 800, 600, 0, GL11.GL_DEPTH_COMPONENT, GL11.GL_FLOAT, (FloatBuffer)null);

        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL14.GL_TEXTURE_COMPARE_MODE, GL14.GL_COMPARE_R_TO_TEXTURE);

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);

        fbo = GL30.glGenFramebuffers();
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo);
        GL30.glFramebufferTexture2D(GL30.GL_FRAMEBUFFER, GL30.GL_DEPTH_ATTACHMENT, GL11.GL_TEXTURE_2D, depthTexture, 0);
        GL11.glDrawBuffer(GL11.GL_NONE);
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
    }


Here is the code I am using to render to the shadow map:
    private void drawShadowScene() {
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, fbo);
        GL11.glViewport(0, 0, 800, 600);
        GL11.glColorMask(false, false, false, false);

        OpenGL.clearScreen();

        lightCamera.lookThrough();

        updateLightingMatrix();

        GL11.glPushMatrix();
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex3f(-20.0f, 0.0f, -20.0f);
        GL11.glVertex3f(20.0f, 0.0f, -20.0f);
        GL11.glVertex3f(20.0f, 0.0f, 0.0f);
        GL11.glVertex3f(-20.0f, 0.0f, 0.0f);
        GL11.glEnd();
        GL11.glPopMatrix();

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glPushMatrix();
        drawCube(0.0f, 2.0f, -10.0f, 1.0f, 1.0f, 1.0f, false, false);
        GL11.glPopMatrix();

        GL11.glColorMask(true, true, true, true);
        GL11.glViewport(0, 0, 800, 600);
        GL30.glBindFramebuffer(GL30.GL_FRAMEBUFFER, 0);
    }


Here is the code to render the scene:
    private void drawCombinedScene() {
        OpenGL.clearScreen();

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, depthTexture);

        shadowMapShader.enable();
        shadowMapShader.setMatrix4Property("lightingMatrix", lightMatrix);
        shadowMapShader.setProperty("depthSampler", 0);

        player.render();

        GL11.glPushMatrix();
        GL11.glColor3f(0.0f, 0.0f, 1.0f);
        GL11.glBegin(GL11.GL_QUADS);
        GL11.glVertex3f(-20.0f, 0.0f, -20.0f);
        GL11.glVertex3f(20.0f, 0.0f, -20.0f);
        GL11.glVertex3f(20.0f, 0.0f, 0.0f);
        GL11.glVertex3f(-20.0f, 0.0f, 0.0f);
        GL11.glEnd();
        GL11.glPopMatrix();

        GL11.glColor3f(1.0f, 0.0f, 0.0f);
        GL11.glPushMatrix();
        drawCube(0.0f, 2.0f, -10.0f, 1.0f, 1.0f, 1.0f, false, false);
        GL11.glPopMatrix();

        shadowMapShader.disable();

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, 0);
    }


Here is my light matrix/bias matrix code:
    public void updateLightingMatrix() {
        FloatBuffer modelView = BufferUtils.createFloatBuffer(16);
        FloatBuffer projection = BufferUtils.createFloatBuffer(16);

        // Texture bias matrix
        float[] biasValues = {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};
        FloatBuffer biasMatrix = (FloatBuffer)BufferUtils.createFloatBuffer(16).put(biasValues).flip();

        GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, modelView);
        GL11.glGetFloat(GL11.GL_PROJECTION_MATRIX, projection);

        GL11.glPushMatrix();

        GL11.glLoadIdentity();
        GL11.glLoadMatrix(biasMatrix);

        GL11.glMultMatrix(modelView);
        GL11.glMultMatrix(projection);

        lightMatrix = BufferUtils.createFloatBuffer(16);
        GL11.glGetFloat(GL11.GL_MODELVIEW_MATRIX, lightMatrix);

        GL11.glPopMatrix();
    }


Finally, here are my vertex and fragment shaders:
varying vec4 shadowCoordinate;

uniform mat4 lightingMatrix;
uniform sampler2DShadow depthSampler;

void main() {
    shadowCoordinate = lightingMatrix * gl_Vertex;
    gl_FrontColor = gl_Color;
    gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
}


and

varying vec4 shadowCoordinate;

uniform sampler2DShadow depthSampler;

void main() {
    float shadow = 1.0f;
    shadow = shadow2DProj(depthSampler, shadowCoordinate).z;

    gl_FragColor = shadow;
}


Thanks for any help you can give, I'm at my wits end with shadows!