Jump to content
  • Advertisement
Sign in to follow this  
stu2000

OpenGL 3D Picking - HELP

This topic is 2628 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)
{
return;
}

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;
glRenderMode(GL_SELECT);
gluPickMatrix(mouseX, mouseY, 1,1, viewport);


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

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

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;
}
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)
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

glLoadIdentity();

// 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);


gameState-&gt;Render();

//glDisable(GL_CULL_FACE);
#ifdef FEATURE_HUD
hud.Render();
#endif

glFlush();
glutSwapBuffers();

//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.


Stu

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!