#include<glut.h>//Include the GLUT functions
This time we are going to not only set the background color, but set the area that we are viewing as well.
gluOrtho2D(); sets left, right, bottom, and top borders of the viewing area, in this case we are viewing 5 units to the left, right, down, and up.
void init()
{
glClearColor(0,0,0,0);//Define our background color
gluOrtho2D(-5,5,-5,5);//(NEW) Define our viewing area
}
We are now going to draw our polygons using the function glBegin and glEnd. All the verteces defined between these two functions will be drawn using the rules given in the glBegin function. The rules that can be used can be found at the end of this tutorial.
glColor3f(); changes the colors of objects, any verteces following this function will be changed to the color defined. The colors are defined just as they are in glClearColor();
glVertex2f(); defines a 2D vertex as an (x, y) vertex. We will use this function to define our polygons.
In the next function we will create three polygons, a triangle, square, and pentagon. Each polygon will have it's own color.
void display()
{
glClear(GL_COLOR_BUFFER_BIT);//Clear the screen
glColor3f(1,0,0);//Change the object color to red
glBegin(GL_TRIANGLES);//Start drawing a triangle
glVertex2f(3,-4);//draw our first coordinate
glVertex2f(3.5,-3);//Our second coordinate
glVertex2f(4,-4);//Our last coordinate
glEnd();//Stop drawing triangles
glColor3f(0,1,0);//Change the object colors to green
glBegin(GL_QUADS);//Start drawing quads
glVertex2f(-4,-4);//first coordinate
glVertex2f(-4,-2);//second coordinate
glColor3f(0,0,1);//Change the color to blue halfway through to create a neat color effect
glVertex2f(-2,-2);//third coordinate (now blue)
glVertex2f(-2,-4);//last coordinate
glEnd();//Stop drawing quads
glColor3f(1,0,0);//Change color to red
glBegin(GL_POLYGON);//Start drawing a polygon
glVertex2f(-2,2);//first vertex
glColor3f(0,1,0);//Change color to green
glVertex2f(-1,3);//second vertex
glColor3f(0,0,1);//Change color to blue
glVertex2f(0,2);//third vertex
glColor3f(1,0,1);//Change color to purple
glVertex2f(-0.5,0);//fourth vertex
glColor3f(1,1,0);//Change color to yellow
glVertex2f(-1.5,0);//last vertex
glEnd();//Stop drawing our polygon
glFlush();//Draw everything to the screen
glutPostRedisplay();//Start drawing again
}
Now we finish the code off with our main function again.
void main(int argc, char ** argv)
{
glutInit(&argc, argv);//Initialize GLUT
glutInitWindowSize(800,600);//define the window size
glutInitWindowPosition(10,50);//Position the window
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//Define the drawing mode
glutCreateWindow("Lesson 2");//Create our window
init();//initialize our variables
glutDisplayFunc(display);//tell Glut what our display function is
glutMainLoop();//Keep the program running
}
As you can see there are many different modes for the function "glBegin()". The modes that can be used and what they are used for are listed below.
GL_POINTS This is used to just draw points, instead of polygons or lines. You only need to define one vertex to see a point
GL_LINES -- This mode lets you draw lines. You need to define 2 verteces at a time.
GL_LINE_LOOP -- This mode creates a loop of lines. You define the verteces much like GL_POLYGON and when you finish it will link the last and first two verteces together with a line.
GL_LINE_STRIP -- This mode does the same as GL_LINE_LOOP except it does not connect the first and second lines
GL_TRIANGLES -- This mode creates triangles and takes 3 verteces at a time
GL_TRIANGLE_STRIP -- This mode creates a strip of triangles. The second triangle uses 2 verteces of the first, and you just define the last vertex of the next triangle
GL_TRIANGLE_FAN -- This function creates a fan of triangles. All the triangles use one same point and 1 point from the triangle before them, you define the last vertex
GL_QUADS -- This creates quadrilaterals and take 4 verteces at a time
GL_QUAD_STRIP -- This mode creates a strip of quadrilaterals. After the first quad, it uses 2 verteces from the preveious quad then you define the other 2
GL_POLYGON -- This mode lets you create a polygon will n verteces
I hope you enjoyed the tutorial and found it helpful. Many more are on their way. Again go ahead and fiddle with the settings and modes, you can learn alot more from playing with the code then just reading it.
Editted on December 18, 2006 to make it more readable


