Weird World Coordinates/ Camera Problem

HI, I have been trying for a few days to sync screen coordinates to world coordinates, particularly the ground plane.

I have gotten some results
the points are plotted where the mouse points and that is great
points

until you zoom out and find them in a completely different location.

this is either a camera issue or a conversion issue…

conversion code(gathered from, a few sources):

def newPoint():
	x, y = pygame.mouse.get_pos()

	glEnable(GL_DEPTH_TEST)

	depth = glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT)
	print (depth)
	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-0.5
	corrected_x = x+0.5

	pointa = np.array(gluUnProject(x, corrected_y, depth, model_view, proj, view))
	originalpt = np.array(gluUnProject(x, corrected_y, 0, model_view, proj, view))
	
	print("pointa", pointa,"  originalpt",originalpt )
	ray = pointa - originalpt
	raydirection = ray/np.linalg.norm(ray)
	print("ray", ray)
	normal = np.array([0.0,1.0,0.0])

	t = -((normal.dot(originalpt))/normal.dot(pointa))

	point = originalpt+(t*raydirection)
	print("tttt", t)
	print(point,"point intrs" )
	mylist.append(point)

camera code(gathered from a few sources):

class createcamera:

    def __init__(self, width, height):
        self.viewport = width, height
        self.viewport_aspect = self.viewport[0] / self.viewport[1]
    eye_r = 20.0     # camera range, in meters
    eye_th = 1   # camera azimuth angle, in radians
    eye_phi = 1    # camera elevation angle, in radians

    viewport = 1000, 1000
    viewport_aspect = viewport[0] / viewport[1]    

    #how much of the world can we see, how wide is our sight
    field_of_view = 45
 

    lookat = (0,0,0)
    location =np.array([eye_r*np.sin(eye_phi)*np.cos(eye_th),eye_r*np.cos(eye_phi),
                                 eye_r*np.sin(eye_phi)*np.sin(eye_th)
                                 ])
    camera_rotation = 0.0

    near_plane = 0.1
    far_plane = 50

    def draw(self):
        pass

    def update(self):

        glViewport(0, 0, self.viewport[0], self.viewport[1])
        glMatrixMode(GL_PROJECTION)
        glLoadIdentity()

       
        gluPerspective(self.field_of_view,
                       1.0 * self.viewport[0] / self.viewport[1],
                       self.near_plane, self.far_plane)

        gluLookAt(self.eye_r*np.sin(self.eye_phi)*np.cos(self.eye_th),self.eye_r*np.cos(self.eye_phi),
                                 self.eye_r*np.sin(self.eye_phi)*np.sin(self.eye_th),
                  self.lookat[0], self.lookat[1], self.lookat[2],
                  0.0, 1.0, 0.0)
        print(self.location[0])
        print(self.eye_r)

        print ((self.location[0], self.location[1], self.location[2],
                  self.lookat[0], self.lookat[1], self.lookat[2],
                  0.0, 1.0, 0.0))
        glMatrixMode(GL_MODELVIEW)
        glLoadIdentity()```

You need to fix the Y coordinate before using it to read from the depth buffer:

	depth = glReadPixels(x, view[3]-1-y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT)

hi thanks for you reply…
still the same problem… if it helps the depth is always equal to 1 no matter what changes I make

any other ideas? Im very stuck and losing hope. I checked many of the lessons and formulas and think Im applying them correctly. pls help if you do not mind

You need to find out why. The glReadPixels should occur after rendering; the depth buffer will be filled with 1.0 by glClear. Ensure that the viewport and matrices used for the gluUnProject call as the same as are used for rendering.

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