(Easiest) VS-2022 to build an opengl development environment

Opengl development prerequisites: C++ foundation, VS development environment

opengl development environment

Using OpenGL in C++ requires configuring several libraries.
●C++ development environment;
●OpenGL / GLSL;
●Window management;
●Extension library;
●Mathematics library;
●Texture management.


window management

OpenGL does not actually draw the image directly to the computer screen, but renders it to a frame buffer, and then the machine needs to be responsible for drawing the contents of the frame buffer to a window on the screen.
GLFW is one of the most popular choices, with built-in support for Windows, macOS, Linux, and other operating systems [GF17]. It can be downloaded from its official website and must be compiled on the machine where it is to be used.

extension library

OpenGL is organized around a set of basic functions and extension mechanisms. As technology evolves, extension mechanisms can be used to support new functionality. Modern versions of OpenGL, such as the version 4+ we use in this book, need to recognize the extensions available on the GPU. There are some built-in commands in the OpenGL core to support these, but to use each modern command, there are quite a lot of fairly complex lines of code that need to be executed.
So it has become standard practice to use an extension library to handle these details, allowing programmers to use modern OpenGL commands directly. Such as Glee, GLLoader and GLEW, and the newer GL3W and GLAD.
Commonly used is GLEW , which means OpenGL Extension Wrangler (OpenGL Extension Wrangler). It supports various operating systems, including Windows, Macintosh, and Linux [GE17]. GLEW is not a perfect choice. For example, it requires an additional DLL. Recently, many developers choose GL3W or GLAD. They have the advantage of being automatically updated, but require Python to be installed. For these reasons, we choose to use GLEW in this book . It can be downloaded from the official website.

math library

3D graphics programming makes heavy use of vector and matrix algebra. Two such libraries that are often used with OpenGL are Eigen and vmath . The latter is used in the popular OpenGL SuperBible [SW15].
The most popular math library:
OpenGL Mathematics, commonly known as GLM . Provides classes and basic mathematical functions related to graphics concepts, such as vectors, matrices, and quaternions. It also contains various utility classes for creating and working with common 3D graphics structures, such as perspective and viewing angle matrices.

texture management

We'll use image files to add "texture" to the objects in our graphics scene. This means we will need to frequently load these image files into our C++/OpenGL code. It is possible to write a texture image loader from scratch. However, given the wide variety of image file formats, it is often better to use a texture loading library.
Examples include FreeImage, DevIL, OpenGL Image (GLI), and Glraw. Simple OpenGL Image Loader (SOIL) is probably the most commonly used OpenGL image loading library, although it's a bit outdated.
The texture image loading library used in this book is SOIL2 - a newer fork of SOIL. Like our previous library of choice, SOIL2 is compatible with a wide variety of platforms.

optional library

There are some more sophisticated off-the-shelf OBJ loaders to choose from, such as Assimp and tinyobjloader.


Install

In order to make the installation easier, the latest C++ package manager is used here. C++ finally has a package manager, ending the era of manual compilation . Of course, if you don't use it, you can only download the source code and compile it manually. And you have to import the package yourself in the project.

Install vcpkg:

Cross-platform c++ package manager. official website

git clone https://github.com/Microsoft/vcpkg.git
.\vcpkg\bootstrap-vcpkg.bat
vcpkg integrate install
# 如果出现未找到vcpkg,那么是因为vcpkg.exe没有配置到环境变量中,所以你只能在安装目录中运行这个命令

Install GLFW using vcpkg:

A window management library for creating windows that can display images.
Run in a command line window:
vcpkg install glfw3:x64-windows

// windows普通C++项目不需要如下配置
// 如果是cmake项目,项目使用库时需要在CMakefile中添加如下语句引入到项目中。
find_package(glfw3 CONFIG REQUIRED)
target_link_libraries(main PRIVATE glfw)

Install GLEW using vcpkg

What is the use of GLEW ? You can regard it as the basic library of OpenGL on Windows, which replaces the original gl.h wgl.h, allowing you to easily call the latest OpenGL functions, including numerous Extensions of OpenGL .
vcpkg install glew:x64-windows

find_package(GLEW REQUIRED)
target_link_libraries(main PRIVATE GLEW::GLEW)

Install GLM using vcpkg

opengl math library
vcpkg install glm:x64-windows

  find_package(glm CONFIG REQUIRED)
  target_link_libraries(main PRIVATE glm::glm)

Install SOIL2 using vcpkg:

image loading library
vcpkg install soil2:x64-windows

    find_package(soil2 CONFIG REQUIRED)
    target_link_libraries(main PRIVATE soil2)

Install opengl:

OpenGL does not need to be installed under Windows, if the system supports it, Windows has already provided it.
opengl32.lib (should already be provided as part of the standard Windows SDK)
needs to be introduced when using:
create an empty project, open "Project" -> "Properties", import opengl.lib in the linker
insert image description here

Run the first opengl project

Create an empty project, add a source file.

#include<gl/glew.h> // 所有的函数库
#include<GLFW/glfw3.h> // 窗口库

int main() {
    
    
    glfwInit(); // 初始化
    GLFWwindow* window = glfwCreateWindow(800, 600, "LearnOpenGL", nullptr, nullptr); // 创建窗口
    while (!glfwWindowShouldClose(window)) // 检查是否关闭
    {
    
    
        glfwPollEvents(); // 处理事件,比如可以让窗口被拖动
        glfwSwapBuffers(window); // 将窗口缓存区的数据进行交换以达到刷新的目的
    }
    glfwTerminate(); // 销毁并回收资源
    return 0;
}

If there is no problem, the following window will appear:
insert image description here

Guess you like

Origin blog.csdn.net/qq_41709801/article/details/127511139