getting orientation and position in opengl

This is a discussion on getting orientation and position in opengl within the Game Programming forums, part of the General Programming Boards category; I'm trying to extract current orientation and position in opengl. I know it is wrapped in the GL_MODELVIEW matrix, but ...

  1. #1
    Registered User
    Join Date
    Mar 2008
    Location
    vancouver bc
    Posts
    28

    Exclamation getting orientation and position in opengl

    I'm trying to extract current orientation and position in opengl. I know it is wrapped in the GL_MODELVIEW matrix, but i am having trouble interpreting it.

    Question: How do i extract current orientation and position in opengl?

    Please note that I am having trouble after i use gluLookAt(), i have no trouble understanding the GL_MODELVIEW matrix before I use gluLookAt(), but after gluLookAt(), the matrix is distorted in a way that no longer makes sense to me. This is an advance opengl problem that I cannot find much information on the net. If you know any webpage that talks about what gluLookAt() does at the implementation level or know how to extract orientation/position in opengl, please help me. Any suggestion is appriciated.

    I have to use gluLookAt() because, i need to render it on the screen, and I need to extract info so that i can do my own math to it and return the result into GL_MODELVIEW matrix.

    I need help ASAP, for there is a deadline.

  2. #2

    Join Date
    May 2005
    Posts
    1,041
    Off the top of my head, I believe you need to use glGetFloatv(GL_MODELVIEW_MATRIX, float*). I might be a tad off so use MSDN to get the exact arguments.

    Anyway, this will throw the current modelview matrix into a 16 float array. Elements 0-3 is the first *row*, 4-7 the second *row* etc.

    Mathematically, OpenGL stores column major matrices, different from a C++ array (which is row major). The last *column* of the OpenGL matrix is the translation applied to the geometry before being rendered. The first three columns constitute the rotation applied to the geometry before being rendered.

    Other than that I'm not particularly sure of what, exactly, you want.

    I have the implementation of the lookat function from an nVidia shader demo, but it's on my other computer, and I don't really know how helpful it'd be. The implementation isn't particularly intuitive. Anywho you could find it online easy enough.
    I'm not immature, I'm refined in the opposite direction.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Location
    vancouver bc
    Posts
    28
    I need the orientation and position of current current frame.

    for example if i translate 5 unit up, i want to somehow read back from GL_MODELVIEW matrix that current origin is at 0,0,5, and orientation is like a 3x3 identity since i didn't change it.

    The problem is that gluLookAt() mess it up. It is looking all funny after gluLookAt().

  4. #4

    Join Date
    May 2005
    Posts
    1,041
    Post the code and post the results you are getting. I'm pretty sure it's something trivial that's farking you up.
    I'm not immature, I'm refined in the opposite direction.

  5. #5
    Registered User
    Join Date
    Mar 2008
    Location
    vancouver bc
    Posts
    28

    Solved: OpenGL Modelview matrix

    Code:
    Just thought might be useful to some other students in the same situation:
    
    1)
    The modelview is a float[16], and write on paper as:
    
    
    Code:
    m0 m4 m8   m12 
    m1 m5 m9   m13
    m2 m6 m10 m14
    m3 m7 m11 m15
    where,
    Code:
    m0 m4 m8   
    m1 m5 m9   
    m2 m6 m10
    is the orientation, {m0 m1 m2} is the where the x-axis of the current currently pointing at in the absolute space, {m4 m5 m6} for y-axis, {m8 m9 m10} for z-axis. {m12 m13 m14} is the {x y z} of the current frame 2) Modelview matrix typically is initially transformed by a translation and orientation, so even though you thought you didn't twist the frame or translate the current cursor in the absolute space, the modelview matrix still may look like: 0.432 0.432 0.185 2 0.858 0.738 0.980 4 0.890 0.280 0.178 5 0 0 0 1 the reason is you are looking at the product of MODEL matrix and the VIEW matrix. more precisely, you are looking at: Identity matrix * the 4x4 matrix you see above and because of the MODEL matrix is currently identity, you are effectively seeing the VIEW matrix, that applies the camera transformation to the MODEL matrix. to reverse the effect of the VIEW matrix (so you can get the position and orientation in the MODEL space, the space we are use to) >take the transpose of the orientation matrix (refer to 1 if you don't know what i mean) and negate the translation vector. >multiply the current camera frame by the transpose of orientation matrix, and add the negated version of translation vector to the current translation vector (this second step is very confusing, so read it again, if you really can't figure out what i am saying, post and i'll attempt to explain further) Doing this right will generate a 4x4 identity matrix, representing untranslated, default-orientated cursor in the absolute space this is the MODEL matrix that everyone is talking about when he says you can easily get the position and orientation of all the objects you draw in opengl. I certainly don't think it's easy and clear at all. Don't even attempt to reverse the effect of gluLookAt(), just use glTranslate() and glRotate() to set up your scene. (by that i mean replace gluLookAt() with the other two function). Do it this way, later you will know this is wiser. At this point, you should walk baby steps and build test function to watch the content of MODELVIEW matrix. If you need help with the math, opengl.org doesn't tell you at all, nobody on the internet bothers to go into the detail, professor is too busy. This seems like a joke, but that's the reality. So really, the only best source is to work it out yourself. It's all matrix algebra. I couldn't find a good tutorial about interpreting modelview matrix. I hope this could be useful for whoever is doing a robotic simulation project in opengl. If you need further information, you can post here, may take a while for me to answer though so be patient and don't depend your project on it. ting

  6. #6
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    Quote Originally Posted by ting View Post
    I need the orientation and position of current current frame.

    for example if i translate 5 unit up, i want to somehow read back from GL_MODELVIEW matrix that current origin is at 0,0,5, and orientation is like a 3x3 identity since i didn't change it.
    Except for academic reasons, I do not see a compelling need to ever have to do this. Since YOU are the one drawing the shapes on the screen, YOU should keep track of their positions and orientations.

    Moreover, there is no reason to assume that the inverse transformation will give you you unique results. Example, you have a short line of length 3 located at the origin of a coordinate system, pointing up. This means that the local origin (0,0) of the line is at the global origin (0,0). The asterisks in the sketch below represent the line:

    Code:
    y
    ^
    |
    *
    *
    *---> x
    We now translate the block 1 unit upward. Which will result in the local origin (0,0) to be at the global location (0,1):

    Code:
    y
    ^
    *
    *
    *
    +---> x
    Now consider the line at the global origin again, i.e. the first sketch. If we were to rotate the line 180 degrees (pi radians), we would get the following:

    Code:
    y
    ^
    |
    |
    |
    *---> x
    *
    *
    If we now translate the line 4 units upward the result is:

    Code:
    y
    ^
    *
    *
    *
    +---> x
    Which is exactly the same. Now what happened? Did the block move up just 1 unit? Did you rotate it 180 degrees and then moved it up 4 units? You can't tell, because the inverse transformation is not unique, so any result you might get from any inverse transformation is just a one of many possibilities.

  7. #7
    Registered User
    Join Date
    Mar 2008
    Location
    vancouver bc
    Posts
    28
    Quote Originally Posted by MWAAAHAAA View Post
    Except for academic reasons, I do not see a compelling need to ever have to do this. Since YOU are the one drawing the shapes on the screen, YOU should keep track of their positions and orientations.
    My project is indeed an academic project, designed to help student learn matrix algebra involved in a robotic system. I considered your design before; it could work, but there are some other reason which make this design less appealling.

    Which is exactly the same. Now what happened? Did the block move up just 1 unit? Did you rotate it 180 degrees and then moved it up 4 units? You can't tell, because the inverse transformation is not unique, so any result you might get from any inverse transformation is just a one of many possibilities.
    Thanks for the reminder; i am well awared of that. I think it doesn't matter in my case as long as i can do the reversal consistently, and the reversal formula yields the same output. As long as the output is unique and repeatable, I am ok.

  8. #8

    Join Date
    May 2005
    Posts
    1,041
    I guess the only answer is what was written above: you need to do some matrix math to extract the position and orientation of an object.

    If you are using OpenGL, the model view matrix is going to end up being comprised of the projection matrix and the transformation matrix.

    The problem you are trying to solve would be super duper trivial if you just had the transformation matrix. To boil this down to the transformation matrix I believe you can multiply the current modelview matrix by the projection matrix inverse. I believe GLSL stores the proj_inverse and modelview_inverse, but otherwise you have to calculate this inverse by itself.

    Once you do that, you then have to deal with the problem of the camera. If you've got a camera that gives the appearance of moving around you are going to have to get rid of that from the modelview matrix. To make the illusion of a camera moving around you must translate and rotate by the inverse of the camera's position and orientation (because technically the camera doesn't move, the objects being drawn do). This step would be a lot easier if you just have the camera at the global origin of 0,0,0 looking down the negative Z axis.

    If you can get this far then the translation will be in the fourth column, and the first three columns comprise the object's local axes of rotation (the rotation matrix, the object's basis, whatever you want to call it).
    I'm not immature, I'm refined in the opposite direction.

  9. #9
    Registered User
    Join Date
    Mar 2008
    Location
    vancouver bc
    Posts
    28
    Quote Originally Posted by BobMcGee123 View Post
    Once you do that, you then have to deal with the problem of the camera. If you've got a camera that gives the appearance of moving around you are going to have to get rid of that from the modelview matrix. To make the illusion of a camera moving around you must translate and rotate by the inverse of the camera's position and orientation (because technically the camera doesn't move, the objects being drawn do). This step would be a lot easier if you just have the camera at the global origin of 0,0,0 looking down the negative Z axis.
    You nailed it. I wish I had heard this three months ago then I'd be done by now.

    By the way i just want to add for others who read this article:

    >Inverse the orientation matrix means taking the top-left 3x3 matrix of the initial MODELVIEW matrix and transpose it.
    >Inverse the translation vector means taking the 4th col 3-vector of the initial MODELVIEW matrix and negate it.

    I don't know how to do them all in one matrix, so i break them up. There should be a way to combine these two into one matrix.

    ting
    Last edited by ting; 07-07-2008 at 08:20 AM.

  10. #10

    Join Date
    May 2005
    Posts
    1,041
    Yeah I'd do the same, should work fine. Let us know what happens.
    I'm not immature, I'm refined in the opposite direction.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OpenGL shooting game
    By sl4nted in forum Game Programming
    Replies: 6
    Last Post: 12-01-2006, 09:37 PM
  2. orientation from velocity
    By ichijoji in forum Game Programming
    Replies: 2
    Last Post: 08-28-2005, 07:22 PM
  3. Can't figure out cameras in OGL...
    By psychopath in forum Game Programming
    Replies: 14
    Last Post: 07-26-2004, 11:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21