Camera direction vector in shader?


I need the direction vector of the camera in my shader. Is there an easy way to find it in a shader ( I don’t wants the direction between the camera and the current vertex but the direction of the camera)

best regards


The camera’s direction is <0, 0, -1> in camera coordinate system. If you would like to get this vector in Model (or World) coordinate system, then multiply it by the CameraToModel (or CameraToWorld) matrix. Notice the direction <0, 0, -1> is represented as <0, 0, -1, 0> in homogenous space.

The OpenGL ModelView matrix is the ModelToCamera matrix (that is, when you multiply a vector by this matrix, you transform it from Model coordinates to Camera coordinates). Therefore, if you take its inverse, you get the ModelToCamera matrix. Multiply that by <0, 0, -1, 0> and you get the camera’s direction vector in Model space: <x, y, z, 0> (you can drop the last zero after).

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.