Sign in to follow this  
dawidjoubert

gluUnProject HELP Please

Recommended Posts

dawidjoubert    161
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
dawidjoubert    161
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

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