Jump to content
  • Advertisement
Sign in to follow this  
Codorke

OpenGL Screen Points - openGL Points

This topic is 4847 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

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

  // ...
}


Share this post


Link to post
Share on other sites
Advertisement
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;


Share this post


Link to post
Share on other sites
* 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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 !!!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!