<p>I'm new to openGL and I was following <a href="https://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/" rel="nofollow noreferrer">this</a> tutorial in which they showed how to make shadow maps but I could't understand how to get it to work for non-directional lights. They have <a href="https://www.opengl-tutorial.org/intermediate-tutorials/tutorial-16-shadow-mapping/#spot-lights" rel="nofollow noreferrer">some instructions</a> on how to extend it to work for point lights as well but I could't understand them and couldn't get it to work when trying to figure it out. Full source code of the tutorial <a href="https://github.com/opengl-tutorials/ogl/tree/master/tutorial16_shadowmaps" rel="nofollow noreferrer">here</a> (simple version .cpp file and shadowmapping shaders) and here follows the snippets I'm working on. First the code for the directional light that works well:</p>
<p>main .cpp file</p>
<pre><code>// Compute the MVP matrix from the light's point of view
glm::vec3 lightInvDir = glm::vec3(0.5f,2,2);
glm::mat4 depthProjectionMatrix = glm::ortho&lt;float&gt;(-10,10,-10,10,-10,20);
glm::mat4 depthViewMatrix = glm::lookAt(lightInvDir, glm::vec3(0,0,0), glm::vec3(0,1,0));

glm::mat4 depthModelMatrix = glm::mat4(1.0);
glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;

// Send our transformation to the currently bound shader, 
// in the &quot;MVP&quot; uniform
glUniformMatrix4fv(depthMatrixID, 1, GL_FALSE, &amp;depthMVP[0][0]);

...

// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
glm::mat4 ProjectionMatrix = getProjectionMatrix();
glm::mat4 ViewMatrix = getViewMatrix();
//ViewMatrix = glm::lookAt(glm::vec3(14,6,4), glm::vec3(0,1,0), glm::vec3(0,1,0));
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;

glm::mat4 normaliseMatrix(
    0.5, 0.0, 0.0, 0.0, 
    0.0, 0.5, 0.0, 0.0,
    0.0, 0.0, 0.5, 0.0,
    0.5, 0.5, 0.5, 1.0
);

// Send our transformation to the currently bound shader, 
// in the &quot;MVP&quot; uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &amp;MVP[0][0]);
glUniformMatrix4fv(NormaliseID, 1, GL_FALSE, &amp;normaliseMatrix[0][0]);
glUniformMatrix4fv(DepthMatrixID, 1, GL_FALSE, &amp;depthMVP[0][0]);

</code></pre>
<p>vertex shader</p>
<pre><code>// Output position of the vertex, in clip space : MVP * position
gl_Position =  MVP * vec4(vertexPosition_modelspace,1);

ShadowCoord = normaliseMVP * depthMVP * vec4(vertexPosition_modelspace,1);
</code></pre>
<p>fragment shader</p>
<pre><code>// Light emission properties
vec3 LightColor = vec3(1,1,1);

float visibility = texture( shadowMap, vec3(ShadowCoord.xy, (ShadowCoord.z)/ShadowCoord.w) );

color = visibility * LightColor;
</code></pre>
<p>And I've tried to edit it per their instructions but I had a hard time understanding what to do. I've tried different things but my code looks something something like the following (only showing the edited rows from the snippets above):</p>
<p>main .cpp file</p>
<pre><code>glm::vec3 lightPos(5, 20, 20);
glm::mat4 depthProjectionMatrix = glm::perspective&lt;float&gt;(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
glm::mat4 depthViewMatrix = glm::lookAt(lightPos, lightPos-lightInvDir, glm::vec3(0,1,0));
</code></pre>
<p>fragment shader</p>
<pre><code>float visibility= 1.0f;
if ( texture( shadowMap, ShadowCoord.xy ).z  &lt; ShadowCoord.z ){
    visibility = 0.0f;
}
</code></pre>
<p>This hasn't worked and I have no idea why or what needs to be done differently. The changes to the fragment shader also throw error messages that I don't know how to resolve.</p>
<p><a href="https://i.sstatic.net/OkZDx.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/OkZDx.png" alt="Directional light" /></a>
Image 1: The directional light that works well, roughly what I expect to see from a point light too.</p>
<p><a href="https://i.sstatic.net/7fU2i.png" rel="nofollow noreferrer"><img src="https://i.sstatic.net/7fU2i.png" alt="No shadows" /></a>
Image 2: Most of my failed attempts show no sign of a shadow.</p>

CC BY-SA 4.0
2022-12-04T22:23:29.887Z
I don't see the cube map creation info here, are you using a cube map for the point light shadows?
pmw1234
CC BY-SA 4.0
2022-12-05T15:34:16.043Z
Have you divided the ShadowCoord by its "w" component? because of the perspective projection your coordinate is weighted wrong. Whenever you calculate Position = Matrix*Vector and "Matrix" is a perspective projection, you need to divide the result (Position) by its "w" component
Thomas
CC BY-SA 4.0
2022-12-06T08:16:58.763Z
I have not used a cube map, I thought I'd start with getting it to work in just a single direction before implementing omnidirectional. And I believe I have missed dividing by w, thank you.
n-l-i
CC BY-SA 4.0
2022-12-06T09:28:26.163Z
@n-l-i does it work now?
Thomas
<opengl><shadow-mapping>
Shadow mapping with non-directional light
n-l-i
# Shadow mapping with non-directional light

(Asked by: username_0 on 2022-12-03)

I'm new to openGL and I was following this tutorial in which they showed how to make shadow maps but I could't understand how to get it to work for non-directional lights. They have some instructions on how to extend it to work for point lights as well but I could't understand them and couldn't get it to work when trying to figure it out. Full source code of the tutorial here (simple version .cpp file and shadowmapping shaders) and here follows the snippets I'm working on. First the code for the directional light that works well:
main .cpp file
// Compute the MVP matrix from the light's point of view
glm::vec3 lightInvDir = glm::vec3(0.5f,2,2);
glm::mat4 depthProjectionMatrix = glm::ortho(-10,10,-10,10,-10,20);
glm::mat4 depthViewMatrix = glm::lookAt(lightInvDir, glm::vec3(0,0,0), glm::vec3(0,1,0));

glm::mat4 depthModelMatrix = glm::mat4(1.0);
glm::mat4 depthMVP = depthProjectionMatrix * depthViewMatrix * depthModelMatrix;

// Send our transformation to the currently bound shader, 
// in the "MVP" uniform
glUniformMatrix4fv(depthMatrixID, 1, GL_FALSE, &depthMVP[0][0]);

...

// Compute the MVP matrix from keyboard and mouse input
computeMatricesFromInputs();
glm::mat4 ProjectionMatrix = getProjectionMatrix();
glm::mat4 ViewMatrix = getViewMatrix();
//ViewMatrix = glm::lookAt(glm::vec3(14,6,4), glm::vec3(0,1,0), glm::vec3(0,1,0));
glm::mat4 ModelMatrix = glm::mat4(1.0);
glm::mat4 MVP = ProjectionMatrix * ViewMatrix * ModelMatrix;

glm::mat4 normaliseMatrix(
    0.5, 0.0, 0.0, 0.0, 
    0.0, 0.5, 0.0, 0.0,
    0.0, 0.0, 0.5, 0.0,
    0.5, 0.5, 0.5, 1.0
);

// Send our transformation to the currently bound shader, 
// in the "MVP" uniform
glUniformMatrix4fv(MatrixID, 1, GL_FALSE, &MVP[0][0]);
glUniformMatrix4fv(NormaliseID, 1, GL_FALSE, &normaliseMatrix[0][0]);
glUniformMatrix4fv(DepthMatrixID, 1, GL_FALSE, &depthMVP[0][0]);


vertex shader
// Output position of the vertex, in clip space : MVP * position
gl_Position =  MVP * vec4(vertexPosition_modelspace,1);

ShadowCoord = normaliseMVP * depthMVP * vec4(vertexPosition_modelspace,1);

fragment shader
// Light emission properties
vec3 LightColor = vec3(1,1,1);

float visibility = texture( shadowMap, vec3(ShadowCoord.xy, (ShadowCoord.z)/ShadowCoord.w) );

color = visibility * LightColor;

And I've tried to edit it per their instructions but I had a hard time understanding what to do. I've tried different things but my code looks something something like the following (only showing the edited rows from the snippets above):
main .cpp file
glm::vec3 lightPos(5, 20, 20);
glm::mat4 depthProjectionMatrix = glm::perspective(glm::radians(45.0f), 1.0f, 0.1f, 100.0f);
glm::mat4 depthViewMatrix = glm::lookAt(lightPos, lightPos-lightInvDir, glm::vec3(0,1,0));

fragment shader
float visibility= 1.0f;
if ( texture( shadowMap, ShadowCoord.xy ).z  < ShadowCoord.z ){
    visibility = 0.0f;
}

This hasn't worked and I have no idea why or what needs to be done differently. The changes to the fragment shader also throw error messages that I don't know how to resolve.

Image 1: The directional light that works well, roughly what I expect to see from a point light too.

Image 2: Most of my failed attempts show no sign of a shadow.

**Tags:** opengl, shadow-mapping
> **Comments:**
> **username_1**: I don't see the cube map creation info here, are you using a cube map for the point light shadows?  
> **username_2**: Have you divided the ShadowCoord by its "w" component? because of the perspective projection your coordinate is weighted wrong. Whenever you calculate Position = Matrix*Vector and "Matrix" is a perspective projection, you need to divide the result (Position) by its "w" component  
> **username_0**: I have not used a cube map, I thought I'd start with getting it to work in just a single direction before implementing omnidirectional. And I believe I have missed dividing by w, thank you.  
> **username_2**: @n-l-i does it work now?  

---

## 0 answers

