OpenGL Mouse to World Coordinates / python

I have been stuck for a few days now. I saw all the sources I could find and tried a more than one technique. Anyway here is my code (gathered from a few places) the closes Ive gotten is close to the mouse cursor but not quite there

	x, y = pygame.mouse.get_pos()

	model_view = np.array(glGetDoublev(GL_MODELVIEW_MATRIX))
	proj = np.array(glGetDoublev(GL_PROJECTION_MATRIX))
	view = np.array(glGetIntegerv(GL_VIEWPORT))
	corrected_y = view[3]-y
	corrected_x = x	

	depth = glReadPixels(x, corrected_y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT)
	print (depth)	
	pointa = np.array(gluUnProject(corrected_x, corrected_y, -1))
	originalpt = np.array(gluUnProject(corrected_x, corrected_y, 1, model_view, proj,view))
	print (originalpt, " original pt")

	print("pointa", pointa,"  originalpt",originalpt)
	ray = originalpt - pointa
	raydirection = np.linalg.norm(ray)
	print (raydirection, "ray dir")
	print("ray", ray)
	normal = np.array([0.00,1.00,0.00,])
	t = -((
	#point = originalpt+ raydirection*t
	point = originalpt + (t*raydirection)
	print("tttt", t)

	print(point,"point intrs")

please not this is not the best version of the code. there was a better version that got me closer, by changing the equation of point to = originalpt + t* pointa

Pass the value from the depth buffer as the third parameter (winZ) to gluUnProject.

Also: the winX/winY parameters should normally be:

	corrected_x = x + 0.5	
	corrected_y = view[3] - y - 0.5

as those are the window coordinates of the centre of the pixel containing the mouse cursor, rather than the top-left corner. The values in the depth buffer are obtained by interpolating the triangle at the centre of each pixel.

This topic was automatically closed 183 days after the last reply. New replies are no longer allowed.