Part of the Khronos Group
OpenGL.org

The Industry's Foundation for High Performance Graphics

from games to virtual reality, mobile phones to supercomputers

Results 1 to 6 of 6

Thread: Getting object position

  1. #1
    Junior Member Newbie
    Join Date
    Jul 2010
    Posts
    11

    Getting object position

    In a simulation I'm working on, I have an object that has gone through a bunch of transformations, and I need to get its current coordinates. The only idea I have is to somehow use the modelview matrix, which I know how to get, but the problem is that it's affected by the the object position and the camera position, and the camera position is changeable. Is there any way to just get the object coordinates/matrix, or to somehow get that out of the modelview matrix?

  2. #2
    Member Regular Contributor DmitryM's Avatar
    Join Date
    Mar 2009
    Location
    Toronto
    Posts
    436

    Re: Getting object position

    There is no such term as object/World matrix in OpenGL. It's an imaginary space you use for convenience, because objects tend to not change it's spatial properties in this space.

    You should track your object position yourself.

  3. #3
    Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    352

    Re: Getting object position

    I always say the DmitryM suggestion, it's really strange that people don't know their objects position in their program.
    But if you organized the program with some strange hierarchical structure using fixed pipeline transformation (FAIL!) and is difficult for you to compute the object position (MEGA-FAIL!) you can compute the objectMatrix like this.

    currentModelViewMatrix = cameraMatrix * objectMatrix

    inverse(cameraMatrix) * currentModelViewMatrix = objectMatrix

    but I really suggest to rethink the design of your application.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

  4. #4
    Junior Member Newbie
    Join Date
    Jul 2010
    Posts
    11

    Re: Getting object position

    Yeah, the problem is that I'm working with code I didn't design, trying to add functionality to it. I've been trying to figure out a way to track the object's position given the current organization of the code, but so far, no success. How do you get the camera matrix? (the whole program can be controlled by this zoom-pan-rotate layer that constantly changes the camera position/angle but doesn't seem to keep track of it)

  5. #5
    Member Regular Contributor DmitryM's Avatar
    Join Date
    Mar 2009
    Location
    Toronto
    Posts
    436

    Re: Getting object position

    You can't.

    Don't use GL context as a communication protocol/buffer between your and existing part of a big program.

  6. #6
    Member Regular Contributor Rosario Leonardi's Avatar
    Join Date
    Aug 2008
    Location
    Italy
    Posts
    352

    Re: Getting object position

    Yep, I got the same problem (modify stranger code) several time.

    You don't even know the camera position and orientation?
    Well... you have a problem then. The final matrix only give you the position of the object in the camera coordinate system, if you don't have the camera matrix you are dead.

    Check in the code, the standard "fixed pipeline" openGl program do the following

    glLoadIdentity()
    setup_Camera_matrix

    glpushMatrix()
    setup Object 1 Matrix
    draw object 1
    glPopMatrix()

    ....
    ....

    glpushMatrix()
    setup Object n Matrix
    draw object n
    glPopMatrix()

    if nobody set the camera matrix then you can consider the camera in the origin looking in the Z negative.
    ~ ~ I tell you, realtime 3D is made of blood, sweat and screams! ~ ~

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •