Recommended Posts

I got me a scene which essentially consists of a perspective-viewed flat plane along which objects are dispersed in a random fashion. There is nothing between these objects. The plane is the xy-plane and I view it along the z axis from a camera with arbitrary coordinates. I want to be able to pick objects with the mouse and do stuff to them.
So I try to use gluUnproject, but it keeps giving me the wrong coordinates. Nothing I've tried works. The code is below; any ideas what might be wrong?

Share this post

Link to post

Share on other sites

Your code is perfectly fine. Are you rendering in wireframe mode? Are you making sure to call SelectSystem after everything is rendered to the screen? If the depth buffer is blank then you'll definitely get odd values.

0

Share this post

Link to post

Share on other sites

Hmm. I'm using SDL and SelectSystem is triggered during a mouseclick event. It's possible that this event is triggered before the render loop. I'll try to synch it and see what happens. ETA: Synching it didn't work. Placed the code directly after I finished rendering the world and got identical result.

Is there otherwise a way to unproject screen coordinates to a plane, parallell to the camera and a given distance from it without doing the linear algebra manually?

0

Share this post

Link to post

Share on other sites

To be honest, I've never gotten this to work with glReadPixels() either. It always threw weird numbers at me, no matter what I did. But there is another way - you can call gluUnProject() twice, with z-coords of 0 and 1. You can then construct a ray (well, a line segment really) between the two points and collide this ray with your geometry (and if there are no collisions, I find the intersection with the y=0 plane). This works fine, assuming this isn't some issue with SDL. I can post some code if you like (it's a bit long and in Java).

0

Share this post

Link to post

Share on other sites

As far as I understand, the "z" coordinate in gluUnProject is the normalized z-buffer. Does that mean that with a viewport that goes from 1 to 1000 in the z direction, a z=0 means, in world coordinates relative to the camera, world_z = 1 and z=1 => world_z = 1000?

As far as I understand, the "z" coordinate in gluUnProject is the normalized z-buffer. Does that mean that with a viewport that goes from 1 to 1000 in the z direction, a z=0 means, in world coordinates relative to the camera, world_z = 1 and z=1 => world_z = 1000?

Exactly, it goes between the near and far plane. The difference of the results is the direction of the ray (in camera space, if I'm not mistaken), and the origin is at the camera.