当前位置:网站首页>Getting to know OpenGL

Getting to know OpenGL

2022-06-26 02:03:00 _ Bruce

OpenGl

It is generally considered to be a API(Application Programming Interface, Application programming interface ), Contains a series of operational graphics 、 Functions of images . However ,OpenGL It's not a API, It's just a matter of Khronos organization Specifications developed and maintained (Specification).OpenGL It's a huge state machine (State Machine): A series of variable descriptions OpenGL How to run at this moment .OpenGL The state of is usually called OpenGL Context (Context). We usually use the following ways to change OpenGL state : set an option , Operation buffer . Last , We use the current OpenGL Context to render . Suppose when we want to tell OpenGL When drawing line segments instead of triangles , We change... By changing some context variables OpenGL state , To tell OpenGL How to draw . Once we change OpenGL The state of is to draw line segments , The next draw command will draw line segments instead of triangles .

GLFW

GLFW It's a special one for OpenGL Of C Language library , It provides some minimal interfaces for rendering objects . It allows users to create OpenGL Context , Define window parameters and handle user input

Know the above two concepts , We started building GLFW Environmental Science :

GLFW Already targeted Visual Studio 2013/2015 Precompiled binary version of and corresponding header file , But for completeness we will start by compiling the source code . So we need to download Source code package .

GLFW Source code download address :https://www.glfw.org/download.html

After downloading the source package , Unzip it and open it . We only need these contents :

  • Compile the generated library
  • include Folder

After unzipping, you will find , There's a cmake file , So we can get through cmake The tool builds the project directly , Avoid building projects step by step .

CMake

CMake Is a project file generation tool . Users can use predefined CMake Script , According to your choice ( Like Visual Studio, Code::Blocks, Eclipse) Make a difference IDE Engineering documents of . This allows us to go from GLFW Create a Visual Studio 2015 Engineering documents , Then compile . First , We download cmake Tools :https://blog.csdn.net/qq_22990635/article/details/107944645

When CMake After successful installation , You can choose from the command line or GUI start-up CMake, Because I don't want to make things too complicated , We choose to use GUI.CMake You need a source code directory and a target file directory to store the compilation results . Source code directory we choose GLFW The root directory of the source code for , Then we build a new  build  Folder , Select as target directory .

After setting the source and target directories , Click on Configure( Set up ) Button , Give Way CMake Read settings and source code . Next we need to choose the generator of the project , Because we use Visual Studio 2015, We choose  Visual Studio 14  Options ( because Visual Studio 2015 The build number of is 14).CMake The optional compile options are displayed to configure the final generated library . Here we use the default settings , And click... Again Configure( Set up ) Button save settings . After preservation , Click on Generate( Generate ) Button , The generated engineering files will be in your build In the folder .

stay build You can find GLFW.sln file , use Visual Studio 2015 open . because CMake The project has been configured , So let's go straight to Build Solution( Build solution ) Button , Then compile the library glfw3.lib( Note that we are using the 3 edition ) It will appear in src/Debug In the folder .

After the library is generated , We need to get IDE Know the location of the library and header files : Create a new directory that contains all third-party library files and header files , And in your IDE Or specify these folders in the compiler . I personally use a separate folder , It contains Libs and Include Folder , Store here OpenGL All third-party libraries and header files used in the project . So that all my third-party libraries are in the same location ( And can share multiple computers ). However, this requires you to tell each time you build a new project IDE/ Where can the compiler find these directories .

Our first project

First , open Visual Studio, Create a new project . If VS There are several options , choice Visual C++, And then choose Empty Project( Empty item )( Don't forget to give your project a proper name ). Now we finally have an empty workspace , Start creating our first OpenGL The program! !

link

In order for our program to use GLFW, We need to take GLFW Library links (Link) Advanced engineering . This can be specified in the linker settings glfw3.lib To complete , This involves vs Reference library settings in .

To add these directories ( need VS Search the library and include Where the papers are ), Let's enter first Project Properties( Engineering properties , Right click the project... In the solution window ), And then choose VC++ Directories(VC++ Catalog ) tab ( Here's the picture ). Add a table of contents in the following two columns :

Now? VS You can find all the documents you need . Last but not least Linker( The linker ) On the tab Input( Input ) Add... To the tab glfw3.lib This file :

To link a library we have to tell the linker its filename . The name of the library is glfw3.lib, We add it to Additional Dependencies( Additional dependency ) Field ( Either manually or with options ). such GLFW It will be linked when compiling . except GLFW outside , You also need to add a link entry to link to OpenGL The library of , However, the library may vary from system to system .

Windows Upper OpenGL library

If you are Windows platform ,opengl32.lib Already included in Microsoft SDK In the , It's in Visual Studio The default installation is . Because this tutorial uses VS compiler , And in Windows On the operating system , We just need to opengl32.lib Just add it to the connector settings .

Next , If you have added GLFW and OpenGL Library to connector settings , You can add... In the following way GLFW The header file :

#include <GLFW\glfw3.h>

GLFW That's all for the installation and configuration of .

GLAD

It's not over here , We still have one more thing to do . because OpenGL It's just a standard / standard , Specific implementation is driven by developers for specific video card implementation . because OpenGL There are many driver versions , Most of its functions cannot be located at compile time , Need to query at run time . So the task falls on the developer , Developers need to get the function address at runtime and save it in a function pointer for later use . How to get the address It's platform specific , stay Windows It will be like this :

//  Define function prototypes 
typedef void (*GL_GENBUFFERS) (GLsizei, GLuint*);
//  Find the correct function and assign it to the function pointer 
GL_GENBUFFERS glGenBuffers  = (GL_GENBUFFERS)wglGetProcAddress("glGenBuffers");
//  Now the function can be called normally 
GLuint buffer;
glGenBuffers(1, &buffer);

You can see that the code is very complex , And it's cumbersome , We need to repeat this process for every possible function . Fortunately, , Some libraries simplify this process , among GLAD It's the latest , It's also the most popular library .

To configure GLAD

GLAD It's a Open source The library of , It can solve the complicated problem we mentioned above .GLAD The configuration of is slightly different from most open source libraries ,GLAD Using an online service . Here we can tell GLAD Need to define OpenGL edition , And load all the relevant OpenGL function .

open GLAD On line services :https://glad.dav1d.de/, Put the language (Language) Set to C/C++, stay API In the options , choice 3.3 The above OpenGL(gl) edition ( We will use... In our tutorial 3.3 edition , But the newer version also works ). After that, the mode (Profile) Set to Core, And guarantee Generate loader (Generate a loader) The option of is selected . Now we can start with ( temporary ) Ignore expansion (Extensions) The content in . After all the choices , Click on Generate (Generate) Button to generate a library file .

GLAD Now we should provide you with a zip Compressed files , Contains two header directories , And a glad.c file . Put two header directories (glad and KHR) Copy to your Include In the folder ( Or add an extra project to point to these directories ), And add glad.c File into your project .

After the previous steps , You should be able to add the following instructions to the top of your program entry file :

#include <glad/glad.h> 

create a window

Create a new entry for our program main.cpp,

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

It should be right now , The code used to create the window is pasted directly below , Learn how to create a window line by line through code :

https://blog.csdn.net/qq_22990635/article/details/108045244

If you run the above code normally, a light green window will appear

原网站

版权声明
本文为[_ Bruce]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/177/202206260023566117.html