How to get screen x &amp; y position of object on a 3D space?

Hello I´m working on an interactive 3d flock.

The idea is to be able to grab and click over the 3d boids. For that I don´t know how to get the x and y position of object that have x, y and z position…

Any idea?

The code is here on my blogs: http://www.patriciogonzalezvivo.com/blog/?p=170

you have 2 options:

1. the non OpenGL, ugly way: render your scene also to an FBO with no depth test and giving every object an rgb code; then you can test your mouse coordinates against the fbo, get the rgb code you’re over and decide which object are you pointing. Let me repeat that this is *UGLY*, but it works and, under particular conditions, can be useful (it saved my day a couple times), so I’m passing it on.

2. the OpenGL, right way: gluProject() and gluUnProject() will do the trick; look here (9.100 and 9.110) to learn more

Thanks Nasea:

I got it work using:

``````
bool Boid::isOver(int x, int y){
GLdouble _x = 0;
GLdouble _y = 0;
GLdouble _z = 0;

GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];

glGetIntegerv(GL_VIEWPORT, viewport);
glGetDoublev(GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev(GL_PROJECTION_MATRIX, projmatrix);

gluProject(loc.x, loc.y, loc.z,
mvmatrix, projmatrix, viewport,
&_x, &_y, &_z);

_y = ofGetHeight() - _y;

if ( ofDist(x,y,_x,_y) <= sc * 4) return = true;
else return false;
}

``````

I don´t know why the _y was inverted… so I have to change it to make it work.
Thanks one more time!

you’re welcome

i think the inverted y might be because of OF’s screen setup