mas*_*nlo 0 opengl matrix unity-game-engine gl-matrix
我正在尝试实施:
transform.InverseTransformPoint(Vector3) 和 transform.InverseTransformDirection(Vector3) 在 opengl 中使用 glm 库。我有每个对象的视图、投影、模型矩阵。
实际上我不知道我必须用这个矩阵做什么才能达到那个方法的功能。
通常,可以通过执行以下数学运算将局部空间中的点转换为 NDC 空间:
Pworld = M * Plocal;
Pview = V * Pworld;
Pndc = P * Pview;
Run Code Online (Sandbox Code Playgroud)
其中 M = 模型,V = 视图,P = 投影。
所以,如果你在世界坐标系中有一个点并想在局部坐标系中得到它,你只需要反转第一个方程:
Plocal = inv(M) * Pworld;
Run Code Online (Sandbox Code Playgroud)
这应该相当于transform.InverseTransformPoint(Vector3)(只需添加第四个坐标向量 H = 1)
要实现transform.InverseTransformDirection(Vector3)不受规模影响的 ,您必须使用以下等式:
Plocal = transpose(inverse(M)) * Pworld
Run Code Online (Sandbox Code Playgroud)
其中 M 是原始模型的左上角 3x3 矩阵。为了理解为什么你应该使用这个数学,我邀请你看这个页面:正常变换