If there are errors in the code, terminology, etc. in the text, please correct me
Article directory
foreword
-
of this program
In order to use OpenGL, and OpenGL functions are defined in the graphics card, the definition location of most functions cannot be determined at compile time, so it needs to be queried at runtime , and the GLAD library needs to be used to obtain the address of the OpenGL function at runtime and save it in The function pointer is used by the program at runtime.
-
Glad Online Service URL
Glad Interpretation
Glad function: Get the address of the OpenGL function at runtime and save it in the function pointer for later use (a function corresponds to a function pointer).
-
glad.h
Take a glGenBuffers function of OpenGL as an example
Declare a function pointer and rename the function pointer with a macro definition
PFNGLGENBUFFERSPROC is the function pointer name
-
glad.C
Get OpenGL function address to define function pointer
-
So using the macro to define glGenBuffers is equivalent to calling the corresponding OpenGL function defined in the graphics card
step
Glad as project configuration
-
online glad option
-
download zip
-
Unzip into Hazel/vendor/Glad
-
Add premake to Glad
project "Glad" kind "StaticLib" language "C" staticruntime "off" targetdir ("bin/" .. outputdir .. "/%{prj.name}") objdir ("bin-int/" .. outputdir .. "/%{prj.name}") files{ "include/glad/glad.h", "include/KHR/khrplatform.h", "src/glad.C", } includedirs{ "include" -- 为了glad.c直接#include <glad/glad.h>,而不用#include <include/glad/glad.h> } filter "system:windows" systemversion "latest" staticruntime "On" filter { "system:windows", "configurations:Release" } buildoptions "/MT"
-
Modify the solution's premake
-- 包含相对解决方案的目录 IncludeDir = { } IncludeDir["GLFW"] = "Hazel/vendor/GLFW/include" IncludeDir["Glad"] = "Hazel/vendor/Glad/include" include "Hazel/vendor/GLFW" include "Hazel/vendor/Glad" project "Hazel" --Hazel项目 -- 包含目录 includedirs{ "%{prj.name}/src", "%{prj.name}/vendor/spdlog/include", "%{IncludeDir.GLFW}", "%{IncludeDir.Glad}" } links { "GLFW", "Glad",-- Glad.lib库链接到Hazel项目中 "opengl32.lib" } -- 如果是window系统 filter "system:windows" -- 预处理器定义 defines{ "HZ_PLATFORM_WINDOWS", "HZ_BUILD_DLL", "HZ_ENABLE_ASSERTS", "GLFW_INCLUDE_NONE" -- 让GLFW不包含OpenGL }
change item
-
Application
// #include <GLFW/glfw3.h> #include <glad/glad.h> ...... // run中使用了opengl函数 glClearColor(1, 0, 1, 1); glClear(GL_COLOR_BUFFER_BIT); ......
-
WindowsWindow.cpp
#include <glad/glad.h> static bool s_GLFWInitialized = false; void WindowsWindow::Init(const WindowProps& props) { ...... // 2.1window创建窗口 m_Window = glfwCreateWindow((int)props.Width, (int)props.Height, m_Data.Title.c_str(), nullptr, nullptr); // 设置glfw当前的上下文 glfwMakeContextCurrent(m_Window); /// // 重点在这,在运行时获取OpenGL函数地址并将其保存在函数指针中供以后使用// int status = gladLoadGLLoader((GLADloadproc)glfwGetProcAddress); HZ_CORE_ASSERT(status, "初始化Glad失败"); // 测试使用OpenGL函数 unsigned int id; glGenBuffers(1, &id);
-
Use OpenGL function effect