views:

137

answers:

2

I'm starting study opengl, and im tring to make a 3d chess like, but i cant figureout, how i can know where i have clicked in the "table" to make the proper animations, any advice ?

A: 

This is called "3D picking". You have to translate screen coordinates into world coordinates. From there, do a ray/collision object (bounding box?) intersection test. If they intersect, that's where the user clicked.

You'll have to do a little bit more than this to solve the depth-order problem, like finding the first time to intersection of each object, then selecting the one with the lowest (positive) time.

If you google for "3D picking" you might find what you are looking for.

Here is a tutorial: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=32

Note that this is not specific to any shape of bounding object, be it a bounding box, a polygon, a curve, etc. You just have to figure out the math for the intersection test for each type of object you want to support.

Edit:

I didn't read that tutorial before I linked it, I just figured NEHE is where all the cool kids learn OpenGL (admittedly ten years ago...).

Here is something from the OpenGL FAQ about picking:

http://www.opengl.org/resources/faq/technical/selection.htm

Merlyn Morgan-Graham
The NeHe tutorial is a bad example, it doesn't remotely work in OpenGL ES.
Matias Valdenegro
@Matias Valdenegro: Ultimately the 3D picking algorithm itself is 3D API agnostic. I agree that the tutorial I linked has too much jammed in it, and I didn't read through it to see if he's using the API to do some of the picking calculations or not. The point of providing a link was to show that there is at least one 3D picking tutorial out there. NEHE often has easy tutorials for new OpenGL programmers to follow, whether they are correct on all fronts, or not. If you have a better one, please provide a link.
Merlyn Morgan-Graham
A: 

waldecir, look for a raypick function. It's the name for sending a ray from the scene's camera center through the pixel you clicked on (actually, through that pixel's translated position on the camera's plane representing the "glass surface of the screen" in the 3D world) and return the frontmost polygon the ray hits together with some information. Usually coordinates within the polygon's surface axes, e.g. UV or texture coordinates. By checking the coordinates, you can determine which square the user clicked on.

Rays can be sent from any position and in any direction, so likely you'd have to get the camera position and its plane center, but the documentation should be able to help you there.

Henrik Erlandsson