Advertisement Jump to content
Sign in to follow this  

OpenGL 3D Picking - HELP

This topic is 2692 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Nevermind this post, please delete if you have the power. After two days hacking away at it, I discovered a multitude of problems, including getting the fov from dividing two integers instead of two floats, and not inverting the y axis.<br><br>Stu<br><br>Hello again!
I'll start by saying I'm completely new to picking. I have only studied the theory and know what it is 'supposed' to do. Implementing it is a whole different story.

So I'm trying to implement 3D picking using OpenGL naming stack feature and have the following code (compiles and runs but doesnt actually work).

The current code is below:

// This function is called by glut whenever mouse is clicked.
void PickObject(int button, int state, int x, int y)
GLuint selectBuf[BUFSIZE];
GLint hits;
GLint viewport[4];

if (button != GLUT_LEFT_BUTTON || state != GLUT_DOWN)

glGetIntegerv (GL_VIEWPORT, viewport);
glSelectBuffer (BUFSIZE, selectBuf);

gameState-&gt;RenderInSelectionMode(x, y, viewport, selectBuf);

void GameState::RenderInSelectionMode(int mouseX, int mouseY, GLint * viewport, GLuint selectBuf[])
GLint hits;
gluPickMatrix(mouseX, mouseY, 1,1, viewport);

gluPickMatrix ((GLdouble) mouseX, (GLdouble) mouseY, 5.0, 5.0, viewport);

uni.Render(); // Universe, just a bunch of asteroids (will eventually get to point where each asteroid has own name)
aiManager.Render(); // Renders all the spaceships (will eventually get to point where each spaceship has own name)

hits = glRenderMode(GL_RENDER);
ProcessHits(hits, selectBuf);

// This code i pretty much pulled from the net, dont care about it much for now, what is important is that it outputs the number of hits and details
void GameState::ProcessHits (GLint hits, GLuint buffer[])
unsigned int i;
unsigned int j;
GLuint ii;
GLuint jj;
GLuint names;
GLuint *ptr;

printf ("hits = %d\n", hits);
ptr = (GLuint *) buffer;

for (i = 0; i &lt; hits; i++)
{ /* for each hit */
names = *ptr;
printf (" number of names for this hit = %d\n", names); ptr++;
printf (" z1 is %g;", (float) *ptr/0x7fffffff); ptr++;
printf (" z2 is %g\n", (float) *ptr/0x7fffffff); ptr++;
printf (" names are ");

for (j = 0; j &lt; names; j++)
/* for each name */
printf ("%d ", *ptr);

if (j == 0) /* set row and column */
ii = *ptr;
else if (j == 1)
jj = *ptr;
printf ("\n");

I have included my normal render method in case this helps, it has my normal rendering matrix setup etc.

void Render(void)


// Set the camera position]
Vector3F cameraPosition = camera.GetPosition();
Vector3F cameraPOI = camera.GetPointOfInterest();
Vector3F cameraUpVector = camera.GetUpVector();
gluLookAt(cameraPosition.x, cameraPosition.y, cameraPosition.z, cameraPOI.x, cameraPOI.y, cameraPOI.z, cameraUpVector.x, cameraUpVector.y, cameraUpVector.z);




//glutPostRedisplay(); /* OpenGL is pipelined, and sometimes waits for a full buffer to execute */

I get the feeling that I am missing the proper matrix setup in the picking process, such as a loadIdentity here and a gluLookat there. Please advize as to what I need. The program should be able to output one name for if you click on an asteroid, and another name for if you click on a spaceship.


Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!