<p>It just depends on which space your light directions are actually specified in. You compute both your normal and view vector correctly in view space in order to perform your lighting computations in view space. So <strong>you need your normalized light direction in view space</strong>, too.</p>

<p>So it depends on how you actually transfer your light direction into the shader. Usually it's best to <strong>transform the light direction into view space before uploading it to the shader</strong> (from whatever space it's originally in in the scene, likely some kind of object space local to a certain light source, but in case of a directional light maybe already in world/view space). This has a few advantages:</p>

<ul>
<li><p>As you realized yourself already, you won't have to perform a tedious matrix transformation for each and every vertex when the light is actiually constant for all vertices of a draw call (it's probably a <code>uniform</code> variable). While a matrix multiplication per-vertex doesn't really kill anyone, there's really no need to do that extra work, in contrast to simply transforming the light direction on the CPU once a frame.</p></li>
<li><p>But there's another problem to transforming it in every shader call. Usually your light direction isn't easily specified in object space at all (or not the local object space of whatever object your currently drawing). If anything, it comes from a local light source that has its <em>own</em> model transformation, but that is <em>not</em> the one encapsulated in the current modelview matrix, which usually depends on the model you're drawing. So using the modelview matrix you transform your vertices with is most likely the wrong way. You could use the view matrix (as you do) if it is specified in world space already, but as said above, that's usually not necessary.</p></li>
</ul>

<p>So what you do <strong>depends on the space in which your light direction is stored</strong> in the scene. If it is stored in world space (e.g. the sun as the directional light example par-excellence), you just have to transform it into view space (e.g. by multiplying it with the camera/view matrix) right before uploading it into the shader (i.e. likely each frame, since it's view dependent). If it is actually view <em>independent</em> (e.g. a kind of "flashlight" always shining straight into the camera direction), you don't really have to transform it at all. And if it's local to some specific scene object (more appropriate for point light sources, though), you transform it by <em>that object</em>'s modelview matrix.</p>

CC BY-SA 3.0
2017-12-17T10:09:24.723Z
Thank you for the clarification! I shall now to avoid extra work in my shaders :)
ChaoSXDemon
CC BY-SA 3.0
2017-12-17T01:25:32.843Z
2017-12-17T01:25:32.843Z
Christian Rau
<p>I am a bit confused in terms of directional light calculation in OpenGL. Generally we care about the direction of the directional light and we use it with the normal and view (toCamera) vector to calculate lighting. Let's say I do my calculation in view-space ... would I need to multiply the direction of the directional light with ViewMatrix (and then normalize) ? I tried both and they don't seem to make a difference ... I'm leaning towards "yes I need to do that". Is this correct? Here's part of my fragment shader that does lighting for directional light:</p>

<pre><code>vec4 calculateDirectionalLightIntensities(in vec3 N, in vec3 V)
{
    vec4 result = vec4(0.0, 0.0, 0.0, 1.0);
    for(int i=0; i&lt;DirectionalLightCount; ++i)
    {
        //vec3 L = normalize( (ViewMatrix * -vec4(DirectionalLightDirections[i], 0.0)).xyz );
        vec3 L = normalize(-DirectionalLightDirections[i].xyz);
        vec4 currentResult = calculateDiffuse(N, L);
        currentResult += calculateSpecular(N, L, V);
        currentResult *= DirectionalLightIntensities[i];
        result += currentResult;
    }
    return result;
}
</code></pre>

<p>Basically N would be the normal vector and V is vector pointing towards camera in view-space and it is calculated as:</p>

<pre><code>void main()
{
    normal = normalize(NormalMatrix * vec4(NORMAL, 0.0)).xyz;       //We use 0 because normals are directional vectors
    uv0 = TEXCOORD0;

    vec4 viewSpacePosition = ModelViewMatrix * vec4(POSITION, 1.0);

#if defined(BLINN_PHONG_LIGHTING)

    toCamera = -viewSpacePosition.xyz; //CALCULATED HERE

#endif

    gl_Position = ProjectionMatrix * viewSpacePosition;
}
</code></pre>

<p>Should I be multiplying my dir of directional light in the vertex shader? If so, is there a performance hit to having multiple lights that results in having array of varying variables being set to the fragment shader?</p>

<opengl><lighting>
Directional Light ViewSpace Calculation
ChaoSXDemon
# Directional Light ViewSpace Calculation

(Asked by: username_0 on 2017-12-16)

I am a bit confused in terms of directional light calculation in OpenGL. Generally we care about the direction of the directional light and we use it with the normal and view (toCamera) vector to calculate lighting. Let's say I do my calculation in view-space ... would I need to multiply the direction of the directional light with ViewMatrix (and then normalize) ? I tried both and they don't seem to make a difference ... I'm leaning towards "yes I need to do that". Is this correct? Here's part of my fragment shader that does lighting for directional light:

```
vec4 calculateDirectionalLightIntensities(in vec3 N, in vec3 V)
{
    vec4 result = vec4(0.0, 0.0, 0.0, 1.0);
    for(int i=0; i<DirectionalLightCount; ++i)
    {
        //vec3 L = normalize( (ViewMatrix * -vec4(DirectionalLightDirections[i], 0.0)).xyz );
        vec3 L = normalize(-DirectionalLightDirections[i].xyz);
        vec4 currentResult = calculateDiffuse(N, L);
        currentResult += calculateSpecular(N, L, V);
        currentResult *= DirectionalLightIntensities[i];
        result += currentResult;
    }
    return result;
}

```

Basically N would be the normal vector and V is vector pointing towards camera in view-space and it is calculated as:

```
void main()
{
    normal = normalize(NormalMatrix * vec4(NORMAL, 0.0)).xyz;       //We use 0 because normals are directional vectors
    uv0 = TEXCOORD0;

    vec4 viewSpacePosition = ModelViewMatrix * vec4(POSITION, 1.0);

#if defined(BLINN_PHONG_LIGHTING)

    toCamera = -viewSpacePosition.xyz; //CALCULATED HERE

#endif

    gl_Position = ProjectionMatrix * viewSpacePosition;
}

```

Should I be multiplying my dir of directional light in the vertex shader? If so, is there a performance hit to having multiple lights that results in having array of varying variables being set to the fragment shader?

**Tags:** opengl, lighting


---

## 1 answers

### Answer by username_1 on 2017-12-17. Score: 3 | Accepted answer

It just depends on which space your light directions are actually specified in. You compute both your normal and view vector correctly in view space in order to perform your lighting computations in view space. So **you need your normalized light direction in view space**, too.

So it depends on how you actually transfer your light direction into the shader. Usually it's best to **transform the light direction into view space before uploading it to the shader** (from whatever space it's originally in in the scene, likely some kind of object space local to a certain light source, but in case of a directional light maybe already in world/view space). This has a few advantages:

* As you realized yourself already, you won't have to perform a tedious matrix transformation for each and every vertex when the light is actiually constant for all vertices of a draw call (it's probably a `uniform` variable). While a matrix multiplication per-vertex doesn't really kill anyone, there's really no need to do that extra work, in contrast to simply transforming the light direction on the CPU once a frame.
* But there's another problem to transforming it in every shader call. Usually your light direction isn't easily specified in object space at all (or not the local object space of whatever object your currently drawing). If anything, it comes from a local light source that has its *own* model transformation, but that is *not* the one encapsulated in the current modelview matrix, which usually depends on the model you're drawing. So using the modelview matrix you transform your vertices with is most likely the wrong way. You could use the view matrix (as you do) if it is specified in world space already, but as said above, that's usually not necessary.

So what you do **depends on the space in which your light direction is stored** in the scene. If it is stored in world space (e.g. the sun as the directional light example par-excellence), you just have to transform it into view space (e.g. by multiplying it with the camera/view matrix) right before uploading it into the shader (i.e. likely each frame, since it's view dependent). If it is actually view *independent* (e.g. a kind of "flashlight" always shining straight into the camera direction), you don't really have to transform it at all. And if it's local to some specific scene object (more appropriate for point light sources, though), you transform it by *that object*'s modelview matrix.

> **Comments:**
> **username_0**: Thank you for the clarification! I shall now to avoid extra work in my shaders :)  

---

