Jump to content
  • Advertisement
Sign in to follow this  
thekid

OpenGL screen click to world conversion

This topic is 5085 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi all, im working on a project that requires me to know what poly on in 3d space was clicked when the user clicks on the screen. In this case it is just a simple grid of cells (minesweeper clone for uni project) i know this is possible but I have had little luck finding good sources of info on the topic. I am using OpenGL for the graphics API and glut for the windowing. thanks in advance.

Share this post


Link to post
Share on other sites
Advertisement
look up gluProject and gluUnproject (for 2d to 3d coords and back), or take take a look at the Selection buffer. Tutorials on both can be found at Nehe, Selection buffer is there for sure, i think he made a game where u shoot things that has it.

HTH

Share this post


Link to post
Share on other sites
in general this is called picking. But I dont think you need anything as complicated as in the general case. If you have a grid of cells, you couldnt ask for anything easier.

position_of_mouse_click_X / size_of_each_cell = some_number

drop the fraction part from some_number, and youve got the cell# in the x direction. repeat for y.

Share this post


Link to post
Share on other sites
thanks to nts, i am trying the gluUnproject, i am getting segmentation faults however when trying to get the mode, projection matrices and the viewport using glGetdoublev and glGetIntegerv. any ideas? yes CosmoKramer i realize that the problem is trivial if the board is always facing the user directly but i want to be able to rotate around the gameboard thus the problem of picking.

Share this post


Link to post
Share on other sites
i would suggest using floats to get the matrix

glGetFloatv(GL_MODELVIEW_MATRIX, mat);
etc, integers for viewport are fine

where are u getting the faults?

EDIT: make sure you have a valid RC, all that i can think of

Share this post


Link to post
Share on other sites
You can choose to do this using immage space, if you have a system that is capable of rendering to textures you can render a version of your checker board where each square has a unique integer stored as color. You can then lock the texture and read the color at the mouse position.

This is relatively simple and fast.

Rgs

Ralf

Share this post


Link to post
Share on other sites
You just need to spawn a ray though the pixel in screen into the world from the camera position. Then intersect that ray with the potential polygons. If you know the cameras field of view and aspect ratio, it's trivial.

Share this post


Link to post
Share on other sites
It's really no help saying things are 'trivial'. They may be trivial to you but people wouldn't post on here if they could already do what they were asking.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!