PDA

View Full Version : Mouse Click to 3d



uniQQQ
11-27-2016, 09:38 AM
Hi.
I need to get a mouse click position to 3d coordinates.
I have the following code for my mouseclick:


void mouseClick(int button, int state, int x, int y)
{
if(button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)
{
point[1].x = x;
point[1].y = y;
point[1].z = 0;
}

}

but its only doing it on the top right quadrant because (0,0) is centre of screen.

Is there a way to convert 2d click to 3d coordinate. I just need the x,y ( i dont mind the dept(z)).

Thanks

Dark Photon
11-28-2016, 06:23 AM
Take your window-space "mouse click" position at the near or far clip plane, and transform it back into whatever space you're interested in (OBJECT, WORLD, or EYE SPACE). This is one use for gluUnProject (https://www.opengl.org/sdk/docs/man2/xhtml/gluUnProject.xml), though you could do it yourself with the transform matrices. A few related links: link (https://www.opengl.org/wiki/Viewing_and_Transformations#How_can_I_find_the_obj ect-space_coordinates_for_a_pixel_on_the_screen.3F), link (https://www.opengl.org/wiki/GluProject_and_gluUnProject_code). Also search the OpenGL forums for references to gluUnProject: link (https://www.google.com/search?q=gluunproject+site%3Aopengl.org+inurl%3Adi scussion_boards&ie=utf-8&oe=utf-8#q=mouse+gluunproject+site:opengl.org+inurl:discu ssion_boards)

Is the projection you're using perspective or orthographic? If perspective, then note that your mouse click doesn't correspond to a specific X,Y in OBJECT, WORLD, or EYE coordinates without presuming a depth value (to make it a specific point in space)? But if orthographic, then it "could" correspond to a specific X,Y in your target space "if" your VIEWING transform has Z in your target space pointing into/out of the screen.