How to figure out which surface I'm clicking?

Hi, JOGL & OpenGL users.

Assume that there’s a cube and each of the faces has a number
like a dice.

The cube rotates around arbitrary axis and degree.

So far, it’s fine.

But I don’t know what to do next step.

The next step is implementing this: when a user clicks a face of the cube,
the application recognizes what face is clicked and notifies a user what number
is choosed.

Any advice is welcomed. :slight_smile:
Thank you.

sincerely,
Junyeong

Implement picking, use an OpenGL name per face. Look at gluPickMatrix, glSelectBuffer, glReadPixels, glRenderMode, glInitNames, glPushName. You can find an example of picking on http://nehe.gamedev.net

Good luck

Except that that is slow.

Use color coding.

Color coding is enough for simple cases but very weak, it does not work on some netbooks (those who simulate some colors of the whole palette) and it might work bad with lights.

Then you’d still have 64K different colors remaining…

That doesn’t make any sense. What’s next? Textures and shaders?

At work, an OpenGL expert explained to me that some “old” netbooks have a very narrow palette of real colors and simulate the other colors not inside this palette with a mix of colors on 4 pixels. In this case, color coding works only if you choose very carefully your colors. He refused me to implement color coding in our professional suite for this reason.

That site appears to have been hacked and defaced. Here’s a Wayback Machine link for it: http://web.archive.org/web/20080508224124/http://www.lighthouse3d.com/opengl/picking/index.php3?color1

Or you could do it properly with a ray cast and get something that’s actually reliable.

  • mathematically accurate
  • Doesn’t depend on user’s resolution or colour depth
  • Doesn’t put an upper limit on the number of objects or surfaces that can be picked.
  • Doesn’t require you to render everthing twice
  • Fast

Fast? Color picking is pixel perfect and hardware accelerated. It’s very hard actually to get that accuracy and performance with raycasting which must be based on the OpenGL transformation matrices for getting near pixel perfect results - without spending significant time on it.

Thank you guys for all advices.

Happy JOGLing ~~ :smiley: