Screen Points - openGL Points

Started by
6 comments, last by Codorke 18 years, 8 months ago
How can i change from openGL coord to screen coord ? for example, i want to know or my mouse is over a rectangle


// -- Class Button -- 
GLvoid button::draw(GLfloat x, GLfloat y, GLfloat z, GLfloat height, GLfloat width) {
  glBegin(GL_QUADS);
    glVertex3f(x, y, z);
    glVertex3f(x+width, y, z);
    glVertex3f(x+width, y-height, z);
    glVertex3f(x, y-width, z);
  glEnd();
}

bool button::IsButtonClicked(GLfloat mouse_x, GLfloat mouse_y) {
  GLfloat x = get_coordx();
  GLfloat y = get_coordy();
  GLfloat width = get_width();
  GLfloat height = get_height();

  // mouse coord are screen coord and x,y are openGL coord, how to solve ???
  if((mouse_x >= x && mouse_y <= x+width) && (mouse_y <= y && mouse_y >= y-height))
     return true;
  return false;
}
// -----------------

int DrawGLScene(GLvoid)	{
  // ...

  // button bttn (x, y, z, height, width);
  button bttn1(8.6f, 9.63f, -1.0f, 0.8f, 3.0f);
  bttn1.draw();

  // ...
}

LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){
  // ...

  case WM_LBUTTONDOWN:
  {
    POINTS ptClicked;
    ptClicked = MAKEPOINTS(lParam);

    if(bttn1.IsButtonClicked(ptClicked.x, ptClicked.y))
      // Do Something

    return 0;
   }

  // ...
}


Advertisement
gluProject converts from OpenGL coordinates to screen coordinates and gluUnProject does it the other way around. An example of this can be found here:
http://www.cs.umu.se/~dva97tha/code/unproject.c
Thanks !!!
It's still not the result it should be.
I debugged it and i still become verry different coord. The coord of making the button and now the glUnProjected coord of the mouse coord are still very different.

What's wrong with this code ?

case WM_LBUTTONDOWN:{  POINTS ptClicked;  ptClicked = MAKEPOINTS(lParam);  GLfloat wx=ptClicked.x, wy = ptClicked.y, wz=0.0f;  GLdouble x, y, z;  GLdouble modelview[16];  GLdouble projection[16];  GLint viewport[4];			  glGetIntegerv(GL_VIEWPORT, viewport);  glGetDoublev(GL_MODELVIEW_MATRIX,modelview);  glGetDoublev(GL_PROJECTION_MATRIX,projection);  gluUnProject(wx, wy, wz, modelview, projection, viewport, &x, &y, &z);  if(bttn_create.IsButtonClicked(x, y))    // Do Something;
* Are you sure ptClicked.x and ptClicked.y are "window coords" and not "screen coords"? You may need to convert them so that they are in the coordinate system of the window. You may also need to invert y so that ptClicked.y=windowHeight-ptClicked.y
* Make sure viewport, modelview and projection contain what was used to render the scene (the gui in this case). In particular, modelview should contain only the viewing transformation (i.e. positioning the camera), and no modelling transformations (positioning different objects in the scene.)
* You set wz to zero, which means that the button must either be at the near clipping plane or the projection is orthographic. This is probably the case, but if not you should get wz with glReadPixels
And how can i be sure i succeed in all these remarks ?
Point 1. Check the documentation to see what form the mouse coordinates have. Or you could just try inverting y and see what happens.
Point 2. The easiest way to be sure is to save viewport and projection just after you've set them up (most likely in the code where you handle setting window size/reshaping the window, or, if you use different kinds of projections in the program, just before you draw the gui). For the modelview matrix, just get it after you've moved the camera (with gluLookAt or whatever method you use.)
Point 3. Well.. Is your projection setup with glOrtho or gluOrtho2D (when drawing the gui)? In that case, there should be no problem. If it's gluPerspective or glFrustum, then you should use glReadPixels to get the z-value.

Thanks very much !!!

I needed indeed to convert my y coord and had to calculate the z with glReadPixels. My buttons are interactive with the mouse now so thanks a lot !!!

This topic is closed to new replies.

Advertisement