Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Kruzty

Finding world coord of mouse click

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

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.

Share this post


Link to post
Share on other sites
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;
}

[CODE]

i hope this helps

Share this post


Link to post
Share on other sites
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);

Share this post


Link to post
Share on other sites

  • 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!