Skip to main content
edited title
Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

How to create a depth-writingrecording shader for shadow mapping?

Clarifying title
Source Link
DMGregory
  • 136.3k
  • 22
  • 247
  • 373

How to create a persepctie matrix that stores depth-writing shader for shadow mapping?

I am attempting to implement shaodwshadow maps.

I am first going to implement spotlights over directional lights (I am aware this is harder).

SInceSince I was already rendering the geometry, my attempt was to recycle my old shaders and modify them to make shaders to create the depth maps.

This created the following vertex shader:

#version 450

layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex

layout(std430, binding = 3) buffer data_buffer
{
    vec4 cubes_info[];//first 3 values are position of object 
};

uniform mat4 view = mat4(1); //Camera orientation and position
uniform mat4 proj = mat4(1); //The projection parameters (FOV, viewport dimensions)

void main()
{
    gl_Position = proj*view*(vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));
}

And fragment shader:

#version 450

layout(location = 0) out float fragmentdepth;

void main()
{
    fragmentdepth = gl_FragCoord.z;
}

As you can see, both shaders are pretty absicbasic, they are a little more complex than they would otherwise be since they are using instancing, but overall they are simple.

However the output of these shaders is:

[![enter image description here][1]][1]Screenshot of voxel terrain, in full 0xFF0000 red

Where the original image is: [![enter image description here][2]][2]

Screenshot of grey-white voxel terrain, with lihting applied

As you can see, the depth is being lost.

I think this is because my perspective, projection and model matrices are identical for both shaders.

In other words, I think that the camera I use for rendering the second image does not store the depth information. I wanted to know how can you generate a similar projection matrix to store the depth? [1]: https://i.sstatic.net/edyZW.png [2]: https://i.sstatic.net/Ju8G9.png

How to create a persepctie matrix that stores depth?

I am attempting to implement shaodw maps.

I am first going to implement spotlights over directional lights (I am aware this is harder).

SInce I was already rendering the geometry, my attempt was to recycle my old shaders and modify them to make shaders to create the depth maps.

This created the following vertex shader:

#version 450

layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex

layout(std430, binding = 3) buffer data_buffer
{
    vec4 cubes_info[];//first 3 values are position of object 
};

uniform mat4 view = mat4(1); //Camera orientation and position
uniform mat4 proj = mat4(1); //The projection parameters (FOV, viewport dimensions)

void main()
{
    gl_Position = proj*view*(vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));
}

And fragment shader:

#version 450

layout(location = 0) out float fragmentdepth;

void main()
{
    fragmentdepth = gl_FragCoord.z;
}

As you can see both shaders are pretty absic, they are a little more complex than they would otherwise be since they are using instancing, but overall they are simple.

However the output of these shaders is:

[![enter image description here][1]][1]

Where the original image is: [![enter image description here][2]][2]

As you can see, the depth is being lost.

I think this is because my perspective, projection and model matrices are identical for both shaders.

In other words, I think that the camera I use for rendering the second image does not store the depth information. I wanted to know how can you generate a similar projection matrix to store the depth? [1]: https://i.sstatic.net/edyZW.png [2]: https://i.sstatic.net/Ju8G9.png

How to create a depth-writing shader for shadow mapping?

I am attempting to implement shadow maps.

I am first going to implement spotlights over directional lights (I am aware this is harder).

Since I was already rendering the geometry, my attempt was to recycle my old shaders and modify them to make shaders to create the depth maps.

This created the following vertex shader:

#version 450

layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex

layout(std430, binding = 3) buffer data_buffer
{
    vec4 cubes_info[];//first 3 values are position of object 
};

uniform mat4 view = mat4(1); //Camera orientation and position
uniform mat4 proj = mat4(1); //The projection parameters (FOV, viewport dimensions)

void main()
{
    gl_Position = proj*view*(vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));
}

And fragment shader:

#version 450

layout(location = 0) out float fragmentdepth;

void main()
{
    fragmentdepth = gl_FragCoord.z;
}

As you can see, both shaders are pretty basic, they are a little more complex than they would otherwise be since they are using instancing, but overall they are simple.

However the output of these shaders is:

Screenshot of voxel terrain, in full 0xFF0000 red

Where the original image is:

Screenshot of grey-white voxel terrain, with lihting applied

As you can see, the depth is being lost.

I think this is because my perspective, projection and model matrices are identical for both shaders.

In other words, I think that the camera I use for rendering the second image does not store the depth information. I wanted to know how can you generate a similar projection matrix to store the depth?

Source Link
Makogan
  • 270
  • 1
  • 14

How to create a persepctie matrix that stores depth?

I am attempting to implement shaodw maps.

I am first going to implement spotlights over directional lights (I am aware this is harder).

SInce I was already rendering the geometry, my attempt was to recycle my old shaders and modify them to make shaders to create the depth maps.

This created the following vertex shader:

#version 450

layout(location = 0) in vec3 position; //(x,y,z) coordinates of a vertex

layout(std430, binding = 3) buffer data_buffer
{
    vec4 cubes_info[];//first 3 values are position of object 
};

uniform mat4 view = mat4(1); //Camera orientation and position
uniform mat4 proj = mat4(1); //The projection parameters (FOV, viewport dimensions)

void main()
{
    gl_Position = proj*view*(vec4(position, 1.0) + vec4(vec3(cubes_info[gl_InstanceID]),0));
}

And fragment shader:

#version 450

layout(location = 0) out float fragmentdepth;

void main()
{
    fragmentdepth = gl_FragCoord.z;
}

As you can see both shaders are pretty absic, they are a little more complex than they would otherwise be since they are using instancing, but overall they are simple.

However the output of these shaders is:

[![enter image description here][1]][1]

Where the original image is: [![enter image description here][2]][2]

As you can see, the depth is being lost.

I think this is because my perspective, projection and model matrices are identical for both shaders.

In other words, I think that the camera I use for rendering the second image does not store the depth information. I wanted to know how can you generate a similar projection matrix to store the depth? [1]: https://i.sstatic.net/edyZW.png [2]: https://i.sstatic.net/Ju8G9.png