Programming in glut lesson 1

t3jem Apr 26, 2008 Computers
My first tutorial on how to program using the opengl utility library AKA glut.
Hello, I'm starting a series on how to program in OpenGL using the OpenGL Utility Toolkit, a.k.a. GLUT. I chose GLUT because it is quick and easy to write, and very easy to learn. In this tutorial I am going to teach you how to create a basic window which we will build off of in later tutorials. Throughout the tutorial I will leave notes to let you know what each command does, and how you can modify it to fit your needs. Everything in the code section can be copied and pasted into your compiler and it should compile proporly, if it does not, please let me know, and I may be able to help you.

Note: It is recommended that you have at least some basic knowledge of C++ before learning OpenGL; though, it is not required, it will help with understanding the tutorials.

Note: I have tested this code myself in Microsoft Visual C++ 6.0; however, it is possible that it may not work on other compilers. If you do find a mistake or you have questions I would be happy to try and answer them.


First we will have to include our header file that contains all of our OpenGL commands

#include//This header file contains all the commands for the OpenGL Utility Toolkit


The init() function will be called after the window is created to initialize the settings for the OpenGL window.
Right now all we will be doing is set the background color to black.
The glClearColor() function defines what the background color will be, the values for each one range from 0 to 1 in Red, Green, Blue order. If you put (1,0,0,0) in it will be solid red, and (1,1,1,0) will be white.

//function to initialize GLUT settings
void init()
glClearColor(0,0,0,0);//(NEW) we define the background color here

The function is the main function in GLUT. We will use this function do draw all of our object; however, in this tutorial there are no objects to draw, but you still need to have some functions in there for the window to refresh.
The glClear() function clears the screen to the default background color (in this case black). Without it your window will be clear and it will not refresh.
The glFlush() function draws all of the objects we defined on to the screen. In this tutorial we have no objects so it doesn't do anything.
glutPostRedisplay() tells the program to start drawing the screen again. This is a very important command for animation, without it your objects will not move.

//The function where all the drawing occurs
void display()
glClear(GL_COLOR_BUFFER_BIT);//Clear the screen

glFlush();//Draw everything to the screen
glutPostRedisplay();//Tell the program to refresh

The main function is the backbone of your program. This is where it begins and where you define your window, initialize GLUT, and define what your vital function are.

glutInit() initializes glut and tells the program that we are using it
glutInitWindowSize() defines the width and height of the window in pixels
glutInitWindowPosition() defines where the window will be placed, in pixels, from the upper right corner of the screen
glutInitDisplayMode() initializes what modes we are using, in this program we are only using a single buffer and red,green, blue coloring.
glutCreateWindow() obviously creates a window. The one parameter for it is the name that will appear in the title bar.

After we create our window we call our init function to set some GLUT variables (in this case the background color), then we tell GLUT where to find our display function
glutDisplayFunc() tells glut which function does all the drawing
glutMainLoop() tells GLUT that we are not done with our program and to keep going through the program

void main(int argc, char ** argv)
glutInit(&argc, argv);//initialize GLUT.
glutInitWindowSize(800,600);//define the window size as 800 pixels wide and 600 pixels high glutInitWindowPosition(10,50);//Set the window position at (10,50)
glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);//Set the initial display mode
glutCreateWindow("Lesson 1");//Create our window
init();//call init()
glutDisplayFunc(display);//tell GLUT what our display function is
glutMainLoop();//Tell the program we are not done yet

I hope you learned something from the tutorial. Go ahead and mess around with settings and figure out how to work them. I will be adding new tutorials soon, but right now im just too tired, possibly tomarrow though.

What did you think of this tutorial?
0 CommentsAdd a Comment
  • Views : 1048
  • Comments : 0
  • Rating : None
  • Last Updated : Apr 26, 2008