Skip to main content
GameDev.net gamedev.net
🔒 Locked

How to let the picking ray pick the nearest object?

Started by littlejedi Apr 5, 2005 at 8:51 AM 2 replies 600+ views
Original Post
littlejedi
littlejedi
Somehow when there are two triangles, one front and one behind, the program will always pick the one in the back. Here's the code, is this enough?Or I need to modify, how? void pick(GLfloat xpos,GLfloat ypos) { xpos,ypos; GLint viewport[4]; GLdouble mvmatrix[16],projmatrix[16]; GLint realy; GLdouble wx,wy,wz; glGetIntegerv(GL_VIEWPORT,viewport); glGetDoublev(GL_MODELVIEW_MATRIX,mvmatrix); glGetDoublev(GL_PROJECTION_MATRIX,projmatrix); realy = viewport[3]-(GLint)ypos -1; gluUnProject((GLdouble)xpos,(GLdouble)realy,0.0,mvmatrix,projmatrix,viewport,&wx,&wy,&wz); g_nearxyz[0] = (GLfloat)wx; g_nearxyz[1] = (GLfloat)wy; g_nearxyz[2] = (GLfloat)wz;//// gluUnProject((GLdouble)xpos,(GLdouble)realy,1.0,mvmatrix,projmatrix,viewport,&wx,&wy,&wz); g_farxyz[0] = (GLfloat)wx; g_farxyz[1] = (GLfloat)wy; g_farxyz[2] = (GLfloat)wz;//// }
Zealot
Zealot
Don't see anything obviously wrong with the code, but I've never used most of those functions (or picking).

I don't know how it could affect picking, but is it possible you're doing something wierd while drawing? Maybe ignoring certain vertex ordering? Got some odd transformation or something? I wouldn't know where to begin, but maybe you should try changing stuff like that and see what it does.


Does it always happen? Regardless of the triangles' positions?

Good luck though.
----------------------------------------------------------You know, I might as well go ahead and say I can't fix the problem... because that's when I figure out how.
pasman
pasman
well from what you've shown in this code so far you only extract 2 points (in the near and far planes). I don't know if you already done this but you will have to build a ray using those 2 points, then intersect it with the bounding volumes of your objects. Then take the list of the intersected objects and take the closest one to the camera. That's your first picked object.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.