Creating your first OpenGL application with GLFW
Now that you have successfully configured your development platform and installed the GLFW library, we will provide a tutorial on how to create your first OpenGL-based application.
Getting ready
At this point, you should already have all the pre requisite tools ready regardless of which operating system you may have, so we will immediately jump into building your first OpenGL application using these tools.
How to do it...
The following code outlines the basic steps to create a simple OpenGL program that utilizes the GLFW library and draws a rotating triangle:
- Create an empty file, and then include the header file for the GLFW library and standard C++ libraries:
#include <GLFW/glfw3.h> #include <stdlib.h> #include <stdio.h>
- Initialize GLFW and create a GLFW window object (640 x 480):
int main(void) { GLFWwindow* window; if (!glfwInit()) exit(EXIT_FAILURE); window = glfwCreateWindow(640, 480, "Chapter 1: Simple GLFW Example", NULL, NULL); if (!window) { glfwTerminate(); exit(EXIT_FAILURE); } glfwMakeContextCurrent(window);
- Define a loop that terminates when the window is closed:
while (!glfwWindowShouldClose(window)) {
- Set up the viewport (using the width and height of the window) and clear the screen color buffer:
float ratio; int width, height; glfwGetFramebufferSize(window, &width, &height); ratio = (float) width / (float) height; glViewport(0, 0, width, height); glClear(GL_COLOR_BUFFER_BIT);
- Set up the camera matrix. Note that further details on the camera model will be discussed in Chapter 3, Interactive 3D Data Visualization:
glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f); glMatrixMode(GL_MODELVIEW); glLoadIdentity();
- Draw a rotating triangle and set a different color (red, green, and blue channels) for each vertex (x, y, and z) of the triangle. The first line rotates the triangle over time:
glRotatef((float)glfwGetTime() * 50.f, 0.f, 0.f, 1.f); glBegin(GL_TRIANGLES); glColor3f(1.f, 0.f, 0.f); glVertex3f(-0.6f, -0.4f, 0.f); glColor3f(0.f, 1.f, 0.f); glVertex3f(0.6f, -0.4f, 0.f); glColor3f(0.f, 0.f, 1.f); glVertex3f(0.f, 0.6f, 0.f); glEnd();
- Swap the front and back buffers (GLFW uses double buffering) to update the screen and process all pending events:
glfwSwapBuffers(window); glfwPollEvents(); }
- Release the memory and terminate the GLFW library. Then, exit the application:
glfwDestroyWindow(window); glfwTerminate(); exit(EXIT_SUCCESS); }
- Save the file as
main.cpp
using the text editor of your choice.
How it works...
By including the GLFW library header, glfw3.h
, we automatically import all necessary files from the OpenGL library. Most importantly, GLFW automatically determines the platform and thus allows you to write portable source code seamlessly.
In the main function, we must first initialize the GLFW library with the glfwInit function in the main thread. This is required before any GLFW functions can be used. Before a program exits, GLFW should be terminated to release any allocated resources.
Then, the glfwCreateWindow function creates a window and its associated context, and it also returns a pointer to the GLFWwindow
object. Here, we can define the width, height, title, and other properties for the window. After the window is created, we then call the glfwMakeContextCurrent function to switch the context and make sure that the context of the specified window is current on the calling thread.
At this point, we are ready to render our graphics element on the window. The while loop provides a mechanism to redraw our graphics as long as the window remains open. OpenGL requires an explicit setup on the camera parameters; further details will be discussed in the upcoming chapters. In the future, we can provide different parameters to simulate perspective and also handle more complicated issues (such as anti-aliasing). For now, we have set up a simple scene to render a basic primitive shape (namely a triangle) and fixed the color for the vertices. Users can modify the parameters in the glColor3f and glVertex3f functions to change the color as well as the position of the vertices.
This example demonstrates the basics required to create graphics using OpenGL. Despite the simplicity of the sample code, it provides a nice introductory framework on how you can create high-performance graphics rendering applications with graphics hardware using OpenGL and GLFW.