gcc -o $1 $1.c\ -I/usr/local/pub/glut-3.6/include -lglut -lGLU -lGL -lXmu -lXi -lXext -lX11 -lm
chmod u+x gccglut
alias gccglut "~/bin/gccglut"
source .cshrc
/*
** 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 squareNote: 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.#include <GL/glut.h> #include <stdio.h>
glutKeyboardFunc(keyboard);
void keyboard(unsigned char key,int xMouse,int yMouse){
switch(key){
case 27:
exit(0);
default:
printf("%s%c\n","key=",key);
}
}