Something about programming

OpenGL "Hello World" - First Program with freeGLUT

Next tutorial: Rendering pipeline in OpenGL

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:

#include <GL/freeglut.h> int windowWidth = 640; int windowHeight = 480; void displayFunc(void); int main(int argc, char* argv[]) { glutInit(&argc, argv); glutInitWindowSize(windowWidth, windowHeight); int id = glutCreateWindow("OpenGL first program"); if(id < 1) { exit(1); } glutDisplayFunc(displayFunc); glutMainLoop(); exit(0); } void displayFunc(void) { glClear(GL_COLOR_BUFFER_BIT); glutSwapBuffers(); }

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.


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(displayFunc); // ... // ... // ... void displayFunc(void) { glClear(GL_COLOR_BUFFER_BIT); glutSwapBuffers(); }

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:

glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

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.


June 17, 2018, 9:42 p.m.
1 Guest
money today <a href="">loan online</a> loan online <a href=>personal bank loans</a>
June 21, 2018, 3:43 p.m.
5 Guest
indian payday loans <a href="">payday loans same day payout</a> loan online - online loans
June 23, 2018, 9:19 p.m.
8 Guest
online loans <a href="">online loans</a> bad credit loans uk <a href=>online loans</a>
June 24, 2018, 1:05 a.m.
10 Guest
loan online <a href="">online loans</a> personal loan rates today - installment loans online
June 24, 2018, 6:20 a.m.
12 Guest
loan online <a href="">online cash advances</a> payday loans <a href=>installment loans online</a>
June 24, 2018, 12:32 p.m.
14 Guest
lender <a href="">quick cash online</a> loan lenders <a href=>lender</a>
June 25, 2018, 12:19 a.m.
16 Guest
loans online no credit check <a href="">loans for fair credit</a> loan online <a href=>installment loans online</a>
June 25, 2018, 6:31 a.m.
18 Guest
cash loans online <a href="">online loan application</a> installment loans online <a href=>installment loans online</a>
June 25, 2018, 12:46 p.m.
20 Guest
installment loans online <a href="">houston payday loans</a> loan online <a href=>payday loan online</a>
June 26, 2018, 3:09 p.m.
22 Guest
lender <a href="">loan estimator</a> direct lender payday loans <a href=>lender</a>
June 18, 2018, 1:14 p.m.
2 Guest
loan money online <a href="">get a loan</a> lender <a href=>lender</a>
June 20, 2018, 6:42 p.m.
3 Guest
lender <a href="">lender</a> direct lender installment loans <a href=>lender</a>
June 21, 2018, 11:56 a.m.
4 Guest
lender <a href="">loans without a checking account</a> lender <a href=>lender</a>
June 21, 2018, 8:16 p.m.
6 Guest
quick cash online <a href="">money lender</a> lender <a href=>loan now</a>
June 23, 2018, 3:45 p.m.
7 Guest
loan online <a href="">personal loans low interest</a> no fee payday loan <a href=>cash loans online</a>
June 24, 2018, 12:15 a.m.
9 Guest
online loan <a href="">online loans</a> loan online <a href=>loan online</a>
June 24, 2018, 3:13 a.m.
11 Guest
faxless payday loans <a href="">faxless payday loans</a> cash loans online <a href=>payday loan online</a>
June 24, 2018, 9:45 a.m.
13 Guest
loans online no credit check <a href="">faxless payday loans</a> get a bank account <a href=>loan online</a>
June 24, 2018, 9:22 p.m.
15 Guest
debt consolidation loans <a href="">hard money lenders</a> installment loans online <a href=>payday loan online</a>
June 25, 2018, 3:19 a.m.
17 Guest
installment loan online <a href="">payday loan online</a> cash loans direct <a href=>installment loans online</a>
June 25, 2018, 9:31 a.m.
19 Guest
small business loans <a href="">low doc loan</a> loan online <a href=>loan online</a>
June 26, 2018, 3:35 a.m.
21 Guest
quick online loans <a href="">payday loans no teletrack check</a> direct lender installment loans <a href=>personal loans with low interest rates</a>