Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

glm::lookAt returns matrix with nan elements

I want to create a view matrix for a camera which perpendicularly look at the ground:

glm::mat4 matrix = glm::lookAt(glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(0.0f, 0.0f, 0.0f), glm::vec3(0.0f, 1.0f, 0.0f));

The last argument is the global up vector so everything seems to be correct but I get following matirx:

-nan    -nan    -0  0   
-nan    -nan     1  0   
-nan    -nan    -0  0   
 nan     nan    -1  1

I guess that I get nan because a look at vector is parallel to up vector, but how can I build a correct view matrix using glm::lookAt function.

like image 524
Irbis Avatar asked Nov 24 '15 08:11

Irbis


1 Answers

The problem is with either your camera's position, or the up vector.

Your camera is 1 unit up (0,1,0), looking down at the origin (0,0,0). The up vector indicates the up direction of the camera, not the world space. For example, if you're looking forward, the up vector would be +Y. If you're looking down, with the top of your head facing +X, then the up vector is +X to you. It has to be something that's not at all parallel with the position vector of the camera.

Solutions:

  • Changing the up vector to anything along the XZ plane
  • or to something that's not (0,0,0) when projected onto the XZ plane
  • Move your camera so that it's anywhere but along the Y axis
like image 185
Alex Avatar answered Oct 11 '22 22:10

Alex