itsraining
itsraining

Reputation: 203

OpenGL transformations

I have an object at a point, say x,y,z with respect to the origin.
I want to apply some transformations to the point, say rotation and translation, and render the object at the transformed point. I'm using the glTranslatef() and glRotatef() functions. It looks some what like this:

glTranslatef(x,y,z); 
glRotatef(Ang,0,1,0); // rotated around Y axis at an arbitrary angle
glTranslatef(xt,yt,zt); // further translation
<render the object>

Even though it saves me from the complicated matrix transformation operations, I'm unaware of the actual coordinate values of the final position with respect to the origin. Is there a method to retrieve the actual position coordinates of the transformed point without going through the matrix operations?

Upvotes: 3

Views: 4195

Answers (2)

Vyktor
Vyktor

Reputation: 20997

I'll give you some theory first. OpenGL works with matrices 4x4 and vectors (point coordinates) with length of 4 (in 3D).

Let's have a vector X = (x,y,z,1). I hope you know how to multiply matrixes. And let's say that you want to translate your point by V = (vx,vy,vz). To do that you'll need to build translation matrix like this:

Translation matrix

And you'll multiply two matrices (matrix * vector/point) : Y = TX:

Translation via matrix

The reason to do this so complicatedly (and not just add X + V) is that you can rotate your point around the axis just by filling the same matrix with different values. It's called a rotation matrix:

Rotation matrices

The best part is that you can chain those matrices. When you need to draw point with those rules:

  • Translate T1
  • Rotate R1
  • Translate T2

You can prepare one matrix which will do all the work at once: F = T1R1T2

And than apply all those transformations to the point at once: Y = FX.

Please note that matrix multiplication is not commutative so order does matter.

OpenGL works with those matrices (there are few more types like scale matrix) internally. You can get current transformation matrix with command (example from here):

glGetDoublev(GL_MODELVIEW_MATRIX, redTransformationMatrix);

And here's how to represent those data correctly (detailed info here):

enter image description here

All images except the last one are taken from wikipedia.org

Upvotes: 13

Alex P.
Alex P.

Reputation: 770

You can retrieve the matrices from the OpenGL state using glGetDoublev:

GLdouble projectionMatrix[16];
glGetDoublev(GL_PROJECTION, projectionMatrix);

GLdouble modelViewMatrix[16];
glGetDoublev(GL_MODELVIEW, modelViewMatrix);

Also note, that when you translate first and then rotate, you are rotating the translation vector, too. Normally, you rotate the object in the origin and then translate it, thus resulting in a rotated object at the translation end-point.

EDIT: To get the transformed coordinates p', you need to transform a point p using those matrices:

p' = projectionMatrix * modelViewMatrix * p

I suggest using glm for these kind of operations.

Upvotes: 3

Related Questions