NeHe OpenGL Lesson04 - Rotation

screen_shot2-300x237 This program shows how to make the objects rotated.  OpenGL API is kind of so low level graphic API that allows the user create some separate matrix components.  Here this sample just give some basic idea how the rotation happened, but I guess in the real time rendering module they never use such kind of 3D api calls. Most of time, they will compute a final one world matrix  for one objects and submit it to GPU, they won’t let the 3D API to collect the separate information, such as the rotations, translates, scale, and let them pass from root to the children and repeat the process until the leaf scene node. Then ask the 3D API themselves to calculate the final matrix. Obviously, this method is very un efficient.

Most of time, we will keep a matrix variable for each scene node. Some one told me that such matrixes update loop also very un efficient. A much better way to improve that is you should build your matrix pool or matrix array. Every frame you will update the whole matrix array, because the data storage is continues, they will be less CPU stall happen here, that means we always process those data that we only need to be updated. Such optimization is kind of very advanced technique.

In 3D graphics, the matrix is a very important concept. The order is very important, that means you could not re-order them as you wish. Most of time, you should not translate first equal to the rotation first. And be careful which space you are currently working on.

glTranslatef(-1.5f, 0.0f, -6.0f);
glRotatef(mRotateTri, 0.0f, 1.0f, 0.0f);
glBegin(GL_TRIANGLES);
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(0.0f, 1.0f, 0.0f);

    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(-1.0f, -1.0f, 0.0f);

    glColor3f(0.0f, 0.0f, 1.0f);
    glVertex3f(1.0f, -1.0f, 0.0f);
glEnd();

 

The whole program coded under Ubuntu 10.4 OS. You need to install gcc, cmake, opengl to compile and run it.

 

The full source code could be found here.

posted @ 2012-08-23 22:58  opencoder  阅读(194)  评论(0编辑  收藏  举报