在需要的地方有条件地包含GLES2

问题描述

我正在尝试使用诸如glCreateShaderglGetShaderivglDeleteProgram之类的功能,但是当我在其中导入GL/gl.hGL/glext.h时它们不存在一个Linux系统。

这可能是因为它的笔记本电脑来自2008年,带有集成的图形报告:

2.1 Mesa 20.1.10

和离散卡:

3.0 Mesa 20.1.10

当我导入GLES2/gl2.h时,一切正常,并且包含的​​文件得以解决

现在,我目前至少有一个月不能访问台式机或具有足够高的图形标准的计算机了-我假设,这些更好的系统具有标准中包含的着色器功能GL/gl.h文件

如何编写条件导入以仅在需要的地方要求GLES?我不想在 Makefile 添加一个变量,我希望能够自动检测仅在使用{{ 1}}序列。

解决方法

您必须使用glewglad之类的OpenGL加载器。 OpenGL(ES)是一种规范。加载程序使您可以访问图形驱动程序提供的API函数。

请参见OpenGL Loading Library - glad

const SDL_GLContext context = SDL_GL_CreateContext(window);
if (context == nullptr) {
    std::cout << "SDL could not create context";
    return 1;
}

if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
{
    std::cout << "Failed to initialize OpenGL context" << std::endl;
    return -1;
}

请参见Initialize GLEW

const SDL_GLContext context = SDL_GL_CreateContext(window);
if (context == nullptr) {
    std::cout << "SDL could not create context";
    return 1;
}

if {glewInit() != GLEW_OK}
{
    std::cout << "Failed to initialize GLEW" << std::endl;
    return -1;
}