You need to enable JavaScript to run this app. 无法访问部分OpenGL函数,调用glGenBuffer()提示未找到该函数
优惠活动
大模型
产品
解决方案
定价
更多

无法访问部分OpenGL函数,调用glGenBuffer()提示未找到该函数

问题原因

  • 函数调用错误:你调用的glGenBuffer()不是标准OpenGL API,正确的缓冲区生成函数为glGenBuffers(),末尾带s。该函数需要传入两个参数:要生成的缓冲区数量、接收缓冲区ID的指针,同时存储缓冲区ID的变量类型应为GLuint而非int,你现有写法的参数、返回值接收方式都不符合API规范。
  • 未加载高版本OpenGL函数:GLFW仅负责OpenGL上下文创建和窗口管理,默认不会加载OpenGL 1.1以上版本的函数,你使用的是OpenGL 3.3核心模式,glGenBuffers()属于核心模式新增的函数,没有手动加载的情况下会提示找不到函数。

解决方案

  1. 引入OpenGL函数加载逻辑
    使用GLAD完成高版本OpenGL函数加载,按你使用的OpenGL 3.3核心模式生成对应GLAD文件后,在项目中引入glad.h和对应的源文件,注意glad.h的引入顺序要早于glfw3.h。
  2. 修正窗口初始化逻辑
    在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();
    }
};
  1. 修正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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.13 13:51:03