Search code examples
c++openglgraphicsglrotate

coordinate values after rotation in opengl


Say I have a cube. Say the coordinate values are like this. (1 unit an arm)

GLfloat vertA[3] = { 0.5, 0.5, 0.5};
GLfloat vertB[3] = {-0.5, 0.5, 0.5};
GLfloat vertC[3] = {-0.5,-0.5, 0.5};
GLfloat vertD[3] = { 0.5,-0.5, 0.5};
GLfloat vertE[3] = { 0.5, 0.5,-0.5};
GLfloat vertF[3] = {-0.5, 0.5,-0.5};
GLfloat vertG[3] = {-0.5,-0.5,-0.5};
GLfloat vertH[3] = { 0.5,-0.5,-0.5};

If I translate it like

glTranslatef(1,2,3);

then 1,2 and 3 will be added to x,y and z coordinates respectively. and those are the new coordinate values of the translated cube. But if I rotate it some degree (with or without a translation)

glRotatef(25,0,0,1);

what is the coordinates of the rotated cube now? I am working new in opengl. I am using c++ on windows.


Solution

  • You should make yourself familiar with linear algebra and transformation matrices.

    What glRotate will do is generating a rotation matrix and post-multiplying it to the current matrix. You should be aware of some things here: the glTranslate will not directly add anything to the vertex coordinates, and the glRotate will also not change the coordinates. All what these do is changing a single matrix. This matrix will accumulate the composition of all the transformations, and will be applied once to all the vertices during the draw call.

    In your case, a rotation of 25 degrees around the z axis is desired, so the z coordinates will not be changed. The rotation matrix will look like this

    |  cos(25°)   -sin(25°)    0        0   |
    |  sin(25°)    cos(25°)    0        0   |
    |     0           0        1        0   |
    |     0           0        0        1   |
    

    To apply this matrix to a vector (x,y,z,w)^T, we just multiply the matrix by the vector. Following the rules of that multiplcation, we get a new vector with

    x' = cos(25°)*x -sin(25°)*y y' = sin(25°)*x +cos(25°)*y z' = z w' = w

    This is just the rotation alone, not considering the translation. But you can put int the values of zour vertex and will get the transformed result back.