Выбрать главу

• 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

Step3 :Creating complicated paths in simple ways

By now you should start thinking that transformations are not so difficult after all. At the moment our code looks like that:

glPushMatrix();

 //draw ten icosahedrons

 for (int i = 0 ; i < 10 ; i++) {

  glPushMatrix();

   glRotatef(36*i,0.0,0.0,1.0);

   glTranslatef(10.0,0.0,0.0);

   glRotatef(2*angle,0.0,1.0,0.0);

   glTranslatef(0.0,0.0,2.0);

   createIcoToruses(i);

  glPopMatrix();

 }

glPopMatrix();

At this point, we can simply add a glRotatecall just before the firstglPushMatrix, in order to rotate the whole system in 1, 2 or 3 dimensions:

glPushMatrix();

 glRotatef(-angle,0.0,0.0,1.0);

 glPushMatrix();

  …previous code…

 glPopMatrix();

glPopMatrix();

Resulting in the following:

Ops, what is this cube doing in the middle of our system? It is there to show how to add a new completely unrelated system of transformations in our scene. To do so we have to add some new code before the previously explained one.

This bit is easy, we just save the matrix, rotate it in all three axes, draw the cube-sphere object and restore the matrix before drawing our system of icosahedrons and toruses:

glPushMatrix();

 glRotatef(angle,1.0,1.0,1.0);

 glColor3f(1.0,0.5,0.0);

 glutSolidSphere(2.5,20,20);

 glColor3f(1.0,0.0,0.0);

 glutSolidCube(4.0);

glPopMatrix() ;

glPushMatrix();

 …the previously explained code that creates the icos. and toruses

glPopMatrix();

Are you impressed? You should not be,… yet. This system of objects looks very impressive but still you can see that the objects follow a certain pattern. If we add another rotation just before drawing the current scene, then things become quite chaotic:

glPushMatrix();

 glRotatef(angle,1.0,1.0,1.0);

 drawCubeSphere();

 drawIcoToruses();

glPopMatrix();

If now we 'forget' to clear the color buffer, but remember to clear the depth buffer we could have a commercially looking screen saver!

PS: Thanks to Chris Mathers for the clearing the depth buffer idea.