Sign in to follow this  

Picking/gluProject problem

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

Sorry to make yet another thread on such familiarly treaded ground, but I have a problem using gluProject to pick some objects. I have some billboards that I'm projecting onto the screen but the y value seems inverted. I thought I could just invert the y value by doing y value = screen height - y value; but this doesn't seem to work. I've seen something liek this done for gluUnproject, but I can't apply this to gluProject. Heres the code:


void Picking( int x, int y )
{

	//Unselect all CPs
	for(vector<theControlPoint>::iterator it = aCurve.controlPointList.begin() ;
		it != aCurve.controlPointList.end() ; 
		it++)
	{
		(*it).mSelected = 0;
	}

	//Project the vertices of the control points onto the screen
	
	Vector3 coords;				//Something to store our return value in
	GLfloat wx, wy, wz;			//Window coordinates
	GLdouble cx, cy, cz;		//Object coordinates
	
	GLdouble mv[16];			//Modelview matrix
	GLdouble proj[16];			//Projection matrix
	GLint vp[4];				//Viewport 

	glGetDoublev( GL_MODELVIEW_MATRIX, mv );
	glGetDoublev( GL_PROJECTION_MATRIX, proj );
	glGetIntegerv( GL_VIEWPORT, vp );

	//re-enable various settings we may have turned off
	glEnable(GL_DEPTH_TEST);	//Re-enable depth testing and masking
	glDepthMask(GL_TRUE);
	glDepthRange(0.0f,1.0f);
	
	for(vector<theControlPoint>::iterator it = aCurve.controlPointList.begin() ;
		it != aCurve.controlPointList.end() ; 
		it++)
	{
		//Get the corners of the object
		Vector3 p1, p2, p3, p4;
		Vector3 q1, q2, q3, q4;		//Final projected coordinates

		Vector3 topLeft, bottomRight;			//top left and bottom right corners of the quad
		Vector3 projTopLeft, projBottomRight;	//as above, but projected into screen space
		GetBillboardCorners((*it).mPos, (*it).mScale, topLeft, bottomRight);

	double tempx, tempy, tempz;
	gluProject( topLeft.x, topLeft.y, topLeft.z, mv, proj, vp, &tempx, &tempy, &tempz );	//Project object coordinates to screen coords
	projTopLeft.x = tempx;		projTopLeft.y = tempy;			projTopLeft.z = tempz;

	gluProject( bottomRight.x, bottomRight.y, bottomRight.z, mv, proj, vp, &tempx, &tempy, &tempz );	//Project object coordinates to screen coords
	projBottomRight.x = tempx;	projBottomRight.y = tempy;		projBottomRight.z = tempz;

	//Now sort the quad values?
	Vector3 trueTopLeft;	//Smallest X highest Y
	if(projTopLeft.x < projBottomRight.x) trueTopLeft.x = projTopLeft.x; else trueTopLeft.x = projBottomRight.x;
	if(projTopLeft.y > projBottomRight.y) trueTopLeft.y = projTopLeft.y; else trueTopLeft.y = projBottomRight.y;

	Vector3 trueBottomRight;	//Smallest Y highest X
	if(projBottomRight.x > projTopLeft.x) trueBottomRight.x = projBottomRight.x; else trueBottomRight.x = projTopLeft.x;
	if(projBottomRight.y < projTopLeft.y) trueBottomRight.y = projBottomRight.y; else trueBottomRight.y = projTopLeft.y;

	//Doesn't work!
	//trueTopLeft.y = SC_HEIGHT - trueTopLeft.y;
	//trueBottomRight.y = SC_HEIGHT - trueBottomRight.y;

		if(x > trueTopLeft.x)
		{
			if(x < trueBottomRight.x)
			{
				if(y < trueTopLeft.y)
				{
					if(y > trueBottomRight.y)
					{
						(*it).mSelected = 1;
					}
				}
			}
		}

	}
}


Also, my current plan is to change the objects position via screen coordinates then reproject that into the world. Would this work?

Share this post


Link to post
Share on other sites
youre goin about it the long about way

for picking u should cast a ray into the scene + then see what billbords this ray passes through

Share this post


Link to post
Share on other sites
Quote:
Original post by zedz
youre goin about it the long about way

for picking u should cast a ray into the scene + then see what billbords this ray passes through


I thought about doing it like that, and only now do I realise I missed out something on the origonal post. I want to be able to click and drag these billboards, how would I do that If i don't project them first?

Share this post


Link to post
Share on other sites

This topic is 3629 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this