Jump to content
  • Advertisement
Sign in to follow this  
private_ctor

OpenGL gluPickMatrix problems

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

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]

Share this post


Link to post
Share on other sites
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)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
jepp. same problem here...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!