Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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

share|improve this question
add comment

2 Answers

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

share|improve this answer
    
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
1  
If you can't use gluUnProject, all you have to do really is take the inverse of your world*view*projection 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
show 1 more comment

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

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.