iDevGames Forums
How do I do Color Picking? - Printable Version

+- iDevGames Forums (
+-- Forum: Development Zone (/forum-3.html)
+--- Forum: Graphics & Audio Programming (/forum-9.html)
+--- Thread: How do I do Color Picking? (/thread-9355.html)

How do I do Color Picking? - dockode - Sep 23, 2011 08:39 AM

I am trying to write a simple OpenGL program that draws two quads. Clicking on each quad changes the render color. (Really just to confirm that the object I wanted to select was hit.)

I am able to draw quads... it is the interaction part that is causing me trouble.

I've done some reading online that confirms that OpenGL's built in selection mode has been deprecated. So I do not wish to use that.

That leaves using a pick ray and color picking... to my knowledge. Is there another method? Is there a reason to use one over the other?

Would anyone be able to explain or provide a simple color picking tutorial based on the test program I am trying to write?

I imagine I need a class that can assign a "picking color" to itself when it is initialized. But I have no idea how to implement it.

I am new to OpenGL and Mac development and it seems that resources for this platform are scarce. I've read that the latest OpenGL SuperBible covers a wrapper written by the authors and not OpenGL itself... Sad

Sadly it seems most OpenGL books geared towards Apple developers are for iOS. I often wonder if there is anything to be learned from these books as far as OS X is concerned. Any input regarding good learning resources would be highly appreciated. I am willing to buy good books on the subject of OpenGL on the Mac and Mac Game Development.

RE: How do I do Color Picking? - OneSadCookie - Sep 23, 2011 01:57 PM

OpenGL ES 2 is essentially 100% relevant to OpenGL on the Mac. Things that are not relevant include precision in GLSL shaders, and context creation stuff.

For color picking, you need to draw each object with a unique flat color, then you use glReadPixels to get the color of the pixel at the cursor position, and then you know which object was clicked.

For ray casting, you use something like gluUnProject to determine the ray from the click point, and implement 3D geometry intersection tests to find which object(s) intersect the ray. It's generally preferable (glReadPixels is terrible for performance, among other things), but not necessarily easier.

RE: How do I do Color Picking? - dockode - Sep 24, 2011 08:02 PM

Thanks for confirming that OpenGL ES 2 is something worth checking out. I will check into some iPhone/iPad books on the subject. Maybe I can glean something for the desktop from them.