coordinate values after rotation in opengl

1.8k views Asked by At

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.

2

There are 2 answers

0
derhass On BEST ANSWER

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.

0
bvs On

Here you are rotating the current matrix 25 degrees in the z axis. This is the the code for glm::rotate which does the same.

template <typename T, precision P>
GLM_FUNC_QUALIFIER detail::tmat4x4<T, P> rotate
(
    detail::tmat4x4<T, P> const & m,
    T const & angle,
    detail::tvec3<T, P> const & v
)
{
    T c = cos(a);
    T s = sin(a);

    detail::tvec3<T, P> axis(normalize(v));
    detail::tvec3<T, P> temp((T(1) - c) * axis);

    detail::tmat4x4<T, P> Rotate(detail::tmat4x4<T, P>::_null);
    Rotate[0][0] = c + temp[0] * axis[0];
    Rotate[0][1] = 0 + temp[0] * axis[1] + s * axis[2];
    Rotate[0][2] = 0 + temp[0] * axis[2] - s * axis[1];

    Rotate[1][0] = 0 + temp[1] * axis[0] - s * axis[2];
    Rotate[1][1] = c + temp[1] * axis[1];
    Rotate[1][2] = 0 + temp[1] * axis[2] + s * axis[0];

    Rotate[2][0] = 0 + temp[2] * axis[0] + s * axis[1];
    Rotate[2][1] = 0 + temp[2] * axis[1] - s * axis[0];
    Rotate[2][2] = c + temp[2] * axis[2];

    detail::tmat4x4<T, P> Result(detail::tmat4x4<T, P>::_null);
    Result[0] = m[0] * Rotate[0][0] + m[1] * Rotate[0][1] + m[2] * Rotate[0][2];
    Result[1] = m[0] * Rotate[1][0] + m[1] * Rotate[1][1] + m[2] * Rotate[1][2];
    Result[2] = m[0] * Rotate[2][0] + m[1] * Rotate[2][1] + m[2] * Rotate[2][2];
    Result[3] = m[3];
    return Result;
}