• Advertisement

Archived

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

Getting the OpenGL coordinates of the mouse... HOW??!

This topic is 5810 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 trying to create a program that draws a point at the place you click. However, I noticed that getting the coordinates of the mouse returns the window coordinate and not the OpenGL coordinate. Is there anyway to overcome this? Thanks. "A computer and man are similar in all respects except for the fact that a computer doesn''t blame another computer for it''s mistakes." ~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~

Share this post


Link to post
Share on other sites
Advertisement
How do you use it?

"A computer and man are similar in all respects except for the fact that a computer doesn''t blame another computer for it''s mistakes."

~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~

Share this post


Link to post
Share on other sites
MSDN doesn''t have anything on OpenGL functions.... hmmm...
could ne1 else try?

"A computer and man are similar in all respects except for the fact that a computer doesn''t blame another computer for it''s mistakes."

~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
I got this from a help file

The gluUnProject function maps window coordinates to object coordinates.

int gluUnProject(

GLdouble winx,
GLdouble winy,
GLdouble winz,
const GLdouble modelMatrix[16],
const GLdouble projMatrix[16],
const GLint viewport[4],
GLdouble * objx,
GLdouble * objy,
GLdouble * objz
);
Parameters

winx, winy, winz

The window coordinates to be mapped.

modelMatrix

The modelview matrix (as from a glGetDoublev call).

projMatrix

The projection matrix (as from a glGetDoublev call).

viewport

The viewport (as from a glGetIntegerv call).

objx, objy, objz

The computed object coordinates.

Return Values

If the function succeeds, the return value is GL_TRUE.
If the function fails, the return value is GL_FALSE.

Remarks

The gluUnProject function maps the specified window coordinates into object coordinates using modelMatrix, projMatrix, and viewport. The result is stored in objx, objy, and objz.

Share this post


Link to post
Share on other sites
#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glu32.lib")
#pragma comment(lib,"glaux.lib")

#include <windows.h>

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
#include <stdio.h>

void display(void)
{
glClear(GL_COLOR_BUFFER_BIT);
glFlush();
}

void reshape(int w, int h)
{
glViewport (0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective (45.0, (GLfloat) w/(GLfloat) h, 1.0, 100.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}

void mouse(int button, int state, int x, int y)
{
GLint viewport[4];
GLdouble mvmatrix[16], projmatrix[16];
GLint realy; /* OpenGL y coordinate position */
GLdouble wx, wy, wz; /* returned world x, y, z coords */

switch (button) {
case GLUT_LEFT_BUTTON:
if (state == GLUT_DOWN) {
glGetIntegerv (GL_VIEWPORT, viewport);
glGetDoublev (GL_MODELVIEW_MATRIX, mvmatrix);
glGetDoublev (GL_PROJECTION_MATRIX, projmatrix);
/* note viewport[3] is height of window in pixels */
realy = viewport[3] - (GLint) y - 1;
printf ("Coordinates at cursor are (%4d, %4d)\n",
x, realy);
gluUnProject ((GLdouble) x, (GLdouble) realy, 0.0,
mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
printf ("World coords at z=0.0 are (%f, %f, %f)\n",
wx, wy, wz);
gluUnProject ((GLdouble) x, (GLdouble) realy, 1.0,
mvmatrix, projmatrix, viewport, &wx, &wy, &wz);
printf ("World coords at z=1.0 are (%f, %f, %f)\n",
wx, wy, wz);
}
break;
case GLUT_RIGHT_BUTTON:
if (state == GLUT_DOWN)
exit(0);
break;
default:
break;
}
}

int main(int argc, char** argv)
{
glutInit(&argc, argv);
glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
glutInitWindowSize (500, 500);
glutInitWindowPosition (100, 100);
glutCreateWindow (argv[0]);
glutDisplayFunc(display);
glutReshapeFunc(reshape);
glutMouseFunc(mouse);
glutMainLoop();
return 0;
}

This is from the red book, so you''ll need the glut dll. This just demostrats how to use the gluUnProject.

Share this post


Link to post
Share on other sites
Thanks a lot guys. I got it to work :-) !!!!
Just one Q , why add this line :
  realy = viewport[3] - (GLint) y - 1;  

Why can''t I just use y?
Thanks again!!

-----------------------------
"Give a man a fish and you feed him for a day, teach him to use the Net and he won''t bother you for weeks."

~wUn LoVe tO aLl ThE mAlUs OuT tHeRe!~

Share this post


Link to post
Share on other sites

  • Advertisement