opengl - Inverting projection and modelview matrices to transform mouse click coordinates to world coordinates intersecting the z=0 plane -


background

using kivy opengl , glu style functions, have gotten modelview , projection matrices working. model matrix not used, modelview matrix transform based upon location of camera.

# cx,cy,cz location of camera. cz positive modelview = matrix().look_at(cx,cy,cz,     # eye position coords                              cx,cy,0.0,    # looking @ pt.                              0.0,1.0,0.0)  # vector pointing 

the projection matrix straightforward , working desired.

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

the look_at , perspective functions come kivy's matrix class emulating/encapsulating glu functions of same (or similar) name.

projection seems working right, but...

problem

a point on screen can thought of defining ray eye of camera off infinity. i'd know world x,y coordinates of ray intersects plane defined z=0.

here (one of) (many) attempt(s) work.

x,y  # <--- mouse coords (i've tried range of [0..1], or [0..screenwidth])  z=0  # have tried z=cz  # create inverse proj. matrix near set distance # camera 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) 

this doesn't work. specifically, when have camera steadily moving away (cz increasing slowly). x,y of mouse clicks not change camera moves.

what doing wrong?

i know there many similar questions floating about, haven't been able find information looking for.