I draw a sphere with OpenGL which will be transformed by glRotate(), glScale(), glTranslate(). How could I get th开发者_运维百科e absolute Position of the Object without calculating all transformations on my own?
I need the coordinates for setting the camera eye position to this point.
When using those transformations, you are manipulating the modelviewmatrix
To get the current one you can use ( http://linux.die.net/man/3/glgetfloatv )
void glGetFloatv(
GLenum pname,
GLfloat * params
);
for example:
float modelview[16];
// save the current modelview matrix
glPushMatrix();
// get the current modelview matrix
glGetFloatv(GL_MODELVIEW_MATRIX , modelview);
should get you the current matrix, multiplying the starting vectors/vertices/coordinates with this matrix will give you the absolute world coordinates.
EDIT: When making an application you usually want control over the absolute coordinates of each object, manipulate them, and use the opengl transformations to draw them where they are defined.
Objects are often modeled in one coordinate system, then scaled, translated, and rotated into the world you're constructing. World Coordinates result from transforming Object Coordinates by the modelling transforms stored in the ModelView matrix. However, OpenGL has no concept of World Coordinates. World Coordinates are purely an application construct.
Object Coordinates are transformed by the ModelView matrix to produce Eye Coordinates.
From opengl.org: 9.120 How do I find the coordinates of a vertex transformed only by the ModelView matrix?
It's often useful to obtain the eye coordinate space value of a vertex (i.e., the object space vertex transformed by the ModelView matrix). You can obtain this by retrieving the current ModelView matrix and performing simple vector / matrix multiplication.
To get the matrix use something like this
float fvViewMatrix[ 16 ];
glGetFloatv( GL_MODELVIEW_MATRIX, fvViewMatrix );
You can read more about opengl transformations here: http://www.opengl.org/resources/faq/technical/transformations.htm
精彩评论