PDA

View Full Version : Selecting 3D Points


BlueAvian
2003.10.07, 11:20 AM
I'm thinking of making a OpenGL RTS... all 3D. But I cant figure out how I would select the units, because I want the camera angle to change at users will... I've heard this type of thread before but dident pay to much attention to it. If you can help me plz do.

Mark Levin
2003.10.07, 11:43 AM
There are a few things you can do...

-Use selection mode to find geometry under the mouse.
-Use the GLU function that maps a screen point to a ray in world space (I don't recall its name) and then do your own raycasting.

NYGhost
2003.10.07, 12:06 PM
to take points from window coords to 3D space the function is gluUnproject()

the other way around is gluProject()

ededed
2003.10.07, 02:45 PM
Use OpenGL picking, here is a tutorial: http://nehe.gamedev.net/data/lessons/lesson.asp?lesson=32

If you can find them there are some good examples at OpenGL.org

Johan
2003.10.07, 02:57 PM
I personally prefer the ray casting approach, simpler and really fast even for a lot of objects.

Here's some code from Kaeru that returns a ray from the mouse position.
typedef struct {
vec_t origin;
vec_t direction;
} ray_t;

/**
* @param sx Mouse position (X)
* @param sy Mouse position (Y)
* @param sw Screen width
* @param sh Screen height
*/
ray_t getRayFromScreenCoords(int sx, int sy, int sw, int sh) {

ray_t ray;
GLdouble modelview[16], projection[16];
GLdouble x, y, z;
GLint viewport[4];

glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
glGetDoublev(GL_PROJECTION_MATRIX, projection);
glGetIntegerv(GL_VIEWPORT, viewport);

gluUnProject(sx, sh - sy, 0.0,
modelview, projection, viewport,
&x, &y, &z);

ray.origin = vvector( (float)x, (float)y, (float)z );

gluUnProject(sx, sh - sy, 1.0,
modelview, projection, viewport,
&x, &y, &z);

ray.direction.x = x - ray.origin.x;
ray.direction.y = y - ray.origin.y;
ray.direction.z = z - ray.origin.z;

vnormalize(&ray.direction);

return ray;
}

Hope this helps.

.johan