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.

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(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.

Conclusion

In this tutorial we created the cross-platform OpenGL program using freeGLUT library. In next tutorials we'll learn main concepts of 3d programming.

Excercises

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.

Comments:

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