Please help me to resolve the following issue:
I want to ‘extract’ the object’s coordinates and pass it to the camera.
The object lies in a different co-ordinate system.
—//translate the co-ord system
—glTranslatef( a, b, c);
---------glTranslatef(obj.x, obj.y, obj.z);
Earlier, camera was focusing on object’s x,y,z. But now, after ’ glTranslatef( a, b, c) ’ ; things have gone wrong.
So how do I get the object’s xyz with respect to GLOBAL COORDINATE system and pass it to the camera.
First of all, you have to define what those “absolute coordinates” are. Because it’s something that is entirely up to your application, OpenGL doesn’t know about it. In this case it would be the coordinate system before the first glPush.
Then you have to accumulate all transformations between this defined coordinate system and the objects coordinate system. Generally you can do so by describing the transformations as 4x4 matrizes and multiplying them. In you special case (because you only have translations) it is enough to sum the translation vectors.
You can then take any object-space vertex and transform it with the accumulated transformation to get the corresponding “absolute-space” vertex. Typically you use the vertex (0, 0, 0, 1) here.
Again, for your special case, you have an accumulated translation of (a + obj.x, b + obj.y, c + obj.z, 0). Translating the coordinate origin gives you (a + obj.x, b + obj.y, c + obj.z, 1), or simply (a + obj.x, b + obj.y, c + obj.z).
If your absolute coordinates happen to be the same as OpenGL’s view coordinate system, you can extract the accumulated transformation in matrix form from OpenGL, using glGetFloatv(GL_MODELVIEW_MATRIX, buffer). You get the tranlation part (the result you get when transforming the vertex (0, 0, 0, 1)) simply by taking the fourth column of that matrix (elements 12, 13, 14 and 15).