Skip to main content
GameDev.net gamedev.net
🔒 Locked

Converting Window Coordinates to Object Coordinates

Started by OpenGL_Guru May 2, 2006 at 10:34 AM 0 replies 400+ views
Original Post
OpenGL_Guru
OpenGL_Guru
i wanted to see if you thought my code is correct for converting window coordinates to openGL (object)coordinates, specifically objects in 3D and objects drawn directly to the screen(2D if you will). i think part of my problem is that i am having to translate away from the camera to get the 2D stuff to be seen. here i will draw 2 little bars on either side of the screen for picking with the mouse later on. i want to be able to click(click and drag) these 2 items across the screen as long as the mouse is over them and the left button is down. oh yeah i am using glut.

void draw_marks()
{
   glLoadIdentity();
   glTranslatef(-1.20, -1.1, -3.0); //must translate

   glBegin(GL_LINES);
    glVertex2f(0.2, 0.03);
    glVertex2f(0.2, -0.13);
   glEnd();
   
   glBegin(GL_LINES);
    glVertex2f(2.1, 0.03);
    glVertex2f(2.1, -0.13);
   glEnd();

  glPushMatrix();
   glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
   glBegin(GL_QUADS);
    glVertex2f(0.18, 0.01);
    glVertex2f(0.18, 0.09);
    glVertex2f(0.22, 0.09);
    glVertex2f(0.22, 0.01);
   glEnd();
   
   glBegin(GL_QUADS);
    glVertex2f(2.08, 0.01);
    glVertex2f(2.08, 0.09);
    glVertex2f(2.12, 0.09);
    glVertex2f(2.12, 0.01);
   glEnd();
    
  glPopMatrix(); 

}

I am using glutPassiveMotion to retrieve the x,y position on the screen(in window coordinates) every frame. i want to be able to send these x,y positions to my function that converts window to object coordinates but i am not getting the correct responses back for some things because i have multiple 2D objects on the screen(will explain a little more after source code). i wanted to give you now the convert code to see where i might be going wrong.

void convert_pixs(int x, int y, GLdouble *obj)
{ 
  GLdouble model[16], proj[16];
  GLint view[4];
  GLfloat z;
  glTranslatef(-1.20, -1.1, 0.0); //this matches where i started drawing the 2D objects on the screen, is this the correct idea?
  glGetDoublev(GL_MODELVIEW_MATRIX, model); 
  glGetDoublev(GL_PROJECTION_MATRIX, proj);
  glGetIntegerv(GL_VIEWPORT, view);
 
  glReadBuffer(GL_FRONT); //do i need this, especially if i have 3D objects that i want to pick as well?
  y = view[3] - (GLfloat) y - 1; 

  glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &z); //get the pixel(in 2D) w/respect to 3D Coordinates
  
  gluUnProject(x, y, z, model, proj, view, obj, obj+1, obj+2);

}

i think my main problem is that i have multiple 2D object on the screen..therefore all of them having different object coordinates relative to window positions. with this in mind i shouldnt(in my mind) have to draw multiple functions for a different glTranslate function to match. for instance i have a little GUI that is on the screen at different positions and after numerous tests i have to do a glTranslate on this for my callback to bring back correct matches for my x,y mouse position. but how do you encapsulate this for multiple separate 2D objects?
heh

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.