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?

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.

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.

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)

You can’t.

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

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.