Results 1 to 6 of 6

Thread: How to get the OpenGL Co-Ordinates ?

  1. #1
    Junior Member Newbie
    Join Date
    Jan 2002
    Posts
    27

    How to get the OpenGL Co-Ordinates ?

    Hi,

    My doubt is how to get the XYZ opengl Co-Ordinates ? I want to get the xyz opengl co-ordinates not screen co-ordinates by clicking on the object using mouse.I try to implement using glReadPixels() and gluUnProject() functions. But i can't got actual value for co-ordinate.

    Can you suggest the proper way or code to get the coordinates.

    Thanx in advance,
    rgrds,
    Jerry

    [This message has been edited by jerin3D (edited 01-09-2002).]

  2. #2
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

    Re: How to get the OpenGL Co-Ordinates ?

    gluUnProject do that for you.

  3. #3
    Junior Member Newbie
    Join Date
    Jan 2002
    Posts
    27

    Re: How to get the OpenGL Co-Ordinates ?

    Originally posted by jide:
    gluUnProject do that for you.
    Hi

    My current problem is that How to get the z screen co-ordinate for the clicked point on the object ? for giving it as a input for the gluUnproject() function.The following shows my implementaion.I know that it is a simple problem but am not expert in OpenGL. pls help me

    glGetDoublev(GL_MODELVIEW_MATRIX, modelMatrix);
    glGetDoublev(GL_PROJECTION_MATRIX, projMatrix);
    glGetIntegerv(GL_VIEWPORT, viewport);
    ////For reading z value////
    GLfloat*zValue=new float[viewport[2]*viewport[3]];
    glFinish();
    glPixelStorei(GL_PACK_ALIGNMENT,4);
    glPixelStorei(GL_PACK_ROW_LENGTH,0);
    glPixelStorei(GL_PACK_SKIP_ROWS,0);
    glPixelStorei(GL_PACK_SKIP_PIXELS,0);
    glReadPixels(0,0,point.x,point.y,GL_DEPTH_COMPONEN T,GL_FLOAT,zValue);
    /////////////////////////////////////////
    float zCoordinate = zValue[(point.y*viewport[2])+point.x]; //// Taking z value from the array
    CRect rect;
    GetClientRect(&rect);
    int y = rect.Height() - point.y;
    if(GL_TRUE==gluUnProject(point.x, y,fHit, modelMatrix, projMatrix, viewport, &objx, &objy, &objz))

    Waiting for ur replay...

    rgrds,

    Jerry

  4. #4
    Senior Member Regular Contributor
    Join Date
    Mar 2001
    Posts
    469

    Re: How to get the OpenGL Co-Ordinates ?

    // glut callback
    // generates ogl word coordiantes based upon mouse click, i assume a glortho projection so setting the z value to -1 or something other does not change result. with gluperspective you get object coords as if the mouse would be in the plane( here: z=-1)
    perhaps you can use your gluperspective znear plane value
    void MouseClick(int button, int state, int x, int y)
    {
    GLdouble modelview[16],projection[16];
    GLint viewport[4];
    GLdouble objx,objy,objz;

    if((button==BUTTON_LEFT )&&(state==GLUT_DOWN))
    {
    // get matrices
    glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
    glGetDoublev(GL_PROJECTION_MATRIX,projection);
    // get viewport
    glGetIntegerv(GL_VIEWPORT,viewport);

    // convert mouse coordinates into world coordinates
    gluUnProject(x,y,-1,modelview,projection,viewport,&objx,&objy,&objz) ;
    // now we have the mouse position in objx, objy, objz
    }
    }

  5. #5
    Senior Member OpenGL Pro
    Join Date
    Jul 2001
    Location
    France
    Posts
    1,662

    Re: How to get the OpenGL Co-Ordinates ?

    sorry, i've used it only once some times ago.
    So i don't remember and i don't have it here.

    Try to see one of the Mesa libraries demo: there are 3 linear cubes that can be moved by the mouse.
    But if you're under Windows, it will be difficult, i convain. That's why i want appologies.
    sorry

    JD

  6. #6
    Senior Member OpenGL Pro
    Join Date
    May 2000
    Location
    Naarn, Austria
    Posts
    1,102

    Re: How to get the OpenGL Co-Ordinates ?

    Here is a part of a project I am working on where I use gluUnProject to pick objects with the mouse:
    Code :
    // draw objects
    board->Draw(true);
     
    // get matrices
    glGetDoublev(GL_MODELVIEW_MATRIX, modelview);
    glGetDoublev(GL_PROJECTION_MATRIX, projection);
    glGetIntegerv(GL_VIEWPORT, viewport);
     
    // convert from windows coordinate system to ogl coordinate system
    y = viewport[3] - y;
     
    // convert x,y coordinates to GLfloat
    ix = (GLfloat)x; iy = (GLfloat)y;
     
    // read z coordinate
    glReadPixels(x, y, 1, 1, GL_DEPTH_COMPONENT, GL_FLOAT, &iz);
     
    // unproject
    gluUnProject(ix, iy, iz, modelview, projection, viewport, &ox, &oy, &oz);
    I hope that helps
    Overmind

Similar Threads

  1. Co-ordinates mess up
    By programmer17 in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 01-09-2012, 12:06 PM
  2. Mouse Co-ordinates
    By Dastagir in forum OpenGL: Advanced Coding
    Replies: 2
    Last Post: 10-13-2009, 12:16 PM
  3. co-ordinates etc.
    By incredible_hulk in forum OpenGL: Basic Coding
    Replies: 6
    Last Post: 12-07-2001, 01:18 PM
  4. glOrtho co-ordinates
    By incredible_hulk in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 08-11-2001, 06:46 AM
  5. Co-ordinates - Newbie
    By rramana in forum OpenGL: Basic Coding
    Replies: 1
    Last Post: 02-29-2000, 12:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean