Chapter 1. Hello, Triangle!

Table of Contents

Framework and FreeGLUT
Dissecting Display
Following the Data
Making Shaders
In Review

It is traditional for tutorials and introductory books on programming languages to start with a program called Hello, World! This program is the simplest code necessary to print the text Hello, World! It serves as a good test to see that one's build system is functioning and that one can compile and execute code.

Using OpenGL to write actual text is rather involved. In lieu of text, our first tutorial will be drawing a single triangle to the screen.

Framework and FreeGLUT

The source to this tutorial, found in Tut1 Hello Triangle/Tut1.cpp, is fairly simple. The project file that builds the final executable actually uses two source files: the tutorial file and a common framework file found in framework/framework.cpp. The framework file is where the actual initialization of FreeGLUT is done; it is also where main is. This file simply uses functions defined in the main tutorial file.

FreeGLUT is a fairly simple OpenGL initialization system. It creates and manages a single window; all OpenGL commands refer to this window. Because windows in various GUI systems need to have certain book-keeping done, how the user interfaces with this is rigidly controlled.

The framework file expects 5 functions to be defined: defaults, init, display, reshape, and keyboard. The defaults function is called before FreeGLUT is initialized; it gives the tutorial the chance to modify the window size or the initialization parameters. The init function is called after OpenGL is initialized. This gives the tutorial file the opportunity to load what it needs into OpenGL before actual rendering takes place. The reshape function is called by FreeGLUT whenever the window is resized. This allows the tutorial to make whatever OpenGL calls are necessary to keep the window's size in sync with OpenGL. The keyboard function is called by FreeGLUT whenever the user presses a key. This gives the tutorial the chance to process some basic user input.

The display function is where the most important work happens. FreeGLUT will call this function when it detects that the screen needs to be rendered to.

Fork me on GitHub