screen click to world conversion

Started by
10 comments, last by thekid 19 years, 5 months ago
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.
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
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.
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.
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
You need to create the float array to hold the matrix first:
float matrix[16];glGetFloatv(GL_MODELVIEW_MATRIX, &matrix[0]);
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
The noble art of losing face.May one day save the human race.And turn into eternal merit. What weaker minds might call disgrace.
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.
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.
Here is complete code for your problem

http://www.gamedev.net/community/forums/topic.asp?topic_id=281401
Author Freeworld3Dhttp://www.freeworld3d.org

This topic is closed to new replies.

Advertisement