INT32GP Graphics Programming: Tutorial #01
Using OpenGL and GLUT

B1.11 3 pm Monday 29 Jul 02
B1.11 12 midday Tuesday 30 Jul 02

Using gcc compiler with OpenGL and GLUT on the Silicon Graphics Machines
To use the gcc compiler with OpenGL and GLUT, we have to include a lot of libraries. So I set up a script with the following command in it. I then create an alias in my .cshrc file so that I can access the command from any subdirectory.
gcc -o $1 $1.c\
	-I/usr/local/pub/glut-3.6/include -lglut -lGLU -lGL -lXmu -lXi -lXext -lX11 -lm

Compile and run example code
Set up a subdirectory for your OpenGL tutorial example programs. Copy and paste the following code into a text editor and save it in your subdirectory as square.c.
/*
** File:          square2.c
** Description:   A simple C program using OpenGL
**                and GLUT to render a yellow square
**                with a blue border on a white background.
** Rev:           1.0
** Created:       26 August 2001
** Last Update:   22 July 2002
** Author:        Fran Soddell
** Email:         F.Soddell@bendigo.latrobe.edu.au
*/
#include <GL/glut.h>
/*
** colour management
*/
#define NUM_COLOURS 8
#define ALPHA 0.0
enum {black,
      red,green,blue,
      yellow,magenta,cyan,
      white
};
typedef GLfloat colourType[3];
colourType colour[NUM_COLOURS]={
   {0.0,0.0,0.0},
   {1.0,0.0,0.0},{0.0,1.0,0.0},{0.0,0.0,1.0},
   {1.0,1.0,0.0},{1.0,0.0,1.0},{0.0,1.0,1.0},
   {1.0,1.0,1.0}
};
/*
** model management
*/
enum {x,y};
typedef GLdouble vertexType [2];
int numVertices=4;
vertexType square[]={
   {0.5,0.5},{-0.5,0.5},{-0.5,-0.5},{0.5,-0.5}
};
/*
**  ***********************************************************
*/
void render(vertexType * image, int numVertices, int primitive){
   int i;
   glBegin(primitive);
      for(i=0;i<numVertices;i++)
         glVertex2d(image[i][x],image[i][y]);
   glEnd();
}
void renderView(){
   glColor3fv(colour[yellow]);
   render(square,numVertices,GL_POLYGON);
   glColor3fv(colour[blue]);
   render(square,numVertices,GL_LINE_LOOP);
}
void display(){
   /*
   ** Clear the window with the background colour before
   ** (re)rendering.
   */
   glClear(GL_COLOR_BUFFER_BIT);
   renderView();
   glFlush();
}
void setUpGLUT(int argc,char ** argv){
   glutInit(&argc,argv);
   glutCreateWindow("Square");
   /*
   ** Register callbacks.
   */
   glutDisplayFunc(display);
}
void initialiseGL(){
   /*
   ** Set the background colour
   ** (clear colour) to white.
   */
   glClearColor(1,1,1,ALPHA);
}
int main(int argc,char ** argv){
   setUpGLUT(argc,argv);
   initialiseGL();
   /*
   ** Display window and enter the GLUT event
   ** processing loop.
   */
   glutMainLoop();
   return 0;
}
Compile the program with the following command (or something similar).
gccglut square
Note: If the name of your program is square.c the gccglut command requires the argument square.

You may get a compiler warning that a library file is not used. Not all libraries are needed for every compilation so you may ignore this warning.

If the compilation has been successful you will now have an executable program file called square. If not, ask for help. When you run square, the output should be a centred yellow square with a blue border on a white background.

Becoming familiar with GLUT
We are going to register a callback to the GLUT function glutKeyboardFunc() and then use it so that we can close the program by pressing the Esc key on the keyboard. Pressing other keys results in their value being printed out to the command line.
void keyboard(unsigned char key,int xMouse,int yMouse){
   switch(key){
      case 27:
         exit(0);
      default:
         printf("%s%c\n","key=",key);
   }
}

Setting up OpenGL and GLUT on your PC at home
Fran Soddell      last updated 26 July 2002 F.Soddell@bendigo.latrobe.edu.au