{steinsoft.net}
 
 
 
 
 
Home/News
Code Snippets
Board
Projects
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Leipzig
Home :: Programming :: Code Snippets :: OpenGL :: Window to OpenGL coordinates

[ Window to OpenGL coordinates ]

Mapping or converting window coordinates to OpenGL object coordinates is indeed a very nice thing. Say you want to drag an object in your scene with the mouse. Simply handle all necessary mouse events and then convert the current mouse position in the corresponding coordinates in OpenGL space. Sounds easy and .. yes it really is!

The function that makes this very, very easy is gluUnProject(..) declared in GL\glu.h. Here's the definition:

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 );

The three floats winx, winy and winz represent the window coordinates that should be converted. modelMatrix, projMatrix and viewport are the corresponding OpenGL state values that can be retrieved with glGetDouble(..) and glGetInteger(..). They are needed to calculate the final object coordinates that will be stored in the pointers objx, objy and objz.

This function works best in othogonal projections only. With perspective projection the results aren't very satisfactory. Also remember to reset the current modelview matrix with glLoadIdentity() before using the function. Besides this function there is a very similar function called gluProject(..) that does the inverse of gluUnProject: it converts object coordinates to window coordinates. It works exactly the same only that the winx etc. and objx parameters and swapped.

Here is an example of how to use gluUnProject(..). Say you have an object that will be translated based on the array double position[3]. Secondly you want this object moved when the mouse moves too. Here's the code (Mfc mouse handler as example):

void CMainWindow::OnMouseMove(UINT nFlags, CPoint point)
{
   glLoadIdentity();
   //only if left mouse button is down
   // (set in the mouse press event for example)
   if (mouseDown)
   {
      //****
      //project window coords to gl coord
      //*****
 
      GLdouble modelMatrix[16];
      glGetDoublev(GL_MODELVIEW_MATRIX,modelMatrix);
      GLdouble projMatrix[16];
      glGetDoublev(GL_PROJECTION_MATRIX,projMatrix);
      int viewport[4];
      glGetIntegerv(GL_VIEWPORT,viewport);
      gluUnProject(
         point.x,
         point.y,
         0,
         modelMatrix,
         projMatrix,
         viewport,
         //the next 3 parameters are the pointers to the final object
         //coordinates. Notice that these MUST be double's
         &position[0], //-> pointer to your own position (optional)
         &position[1], // id
         &position[2] // id
      );
      //coords of the object are now saved in position.
   }
 
  CWnd::OnMouseMove(nFlags, point);
}

If you have further questions or doubts, don't hesitate to m@il me or to ask your questions in the message board. Happy Coding !

 Last edited 2002-12-07 21:05:30 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0