## Friday, January 26, 2007

### NeHe Lesson 04: Rotation

The fourth NeHe lesson is about using the glRotate*() functions. Go read what he has to say about them here. The lesson builds on the previous one. I'll show the parts the have changed here.

First of we need to declare two need variables for the rotation.
`/// Angle For The TriangleGLfloat rtri = 0.0f;/// Angle For The QuadGLfloat rquad = 0.0f;`

Here you must be careful with how D initializes floating point variables. For example if we had declared them like this: (Like the C-version)
`/// Angle For The TriangleGLfloat rtri;/// Angle For The QuadGLfloat rquad;`

Then the variables would have been initialized to NaN and the triangle and the quad would not have shown on the screen at all. I've fallen for this many times, especially with these tutorials.

The drawGLScene function is the same except the calls to glRotatef and the glLoadIdentity in the middle. Try to leave it out and see what happens.

Having the variables rtri and rquad initialized to 0.0f wont do us much good if we don't change them, so there is at the end we change them slightly to archive the moving rotation effect.
`/*** The drawing function. Here we will draw the triangle and the quad.* Now with color and rotation.*/void drawGLScene(){  // Clear The Screen And The Depth Buffer  glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);  // Reset The Current Modelview Matrix  glLoadIdentity();  // Move Left 1.5 Units And Into The Screen 6.0  glTranslatef(-1.5f, 0.0f, -6.0f);  // Rotate The Triangle On The Y axis  glRotatef(rtri, 0.0f, 1.0f, 0.0f);  // Drawing Using Triangles  glBegin(GL_TRIANGLES);      // Set The Color To Red      glColor3f(1.0f, 0.0f, 0.0f);      // Top      glVertex3f(0.0f, 1.0f, 0.0f);      // Set The Color To Green      glColor3f(0.0f, 1.0f, 0.0f);      // Bottom Left      glVertex3f(-1.0f, -1.0f, 0.0f);      // Set The Color To Blue      glColor3f(0.0f, 0.0f, 1.0f);      // Bottom Right      glVertex3f(1.0f, -1.0f, 0.0f);  // Finished Drawing The Triangle  glEnd();  // Reset The Current Modelview Matrix  glLoadIdentity();  // Move Right 1.5 Units And Into The Screen 6.0  glTranslatef(1.5f, 0.0f, -6.0f);  // Rotate The Quad On The X axis  glRotatef(rquad, 1.0f, 0.0f, 0.0f);  // Set The Color To Blue One Time Only  glColor3f(0.5f, 0.5f, 1.0f);  // Draw A Quad  glBegin(GL_QUADS);      // Top Left      glVertex3f(-1.0f, 1.0f, 0.0f);      // Top Right      glVertex3f(1.0f, 1.0f, 0.0f);      // Bottom Right      glVertex3f(1.0f, -1.0f, 0.0f);      // Bottom Left      glVertex3f(-1.0f, -1.0f, 0.0f);  // Done Drawing The Quad  glEnd();  // Increase The Rotation Variable For The Triangle  rtri += 0.2f;  // Decrease The Rotation Variable For The Quad  rquad -= 0.15f;}`