3
$\begingroup$

I recently started to create a shadow mapping system in OpenTK. I ran into a problem: The depthtexture is always completly white -> no depth. Even if the color from the shader is changed (I know it shouldn't effect anything.). Here is my shadow mapper class:

class ShadowMapper
{
    static int frameBuffer = 0;
    static int depthTexture;
    static readonly int shadowMapResolution = 1024;
    static ShadowShader shader;
    public static void Init()
    {
        shader = new ShadowShader();

        frameBuffer = GL.GenFramebuffer();
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
        depthTexture = GL.GenTexture();
        GL.BindTexture(TextureTarget.Texture2D, depthTexture);
        GL.TexImage2D(TextureTarget.Texture2D, 0, PixelInternalFormat.DepthComponent16, shadowMapResolution, shadowMapResolution, 0, PixelFormat.DepthComponent, PixelType.Float, (IntPtr)null);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMagFilter, (int)TextureMagFilter.Nearest);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureMinFilter, (int)TextureMinFilter.Nearest);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)TextureWrapMode.ClampToEdge);
        GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)TextureWrapMode.ClampToEdge);

        GL.FramebufferTexture(FramebufferTarget.Framebuffer, FramebufferAttachment.DepthAttachment, depthTexture, 0);

        GL.DrawBuffer(DrawBufferMode.None);
        Console.WriteLine(GL.CheckFramebufferStatus(FramebufferTarget.Framebuffer) == FramebufferErrorCode.FramebufferComplete);
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
    }

    public static void Render(Dictionary<TexturedModel, List<Entity>> entities, Light sun)
    {
        Matrix4 depthProjectionMatrix = Matrix4.CreateOrthographic(-10, 10, -10, 10);
        Matrix4 depthViewMatrix = Matrix4.LookAt(sun.Position, sun.ToLookPosition, new Vector3(0, 1, 0));

        GL.BindTexture(TextureTarget.Texture2D, 0);
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, frameBuffer);
        GL.Viewport(0, 0, shadowMapResolution, shadowMapResolution);

        GL.Enable(EnableCap.DepthTest);
        GL.Clear(ClearBufferMask.DepthBufferBit);
        shader.Start();
        foreach (TexturedModel model in entities.Keys)
        {
            RawModel rawModel = model.Model;
            GL.BindVertexArray(rawModel.VaoID);
            GL.EnableVertexAttribArray(0);
            foreach (Entity entity in entities[model])
            {
                Matrix4 depthModelMatrix = Maths.CreateTransformationMatrix(entity.Position, entity.RotationX, entity.RotationY, entity.RotationZ, entity.Scale);
                Matrix4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;
                shader.LoadMvpMatrix(depthMVP);

                GL.DrawElements(PrimitiveType.Triangles, rawModel.VertexCount, DrawElementsType.UnsignedInt, 0);
            }
        }

        GL.DisableVertexAttribArray(0);
        GL.BindVertexArray(0);
        GL.Disable(EnableCap.DepthTest);
        shader.Stop();
        GL.BindFramebuffer(FramebufferTarget.Framebuffer, 0);
        GL.Viewport(0, 0, Window.Instance.Width, Window.Instance.Height);
    }

    public static int DepthTexture
    {
        get
        {
            return depthTexture;
        }
    }
}

And my vertex/fragment shaders:

 Vertex Shader:
 #version 330 core

// Input vertex data, different for all executions of this shader.
in vec3 vertexPosition_modelspace;

// Values that stay constant for the whole mesh.

uniform mat4 depthMVP;

void main(){

gl_Position =  depthMVP * vec4(vertexPosition_modelspace,1);

}


Fragment Shader:
 #version 330 core



 // Ouput data

 out float fragmentdepth;
 out vec4 out_colour;


 void main(){

 // Not really needed, OpenGL does it anyway

 fragmentdepth = gl_FragCoord.z;
 out_colour = vec4(1.0, 0.0, 0.0, 0.0);
 }

Any help?

$\endgroup$
6
  • 2
    $\begingroup$ The normal depth value is not linear and goes against 1 relatively quickly. Are you sure it is really white as in value 1.0? Could be that the values are just close to 1.0 so that you cannot visibly distinguish them? (there are only 255 white-gray-black values but far more values in a big depth-buffer). $\endgroup$
    – Dragonseel
    Feb 9, 2016 at 10:30
  • $\begingroup$ I tried it from different view points and with different entities. But still all I can see is a wait blank texture. $\endgroup$
    – Flamy
    Feb 11, 2016 at 14:59
  • $\begingroup$ How do you check the framebuffer texture? And have you tried to leave out the 'out_color' from the fragment shader? $\endgroup$
    – Dragonseel
    Feb 11, 2016 at 15:16
  • $\begingroup$ I'm using a gui system to show the texture and then I screenshot it to check it with better detail in a editor program. I removed the 'out_color' but still it is just blank white space. $\endgroup$
    – Flamy
    Feb 12, 2016 at 15:35
  • $\begingroup$ Okay. My last idea is that you try to bind a color attachment that you write a static random color into. Maybe there is some optimisation bug. I got my shadow mapping running with such a "fake" texture attached. $\endgroup$
    – Dragonseel
    Feb 12, 2016 at 21:20

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.