Jump to content
  • Advertisement
Sign in to follow this  
TFS_Waldo

OpenGL gluUnProject supplying me with weird values??

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

Hey everyone. I am trying to use "gluUnProject" to get the mouse click coordinates in OpenGL coordinates. Well, I am using OpenGL in MFC, in a splitter-window pane. I am working on a 3D world editor, and I want to go ahead and get this part out of the way. Anyhow, here is how I initialize OpenGL:
BOOL CWorldEditorView::InitGL() 
{
	// TODO: Initialize OpenGL for rendering
	glShadeModel(GL_SMOOTH);    // Set the shading model

	glClearColor(0.0f, 0.0f, 0.0f, 0.0f);   // Set the default clear color
	glClearDepth(1.0f);    // Set the clearing depth

	glFrontFace(GL_CW);      // Set the culling face
	glEnable(GL_CULL_FACE);  // and set
	glCullFace(GL_FRONT);    // the culling mode

	glEnable(GL_DEPTH_TEST);     // Enable depth testing
	glDepthFunc(GL_LEQUAL);      // Set the depth-func.

	glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);

	glMatrixMode(GL_PROJECTION);  // Setup the
	glLoadIdentity();             // projection matrix

	gluPerspective(45.0f, m_fWHRatio, 1.0f, 100.0f);   // Setup the perspective

	return TRUE;
}

And here's the left-mouse button click handler:
void CWorldEditorView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	int    vp[4];
	double mv[16], pm[16];
	float  winX, winY, winZ;
	double x2, y2, z2;
	double y;
	CPoint cursor;

	GetCursorPos(&cursor);
	ScreenToClient(&cursor);

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

	winX = cursor.x;
	winY = cursor.y;
	y = (float) vp[3] - winY;

	glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

	gluUnProject(winX, y, 0.0f, mv, pm, vp, &glX, &glY, &glZ);
	gluUnProject(winX, y, 1.0f, mv, pm, vp, &x2, &y2, &z2);

	CString str;
	str.Format("%f, %f, %f", glX-x2, glY-y2, glZ-z2);
	AfxMessageBox(str);

	bLines = TRUE;
	
	CView::OnLButtonDown(nFlags, point);
}

BTW, I do this in the "OnSize" function:
	CView::OnSize(nType, cx, cy);
	
	// TODO: Add your message handler code here
	if (cy == 0) 
	    m_fWHRatio = (float) cx;    // Set the width-height ratio
	else 
		m_fWHRatio = (double) cx / (double) cy;    // Same as above

	glViewport(0, 0, cx, cy);    // Setup the viewport

	InitGL();     // Initialize OpenGL for rendering

Everything there is fine. But when I use the "OnLButtonDown" to display the clicked coordinates, even near the middle of the screen, I get values like "-3.04756, -1.08705, 93.73439". The "93" is coming from the z-value. I have used "gluUnProject" before, without this problem. Can anyone tell me why it is doing this? Also, when I draw a line from 0.0f,0.0f to 1.0f,0.0f, it goes from the center of the screen all the way to the right side of the screen. Thanks in advance, Matt U. ("Waldo")

Share this post


Link to post
Share on other sites
Advertisement
Guest Anonymous Poster
I gotta tell you this IS strange I dont see why you need the pixel read but other than that youre code looks good. Is this a windowed app?

Share this post


Link to post
Share on other sites
Quote:


glReadPixels(winX, winY, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);

gluUnProject(winX, y, 0.0f, mv, pm, vp, &glX, &glY, &glZ);
gluUnProject(winX, y, 1.0f, mv, pm, vp, &x2, &y2, &z2);

CString str;
str.Format("%f, %f, %f", glX-x2, glY-y2, glZ-z2);


When you use gluUnProject, the third parameter is the depth component (in your case, winZ from glReadPixels, which is a value from 0 (near plane) to 1 (far plane)). Why would you need the substraction?

Quote:
Also, when I draw a line from 0.0f,0.0f to 1.0f,0.0f, it goes from the center of the screen all the way to the right side of the screen.


Since you use perspective projection (not ortho2D), what is the z value for this line? The default is 1.0 I think. Besides, the line should go from center to right, because the center of the screen is (0, 0, 0) if you use gluPerspective.

Share this post


Link to post
Share on other sites
It is only giving me weird values when I click off in the distance. But when I click on a triangle it gives me the correct coordinates.

And I did change "gluPerspective" to just one call, taking 'winZ' as the third parameter. I think it is just because I was clicking off in the distance. Now I'm looking into ray-plane collision detection. I have created a 'Point3D' class, and I made one (for testing) called 'begin' and 'end'. 'begin' is the click-coords., but with 'z' as '0.0f', and 'end' is 'glX', 'glY', and 'glZ' that I retrieved from "gluUnProject". I am just trying to get the hang of all this, so I know how to do it when I put A LOT of effort into this editor. =)

Thanks for the help, but I think I got it now! =)

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!