openglunity-game-enginematrixgl-matrix

how to convert world space transform to object space transform?


i'm trying to implement :

transform.InverseTransformPoint(Vector3) and transform.InverseTransformDirection(Vector3) using glm library in opengl. i have view ,projection ,model matrices for each object.

actually i don't know what i must to do with this matrices for got to that methods functionality.


Solution

  • Usually, a point in local space can be transformed to NDC space by doing the following math:

    Pworld = M * Plocal;
    Pview = V * Pworld;
    Pndc = P * Pview;
    

    where M = model, V = view and P = projection.

    So, if you have a point in world coordinate system and want to get it in local coordinate system, you just have to invert the first equation:

    Plocal = inv(M) * Pworld;
    

    This should be equivalent to transform.InverseTransformPoint(Vector3) (just add a fourth coordinate vector H = 1)

    To implement transform.InverseTransformDirection(Vector3), which is not affected by scale, you must use this equation:

    Plocal = transpose(inverse(M)) * Pworld
    

    where M is the upper-left 3x3 matrix from your original Model. To understand why you sould use this math, I invite you to look at this page: normal transformation