Noh | エンジニア向け情報共有コミュニティ
Signup / Login

Display a window using GLFW

投稿日: Invalid Date

更新日: 2024/07/02

As mentioned in the title, we will proceed using GLFW. Although it is also fine to use GLUT (or FreeGLUT), GLUT is somewhat less intuitive, difficult to understand in terms of context handling, and is not well maintained. Therefore, we will use GLFW for these reasons.

Let's get started.

GLFW sample and explanation

First is the entire program.

#include "stdafx.h" #include <iostream> #include <gl/glew.h> #include <GLFW/glfw3.h> int main() { // Initialize GLFW if (glfwInit() == GL_FALSE) { return -1; } // Create window GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Simple", NULL, NULL); if (!window) { glfwTerminate(); return -1; } // using version glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1); // Create context glfwMakeContextCurrent(window); glfwSwapInterval(1); // Initialize GLEW if (glewInit() != GLEW_OK) { return -1; } // Loop while (glfwWindowShouldClose(window) == GL_FALSE) { // Clear buffers glClearColor(0.2f, 0.2f, 0.2f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); // swap buffers glfwSwapBuffers(window); glfwPollEvents(); } // Finish GLFW glfwTerminate(); return 0; }

I will explain.

#include "stdafx.h" #include <iostream> #include <gl/glew.h> #include <GLFW/glfw3.h>

Consider stdafx.h as something necessary for Visual C++. Various places create compilers according to the specifications of C++. Specifications may differ slightly depending on the compiler.

iostream is one of the standard functions in C++.

The glew.h must be included before #include <gl/GL.h> to avoid errors. Some of you may already have noticed, but in this program, we are not including gl/GL.h.

This is considered to be included in glfw3.h. GLFW is a very rational library for using OpenGL. So if you use GLUT, please write "#include <gl/GL.h>".

// Initialize GLFW if (glfwInit() == GL_FALSE) { return -1; }

Initializing GLFW.

// Create window GLFWwindow* window = glfwCreateWindow(640, 480, "OpenGL Simple", NULL, NULL); if (!window) { glfwTerminate(); return -1; }

I am creating a window using glfwCreateWindow(). The first argument is the width, the second argument is the height, and the third argument is the window title.

If the window cannot be created properly afterwards, the window termination process is performed by glfwTerminate().

// version glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);

Specify the OpenGL version using glfwWindowHint().

By the way, this is how you write in GLFW3. In GLFW2, it was glfwOpenWindowHint(), so older samples may use this function.

The changes are summarized here.

// Create context glfwMakeContextCurrent(window); glfwSwapInterval(1); // GLEW初期化 if (glewInit() != GLEW_OK) { return -1; }

We create a context with glfwMakeContextCurrent(). glfwSwapInterval() is a setting related to glfwSwapBuffers() that specifies the timing of swapping double buffering buffers. I think there is almost no need to set anything other than 1.

I am initializing GLEW, but please note that this should be done after creating the context.

while (glfwWindowShouldClose(window) == GL_FALSE) { // Clear buffers glClearColor(0.2f, 0.2f, 0.2f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); // Swap buffers glfwSwapBuffers(window); glfwPollEvents(); }

glClearColor(Red, Green, Blue, Alpha value) sets the color used to clear the color buffer when glClear() is later executed. Each component takes a value in the range [0, 1].

When updating the screen, you use functions like glFlush() and SwapBuffers().

glFlush() means to immediately perform drawing. However, when using this for animation, flickering may occur when updating.

Therefore, if you need to frequently update the screen like this time, use glfwSwapBuffers().

If you don't understand, you can just use glfwSwapBuffers().

glfwPollEvents() gets events such as mouse, keyboard, and window size changes. Some sites say it is not needed as it is included in glfwSwapBuffers(), but my program did not work without it. I guess it is necessary (abandoning thoughts).

Table of Contents