//Thanks to Nehe on this one.
//Selection Buffer
unsigned int buffer[512];
//Number of Objects that we clicked on
int hits;
//create a viewport just for the selection
int viewport[4]; //0 - x, y - 1, length - 2, width - 3
// //set the viewport to the size and location of the screen
// glGetIntegerv( GL_VIEWPORT, viewport );
//set the viewport to the size and location of the screen
glGetIntegerv( GL_VIEWPORT, viewport );
//Tell OpenGL to use our array for selection
glSelectBuffer( 512, buffer );
//put openGL in selection mode;
glRenderMode( GL_SELECT );
//initialize the name stack
glInitNames();
//push at least one entry on to the stack
glPushName( 0 );
//Now we're restricting drawing to just under the cursor. We need the projection matrix
//for this. Push it to the stack then reset the new matrix with load identity
glMatrixMode( GL_PROJECTION );
glPushMatrix();
glLoadIdentity();
//Rotate the camera, Z, X, Y
glRotatef( m_Camera.pitch, 1.0f, 0.0f, 0.0f );
glRotatef( m_Camera.yaw, 0.0f, 1.0f, 0.0f );
glRotatef( m_Camera.roll, 0.0f, 0.0f, 1.0f );
//Move the camera
glTranslatef( m_Camera.location.x, -( m_Camera.location.y + m_Camera.height ), m_Camera.location.z );
//Now restrict drawing using gluPickMatrix(). The first parameter is our current
//mouse position on the x-axis, the second is the current mouse y axis. Then the width
//and height of the picking region. Finally the current viewport indicates the current boundries.
//mouse_x and y are the center of the picking region.
gluPickMatrix( mouse_x, (viewport[3]-mouse_y), 1.0f, 1.0f, viewport );
//gluPerspective will restrict the drawing to the area requestd by gluPickmatrix()
gluPerspective( 45.0f, (viewport[2] - viewport[0]) / (viewport[3] - viewport[ 1 ]), 0.1f, 100.0f );
//set the modelview to render our targets properly
glMatrixMode( GL_MODELVIEW );
//"render" the targets to our modelview
RenderObjects();
glMatrixMode( GL_PROJECTION );
glPopMatrix();
glMatrixMode( GL_MODELVIEW );
//find out how many objects we hit
hits = glRenderMode( GL_RENDER );
//as long as we have more than 0, we select the first object drawn to the area.
if( hits > 0 )
{
gluPickMatrix problems
I'm using NeHe's picking code from lesson 32 but I seem to be having a problem.
The picking works great, within a pixel accuracy, but it only seems to pick if my geometry is within the center 10% of the screen. If I turn slightly the polys are unselectable. If I strafe over ( same rotation ) so that they're within the center 10% of the screen ( Y value doesn't seem to matter, only X as far as screen coordinates ), it works fine again.
here is the code below but you will see its very similar as NeHe lesson 32.
All replies welcome.
[Edited by - phantom on August 23, 2005 12:33:49 PM]
Well, I don't know if it has anything to do with the problem, but I see you're doing wacky things on your projection matrix. Positioning the camera (your glTranslate and glRotate calls) should go on the modelview matrix. Try moving that and see if it helps 8)
Commented out the translation/rotations and still the same results. =(
Does anyone else have this problem?
Does anyone else have this problem?
as TomasH mentioned basically everything is wrong
u need the gluPerpective in the projection part and the rest in the modelview part see www.opengl.org for the faq
u need the gluPerpective in the projection part and the rest in the modelview part see www.opengl.org for the faq
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement