How do I do Color Picking?

Apprentice
Posts: 9
Joined: 2011.09
Post: #1
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.

Everyone has to believe in something.
I believe I will have another cookie.
Quote this message in a reply
Luminary
Posts: 5,143
Joined: 2002.04
Post: #2
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.
Quote this message in a reply
Apprentice
Posts: 9
Joined: 2011.09
Post: #3
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.

Everyone has to believe in something.
I believe I will have another cookie.
Quote this message in a reply
Post Reply 

Possibly Related Threads...
Thread: Author Replies: Views: Last Post
  Issues with picking FFaqui 0 2,145 Nov 21, 2009 04:37 PM
Last Post: FFaqui
  ? Find color value of 'pixel' in color buffer? Elphaba 1 4,273 Jul 22, 2009 01:23 PM
Last Post: Bachus
  Opengl picking problem (zip file) papillon68 1 4,312 Mar 1, 2009 08:49 PM
Last Post: chronus
  Picking and selection... chalamov33 2 3,279 Mar 31, 2008 02:08 PM
Last Post: wyrmmage
  glut picking clashes with drawing wyrmmage 2 3,431 Jan 24, 2008 06:39 PM
Last Post: wyrmmage