Step2 : Placing multiple toruses-icosahedron systems around a circle path

Ok, lets forget for a moment what we created in the previous section and think what should we do in order to place 10 points on a circle path on the x-y plane on the screen. We could either do some trigonometry and calculate where each point should be or use OpenGL's transformations to place the points on such a path.

If you noticed the transformations figure in the introduction of this tutorial, you should remember that when you rotate an object and then translate it, the translation you apply to it is done in respect not to the object but to the center of the axes. This means that if we translate a point radiusunits on the x-axis and then rotate it angle degrees on the z-axis we would get our object sitting on a circle path, angleunits from the original x-axis.

Thus to place multiple, in this case 10 objects, on a circle path, with each point equally displaced from its neighbors we can divide 360 degrees (a full circle) by 10 (objects) and thus place the objects 36 degrees apart from each other.

The sequence of OpenGL transformations that would result in such a scene is (remember OpenGL 'thinks' in the opposite way):

• rotate 36 degrees on the z-axis

• Translate radius units on the x-axis

• draw the object

• Translate -radius units on the x-axis (to cancel the previous transformation

• rotate 36 degrees more on the z-axis

• draw the second object

• repeat

Note: do not repeat for ever like in the advertisement of the famous shampoo (apply shampoo, massage scalp, rinse, repeat). When you have 10 objects drawn you should stop the loop. This makes it perfect for a for loop.

The same result can happen also with the following sequence:

• Push the matrix

• rotate 36 degrees on the z-axis

• Translate radius units on the x-axis

• draw the object

• pop the matrix

• push the matrix

• rotate 36 degrees more on the z-axis

• draw the second object

• pop the matrix

• repeat

This code, while slightly slower (the difference can be nearly zero) than the previous one as it involves pushing and popping matrices, provides us with the extra ability of inserting more transformations and rotations without complicating thinks.

For example, we can add two extra lines of code in order to make the icosahedron-toruses system to rotate around its circle path:

• Push the matrix

• rotate 36 degrees on the z-axis

• Translate radius units on the x-axis

• rotate 2*radius degrees on the y axis

• Transform 2 units on the z-axis

• draw the object

• pop the matrix

• repeat


Загрузка...