Will gluUnProject work for this?

I have a 3D point (Point1) and I have a 2D screen coodinate.

I want to take the current camera view angle as a plane normal and “push” the plane back along that normal until it touches Point1. I then want to project a line from my 2D coordinates along that normal until it intersects the plane. The resulting 3D coodinate of that last intersection is what I need.

I really thought I could gluProject Point1 to get the screen Z. Then pluUnProject my 2D coordinates using that Z and find what I needed. I haven’t had much luck getting anything useful from UnProject though.

A short version of what my code kinda looks like:

glu [img]http://www.opengl.org/discussion_boards/ubb/tongue.gif[/img]erspective(45.0, W/H, 0.25, 1000.0),
gl:translatef(0.0, 0.0, -Dist),
gl:rotatef(Elevation, 1.0, 0.0, 0.0),
gl:rotatef(Azimuth, 0.0, -1.0, 0.0),
ModelMatrix = gl:getDoublev(?GL_MODELVIEW_MATRIX),
ProjMatrix = gl:getDoublev(?GL_PROJECTION_MATRIX),


I can post more, but I’m wondering if there is something wrong with the way I’m rotating/translating, causing the weird values I get back from UnProject.

Thanks for any info!

What value are you using for zDepth? Its supposed to be between 0 and 1 (inclusive) where 0 is the near clipping plane and 1 is the far clipping plane.

Sorry it took me so long to reply…

Actually, I was plotting a line from Z depth 1.0 to 0. It seemed to always run up and down on the Y axis for some reason.

I ended creating my own “project” and “unproject” using the view frustum. This works as I would expect, and I was able to finish what I was doing. I never did get gluUnProject to work right.

This is how I use gluUnProject. You can have a look and see if you were missing anything.

	// Assume px and py are the 2D windows co-ordinates.
	GLdouble pz = 0;		// Set to a depth between 0 and 1 inclusive.
	GLdouble x, y, z;		// Stores resulting 3D position of mouse.
	GLint ViewPort[4];		// Holder for viewport.
	GLdouble ModelViewMatrix[16], ProjectionMatrix[16];
				// Holder for the Model View and Projection matrices.
	glGetIntegerv(GL_VIEWPORT, ViewPort);
				// Get current viewport.
	glGetDoublev(GL_MODELVIEW_MATRIX, ModelViewMatrix);
				// Get current model view matrix.
	glGetDoublev(GL_PROJECTION_MATRIX, ProjectionMatrix);
				// Get current projection matrix.
	py = ViewPort[3] - py - 1;	// Calculate y coordinate in OpenGL terms. Works only
				//  if Viewport dimensions match window dimensions.
				//  Otherwise replace "Viewport[3]" with window's
				//  height.
	gluUnProject((GLdouble)px, (GLdouble)py, pz, ModelViewMatrix, ProjectionMatrix,
		 ViewPort, &x, &y, &z);
				// Get position of mouse in virtual world.

	// Your code using result...