gluPickMatrix problems

Started by
5 comments, last by private_ctor 18 years, 8 months ago
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.

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

All replies welcome. [Edited by - phantom on August 23, 2005 12:33:49 PM]
-------------------------------Sometimes I ~self();
Advertisement
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?
-------------------------------Sometimes I ~self();
jepp. same problem here...
/bump in case anyone else may read this and know a solution.

-------------------------------Sometimes I ~self();
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
Actually that wasn't what was causing the problem.

The int viewport[4] was truncating when I was figuring the aspect ratio. casted it as a float to return a value of 1.3333 instead of 1 and it works fine now.

Posting this in case anyone else had the same issue.
-------------------------------Sometimes I ~self();

This topic is closed to new replies.

Advertisement