0

背景

将 kivy 与 openGL 和 GLU 风格的函数一起使用,我得到了modelview矩阵projection的工作。没有使用模型矩阵,因此模型视图矩阵实际上只是基于相机位置的变换。

# cx,cy,cz are the location of the camera. cz is always positive
modelview = Matrix().look_at(cx,cy,cz,     # eye position coords
                             cx,cy,0.0,    # looking at this pt.
                             0.0,1.0,0.0)  # a vector pointing up

投影矩阵也很简单,可以按需要工作。

aspect_ratio = float(Window.width)/Window.height
projection.perspective(90.0, aspect_ratio, 1.0, 400.0)

look_atandperspective函数来自kivy 的 Matrix 类,该类模拟/封装了一些相同(或相似)名称的 GLU 函数。

投影似乎工作得很好,但是......

问题

屏幕上的一个点可以被认为定义了一条从相机的眼睛到无限远的光线。我想知道该射线与定义的平面相交的世界 x,y 坐标z=0

这是我的(许多)尝试中的一个(其中之一),以使其发挥作用。

x,y  # <--- mouse coords (I've tried range of [0..1], or [0..screenwidth]) 
z=0  # I have also tried z=cz

# create inverse proj. matrix with near set to the distance
# the camera is from z=0
proj = Matrix().identity()
proj.perspective(90.0, aspect_ratio, cz, cz+1.0)
proj_i = proj.inverse()

# create inverse modelview matrix
modelview_i = modelview_mat.inverse()

x,y,z = proj_i.transform_point(x,y,z)
x,y,z = modelview_i.transform_point(x,y,z)

这行不通。具体来说,当我让相机稳步移开时(cz 缓慢增加)。鼠标点击的 x,y 不会随着相机的移动而改变。

我究竟做错了什么?

我知道有很多类似的问题浮出水面,但我无法找到我正在寻找的信息。

4

0 回答 0