# Directional Light ViewSpace Calculation

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?

• 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.