Transforming normals from worldspace to the viewsp

2019-08-09 07:48发布

问题:

Let's say I have my normals in worldspace and I want to transform them into viewspace. What should I do? I have already tried to multiply them by viewProjection matrix and by inversed transformed view matrix, but both didn't work. Blue channel (z axis) appears to be missing. I am confused.

回答1:

  1. on fixed pipeline do nothing it does the work for you

    to work properly all world/object transforms should be inside modelview matrix else some advanced things like FOG will not work properly (ie GL_PROJECTION matrix abuse) but in most cases you do not see a thing ...

  2. on shaders you can handle lighting your self so this is the way

    point'  = object_2_world_matrix * world_2_camera_matrix * projection_matrix * point
    vector' = object_2_world_matrix(with origin 0,0,0) * world_2_camera_matrix(with origin 0,0,0) * vector
    


    object_2_world_matrix is transformation matrix representing rendered model space
    world_2_camera_matrix is inverted transformation matrix representing camera space
    Z axis is usually view direction
    projection_matrix is just perspective matrix (used for rendering ...)

    you can make any matrix with origin (0,0,0) by setting its 12,13,14 element to zero (elements are 0,..,15)

You did not remove matrix positions which transforms normal vectors to position of vertex ... That is the reason for weirdness ... so either substract the offset of matrices from the result or multiply by matrices without offset (origin = (0,0,0))