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
- Decide where you want to keep the command. I have a subdirectory called bin for this purpose.
- Use a text editor (such as nedit) to create the file in the appropriate subdirectory. Very Important: the indent on the second line is one tab stop.
- Decide what name you want for your command. It cannot have the same name as other Unix commands (I use gccglut).
- Save the file using the name you have chosen.
- Change your permission for this file. You need executable permission for a script file.
- Assuming you have named your file gccglut, type the following on the Unix command line.
chmod u+x gccglut
Create an alias so that you can access your new command (gccglut) from anywhere in your directory structure.
- Edit your .cshrc file by adding the following line or something similar.
alias gccglut "~/bin/gccglut"
To activate the alias without having to log out and in again, use the following command.
source .cshrc
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.
- Include the header file, stdio.h, that tells the compiler where to find the functions that print ASCII to the command line. Include it at the top of your program (under the include for glut.h). Some compilers will not complain if you forget this but others will and we want to write portable code.
#include <GL/glut.h>
#include <stdio.h>
Register your keyboard function (to be called keyboard) in the setUpGlut()function.
glutKeyboardFunc(keyboard);
Write your function. Position it above setUpGlut. Some C compilers may not complain if you put functions in the wrong order but others will (write portable code). Note that GLUT passes an unsigned char (key pressed) and two ints (x and y position of the mouse on the screen) to the function.
void keyboard(unsigned char key,int xMouse,int yMouse){
switch(key){
case 27:
exit(0);
default:
printf("%s%c\n","key=",key);
}
}
- Compile and run the program again. Does it perform as expected?
- What happens to the yellow square when you resize the GLUT window?
- Here is the 1996 version of the GLUT API specification. It is a pdf file (Acrobat reader).
- Consult the GLUT API for more details about the function used in this tutorial.
- Investigate other functions that you are interested in using.
Setting up OpenGL and GLUT on your PC at home
Fran Soddell
    last updated 26 July 2002
F.Soddell@bendigo.latrobe.edu.au