OpenGL "Hello World" - First Program with freeGLUT
In this tutorial we'll write our first program in C language that uses OpenGL and freeGLUT library. It will be cross-platform, so you can compile it for different operating systems. And it will be as simple as possible. Let's look at the code first:
The Open-Source OpenGL Utility Toolkit (freeGLUT)
In fist line we include the file freeglut.h. This file is from the Open-Source OpenGL Utility Toolkit (or freeGLUT). We need this toolkit to make our programs portable. Different operating systems have different ways (and code) of creating windows. FreeGLUT deals with windows system of operating system by itself. So you only need to worry about OpenGL code. Also you need to know that before freeGLUT there was proprietary library that have similar purpose: GLUT - now it's not used anymore (freeGLUT is the replacement of GLUT).
We use several functions from freeGLUT: glutInit, glutInitWindowSize, glutCreateWindow, glutDisplayFunc, glutMainLoop.
FreeGLUT or GLFW
There is another library for cross-platform initialization of OpenGL - GLFW. We'll get look at GLFW in another tutorial. Both, freeGLUT and GLFW are used for same purpose.
Initialization of OpenGL with freeGLUT
Main function starts with the initialization of OpenGL by calling glutInit. We just pass arguments from command line to it. FreeGLUT library will do all the work.
Then we set window size. In the example I used window with dimensions 640x480.
Next step: create window with glutCreateWindow. We pass title to the function. It returns identifier of the window. OpenGL sets identifiers starting from one. On next line we check if id is less than one - that means that window was not created and we exit the program with status 1 (something wrong happened).
OpenGL - freeGLUT display function
glutDisplayFunc sets the callback function that will be called when it's required to display image in window. We'll discuss it later. Now you just need to know that at some situations OpenGL will call the function that you pass to glutDisplayFunc. In our case it's displayFunc. It's definition have two calls: glClear and glutSwapBuffers.
glClear clears different buffers which we can set with the flags. We use flag GL_COLOR_BUFFER_BIT - this flag tells OpenGL to clear color buffer. After the call of this function your window will be filled with the background color. Default it's black, but you can change it (see exercises).
glutSwapBuffer actually shows the content of the scene on the screen. Just think for now that this function draws "everything" in the window. We'll talk a lot about bufferization later.
OpenGL freeGLUT main loop
After we set display function there is a call of glutMainLoop. Main loop is just a while loop that iterates infinitely. Every iteration OpenGL redraws the scene. This loop breaks once you decide to close the program.
In this tutorial we created the cross-platform OpenGL program using freeGLUT library. In next tutorials we'll learn main concepts of 3d programming.
1. In example of this tutorial we used version of OpenGL provided by your operating system. You can check what version your program uses with the call: glGetString(GL_VERSION). Print the version using this function right before main loop is started.
2. When you run the program from this tutorial you'll see black window. You can set what color will be used to clear color buffer with the function glClearColor. This function accepts four arguments of type float. 4th - alpha channel, it sets transparency of color. Pass 1.0f. Other arguments - color channels: red, green blue:
Try to experiment with different colors. Each argument should be in the interval from 0.0f to 1.0f. You need to call glClearColor before main loop is started.