Jump to content
  • Advertisement
Sign in to follow this  

gluUnProject HELP Please

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

Hi there im trying to convert screen coords to game coords or wold space coords... Here is my rendering function
[Source]
	glViewport(0,0,CurrentWidth,CurrentHeight);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	gluPerspective(45,1,1.0f,250.0f);
	glMatrixMode(GL_MODELVIEW);
	glLoadIdentity();	
	glTranslatef(Cx,Cy,Cz);
	glRotatef(Rx,1,0,0);
	glRotatef(Ry,0,1,0);
	glRotatef(Rz,0,0,1);
	glTranslatef(float(map.Width) / -2.0f,float(map.Height) / -2.0f,0);
	mouseCursor = CalculateMouse(); // Grab Mouse Position

 ... Then the map is rendered and the MousePointer's Position
 ... that is aquired 

[/Source]
[Source]
CVector3	CalculateMouse()
{
	POINT mouse;					// Stores The X And Y Coords For The Current Mouse Position
	GLint viewport[4];				// Viewport Constraints
	GLdouble modelview[16];			// Model View Matrix
	GLdouble projection[16];		// Project Matrix
	GLfloat winX, winY, winZ;		// Windows X,Y,Z (Positions)
	GLdouble posX=0, posY=0, posZ=0;		// World or (Identity) for the Positions

	glGetIntegerv(GL_VIEWPORT, viewport);			// Retrieves The Viewport Values (X, Y, Width, Height)
	glGetDoublev(GL_MODELVIEW_MATRIX, modelview);	// Retrieve The Modelview Matrix
	glGetDoublev(GL_PROJECTION_MATRIX, projection);	// Retrieve The Projection Matrix

	GetCursorPos(&mouse);							// Gets The Current Cursor Coordinates (Mouse Coordinates)
	ScreenToClient(hWnd, &mouse);

	winX = (float)mouse.x;							// Holds The Mouse X Coordinate
	winY = (float)viewport[3] - (float)mouse.y;			// Holds The Mouse Y Coordinate

	// Get Distance of Point under Mouse Icon
	glReadPixels(winX, int(winY), 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &winZ);
	mouseDistance = winZ;

	// And Now Finaly We Use UnProject (which is basically like using the inverse matrix)
	gluUnProject( winX, winY, winZ, modelview, projection, viewport, &posX, &posY, &posZ);
	return CVector3(posX,posY,posZ);
};
[/Source]
SO the PosX,PosY,PosZ i get back from this GetMouse function is always as far away from the Camera/Rendered Position as possible... In a sense you could say WinZ is always == FarPlane if any1 can help i will appreciate it

Share this post


Link to post
Share on other sites
Advertisement
okay so on futher inspection it turns out that

WinZ was always == to 1.0f (100%) and that is because

[Source]
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT
.. All the Matrix and Camera Stuff done here
mouseCursor = CalculateMouse();
[/Source]


So now calculateMouse reads from the DEPTH_BUFFER to get the depth of where the mouse's pointer is... But because glClear(GL_DEPTH_BUFFER) is called first it cant get the depth and the depth is always 1.0f... Meaning the Darkness is as far as possible...

;-)

So by moving glClear() to just after the CalculateMouse() all is working well now... here is how the source looks

[Source]
glViewport(0,0,CurrentWidth,CurrentHeight); // Set Viewport
//
glMatrixMode(GL_PROJECTION); // Grab projection matrix
glLoadIdentity();
gluPerspective(45,1,1.0f,250.0f);

glMatrixMode(GL_MODELVIEW); // Set current Matrix to model view matrix
glLoadIdentity();
glTranslatef(Cx,Cy,Cz);
glRotatef(Rx,1,0,0);
glRotatef(Ry,0,1,0);
glRotatef(Rz,0,0,1);
glTranslatef(float(map.Width) / -2.0f,float(map.Height) / -2.0f,0);
mouseCursor = CalculateMouse();
//
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Clear Screen And Depth Buffer
[/Source]

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.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!