Archived

This topic is now archived and is closed to further replies.

feagle814

2d Mouse coordinates > 3d World coordinates: possible?

Recommended Posts

I''m wondering if there''s a better way than gluUnProject() for finding an object pointed to by the mouse. I was thinking if there was a way to access the depth buffer, then reverse the matrix transformations, it would be possible, but as of yet I''ve not seen any way to read the depth buffer. Is there a way?

Share this post


Link to post
Share on other sites
You can read from the depth buffer any use the result as the z cord if the mouse position in gluunproject, there are just two problems:

1- You still use gluunproject, and
2- I haven''t done it in a while, so I can''t tell you exacly how, but it is possible, something along the lines of

glReadPixels(

GLint x, // Mouse x pos
GLint y, // Mouse y pos, upside down I think
GLsizei width, // 1
GLsizei height, // 1
GLenum format, // GL_DEPTH_COMPONENT
GLenum type, // GL_FLOAT
GLvoid *pixels // A pointer to the floating point number that will be used with gluunproject
);


You figure it out. I''m lazy.

Share this post


Link to post
Share on other sites
... Ill say this first ... this is probably all wrong so dont get to mad ;-)

to use the ray casting technique first you have your mouses 2d coords in world space. this is fairly easy todo if you are drawing the mouse...

after that just cast a ray into the screen at that coordinates and check to see what it "collides" with. This is how i do it neway... and it works ok sept for some precision errors id like to fix but owell

Good luck

Share this post


Link to post
Share on other sites
I used my method in a 3d physics simulation and it ran at an okay speed. The slowest part is where you read from the buffer, and you only need to read it once when you click the mouse, and just reuse the same value. I also read from the depth buffer for a lens flare effect (to making sure the light wasn''t behind something). I did it about 64 times per frame, and at was okay, too.

The later was on Nehe''s site, but that part of the site is gone now, at least I can''t find it.

Share this post


Link to post
Share on other sites