VS2022 configures OpenGL+GLAD

Glew (The OpenGL Extension Wrangler Library) is an encapsulation of the underlying OpenGL interface, allowing your code to cross-platform. Glad has the same function as Glew and can be regarded as an upgraded version of it.

Freeglut (OpenGL Utility Toolkit) is mainly used to create and manage windows and OpenGL contexts, handle handles, keyboard, mouse input functions, etc. Glfw (Graphics Library Framework) is an upgraded version of Freeglut.

Usually Glad is used in conjunction with Glfw.

I plan to use OpenGL and GDAL to read and process TIF images. As for the reason, you can read the answer from ChatGPT haha

The general steps for reading TIF with GDAL and OpenGL are as follows:

The combination of GDAL and OpenGL can realize the reading and rendering of TIF images in OpenGL, so as to realize the function of displaying geospatial images in the graphical interface. Here are the general steps:

  1. Use the GDAL library to read TIF images, and obtain image data and metadata, such as image width, height, pixel type, and geospatial coordinate information. You can refer to the sample code of GDAL reading TIF image provided earlier.

  2. Pass the image data read by GDAL to OpenGL, and you can load the image data by creating an OpenGL texture (Texture). You can use the OpenGL texture mapping (Texture Mapping) function to map the image data to the OpenGL texture object. You can use the OpenGL API to create texture objects, set texture parameters, load image data to texture objects, and so on.

  3. In the OpenGL rendering loop, the TIF image is rendered to the OpenGL window or canvas by using a texture object. You can use the 2D or 3D drawing function of OpenGL to apply the texture object as a texture map (Texture Mapping) to the geometry of OpenGL, so as to render the image on the screen.

What are the advantages?

The combination of GDAL and OpenGL can realize the function of rendering and displaying geospatial images in a graphical interface in real time. Compared with using GDAL alone, it has the following advantages:

  1. Real-time rendering: OpenGL can be used to display geospatial images in a real-time rendering environment, and can view and interact with geospatial data in real time, such as zooming in, zooming out, rotating, and panning, to provide a more intuitive user experience.

  2. 3D rendering: OpenGL supports 3D rendering, which can render geospatial images onto 3D models to achieve more complex 3D geographic visualization effects, such as rendering of terrain and buildings.

  3. Hardware acceleration: OpenGL uses graphics hardware to accelerate rendering, which can make full use of the GPU resources of the computer and provide high-performance rendering speed, especially for large-scale and high-resolution geospatial images, which can achieve real-time rendering effects.

  4. Graphical interface interaction: Combined with OpenGL, richer graphical interface interaction can be realized, such as using mouse, keyboard and other input devices for user interaction, such as selection, labeling, measurement and other operations, providing more flexible and convenient user operations.

  5. Scalability: As a cross-platform graphics rendering library, OpenGL can run on a variety of operating systems and hardware platforms, has good scalability, and can be easily integrated with other graphics libraries or tools to achieve more complex geographic information system (GIS) applications.

To sum up, the combined use of GDAL and OpenGL can give full play to the advantages of both, and realize more powerful and flexible geospatial image rendering and display functions.

2023/4/23 Supplement: VS2022 + GDAL has been configured, the tutorial is as follows

VS2022 configures GDAL_Programmer monitor’s blog-CSDN blog

Some students may ask, What is the difference between GDAL and GLAD?

Glad and GDAL are two different libraries with distinct differences in functionality and purpose.

  1. Glad is a C/C++ library for managing and loading OpenGL function pointers. It can generate code for loading OpenGL functions, allowing developers to use the latest OpenGL functions in OpenGL applications. Glad provides a simple, cross-platform method to load and manage OpenGL function pointers, so that developers can easily use various functions of OpenGL.

  2. GDAL (Geospatial Data Abstraction Library) is an open source, cross-platform library for processing and manipulating geospatial data. GDAL supports many geospatial data formats, such as raster data (such as remote sensing images, DEM, etc.) and vector data (such as vector maps, vector datasets, etc.). GDAL provides a wealth of functions, including data reading, writing, conversion, processing, projection transformation, etc., which can be used to access, process and analyze various geospatial data.

In terms of differences and connections, they can be summarized as follows:

  1. Function: Glad is used to load and manage OpenGL function pointers in order to use the latest OpenGL features in OpenGL applications. GDAL is used to process and manipulate geospatial data, supporting a variety of geospatial data formats.

  2. Purpose: Glad is mainly used for OpenGL applications in graphics programming, while GDAL is mainly used for geospatial data processing and analysis, such as GIS (Geographic Information System), remote sensing, cartography and other applications.

  3. Field: Glad is mostly used in the field of computer graphics and game development, while GDAL is mostly used in the field of geospatial information.

  4. Cross-platform support: Glad is a cross-platform library that can be used on multiple operating systems, including Windows, macOS, Linux, etc. GDAL is also a cross-platform library that can be used on multiple operating systems, and supports relatively comprehensive geospatial data formats.

In general, Glad is used to load and manage OpenGL function pointers for OpenGL applications in graphics programming; while GDAL is used to process and process geospatial data, mainly used in the field of geospatial information. Which library you choose to use depends on your specific needs and application domain.

VS2022 configuration OpenGL + GLAD tutorial is as follows

1. The environment and tools that need to be downloaded and installed

1. Visual Studio 2022 professional
Visual Studio: IDE and Code Editor for Software Developers and Teams

Visual Studio: IDE and Code Editor for Software Developers and Teams

Download, install and activate (please obtain the activation code yourself, which is not provided in this tutorial).

2, GLFW

Download | GLFW

Download the Source package, decompress the compressed package after the download is complete, and keep it for later use.

3, Glad

https://glad.dav1d.de/

The gl version here needs to be version 3.3 or above, and the machine configuration is good enough and can even be fully loaded. Here, version 4.0 is taken as an example.

Then download the generated compressed package

(1) After the download is complete, decompress the compressed package.

(2) Find a place to create a new folder at will, and be careful not to have a Chinese path. Put the above decompressed glfw and glad folders into this newly created folder.

For example, my folder is C:\OpenGL, and the content of the folder is as follows

4. CMake

Download | CMake

Download and install, the installation options are all default.

2. Configuration environment

1. Preparation

Create a new build folder in the glfw-3.3.8 folder.

2. Compile GLFW source files

(1) Open CMake.

(2) Click the “Browse Source” button, find the glfw-3.3.8 folder in the OpenGL folder and select it. Then click the “Browse Build” button to select the build directory you just created in the glfw-3.3.8 folder. Then click the “Configure” button.

(3) A dialog box will pop up at this point, select the compiler you need, that is, Visual Studio 2022, and there is no need to change other places, and then click the “finish” button.

(4) Wait for the configuration to complete, that is, “Configuring done” appears in the notification bar, and click the “Generate” button.

(5) After the generation is successful, the word “Generating done” will appear in the notification bar, and several files and folders will appear in the build folder. At this time, you can close CMake.

(6) Use Visual Studio 2022 to open the GLFW.sln project file in the build.

(7) Click the “Generate Solution” button, wait for the dialog box to prompt that all builds are successful, and then close Visual Studio 2022.

3. Configure Visual Studio 2022 directory

(1) Re-open Visual Studio 2022, click Create New Project, and create a new C++ empty project (or open an existing project).

(2) Select x64 for Debug, then right-click on the solution name, and click “Properties” (note that it is not the right-click on the four words of the solution).

(3) Click “VC++ Directories”, click the drop-down arrow on the right Include Directories and click “Edit”.

(4) Copy and enter the first path under Calculated value, mine is “C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools \MSVC\14.35.32215\include”.

(5) Open the OpenGL folder, copy the GLFW folder of C:\OpenGL\glfw-3.3.8\include and the glad and KHR folders of C:\OpenGL\glad-4.0\include to In the C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC\14.35.32215\include folder that was just opened.

(6) Still in the VC++ directory, click the drop-down arrow on the right Library directory and click “Edit”. Copy and enter the first path under Calculated value, mine is “C:\Program Files\Microsoft Visual Studio\2022\Professional\VC\Tools\MSVC \14.35.32215\lib\x64”.

(7) Open the OpenGL folder, copy the glfw3.lib file of C:\OpenGL\glfw-3.3.8\build\src\Debug to the C:\Program Files\Microsoft Visual just opened Studio\2022\Professional\VC\Tools\MSVC\14.35.32215\lib\x64 folder.

4. Configure the Visual Studio 2022 linker

(1) Click “Input” in “Linker” under VC++ directory, and click Additional dependencies on the right item click the drop-down arrow and click Edit.

(2) Add “glfw3.lib” and “opengl32.lib”.

Finally, click OK.

3. Test environment

(1) Add the glad.c file in C:\OpenGL\glad-4.0\src to the empty Visual Studio 2022 project you just created, and create a new test.cpp file.

(2) Copy the code below into the test.cpp file.

#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <iostream>

void framebuffer_size_callback(GLFWwindow* window, int width, int height);
void processInput(GLFWwindow *window);

// settings
const unsigned int SCR_WIDTH = 800;
const unsigned int SCR_HEIGHT = 600;

int main()
{
    // 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);

#ifdef __APPLE__
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);
#endif

    // 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);
    glfwSetFramebufferSizeCallback(window, framebuffer_size_callback);

    // glad: load all OpenGL function pointers
    // ---------------------------------------
    if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
    {
        std::cout << "Failed to initialize GLAD" << std::endl;
        return -1;
    }

    // render loop
    // -----------
    while (!glfwWindowShouldClose(window))
    {
        // input
        // -----
        processInput(window);

        // render
        // ------
        glClearColor(0.2f, 0.3f, 0.3f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        // glfw: swap buffers and poll IO events (keys pressed/released, mouse moved etc.)
        // ------------------------------------------------ --------------------------------
        glfwSwapBuffers(window);
        glfwPollEvents();
    }

    // glfw: terminate, clearing all previously allocated GLFW resources.
    // ------------------------------------------------ ------------------
    glfwTerminate();
    return 0;
}

// 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);
}

// 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);
}

(3) Click the local Windows debugger, an empty OpenGL canvas appears, and the configuration is successful.