Reputation: 65
How can I learn the new coordinates of the object after some transformation?
ex :
a,x,y,z any float numbers
glTranslatef( x, y,z ) ;
glRotate( a,0.0f, 1.0f, 0.0f ) ;
sketchSomething() ;
I want to know the coordinates of the object after this transformation.
Upvotes: 1
Views: 909
Reputation: 16612
It kind of depends on what co-ordinate you're looking for.
If you want a 2D screen-coordinate, you can use gluProject() to apply the current matrices to a single point.
If you want to just apply the current modelview matrix to a point, you're probably better off just reading back that matrix and applying the transform yourself.
Note that if you simply want the position of your object's origin within the world space, you just need to read the translation portion of the matrix and not actually perform a full transform.
Upvotes: 0
Reputation: 162317
OpenGL does not "think" in objects. When you draw an object, OpenGL treats each primitive (point, line, triangle) of the geometry on its own, draws it and then forgets about it. Transformations merely influence where on the screens will show up.
But of course you can assume that the geometry forms an object in some model space, that's transformed into a world space, then eye space, then clip space and finally NDC space.
Regarding your question: glTranslate, glRotate and some other functions don't manipulate objects. They apply transformation matrix in-place multiplication on the matrix on top of the currently active stack. There may have been an infinite number of transformations being applied previously. So what you can do is retrieving the current matrix from OpenGL and do the transformation yourself. This gives you the object geometry in the transformed space. And of course you can just multiply a center position vector yielding the center position of the object.
Also, instead of relying on OpenGL's matrix routines, which are cumbersome to work with, I strongly suggest you make use of a dedicated matrix math library (GLM, Eigen, linmath.h), do all the transformation matrix operations using that one, and load the prepared matrices into OpenGL using glLoadMatrix or glUniformMatrix.
Upvotes: 3