>
GLFW+ Glad +VS2017, refer to the above tutorial for the specific process.
GLFW is a C library specifically for OpenGL that provides some minimal interfaces for rendering objects. It allows users to create OpenGL contexts, define window parameters, and process user input.
Because OpenGL is just a standard/specification, the specific implementation is made by the driver developer for the specific graphics card. Due to the numerous versions of OpenGL drivers, the location of most of its functions cannot be determined at compile time and needs to be queryed at run time. So it falls to the developer to get the function address at run time and save it in a function pointer for later use. Fortunately, there are libraries that make this process easier, and Glad is the latest and most popular library out there.
Configuration properties of the project: new project – solution Right attribute, vc + + directory contains the directory directory, library, respectively, to include and libc folders added
Linker — Input — Add glfw3.lib, opengl32.lib.
To create the project later without having to configure it, go to View — Other Windows — Properties Manager and set the parent properties directly.
Check the version of OpenGL on your computer: Install the OpenGL Extensions Viewer to see the version number. For compatibility reasons, use version 3.3 as shown in the tutorial.
To use it you need to add:
#include <glad\glad.h>
#include <GLFW\glfw3.h>
Note that the GLAD header file contains the correct OpenGL header (e.g. GL/ GL.h), so you need to include GLAD before any other OpenGL dependent header files.
Create window
Instantation of GLFW window:
// glfw: initialize and configure
// ------------------------------
glfwInit();
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
Set the version number to 3.3 and use the core mode.
Next create the window object:
// glfw window creation
// --------------------
GLFWwindow* window = glfwCreateWindow(SCR_WIDTH, SCR_HEIGHT, "LearnOpenGL", NULL, NULL);
if (window == NULL)
{
std::cout << "Failed to create GLFW window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
Glad is used to manage OpenGL function Pointers, so we need to initialize GLAD before calling any OpenGL functions:
// glad: load all OpenGL function pointers
// ---------------------------------------
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
std::cout << "Failed to initialize GLAD" << std::endl;
return -1;
}
One more important thing to do before we start rendering is to tell OpenGL the size of the render window, the Viewport, so that OpenGL only knows how to display data and coordinates based on the size of the window. We can set the Dimension of the window by calling the glViewport function. The first two arguments to the glViewport function control the position in the bottom left corner of the window. The third and fourth parameters control the width and height (in pixels) of the render window.
// glfw: whenever the window size changed (by OS or user resize) this callback function executes
// ---------------------------------------------------------------------------------------------
void framebuffer_size_callback(GLFWwindow* window, int width, int height)
{
// make sure the viewport matches the new window dimensions; note that width and
// height will be significantly larger than specified on retina displays.
glViewport(0, 0, width, height);
}
A Callback Function is registered on the window and is called every time the window size changes.
We also need to register this function, telling GLFW that we want this function to be called whenever the window is resized, and it will be called the first time it is displayed:
glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);
To maintain the display until we actively close, we need to keep drawing the image, so we need to add a Render Loop:
// render loop
// -----------
while (!glfwWindowShouldClose(window))
{
// input
// -----
processInput(window);
// glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
// -------------------------------------------------------------------------------
glfwSwapBuffers(window);
glfwPollEvents();
}
The GLFWWindowShouldClose function checks once before we start each loop to see if GLFW has been asked to exit. If so, the function returns true and the render loop ends, after which we can close the application.
ProcessInput checks whether the user has pressed ESE:
// process all input: query GLFW whether relevant keys are pressed/released this frame and react accordingly
// ---------------------------------------------------------------------------------------------------------
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
}
The GLFWSWAPBuffers function swaps the color buffer (a large buffer that stores the color value of each pixel of the GLFW window), which is used to draw in this iteration, and will be displayed on the screen as the output.
The glfwpolleEvents function checks if any events have been triggered (such as keyboard input, mouse movement, etc.), updates the state of the window, and calls the corresponding callback function (which can be set manually through the callback method).