<p>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:</p>

<pre><code>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&lt;TexturedModel, List&lt;Entity&gt;&gt; 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;
        }
    }
}
</code></pre>

<p>And my vertex/fragment shaders:</p>

<pre><code> 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);
 }
</code></pre>

<p>Any help?</p>

CC BY-SA 3.0
2016-02-09T10:30:13.477Z
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).
Dragonseel
CC BY-SA 3.0
2016-02-11T14:59:10.137Z
I tried it from different view points and with different entities. But still all I can see is a wait blank texture.
Flamy
CC BY-SA 3.0
2016-02-11T15:16:53.333Z
How do you check the framebuffer texture? And have you tried to leave out the 'out_color' from the fragment shader?
Dragonseel
CC BY-SA 3.0
2016-02-12T15:35:00.743Z
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.
Flamy
CC BY-SA 3.0
2016-02-12T21:20:02.317Z
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.
Dragonseel
CC BY-SA 3.0
2016-02-12T21:22:34.177Z
When you say GUI-system do you mean some software or a self written texture renderer with orthogonal projection? If self written you may have a slight error there an hour could give the relevant shader that reads the shadow map.
Dragonseel
<opengl><shader><shadow>
Shadow Mapping Errors
Flamy
# Shadow Mapping Errors

(Asked by: username_0 on 2016-02-09)

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?

**Tags:** opengl, shader, shadow

> **Comments:**
> **username_1**: 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).  
> **username_0**: I tried it from different view points and with different entities. But still all I can see is a wait blank texture.  
> **username_1**: How do you check the framebuffer texture? And have you tried to leave out the 'out_color' from the fragment shader?  
> **username_0**: 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.  
> **username_1**: 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.  
> **username_1**: When you say GUI-system do you mean some software or a self written texture renderer with orthogonal projection? If self written you may have a slight error there an hour could give the relevant shader that reads the shadow map.  

---

## 0 answers

