San Andreas
San Andreas

Reputation: 35

Shadow mapping for point light

The view matrix in OpenGL is mostly defined by the glm::lookAt() function with 3 parameters: position vector, target vector and up vector.

So, why these lines of code use for shadow mapping for point light define the last parameter (I mean up vector) like this:

float aspect = (float)SHADOW_WIDTH/(float)SHADOW_HEIGHT;
float near = 1.0f;
float far = 25.0f;
// This is the projection matrix
glm::mat4 shadowProj = glm::perspective(glm::radians(90.0f), aspect, near, far); 

// This is for view matrix
std::vector<glm::mat4> shadowTransforms;
shadowTransforms.push_back(shadowProj * 
                 glm::lookAt(lightPos, lightPos + glm::vec3( 1.0, 0.0, 0.0), glm::vec3(0.0,-1.0, 0.0));
shadowTransforms.push_back(shadowProj * 
                 glm::lookAt(lightPos, lightPos + glm::vec3(-1.0, 0.0, 0.0), glm::vec3(0.0,-1.0, 0.0));
shadowTransforms.push_back(shadowProj * 
                 glm::lookAt(lightPos, lightPos + glm::vec3( 0.0, 1.0, 0.0), glm::vec3(0.0, 0.0, 1.0));
shadowTransforms.push_back(shadowProj * 
                 glm::lookAt(lightPos, lightPos + glm::vec3( 0.0,-1.0, 0.0), glm::vec3(0.0, 0.0,-1.0));
shadowTransforms.push_back(shadowProj * 
                 glm::lookAt(lightPos, lightPos + glm::vec3( 0.0, 0.0, 1.0), glm::vec3(0.0,-1.0, 0.0));
shadowTransforms.push_back(shadowProj * 
                 glm::lookAt(lightPos, lightPos + glm::vec3( 0.0, 0.0,-1.0), glm::vec3(0.0,-1.0, 0.0))

Why not glm::vec3(0.0, 1.0, 0.0) for all six faces?

Upvotes: 1

Views: 215

Answers (1)

derhass
derhass

Reputation: 45352

Why not glm::vec3(0.0, 1.0, 0.0) for all six faces?

Because that wouldn't make the slightest sense. If you define your view transform via a lookAt function, you specify the camera position and the viewing direction. What the 3D up vector actually is defining is the angle of rotation around the view axis (so it is actually only one degree of freedom).

Think of rotating a real camera into landscape or portrait orientation, or in any arbitrary rotation., to get the image you want.

Since the lookAt convention has always been that the up vector is some world space vector which should be mapped to the upward axis in the resulting image, you will get a problem if you define the up vector into the same direction as your viewing direction (or it's negated direction). It simply is impossible to map the same vector to both point into the viewing direction and upwards in the resulting image,and it does not describe any orientation at all.

So in the case of 2 of the 6 faces, the math would simply break down. In the case of the other 4 faces, you could technically use (0,1,0) for all of these. However, usually, you use these sort of configuration to render to the 6 faces of a cube map texture. And for cube maps, the actual orientation of each face is defined in the GL spec. So for directly rendering to cube maps, you must orient your camera accordingly, otherwise the individual faces of the cube map will simply be rotated wrongly and won't fit together at all.

Upvotes: 1

Related Questions