2
\$\begingroup\$

Hello I'm pretty good programmer I've made my own 2D games in SDL and made a gui in 3D using Old OpenGL and Modern OpenGL but.. I'm having problems with trying to click 3D models with opengl I have no idea what to do too be honest.

Do I read the area that I've clicked?

or what do I do?

100% shore this has been asked before but I just don't know what to do...??

using :

OpenGL 3.0

WIN32 API

C++

\$\endgroup\$
1

2 Answers 2

1
\$\begingroup\$

I know two ways how to do this:

1) Use frame buffer, where you render all objects, each with different color (no lighing, no texturing, ...). Then render normal scene.

When you click with your mouse, you read 2D coordinates, look in frame buffer and find color of pixel, where you have clicked. Then you will find object according to this color.

This method is called pixel picking.

2) Render your scene, read clicked coordinates. Now you have to transfer 2D point back to 3D - create ray from camera to clicked point. Then do ray tracing and find first object in path (ray object collision).

// EDIT:

I have found this article, it could be useful.

Or you can create 3D points - your screen coordinates (x, y) - devided by screen size (you have to get coords in interval <-1,1>), and z coordinate will be -1 (starting point) and 1 (ending point). These are NDC (normalized device coordinates). Then multiply them by inverse projection matrix and inverse modelview matrix. Results should be starting and ending points of ray. You can find something here - it will help you understand those transformations.

Maybe you can use gluUnProject for this purpose...

\$\endgroup\$
6
  • \$\begingroup\$ How do I do the ray one? please be detailed with your description because It will help me know what to do exactly. \$\endgroup\$ Commented May 30, 2012 at 10:06
  • \$\begingroup\$ I have edited post above. \$\endgroup\$
    – zacharmarz
    Commented May 30, 2012 at 12:55
  • 1
    \$\begingroup\$ If you can't use gluUnProject, all you have to do really is take the inverse of your worldviewprojection matrix, and transform your start and end ponts with to world space. \$\endgroup\$
    – user13213
    Commented May 30, 2012 at 13:37
  • \$\begingroup\$ This is a pain to figure out because I don't want to figure it out I just want it to be simple from a article that I can follow and understand so I can learn from it after woulds and if I figure it out I will make a tutorial to simplify this down \$\endgroup\$ Commented May 31, 2012 at 6:34
  • \$\begingroup\$ Consider the alternative then. Render the scene to a buffer where the color represents the object ID, making picking an object as easy as sampling the selected pixel. If you are going to implement deferred shading/lighting at some point, you might need an Object-ID channel in your G-Buffer anyway. \$\endgroup\$
    – user13213
    Commented May 31, 2012 at 14:16
1
\$\begingroup\$

Sorry, selection API is not in OpenGL 3.0, but here's a question with a good answer on doing ray (un-)projection for picking: How can I convert a mouse click to a ray?

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .