Jump to content
  • Advertisement
Sign in to follow this  
jon723

OpenGL OpenglGL selection and moving camera

This topic is 5221 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 having trouble with selecting objects after I rotate my camera. Things work correctly if I dont apply any rotation to the camera but one I try to view my object from another angle and then select something it wont dtect any hits. Can someone tell me whats wrong with my code??

void Mouse(int button, int state, int x, int y)
{
	Camera *pPQ = camera; // pointer to the PQ field of the camera...
	VRp3 rp;
	VRq rq;

	float matrix[16];	// matrix to apply the camera transformation
	unsigned int selectbuf[512];
	int hits;
	GLint viewport[4];	

	int mod = glutGetModifiers();

	switch(button)
	{ 
		
	case GLUT_LEFT_BUTTON:
		
		if(state == GLUT_DOWN && mod == GLUT_ACTIVE_ALT)
		{
			glClear(GL_DEPTH_BUFFER_BIT);

			glGetIntegerv(GL_VIEWPORT, viewport);	// store viewport information in an array

			glSelectBuffer(512, selectbuf);

			glViewport(0, 0, 570, 355);

			glMatrixMode(GL_PROJECTION);
		
			glPushMatrix();			
			
			glInitNames();	// clear the name stack
		
			glLoadIdentity();	
			
			glRenderMode(GL_SELECT);		// switch to select mode (nothing is drawn)

			gluPickMatrix(x, viewport[3]-y,  1, 1, viewport);	// create an area around the mouse
			
			gluPerspective(pPQ->fieldofview*m_180pi, pPQ->aspect, pPQ->nearclip, pPQ->farclip);

			tomatrix(-pPQ->position, ~pPQ->orientation, matrix);
	
			glMatrixMode(GL_MODELVIEW);
			
			glLoadMatrixf(matrix);
			
			root->Draw(VRp3(), VRq(), GL_SELECT);	
			
			hits = glRenderMode(GL_RENDER);		// return the amout of selected objects

			glMatrixMode(GL_PROJECTION);
			
			glPopMatrix();
			
			glMatrixMode(GL_MODELVIEW);

			// loop through all the hits and find the one closest to the one we selected
			if(hits > 0)
			{
				currNode = (SceneNode*)(selectbuf[3]);
				unsigned int depth = selectbuf[1];

				if(currNode->selected == false)
				{
					currNode->selected = true;
				}
				else
				{
					currNode->selected = false;
				}


				for(int n = 1; n < hits; n++)
				{
					if(selectbuf[n*4+1] < depth)
					{
						currNode = (SceneNode*)(selectbuf[n*4+3]);
						depth = selectbuf[n*4+1];
						
						if(currNode->selected == false)
						{
							currNode->selected = true;
						}
						else
						{
							currNode->selected = false;
						}	
					}
				}
			}	
		}
		break;

	default:
		break;
	}
	
	glutPostRedisplay();
}

Share this post


Link to post
Share on other sites
Advertisement
you may have better luck if you post the code you use for plain rendering as well. every time i have had problems of this sort, it was because the matrix state was not exactly the same as when i rendered the objects the first time (non select mode) as i had thought. an easy way to check this is to comment out the glRenderMode call and perhaps throw a global bool in there to tell you when the mouse button is depressed, so you can disable regular drawing. this way while you're holding the mouse button you see exactly what your picking code is rendering - can be very helpful.

again, if you posted the rendering code it would be a lot easier to see the problem..

-darren

Share this post


Link to post
Share on other sites
ok thanks, this is the code I call for rendering my objects in GL_SELECT mode:


// Draws the geometry to the screen
void Draw(const VRp3 &pos, const VRq &orien, unsigned int mode)
{
VRp3 p;
VRq q;

float matrix[16];

if(isEnabled)
{
p = pos + rotate(position, orien);
q = orien * orientation; // apply inocoming orientation to 'this' orientation

worldposition = p;
worldorientation = q;
worldvalid = true;

tomatrix(position, orientation, matrix);

glPushMatrix();
glMultMatrixf(matrix);

if(mode == GL_SELECT)
{
glPushName((unsigned int)this); // assign this geometry a name for picking
}

Update(p, q);

if(mode == GL_SELECT)
{
glPopName();
}

for (int i = 1; i <= childrenlist.count(); i++)
{
childrenlist.Draw(p, q, mode);
}

glPopMatrix();
}
}


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.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!