为什么需要检查 GLAD 或 GLEW 是否已正确初始化?
Why is needed to check if GLAD or GLEW was initialized correctly?
我正在使用 GLFW + GLAD 或 GLEW (#include glad/glad.h
#include <GL/glew.h>
) 设置 opengl。
在函数 glfwMakeContextCurrent(window);
之后,我需要检查 GLAD 或 GLEW 是否已正确初始化,否则我将在 Visual Studio 2019 中 运行 进入 异常错误 。
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
int main(void) {
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Check if GLEW is ok after valid context */
/*if (glewInit() != GLEW_OK) {
cout << "Failed to initialize GLEW" << endl;
}*/
/* or Check if GLAD is ok after valid context */
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "Failed to initialize GLAD" << endl;
return -1;
}
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
...
}
glfwTerminate();
return 0;
}
这里我用的是GLAD。我真的不明白为什么我必须检查这个。提前致谢。
Here I am using GLAD. I really can't understand why I have to check this
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
这里的重点不是检查,而是对gladLoadGLLoader
的调用,它实际上初始化了GLAD并加载了所有的GL函数指针。所以你在这里做的是初始化 GLAD,然后检查那个函数的return值,它告诉你GLAD是否能够正确初始化。
因此,如果您不想要“支票”,正确的代码就是
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
省略检查当然不是一个好主意,因为这意味着您可能会尝试调用可能未加载的 GL 函数指针,这很可能会使您的应用程序崩溃 - 与未加载时您会看到的错误相同甚至尝试初始化它,就像您已经做的那样。
我正在使用 GLFW + GLAD 或 GLEW (#include glad/glad.h
#include <GL/glew.h>
) 设置 opengl。
在函数 glfwMakeContextCurrent(window);
之后,我需要检查 GLAD 或 GLEW 是否已正确初始化,否则我将在 Visual Studio 2019 中 运行 进入 异常错误 。
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#include <iostream>
using namespace std;
int main(void) {
GLFWwindow* window;
/* Initialize the library */
if (!glfwInit())
return -1;
/* Create a windowed mode window and its OpenGL context */
window = glfwCreateWindow(640, 480, "Hello World", NULL, NULL);
if (!window)
{
glfwTerminate();
return -1;
}
/* Make the window's context current */
glfwMakeContextCurrent(window);
/* Check if GLEW is ok after valid context */
/*if (glewInit() != GLEW_OK) {
cout << "Failed to initialize GLEW" << endl;
}*/
/* or Check if GLAD is ok after valid context */
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
cout << "Failed to initialize GLAD" << endl;
return -1;
}
/* Loop until the user closes the window */
while (!glfwWindowShouldClose(window))
{
/* Render here */
glClear(GL_COLOR_BUFFER_BIT);
...
}
glfwTerminate();
return 0;
}
这里我用的是GLAD。我真的不明白为什么我必须检查这个。提前致谢。
Here I am using GLAD. I really can't understand why I have to check this
if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
这里的重点不是检查,而是对gladLoadGLLoader
的调用,它实际上初始化了GLAD并加载了所有的GL函数指针。所以你在这里做的是初始化 GLAD,然后检查那个函数的return值,它告诉你GLAD是否能够正确初始化。
因此,如果您不想要“支票”,正确的代码就是
gladLoadGLLoader((GLADloadproc)glfwGetProcAddress);
省略检查当然不是一个好主意,因为这意味着您可能会尝试调用可能未加载的 GL 函数指针,这很可能会使您的应用程序崩溃 - 与未加载时您会看到的错误相同甚至尝试初始化它,就像您已经做的那样。