背景
将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_at
和perspective
函数来自kivy's Matrix class,它正在模拟/封装相同(或类似)名称的某些GLU函数。
预测似乎正常,但......
问题
屏幕上的一个点可以被认为是定义从相机眼睛到无限远的光线。我想知道光线与z=0
定义的平面相交的世界x,y坐标。
这是我(很多)尝试使用它的一种方法。
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不会改变。
我做错了什么?
我知道有很多类似的问题浮出水面,但我还没能找到我想要的信息。