資料來源:http://www.dev-gallery.com/programming/opengl/index.htm
Advanced Transformations (Multiple Translations and Rotations)
![](http://www.dev-gallery.com/programming/opengl/transformations/intro.gif)
OpenGL is a very powerful and simple graphics library but it still needs some familiarity in order to use it. One of the most problematic areas for new programmers is the transformations which include modeling, viewing and projection operations. The topic of this tutorial is modeling and viewing transformations. These are translate, rotate and scale. The most important thing to remember is that the order of transformations matters! Imagine that you have an object in the center of your world and you are thinking of rotating it 30 degrees and translating it by 10 units on the positive x-axis. As you can see in the following figure the order that you apply these two transformations not only matters but is critical, as translating and then rotating and rotating and then translating results in two extremely different results.
![](http://www.dev-gallery.com/programming/opengl/transformations/trans_rot_order.gif)
In this tutorial we will create a quite complicated scene consisting of 1 cube, 1 sphere, 10 icosahedrons and 20 toruses moving in the pattern shown in the introductory image of this page. The middle screenshot is quite informative as it shows the trajectory of our objects. It was created by repeatedly drawing on the buffer without clearing it. In the final application the user can interactively select whether the color and depth buffers should be cleared or not using the 'a' and 's' keys. OpenGL applies transformations in the reverse order. This means that if in our OpenGL code we first translate and then rotate an object, it will appear rotated on the x axis, which is exactly the opposite as shown in the figure. We should instead translate and then rotate the object, if we want the results appearing in the left hand corner of the previous picture. Because of this reason, it is better to construct our scene in a reverse order. So, instead of trying to place everything in the correct place the first time we will start from the end and move towards the start of the hierarchy. Note that a hierarchy exists as the toruses rotate around the rotating icosahedrons and the icosahedron-torus system rotates around the origin where the cube-sphere object in turn rotates around itself. When you are confident with OpenGL transformations you can create great looking effects in a very little time and effort. Imagine that the graphics shown in the figure at the bottom of the page were created by simply inserting an extra glRotate call in the proper place in the scene. When running the application you can use the 'd' key to switch the two different modes. Use the links at the bottom of this page to continue with the actual tutorial and or get the code and windows application.
![](http://www.dev-gallery.com/programming/opengl/transformations/intro2.jpg)
The Tutorial :http://www.dev-gallery.com/programming/opengl/transformations/trans2.htm
Application and Code downloads:http://www.dev-gallery.com/programming/main.htm