The hurdles that OpenGL development must pass ------ preparation of the development environment (Windows10)

foreword

Graphics programming has long been one of the most challenging topics in computer science. With the rise of limited VR technology, more and more companies are starting to get involved in the VR field. At present, Unity3d is the most used for development, but like browsers, 2D applications need to be 3Dized (the 2D application interface is projected into a 3D scene, and the user can operate the 2D interface through the handle). These requirements require 3D graphics. Programming is very familiar in order to do these cool scenes. The current graphics programming is based on the graphics library, that is, the programmer calls a pre-defined library or a series of functions in the library, and these libraries provide support for the underlying graphics operations.

There are many graphics libraries on the market now, but the common platform-independent graphics library is called OpenGL (Open Graphics Library). This series of blogs is also to introduce how to use OpenGL in C++ for 3D graphics programming. Before introducing the use of OpenGL for 3D graphics programming, let's first do a hurdle that must be passed before getting started-----OpenGL development environment configuration preparation.

1. Environment configuration preparation

As the saying goes: "Everything is difficult at the beginning". Many students are very interested in OpenGL, but they are discouraged by the environment configuration stage when they do it. The entry threshold for interested students. First of all, let's introduce the environment and libraries needed to use OpenGL in C++, as follows:

Development IDE: Visual Studio2022, other versions are also available, the configuration is similar to the
OpenGL/GLSL
window management library (GLFW)
extension library (GLEW)
math library (GLM)
texture image loading library (SOIL2)

Next, we will prepare to configure the environment listed above. OpenGL/GLSL does not need to be configured by us. The system has it by default. GLSL is a shader language introduced in OpenGL2.0. This introduction allows programmers to achieve more and more realistic effects by writing shader languages.

2. Download related libraries

2.1 Download window management library (GLFW)

OpenGl does not actually draw the image directly to the computer screen, but renders it into a frame buffer, and then the computer is responsible for drawing the contents of the frame buffer to a window on the screen. To complete this function, you can choose to use the GLFW library. This library supports Windows, MacOS, Linux and other operating systems. We can get it from the official website: GLFW official website download
address
If you are a member of CSDN, you can go to the resources to download, no need Points, if you don’t have a membership, go directly to the official website to download, there is no need to spend money to open a membership in order to download a resource:
GLFW CSDN download address

2.2 Download OpenGL extension library

GLEW (OpenGL extension wrangler), that is, OpenGL extension cowboy, OpenGL is organized around a set of basic functions and extension mechanisms. With the development of technology, the extension mechanism can be used to support new functions, so GLEW is used to do this job of. Downloading this library also gives readers two addresses:
GLEW official website download address

GLEW CSDN download address

2.3 Download OpenGL math library

GLM is the OpenGL Mathematics mathematics library, it is a C++ library with only header files, compatible with Windows, MacOS and Linux, it can provide classes and basic mathematical functions related to graphics concepts, such as vectors, matrices and quaternions, it also Contains various tool classes for creating and using common 3D graphics structures, such as perspective matrix and view matrix
GLM official website download address

GLM CSDN download address

2. 4 Texture image loading library SOIL2 download

SOIL2 is used to load OpenGL image textures. We can write a texture image loader from scratch, but considering a variety of image file formats, it is usually a wiser choice to use a texture image loading library. Installing the integrated SOIL2 image texture loading library requires me to download the SOIL2 library and an auxiliary tool premake
SOIL2 GitHub download
premake tool official website download

SOIL2 CSDN download
premake tool CSDN download

3. Prepare shared library files (lib) and include folders

Because developing OpenGL on Windows requires configuring the environment, our best way is to configure the OpenGL development environment as a project template, so that we can develop based on our pre-configured template every time we create a new OpenGL project. Before making a project template, we need to find a place to put the corresponding library, and then create a reference to the library in Visual Studio. First, we need to choose a location to store our library file. Here we can choose to store it in any location. The following figure is the folder structure of the folder.

library folder structure
According to the above library file structure diagram, let's go to practice. Here I created a folder on my D drive to store these related libraries of OpenGL.
insert image description hereAfter creating the structure of the library file, we start to prepare the corresponding library

3.1 Prepare the window management library GLFW

After we downloaded the compressed package of GLFW and decompressed it, we will get a directory as follows.
glfw decompressed directoryAfter decompressing, we can see that there are different versions of Visual Studio's lib. I use Visual studio 2022, so I put all the contents under lib-vc2022 Copy it to D:\OpenGL\OpenGLTemplate\libthe , and then go back to the glfw folder we decompressed, which is the folder in the above picture. Click include and you will find a GLFW directory under the directory. Copy the entire directory to the folder we created. Under the include directory under the OpenGLTemplate folder, the path is: D:\OpenGL\OpenGLTemplate\include, so far, the preparation of GLFW is completed.

3.2 Prepare the OpenGL extension library GLEW

Similarly, decompress the GLEW compressed package obtained earlier, and you will get the file structure shown in the figure below:
The file structure of the expansion card glewAfter decompressing the glew library, click lib->Release->x64 and copy all of them to the OpenGLTemplate folder we created In the lib directory: path D:\OpenGL\OpenGLTemplate\lib, then go back to the glew directory we decompressed, find the include directory, click on it and find a GL directory inside, copy all of it to the include directory under OpenGLTemplate. Path: D:\OpenGL\OpenGLTemplate\includeAt this point, the integration of the GLEW library is complete.

3.3 Prepare the OpenGL math library GLM

The integration of this library is relatively simple. After we get the compressed package of this library and decompress it, we will get the directory shown in the figure below.
The file directory structure of the OpenGL math library glmWe directly copy the entire glm directory under the decompressed directory to the include directory under the OpenGLTemplate directory we created. The glm library is integrated

3.4 Prepare texture image loading library SOIL2

The integration of SOIL2 is a bit more troublesome. We need the assistance of the premake tool. We first obtain the zip of SOIL2 and decompress it, then obtain the zip package of premake and decompress it. After decompression of premake, we will get a premake5.exe, copy it to our decompression under the SOIL2 directory. If the reader obtains SOIL2 from the CSDN download address I provided, I compiled it and can use it directly. If it is downloaded from the official website, we need to compile it.
Download the SOIL2 CatalogObtain the compressed package of premake and decompress it to get the following directory.
The directory where the premake compressed package is decompressedCopy premake5.exe to the SOIL2 directory after we decompress it, that is,
D:\OpenGL\installPKG\SOIL2-1.3.0under, and then.
SOIL2 directory after adding premake5.exeThen run the CMD command line under SOIL2-1.3.0: Enter the command: premake5 vs2022
insert image description here注意,vs2022是我的visual studio的版本,具体的版本根据安装的版本来定
After running the above command, a make directory will be generated under SOIL2-1.3.0
The SOIL2 directory after running the premake commandThen we enter the make->windwos directory:
insert image description hereuse Visal Studio to open the above picture Files marked with red boxes.
insert image description hereAfter opening the above file, as shown in the figure, we click the right mouse button and select Generate in the pop-up menu. After the generation is completed, close Vsual Studio. At this time, there will be an additional lib in the SOIL2-1.3.0 directory, as shown
below Next, we will copy all the libraries under lib to the lib directory under the OpenGLTemplate directory we created earlier. Then go back to the SOIL2 directory and copy all the src->SOIL2 directories to the include directory under the OpenGLTemplate directory. In this way, our image texture loading library SOIL2 is integrated.

到这里,我们对比下我们之前给到的OpenGLTemplate的文件目录图发现,还差一个glew32.dll文件需要放到OpenGLTemplate目录下,没有 这个库我们是无法运行OpenGL的代码的,这个库位于我们解压后的Glew目录下,我们找到解压的Glew目录,然后在其bin->Release->x64下找到这个库
insert image description hereCopy it into the OpenGLTemplate directory, and our library is ready. Next we make an OpenGL project startup template

4. Use Visual Studio to make an OpenGL project template

4.1 Template configuration

Open our Visual Studio, then create a new C++ empty project and
insert image description hereopen it after setting the name. For example, here we call it OpenGLTemplate, and then open it.
insert image description hereWe first configure the library reference in Debug mode.
We find the project->properties to open the property configuration interface, and then Configure the include path in our previous OpenGLTemplate directory to the "include directory" marked in the figure below.
insert image description hereinsert image description hereThen we still find "Linker -> General" on the property setting panel of Visual Studio and configure the lib directory under the OpenGLTemplate directory we prepared before here

insert image description hereThen we also add glfw3.lib, glew32.lib, soil2-debug.lib, opengl32.lib in the lib under the OpenGLTemplate directory under "Linker -> Input -> Add Dependencies" (should already be part of WindowSDK Yes, it doesn’t matter if you don’t configure it, but it’s better to configure
insert image description hereit for insurance) Save it after configuration, and then switch to Release mode, repeat the operation and configure it again, so I won’t go into details here.
insert image description here

4.2 Template export

insert image description here
insert image description hereAfter clicking Finish, when we create a new project again, we will find an additional project template, as shown below:
insert image description hereWe can create an OpenGL project by clicking on the template we defined.

5. Test the OpenGL environment

First, right-click on the source file directory of the project to create a new cpp file: FirstOpenGLDemo.cpp
insert image description hereInput code:

#include<GL/glew.h>
#include<GLFW/glfw3.h>
#include<iostream>

using namespace std;

void init(GLFWwindow* window) {
    
    

}

void display(GLFWwindow* window, double currentTime) {
    
    
	glClearColor(1.0, 0.0, 0.0, 1.0);
	glClear(GL_COLOR_BUFFER_BIT);
}

int main(void) {
    
    
	if (!glfwInit()) {
    
    
		exit(EXIT_FAILURE);
	}
		glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
		glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
		GLFWwindow* window = glfwCreateWindow(600, 600, "OpenGl-Demo", NULL, NULL);
		glfwMakeContextCurrent(window);
		if (glewInit() != GLEW_OK) {
    
     exit(EXIT_FAILURE); }
		glfwSwapInterval(1);
		init(window);

		while (!glfwWindowShouldClose(window)) {
    
    
			display(window, glfwGetTime());
			glfwSwapBuffers(window);
			glfwPollEvents();
		}

		glfwDestroyWindow(window);
		glfwTerminate();
		exit(EXIT_SUCCESS);
}

At this time, we found that the relevant libraries can be referenced successfully, but they can’t run, and the following error will be reported. At
insert image description herethis time, we need to put the glew32.dll dynamic link library we put in the OpenGLTemplate directory, in the directory of our project under the directory with the same name.

insert image description hereAt this time, we run the code again, and we will find that the operation is successful.
insert image description hereOur code above simply shows a red background. So far, the configuration of the OpenGL development environment has been completed.

Summarize

This article mainly introduces the OpenGL development environment built on Visual Studio 2022 on Windows 10, mainly introduces the configuration and integration of the window management library GLFW, the OpenGL extension library GLEW, the OpenGL math library GLM and the image texture loading library SOIL2, and finally customizes a Visual Studio project template, and finally tested our configured environment with a test Demo. It is a pity that glew32.dll must be manually copied to the specified directory of the project, otherwise the OpenGL program cannot be run. If readers have a better and more convenient way here, welcome to communicate in the comment area.

Guess you like

Origin blog.csdn.net/zxj2589/article/details/130467844