# The Industry's Foundation for High Performance Graphics

1. ## mouse coordinates

Hello,
Can somebody tell me how can I obtain a
glVertex3d from mouse coordinates ?
Thanks,
Franco

2. ## Re: mouse coordinates

Well, as I understand it you would click with the mouse on the screen, and a vertex should show up in a perspective view. This is not possible unless you use an already defined z-value. Then you could compute the x- and y-value based on the mouse x- and y-values. Which in term would cause the vertices to show up in the same plane in space. If you give me more information about your task, I can help you further.

Martin

3. ## Re: mouse coordinates

use gluUnProject to map window-coords to world space...

gluUnProject(int winx, int winy, int winz, double *model, double *proj, int *view, double *x, double *y, double *z)

winx and winy would be your mouse coords, model is the modelview matrix, proj is the projection matrix, view is the viewport, x y and z are your resultant world space coords. use the glGetDoublev function to get the matrices and glGetIntegerv to get the viewport. you may want to play around with the winz value. if you call the function twice and use 0 for winz in the first call, then use 1, your resultant points define a ray in your world space which you can then use to select objects (use collision detection with the ray and your objects). good luck

b

4. ## Re: mouse coordinates

ocvan
Originally posted by coredump:
use gluUnProject to map window-coords to world space...

gluUnProject(int winx, int winy, int winz, double *model, double *proj, int *view, double *x, double *y, double *z)

winx and winy would be your mouse coords, model is the modelview matrix, proj is the projection matrix, view is the viewport, x y and z are your resultant world space coords. use the glGetDoublev function to get the matrices and glGetIntegerv to get the viewport. you may want to play around with the winz value. if you call the function twice and use 0 for winz in the first call, then use 1, your resultant points define a ray in your world space which you can then use to select objects (use collision detection with the ray and your objects). good luck

b
I put this code in OnLButtonDown

GLdouble model[16];
double x;
double y;
double z;
GLdouble proj[16];
GLint view[4];
glGetDoublev(GL_MODELVIEW_MATRIX, model);
glGetDoublev(GL_PROJECTION_MATRIX, proj);
glGetIntegerv(GL_VIEWPORT, view);
gluUnProject((GLdouble)point.x, (GLdouble)point.y, (GLdouble)0, model, proj, view, &x, &y, &z );
CString str;
str.Format("x = %d, y = %d, z = %d", x, y, z);
AfxMessageBox(str);

so I have really weird values in x, y, z.

What I want to do is :
having a drawble entity let say a cube
i wish to move it in 3d space with the mouse

Thanks,
Franco

5. ## Re: mouse coordinates

this can be done many different ways. one is to save the last mouse position and each time you get a click, use it with the last to obtain a delta, or change in position. use this delta to translate your geometry.

...
delta.x = last.x - cur.x;
delta.y = last.y - cur.y;
...

now use the delta values in translate, or rotate. you may want to scale it (ex. delta * 0.3) just try different values until you acquire the results you need. this works well for x and y translations. if you want a z translation, you will have to come up with a different scheme, perhaps using the right mouse button.

b

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•