Finding world coord of mouse click

Started by
2 comments, last by Kruzty 19 years, 11 months ago
I''m sure a recently saw some example code of how to use gluUnProject (I think) to get the world coordinates of a mouse click. I can''t quite remember where though. Anyway, I now need to do this. I tried doing it myself, but it doesn''t seem to be working quite right. The hard part is that I know the Z plane that the click should be in (I know the click will land on a ply in the z=1 plane), but I''m not sure how to use this to unproject. Anyway, does anyone know of some example code for this or something? Thanks.
Advertisement
hi there, i have done this before on a game and this is how i did it...

bool CURSOR:ickCoords(float &coordX, float &coordZ){	glRenderMode(GL_SELECT);	glInitNames();	glPushName(0);	glPushMatrix();		glLoadIdentity();		glRotatef(-camera.rotX, 1.0f, 0.0f, 0.0f);		glRotatef(-camera.rotY, 0.0f, 1.0f, 0.0f);		glTranslatef(-camera.posX, -camera.posY, -camera.posZ);		terrain.Render(SELECTION);	glPopMatrix();	int hits=glRenderMode(GL_RENDER);	if (hits > 0)	{		int	choose = selectionBuffer[3];							// Make Our Selection The First Object		int depth = selectionBuffer[1];								// Store How Far Away It Is 		for (int loop = 1; loop < hits; loop++)						// Loop Through All The Detected Hits		{			if (selectionBuffer[loop*4+1] < (GLuint)depth)			{				choose = selectionBuffer[loop*4+3];					// Select The Closer Object				depth = selectionBuffer[loop*4+1];					// Store How Far Away It Is			}       		}		int patchID=choose;				float startX=(float)((patchID%MAPSIZE)*PATCHSIZE*MAPSCALE);		float startY=(float)((patchID/MAPSIZE)*PATCHSIZE*MAPSCALE);		hits=0;		for(float loopY=0; loopY<((PATCHSIZE*MAPSCALE)/PICK_DETAIL); loopY+=(8.0f*PICK_DETAIL))		{			for(float loopX=0; loopX<((PATCHSIZE*MAPSCALE)/PICK_DETAIL); loopX+=(8.0f*PICK_DETAIL))			{				glRenderMode(GL_SELECT);				glInitNames();				glPushName(0);				glPushMatrix();					glLoadIdentity();					glRotatef(-camera.rotX, 1.0f, 0.0f, 0.0f);					glRotatef(-camera.rotY, 0.0f, 1.0f, 0.0f);					glTranslatef(-camera.posX, -camera.posY, -camera.posZ);					terrain.RenderRT(startX+loopX, startY+loopY);				glPopMatrix();				hits=glRenderMode(GL_RENDER);				if (hits>0)				{					choose = selectionBuffer[3];							// Make Our Selection The First Object					depth = selectionBuffer[1];								// Store How Far Away It Is 					for (int loop = 1; loop < hits; loop++)					// Loop Through All The Detected Hits					{						if (selectionBuffer[loop*4+1] < (GLuint)depth)						{							choose = selectionBuffer[loop*4+3];				// Select The Closer Object							depth = selectionBuffer[loop*4+1];				// Store How Far Away It Is						}       					}					coordX=(float)(startX+loopX+((choose%8)*PICK_DETAIL));					coordZ=(float)(startY+loopY+((choose/8)*PICK_DETAIL));				}			}		}		return true;	}	return false;}i hope this helps     
-[ thx ]-
I ansver last time to when you ask... you can say if im not understand your question ^^
-[ thx ]-
anyway you need to set up the pickmode first like this.

GLint viewport[4];

glGetIntegerv(GL_VIEWPORT, viewport);
glSelectBuffer(512, selectionBuffer);

glPushMatrix();
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glPushMatrix();
glLoadIdentity();
gluPickMatrix( (GLdouble) (x), (GLdouble) (y), (GLdouble)(s_system.width/50), (GLdouble)(s_system.height/50), viewport);
gluPerspective(45.0f, (GLfloat) (viewport[2]-viewport[0])/(GLfloat) (viewport[3]-viewport[1]), 0.1f, 100.0f);
glMatrixMode(GL_MODELVIEW);
-[ thx ]-

This topic is closed to new replies.

Advertisement