Maya正交投影矩阵

2024-10-01 00:14:21 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图计算玛雅的正交投影矩阵。有人知道怎么计算吗?我对透视图不感兴趣,只对相机的正交视图感兴趣。在

import math
import maya.cmds as cmds
import maya.OpenMaya as OpenMaya
import maya.OpenMayaUI as OpenMayaUI

# Get view matrix.
view = OpenMayaUI.M3dView.active3dView()
mayaProjMatrix = OpenMaya.MMatrix()
view.projectionMatrix(mayaProjMatrix)

print("\nMaya Projection:")
for x in xrange(0, 4):
    print(round(mayaProjMatrix(x, 0), 3),
          round(mayaProjMatrix(x, 1), 3),
          round(mayaProjMatrix(x, 2), 3),
          round(mayaProjMatrix(x, 3), 3))

Maya Projection:
(0.067, 0.0, 0.0, 0.0)
(0.0, 0.132, 0.0, 0.0)
(0.0, 0.0, -0.0, 0.0)
(0.0, 0.0, -1.0, 1.0)

我已经研究了很多网站,但我不太确定如何在玛雅(http://www.songho.ca/opengl/gl_projectionmatrix.html#ortho)中翻译相同的想法。在


Tags: importview视图as矩阵感兴趣透视图print
2条回答

在计算机图形学中,投影矩阵只是定义了某个体积到一个定义的标准体积(通常是立方体)的仿射或投影变换。

我不知道玛雅在这里的约定,所以我用的是GL,原则在任何情况下都是一样的。

在GL中,观察体积由单位立方体[-1,1]表示,在标准化设备空间的所有3个维度上。而投影的矩阵工作是将数据转换成剪辑空间。剪辑空间与规范化设备空间的区别在于后者在透视分割之后。但是,因为你不需要透视,只需要一个正交矩阵,除数总是1,所以在这种情况下,我们可以将规范化设备空间和剪辑空间视为相同的。

现在用你的矩阵(从评论中得到的更精确的版本,你粘贴在问题中的那个实际上使你无法重建z)并将一个点与之相乘得到:

(0.066666667  0.0          0.0          0.0    )  (x)
(0.0          0.131734838  0.0          0.0    )  (y)
(0.0          0.0         -0.000200002 -1.00002)  (z) 
(0.0          0.0          0.0          1.0    )  (1)

x' =  0.066666667 * x
y' =  0.131734838 * y
z' = -0.000200002 * z -1.00002

所以这个矩阵非常好,因为它可以简单地通过分别反演每个方程来求逆。您需要找出的是xy和{}在剪辑空间中显示体积的边缘,因此x'=-1x'=1y'=-1等等。

这将导致x的查看体积为[-15,15]、y为[-7.591,7.591](与您在评论中提到的1401/709的纵横比相匹配)和在z中的[-0.1,-5000]。在典型的GL术语中,该矩阵是由于:

^{pr2}$

(clip near和clip far的z值按惯例取反,因为相机应该沿着-z看,而clip值是距离)。

所有这些都在你给出的链接中得到了解释。我会在这里加上同样的链接,如果不是已经在问题中。。。

我想我会发布我正在使用的玛雅代码。显然,在maya中有一个正交属性,它可以帮助根据derhass的答案计算这个称为“正交宽度”(orthographic Width)。

# Get view matrix.
view = OpenMayaUI.M3dView.active3dView()
mayaProjMatrix = OpenMaya.MMatrix()
view.projectionMatrix(mayaProjMatrix)

# Get camera MFnDagPath.
dagCam = OpenMaya.MDagPath()
view.getCamera(dagCam)

width = float(view.portWidth())
height = float(view.portHeight())
aspect = width/height

n = cmds.getAttr("%s.nearClipPlane" % dagCam.fullPathName())
f = cmds.getAttr("%s.farClipPlane" % dagCam.fullPathName())

w = 2.0/cmds.getAttr("%s.orthographicWidth" % dagCam.fullPathName())
h = w * aspect
z = -2.0/(f-n)
v = -1.0 * ((f + n) / (f-n))

mat = [0.0] * 16
mat[0] = w
mat[1] = 0.0
mat[2] = 0.0
mat[3] = 0.0

mat[4] = 0.0
mat[5] = h
mat[6] = 0.0
mat[7] = 0.0

mat[8] = 0.0
mat[9] = 0.0
mat[10] = z
mat[11] = 0.0

mat[12] = 0.0
mat[13] = 0.0
mat[14] = v
mat[15] = 1.0

projMatrix = OpenMaya.MMatrix()
OpenMaya.MScriptUtil().createMatrixFromList(mat, projMatrix)

print("\nMaya Projection:")
for x in xrange(0, 4):
    print(round(mayaProjMatrix(x, 0), 9),
          round(mayaProjMatrix(x, 1), 9),
          round(mayaProjMatrix(x, 2), 9),
          round(mayaProjMatrix(x, 3), 9))

print("\nMy Projection:")
for x in xrange(0, 4):
    print(round(projMatrix(x, 0), 9),
          round(projMatrix(x, 1), 9),
          round(projMatrix(x, 2), 9),
          round(projMatrix(x, 3), 9))

Maya Projection:
(0.04116922, 0.0, 0.0, 0.0)
(0.0, 0.061226019, 0.0, 0.0)
(0.0, 0.0, -0.000200002, 0.0)
(0.0, 0.0, -1.00002, 1.0)

My Projection:
(0.04116922, 0.0, 0.0, 0.0)
(0.0, 0.061226019, 0.0, 0.0)
(0.0, 0.0, -0.000200002, 0.0)
(0.0, 0.0, -1.00002, 1.0)

相关问题 更多 >