Lesson 4 of 6. I hope you are enjoying them laugh.gif .

QUOTE
Hello, in this tutorial we will be creating a 3D pyramid. We are building this tutorial from Lesson 3, but I took out the 2D objects and placed a 3D pyramid in there instead. The 3rd axis for drawing can be a litle confusing, but after you get the hand of it you'll do fine. Now when you are setting a 3D vertex just remember that the camera is on the positive end of the z axis. So things that have a more positive z axis value are closer than verteces with a more negative value. Well let's get started


We always start by including the glut library
CODE
#include<glut.h>


the time variable will be used to keep track of how much time has passed.
CODE
float time = 0;// set time variable to 0


Our first new function is glOrtho();, this function is much like gluOrtho2D(), but glOrtho() defines how far along the z axis you can view objects as well, this is very important when drawing 3D objects.

Next we want to enable depth testing to let our program test which polygons are where and draw them in the correct order for us, to do this we use glEnable() with the argument GL_DEPTH_TEST.
CODE
void init()
{
    glClearColor(0,0,0,0);
    glOrtho(-5,5,-5,5,-5,5);//(NEW) set up our viewing area

    glEnable(GL_DEPTH_TEST);//(NEW) Enable depth testing
}


In our display function we will create a 3D pyramid. To set the verteces we will use glVertex3f() instead of glVertex2f() so we can define where the point is on the z axis to add depth.

in our glClear() function we add a new argument that we seperate with a "|". The new argument is GL_DEPTH_BUFFER_BIT to enable the depth buffer so our program can draw 3D objects correctly.
CODE
void display()
{
    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);//(NEW) setup our buffers

    glPushMatrix();
    glRotatef(time,0.8,5,0);//rotate our objects

    glBegin(GL_TRIANGLES);
    //This is going to be the face facing the camera
    //We want this face to be red
    glColor3f(1,0,0);
    glVertex3f(-1,-1,1);//(NEW) We are now defining the vertex position on all three axis
    glVertex3f(0,1,0);//the top vertex
    glVertex3f(1,-1,1);//last vertex of this face

    //We do not need to enter glEnd() here because we are still drawing triangles

    //The back left face
    //This face will be green
    glColor3f(0,1,0);
    glVertex3f(-1,-1,1);//Vertex closest to us
    glVertex3f(0,1,0);//The top vertex
    glVertex3f(0,-1,-1);//The vertex farthest away

    //The back right face
    //This face will be blue
    glColor3f(0,0,1);
    glVertex3f(0,-1,-1);//The farthest vertex
    glVertex3f(0,1,0);//The heighest vertex
    glVertex3f(1,-1,1);//The closest vertex

    //The bottom face
    //This is face will be white
    glColor3f(1,1,1);
    glVertex3f(-1,-1,1);//Each corner of the base of the pyramid
    glVertex3f(0,-1,-1);
    glVertex3f(1,-1,1);

    glEnd();//done drawing our triangle

    glPopMatrix();

    glFlush();
    glutPostRedisplay();//This function is crucial in animation, it refreshes the screen
}


next we create our idle function which will update our time variable
CODE
void idle()
{
    time += 0.1;// increase our time variable

    if(time > 360)
        time = 0;// reset time variable
}


next is our main function. The only difference is in the gluInitDisplayMode() function, we added GLUT_DEPTH as an argument to tell the program to use the depth buffer.
CODE
void main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitWindowSize(800,600);
    glutInitWindowPosition(10,50);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB|GLUT_DEPTH);//tell the program we are running the depth buffer
    glutCreateWindow("Lesson 4");
    init();
    glutDisplayFunc(display);
    glutIdleFunc(idle);// This function calls our idle function to update our variables
    glutMainLoop();
}


depth testing is very important when creating 3D objects, you must enable, tell the program to clear it, and you must tell it that you are using the depth buffer. If you don't do all of these than your program will draw everything in the wrong order and it will look very confusing.

I hope you enjoyed yet another GLUT tutorial. The next tutorial will include texturing and keyboard interaction.

Edited on December 28 2006 to make more readable.

 

 

 


Reply