Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

selection mechanism

This topic is 5153 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

hi there, i just implemented a simple selection mechanism following a tutorial i found on the net. It uses the selection buffer in OpenGL and seems tobe working fine until i rotate the object (it consists of 8 smaller objects) after that, everything breaks down. I use display lists to optimise performance. Below is the code for the selection mechanism. It''s in Java and uses Gl4Java. Hope someone helps me out... Thx. /** * Get the object under a mouse click * @param x the x coordinate * @param y the y coordinate * @return int the ObjectID */ int RetrieveObjectID(int x, int y) { int objectsFound = 0; // This will hold the amount of objects clicked int[] viewportCoords = new int[4]; // We need an array to hold our view port coordinates // This will hold the ID''s of the objects we click on. // We make it an arbitrary number of 32 because openGL also stores other information // that we don''t care about. There is about 4 slots of info for every object ID taken up. int[] selectBuffer = new int[32]; // glSelectBuffer is what we register our selection buffer with. The first parameter // is the size of our array. The next parameter is the buffer to store the information found. // More information on the information that will be stored in selectBuffer is further below. gl.glSelectBuffer(32, selectBuffer); // Setup our selection buffer to accept object ID''s // This function returns information about many things in OpenGL. We pass in GL_VIEWPORT // to get the view port coordinates. It saves it like a RECT with {top, left, bottom, right} gl.glGetIntegerv(GL_VIEWPORT, viewportCoords); // Get the current view port coordinates // Now we want to get out of our GL_MODELVIEW matrix and start effecting our // GL_PROJECTION matrix. This allows us to check our X and Y coords against 3D space. gl.glMatrixMode(GL_PROJECTION); // We want to now effect our projection matrix gl.glPushMatrix(); // We push on a new matrix so we don''t effect our 3D projection gl.glRenderMode(GL_SELECT); // Allows us to render the objects, but not change the frame buffer gl.glLoadIdentity(); // Reset our projection matrix glu.gluPickMatrix(x, viewportCoords[3] - y, 2, 2, viewportCoords); // Next, we just call our normal gluPerspective() function, exactly as we did on startup. // This is to multiply the perspective matrix by the pick matrix we created up above. glu.gluPerspective(45.0f, (float) viewportCoords[2] / (float) viewportCoords[3], 0.1f, 150.0f); gl.glMatrixMode(GL_MODELVIEW); // Go back into our model view matrix //this is where we render the scene once... gl.glPushMatrix(); //Rotate On The X Axis gl.glRotatef(xrot, 1.0f, 0.0f, 0.0f); // Rotate On The Y Axis gl.glRotatef(yrot, 0.0f, 1.0f, 0.0f); // Rotate On The Z Axis gl.glRotatef(zrot, 0.0f, 0.0f, 1.0f); gl.glCallList(RenderList); gl.glPopMatrix(); // If we return to our normal render mode from select mode, glRenderMode returns // the number of objects that were found in our specified region (specified in gluPickMatrix()) objectsFound = gl.glRenderMode(GL_RENDER); // Return to render mode and get the number of objects found gl.glMatrixMode(GL_PROJECTION); // Put our projection matrix back to normal. gl.glPopMatrix(); // Stop effecting our projection matrix gl.glMatrixMode(GL_MODELVIEW); // Go back to our normal model view matrix if (objectsFound > 0) { // Set the lowest depth to the first object to start it off. // 1 is the first object''s minimum Z value. // We use an unsigned int so we don''t get a warning with selectBuffer below. int lowestDepth = selectBuffer[1]; // Set the selected object to the first object to start it off. // 3 is the first object''s object ID we passed into glLoadName(). int selectedObject = selectBuffer[3]; // Go through all of the objects found, but start at the second one for (int i = 1; i < objectsFound; i++) { // Check if the current objects depth is lower than the current lowest // Notice we times i by 4 (4 values for each object) and add 1 for the depth. if (selectBuffer[ (i * 4) + 1] < lowestDepth) { // Set the current lowest depth lowestDepth = selectBuffer[ (i * 4) + 1]; // Set the current object ID selectedObject = selectBuffer[ (i * 4) + 3]; } } // Return the selected object return selectedObject; } return -1; }

Share this post


Link to post
Share on other sites
Advertisement
I''m not sure what''s wrong with you code, but I always recommend for object picking that you do your own math - you don''t need that select buffer stuff.

You can use gluUnProject to get a point on the front and back of the viewing frustum and construct a line, then use line/polygon collision to check for "picking". www.gametutorials.com has a good tutorial on that.

I have a demo on my site that shows dragging an object in 3D.

Love means nothing to a tennis player

My nothing-to-write-home-about OpenGL webpage. (please pardon the popups!)

Share this post


Link to post
Share on other sites
hello!
just debugged the code! it works fine now. it wasn''t in the selection mechanism, but when i was compiling it.

to DalTXColtsFan: thanx a lot for the reply will check out your web site.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!