Tuesday, January 30, 2007

NeHe Lesson 05: 3D Shapes

The last lesson of the very basics is here. This lesson doesn't bring anything new to the table, but shows you how to combine triangles and quads into pyramids and cubes.
Go read the lesson here first.

I'll put the changed drawing code here for your enjoyment.

/**
* The drawing function. Here we will draw the pyramid and cube.
*/
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);

// Start Drawing The Pyramid
glBegin(GL_TRIANGLES);
glColor3f(1.0f,0.0f,0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Front)
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f); // Left Of Triangle (Front)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f); // Right Of Triangle (Front)

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Right)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(1.0f, -1.0f, 1.0f); // Left Of Triangle (Right)
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f); // Right Of Triangle (Right)

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Back)
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(1.0f, -1.0f, -1.0f); // Left Of Triangle (Back)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f); // Right Of Triangle (Back)

glColor3f(1.0f, 0.0f, 0.0f); // Red
glVertex3f(0.0f, 1.0f, 0.0f); // Top Of Triangle (Left)
glColor3f(0.0f, 0.0f, 1.0f); // Blue
glVertex3f(-1.0f, -1.0f, -1.0f); // Left Of Triangle (Left)
glColor3f(0.0f, 1.0f, 0.0f); // Green
glVertex3f(-1.0f, -1.0f, 1.0f); // Right Of Triangle (Left)
// Done Drawing The Pyramid
glEnd();

// Reset The Current Modelview Matrix
glLoadIdentity();

// Move Right And Into The Screen
glTranslatef(1.5f, 0.0f, -7.0f);

// Rotate The Cube On X, Y & Z
glRotatef(rquad, 1.0f, 1.0f, 1.0f);

// Set The Color To Blue One Time Only
glColor3f(0.5f, 0.5f, 1.0f);

// Start Drawing The Cube
glBegin(GL_QUADS);
glColor3f(0.0f, 1.0f, 0.0f); // Set The Color To Green
glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Top)
glVertex3f(-1.0f, 1.0f, 1.0f); // Bottom Left Of The Quad (Top)
glVertex3f(1.0f, 1.0f, 1.0f); // Bottom Right Of The Quad (Top)

glColor3f(1.0f, 0.5f, 0.0f); // Set The Color To Orange
glVertex3f(1.0f, -1.0f, 1.0f); // Top Right Of The Quad (Bottom)
glVertex3f(-1.0f, -1.0f, 1.0f); // Top Left Of The Quad (Bottom)
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Bottom)
glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Bottom)

glColor3f(1.0f, 0.0f, 0.0f); // Set The Color To Red
glVertex3f(1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Front)
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Front)
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Front)
glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Front)

glColor3f(1.0f, 1.0f, 0.0f); // Set The Color To Yellow
glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Back)
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Back)
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Back)
glVertex3f(1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Back)

glColor3f(0.0f, 0.0f, 1.0f); // Set The Color To Blue
glVertex3f(-1.0f, 1.0f, 1.0f); // Top Right Of The Quad (Left)
glVertex3f(-1.0f, 1.0f, -1.0f); // Top Left Of The Quad (Left)
glVertex3f(-1.0f, -1.0f, -1.0f); // Bottom Left Of The Quad (Left)
glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Right Of The Quad (Left)

glColor3f(1.0f, 0.0f, 1.0f); // Set The Color To Violet
glVertex3f(1.0f, 1.0f, -1.0f); // Top Right Of The Quad (Right)
glVertex3f(1.0f, 1.0f, 1.0f); // Top Left Of The Quad (Right)
glVertex3f(1.0f, -1.0f, 1.0f); // Bottom Left Of The Quad (Right)
glVertex3f(1.0f, -1.0f, -1.0f); // Bottom Right Of The Quad (Right)
// Done Drawing The Cube
glEnd();

// Increase The Rotation Variable For The Triangle
rtri += 0.2f;
// Decrease The Rotation Variable For The Quad
rquad -= 0.15f;
}


You can download the code from here.