Introduction to Glut

My slides for the first tutorial

Our example program

(:source lang=C++ :)

  1. include <GL/glut.h>
  2. include <stdio.h>
  3. include <stdlib.h>

void display(void) {

  glClearColor(1.0,0.0,0.0,0.0);
  glClear(GL_COLOR_BUFFER_BIT);
  glutSolidSphere(0.5,30,30);
  glFlush();

}

void keyboard(unsigned char key, int x, int y) {

  if(key == 27) exit(0);
  else printf("You pressed key %d \n",key);

}

int main(int argc, char** argv) {

  glutInit(&argc, argv);
  glutInitWindowSize(500,500);
  glutInitWindowPosition(100,100);
  glutCreateWindow("Empty Window");
  glutDisplayFunc(display);
  glutKeyboardFunc(keyboard);
  glutMainLoop();

  return 0;

}

(:sourceend:) Download the source code

The example Makefile

There are a couple of ways to compile c++ programs in the 239 lab. A makefile is one of them.

For example: save this code in a file called "Makefile"

(:source lang=C++ :)

CC = g++

LIBDIRS = -L /usr/lib -L /usr/X11R6/lib LIBS = -lglut -lGLU -lGL -lXmu -lXext -lX11 -lXi -lm

  1. LIBS = -lGLUT32 -lGLU32 -lOpenGl32

INCLUDE =

EXE=simpleGlut

simpleGlut: simpleGlut.cpp

	$(CC) -o $(EXE) $? $(INCLUDE) $(LIBDIRS) $(LIBS)

(:sourceend:)

Important! Make sure you have a TAB before the last line in the makefile!

Then call "make" which will run the g++ compiler and compile the simpleGlut program. If you want to compile the code under windows using cygwin then uncomment the second LIBS line and comment out the first one.

If you see no error messages, call "./simpleGlut" and you should see your first wonderful openGL window! Wohoo!