Skip to main content
GameDev.net gamedev.net
🔒 Locked

Pan & gluUnProject

Started by Libe Dec 10, 2005 at 1:19 PM 0 replies 950+ views
Original Post
Libe
Libe
Hello, I'm having an hard time trying to understand why gluUnProject doesn't work as expected. My app has several orthographics views (front, top, left) and I'd like to implement pan on each of them. To pan I use gluUnProject to get the object coordinates of the mouse drag, then offset my viewing volume with glOrtho. The problem is that I can pan properly in the front view; in the others I can pan only in one direction (x or y) but none both. Here's the function I use to campute object space offset of the mouse drag, oldP and newP are pixel coordinates of the mouse drag, dx and dy is the object space offset.

void ComputeDistances(int oldP[2], int newP[2], float* dx, float* dy)
{
	GLdouble modelview[16];
	GLdouble proj[16];
	GLint view[4];
	GLdouble nP[3], oP[3];

	glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
	glGetDoublev( GL_PROJECTION_MATRIX, proj );
	glGetIntegerv( GL_VIEWPORT, view );

	/* flip y to get it openGL like (0,0) bottom left */
	gluUnProject( newP[0], view[3] - newP[1], 0.0f,
		modelview,
		proj,
		view,
		&nP[0],
		&nP[1],
		&nP[2] );
	gluUnProject( oldP[0], view[3] - oldP[1], 0.0f,
		modelview,
		proj,
		view,
		&oP[0],
		&oP[1],
		&oP[2] );

	*dx = nP[0] - oP[0];
	*dy = nP[1] - oP[1];
}

Once I get dx and dy I offset my view volume like this:

	orthoViews[side].cx -= dx;
	orthoViews[side].cy -= dy;

In my display function I use this simple code to set up my projection and modelview matrix:

void display()
{  
	glMatrixMode (GL_PROJECTION);
	glLoadIdentity ();
	glOrtho(orthoViews[side].cx - 2,
		orthoViews[side].cx + 2,
		orthoViews[side].cy - 2,
		orthoViews[side].cy + 2,
		-2,
		2);

	glClear(GL_COLOR_BUFFER_BIT);
	
	glMatrixMode (GL_MODELVIEW);
	glLoadIdentity();
	
	switch(side) {
	case TOP:
		glRotatef(90.0, 1.0, 0.0, 0.0);
		break;
	case RIGHT:
		glRotatef(-90.0, 0.0, 1.0, 0.0);
		break;
	case FRONT:
		/* default gl orientation */
		break;
	default:
		break;
	}

	glutWireTeapot(0.5);

	glutSwapBuffers();
}

So basically, in the front view pan works ok, but in the top and in the right view I get always respectively dy and dx = 0!...don't know why. Hope some of you guys can help cos I'm really stuck. Thanks Libe P.S: I could post I little glut test to show my problem if of any help.
Libero Spagnolini
Libe
Libe
I solved it by choosing different coordinates for different views to compute pan offset:

void ComputeDistances(int oldP[2], int newP[2], float* dx, float* dy){	GLdouble modelview[16];	GLdouble proj[16];	GLint view[4];	GLdouble nP[3], oP[3];	GLdouble winX, winY, winZ;	glGetDoublev( GL_MODELVIEW_MATRIX, modelview );	glGetDoublev( GL_PROJECTION_MATRIX, proj );	glGetIntegerv( GL_VIEWPORT, view );		winX = newP[0]; winY = view[3] - newP[1];	gluUnProject( winX, winY, 0.0f,		modelview,		proj,		view,		&nP[0],		&nP[1],		&nP[2] );	winX = oldP[0]; winY = view[3] - oldP[1];	gluUnProject( winX, winY, 0.0f,		modelview,		proj,		view,		&oP[0],		&oP[1],		&oP[2] );	switch(side){		case FRONT:			*dx = nP[0] - oP[0];			*dy = nP[1] - oP[1];			break;		case TOP:			*dx = nP[0] - oP[0];			*dy = -(nP[2] - oP[2]);			break;		case RIGHT:			*dx = -(nP[2] - oP[2]);			*dy = nP[1] - oP[1];	}}


I guess this works because gluUnProject return object coordinates so I must, for instance in the top view, move along object z to get window y.

Libe
Libero Spagnolini

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.