无法访问部分OpenGL函数,调用glGenBuffer()提示未找到该函数
问题原因
- 函数调用错误:你调用的
glGenBuffer()不是标准OpenGL API,正确的缓冲区生成函数为glGenBuffers(),末尾带s。该函数需要传入两个参数:要生成的缓冲区数量、接收缓冲区ID的指针,同时存储缓冲区ID的变量类型应为GLuint而非int,你现有写法的参数、返回值接收方式都不符合API规范。 - 未加载高版本OpenGL函数:GLFW仅负责OpenGL上下文创建和窗口管理,默认不会加载OpenGL 1.1以上版本的函数,你使用的是OpenGL 3.3核心模式,
glGenBuffers()属于核心模式新增的函数,没有手动加载的情况下会提示找不到函数。
解决方案
- 引入OpenGL函数加载逻辑
使用GLAD完成高版本OpenGL函数加载,按你使用的OpenGL 3.3核心模式生成对应GLAD文件后,在项目中引入glad.h和对应的源文件,注意glad.h的引入顺序要早于glfw3.h。 - 修正窗口初始化逻辑
在glfwMakeContextCurrent(window)之后添加函数加载逻辑,修改后的Create_Window函数代码如下:
#include <glad/glad.h> #include <GLFW/glfw3.h> #include <iostream> using namespace std; class Window_Manager { public: GLFWwindow* window; int Create_Window(int width, int height, const char* title) { if (!glfwInit()) { return -1; } glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); glfwWindowHint(GLFW_FOCUSED, GL_TRUE); window = glfwCreateWindow(width, height, title, NULL, NULL); if (window == NULL) { cout << "Failed to create a window! Aborting early..." << endl; Terminate_Window(window); return -1; } glfwMakeContextCurrent(window); // 新增:加载OpenGL函数 if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress)) { cout << "Failed to initialize GLAD" << endl; return -1; } glViewport(0, 0, width, height); return 1; } void Terminate_Window(GLFWwindow* window) { glfwSetWindowShouldClose(window, GL_TRUE); glfwTerminate(); } };
- 修正Shape2D类的代码逻辑
#include <glad/glad.h> #include <GLFW/glfw3.h> #include <Engine_Manager.h> #include <iostream> using namespace std; class Shape2D { GLuint VBO; // 类型改为GLuint int Setting_Up(){ glGenBuffers(1, &VBO); // 修正函数名和传参 return 1; } };
内容的提问来源于stack exchange,提问作者IHaydot
相关产品推荐
相关产品推荐

