如何创建一个;核心;OpenGL上下文与GLX?

如何创建一个;核心;OpenGL上下文与GLX?,opengl,glx,Opengl,Glx,我想使用GLX和“核心”配置文件创建一个OpenGL上下文 为了进行比较,可以使用QGLFormat::CoreProfile创建QOpenGLContext 我已经找到了这些说明:但是我没有看到提到核心/兼容性概要文件 /* gcc this.c -lGL -lX11 */ #include <stdbool.h> #include <stdio.h> #include <stdlib.h> #include <GL/glx.h> #inclu

我想使用GLX和“核心”配置文件创建一个OpenGL上下文

为了进行比较,可以使用
QGLFormat::CoreProfile
创建
QOpenGLContext

我已经找到了这些说明:但是我没有看到提到核心/兼容性概要文件

/* gcc this.c -lGL -lX11 */

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <GL/glx.h>
#include <X11/Xlib.h>

/*
    License: Public domain

    Contents
    --------
    - Create_display_and_window
    - Create_the_modern_OpenGL_context
    - Show_the_window
    - Application_loop
*/

typedef GLXContext (*glXCreateContextAttribsARBProc)
    (Display*, GLXFBConfig, GLXContext, Bool, const int*);

int main()
{
    Display* disp = 0;
    Window win = 0;

    /* Create_display_and_window
       -------------------------
       Skip if you already have a display and window */
    disp = XOpenDisplay(0);
    win = XCreateSimpleWindow(disp, DefaultRootWindow(disp),
                              10, 10,   /* x, y */
                              800, 600, /* width, height */
                              0, 0,     /* border_width, border */
                              0);       /* background */

    /* Create_the_modern_OpenGL_context
       -------------------------------- */
    static int visual_attribs[] = {
        GLX_RENDER_TYPE, GLX_RGBA_BIT,
        GLX_DRAWABLE_TYPE, GLX_WINDOW_BIT,
        GLX_DOUBLEBUFFER, true,
        GLX_RED_SIZE, 1,
        GLX_GREEN_SIZE, 1,
        GLX_BLUE_SIZE, 1,
        None
    };

    int num_fbc = 0;
    GLXFBConfig *fbc = glXChooseFBConfig(disp,
                                         DefaultScreen(disp),
                                         visual_attribs, &num_fbc);
    if (!fbc) {
        printf("glXChooseFBConfig() failed\n");
        exit(1);
    }

    /* If we were on Windows (i.e. WGL), we would need to create an old
       dummy OpenGL context here, before calling GetProcAddress(). This is
       unnecessary on Linux (GLX).

       For details, refer to the spec
       (https://www.khronos.org/registry/OpenGL/extensions/ARB/GLX_ARB_get_proc_address.txt)
       which says:
           > Are function pointers context-independent? Yes. The pointer to an
           > extension function can be used with any context [...]

       This is in direct contrast to WGL's wglGetProcAddress. */

    glXCreateContextAttribsARBProc glXCreateContextAttribsARB = 0;
    glXCreateContextAttribsARB =
        (glXCreateContextAttribsARBProc)
        glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB");

    /* If we were on Winows, we would destroy the dummy context here. Again,
       this is unnecessary on Linux.*/

    if (!glXCreateContextAttribsARB) {
        printf("glXCreateContextAttribsARB() not found\n");
        exit(1);
    }

    /* Set desired minimum OpenGL version */
    static int context_attribs[] = {
        GLX_CONTEXT_MAJOR_VERSION_ARB, 4,
        GLX_CONTEXT_MINOR_VERSION_ARB, 2,
        None
    };
    /* Create modern OpenGL context */
    GLXContext ctx = glXCreateContextAttribsARB(disp, fbc[0], NULL, true,
                                                context_attribs);
    if (!ctx) {
        printf("Failed to create OpenGL context. Exiting.\n");
        exit(1);
    }

    /* Show_the_window
       --------------- */
    XMapWindow(disp, win);
    glXMakeCurrent(disp, win, ctx);

    int major = 0, minor = 0;
    glGetIntegerv(GL_MAJOR_VERSION, &major);
    glGetIntegerv(GL_MINOR_VERSION, &minor);
    printf("OpenGL context created.\nVersion %d.%d\nVendor %s\nRenderer %s\n",
           major, minor,
           glGetString(GL_VENDOR),
           glGetString(GL_RENDERER));

    /* Application_loop
       ---------------- */
    while(1) {}

    return 0;
}
/*gcc this.c-lGL-lX11*/
#包括
#包括
#包括
#包括
#包括
/*
许可证:公共领域
目录
--------
-创建\u显示\u和\u窗口
-创建\u现代\u OpenGL\u上下文
-显示窗口
-应用程序循环
*/
typedef GLXContext(*glXCreateContextAttribsARBProc)
(Display*,GLXFBConfig,GLXContext,Bool,const int*);
int main()
{
显示*disp=0;
窗口温=0;
/*创建\u显示\u和\u窗口
-------------------------
如果已经有显示和窗口,请跳过*/
disp=XOpenDisplay(0);
win=XCreateSimpleWindow(disp,DefaultRootWindow(disp),
10,10,/*x,y*/
800,600,/*宽度,高度*/
0,0,/*边框\宽度,边框*/
0);/*背景*/
/*创建\u现代\u OpenGL\u上下文
-------------------------------- */
静态int视觉属性[]={
GLX_渲染_类型,GLX_RGBA_位,
GLX_可绘图_类型,GLX_窗口_位,
GLX_双缓冲区,对,
GLX_红色_尺寸,1,
GLX_绿色_尺寸,1,
GLX_蓝色_尺寸,1,
没有一个
};
int num_fbc=0;
GLXFBConfig*fbc=glXChooseFBConfig(disp,
默认屏幕(显示),
视觉属性和数量;
如果(!fbc){
printf(“glXChooseFBConfig()失败\n”);
出口(1);
}
/*如果我们在Windows(即WGL)上,我们需要创建一个旧的
在调用GetProcAddress()之前,此处为虚拟OpenGL上下文。这是
Linux(GLX)上不需要。
有关详细信息,请参阅规范
(https://www.khronos.org/registry/OpenGL/extensions/ARB/GLX_ARB_get_proc_address.txt)
上面说:
>函数指针是否与上下文无关?是的。指向
>扩展函数可用于任何上下文[…]
这与WGL的wglGetProcAddress形成直接对比*/
glXCreateContextAttribsARB程序glXCreateContextAttribsARB=0;
glXCreateContextAttribsARB=
(glXCreateContextAttribsARBProc)
glXGetProcAddress((const GLubyte*)“glXCreateContextAttribsARB”);
/*如果我们在Winows上,我们会破坏这里的虚拟上下文。再一次,
这在Linux上是不必要的*/
如果(!glXCreateContextAttribsARB){
printf(“未找到glXCreateContextAttribsARB()”\n);
出口(1);
}
/*设置所需的最低OpenGL版本*/
静态int上下文_attribs[]={
GLX\U上下文\U主要\U版本\U ARB,4,
GLX_上下文_次要_版本_ARB,2,
没有一个
};
/*创建现代OpenGL上下文*/
GLXContext ctx=glXCreateContextAttribsARB(disp,fbc[0],NULL,true,
上下文(属性);
如果(!ctx){
printf(“未能创建OpenGL上下文。正在退出。\n”);
出口(1);
}
/*显示窗口
--------------- */
XMapWindow(disp,win);
glXMakeCurrent(disp、win、ctx);
int主要=0,次要=0;
GLGEINTEGERV(德国劳埃德船级社主要版本和主要版本);
GLGEINTEGERV(GL_次要版本和次要版本);
printf(“已创建OpenGL上下文。\n版本%d.%d\n域%s\n描述符%s\n”,
大调,小调,
GLGETSING(GLU供应商),
glGetString(GL_渲染器));
/*应用程序循环
---------------- */
而(1){}
返回0;
}
警察说

您可以通过此查询检测上下文支持的配置文件:

glGetIntegerv(GL_CONTEXT_PROFILE_MASK,*)

这可以包含位
GL\u CONTEXT\u CORE\u PROFILE\u BIT
GL\u CONTEXT\u COMPATIBILITY\u PROFILE\u BIT
,但不能同时包含两者

但这是在查询已经创建的上下文的功能


如何使用GLX创建核心上下文?

您的
context\u attribs
应包括
GLX\u context\u PROFILE\u MASK\u ARB
参数(如本答案所示):

static int context\u attribs[]={
GLX\U上下文\U主要\U版本\U ARB,4,
GLX_上下文_次要_版本_ARB,2,
GLX\上下文\配置文件\掩码\ ARB、GLX\上下文\核心\配置文件\位\ ARB、,
没有一个
};
GLXContext ctx=glXCreateContextAttribsARB(disp,fbc[0],NULL,true,
上下文(属性);

您现在甚至没有gl loader。使用环氧树脂或glewbut
glXCreateContextAttribsARB
表示核心…或使用。所有的基础知识(除此之外还有很多)都在中进行了概述。