# How can I do Mouse Selection In OpenGL 3.0?

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++

-

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:

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...

-
How do I do the ray one? please be detailed with your description because It will help me know what to do exactly. – NoobScratcher May 30 '12 at 10:06
I have edited post above. – zacharmarz May 30 '12 at 12:55
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. – melak47 May 30 '12 at 13:37
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 – NoobScratcher May 31 '12 at 6:34
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. – melak47 May 31 '12 at 14:16

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: converting a mouse click to a ray

-