3

我正在尝试在 Linux 中使用 OpenGL 3.2 核心配置文件和 GLEW 设置一个非常基本的程序。我在这篇文章的帮助下试过了。

这是我的代码:

#define GLEW_STATIC
#include <iostream>
#include <cstdio>
#include <string>
#include <GL/glew.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#include <GL/gl.h>
#include <GL/glx.h>

//#include <GL/glut.h>
#include "stb_image_write.h"
#include <cstdlib>
#include <GL/glfw.h>


static float _viewPortHeight = 30;
static float _viewPortWidth = 10;

typedef GLXContext (*glXCreateContextAttribsARBProc)(Display*, GLXFBConfig, GLXContext, Bool, const int*);
typedef Bool (*glXMakeContextCurrentARBProc)(Display*, GLXDrawable, GLXDrawable, GLXContext);
static glXCreateContextAttribsARBProc glXCreateContextAttribsARB = NULL;
static glXMakeContextCurrentARBProc   glXMakeContextCurrentARB   = NULL;

int main (int argc, char *argv[]){
    glXCreateContextAttribsARB = (glXCreateContextAttribsARBProc) glXGetProcAddressARB( (const GLubyte *) "glXCreateContextAttribsARB" );
    glXMakeContextCurrentARB   = (glXMakeContextCurrentARBProc)   glXGetProcAddressARB( (const GLubyte *) "glXMakeContextCurrent");
    Display *display = XOpenDisplay(NULL);
    if (display == NULL){
        std::cout  << "error getting the X display";
        return -1;
     }

    static int visualAttribs[] = {None};
    int numberOfFrameBufferConfigurations;
    GLXFBConfig *fbConfigs = glXChooseFBConfig(display, DefaultScreen(display), visualAttribs, &numberOfFrameBufferConfigurations);

     int context_attribs[] = {
          GLX_CONTEXT_MAJOR_VERSION_ARB ,3,
          GLX_CONTEXT_MINOR_VERSION_ARB, 2,
          GLX_CONTEXT_FLAGS_ARB, GLX_CONTEXT_DEBUG_BIT_ARB,
          GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_CORE_PROFILE_BIT_ARB,None
     };

    std::cout << "initialising context...";
    GLXContext openGLContext = glXCreateContextAttribsARB(display, fbConfigs[0], 0, True, context_attribs);

    int pBufferAttribs[] = {
        GLX_PBUFFER_WIDTH, 32,
        GLX_PBUFFER_HEIGHT, 32,
        None
    };

    GLXPbuffer pbuffer = glXCreatePbuffer(display, fbConfigs[0], pBufferAttribs);
    XFree(fbConfigs);
    XSync(display, False);
    if(!glXMakeContextCurrent(display, pbuffer, pbuffer, openGLContext)){
        std::cout << "error with content creation";
        return -1;
    }
    glXMakeCurrent(display, pbuffer, openGLContext);

    GLenum error = glewInit();
    if (error != GLEW_OK){
       std::cout << "error with glew init()\n";
    }else{
        std::cout << "glew is ok\n\n";
    }
    GLuint test;
    GLuint framebuffer;
    glGenFramebuffers(1, &framebuffer);

    glClearColor(1.0f, 0.0f, 0.0f, 1.0f);
    glClear(GL_COLOR_BUFFER_BIT);

    std::string path("output.png");
    exportToPath(path);

    return 0;
 }

我得到的输出是这样的:

初始化上下文... glew 没问题

分段错误(核心转储)

导致问题的行在glGenFrameBuffers调用处,这也是对 GLEW 生成的函数的第一次调用。这里出了点问题,但我似乎无法弄清楚是什么以及为什么。

谁能指出我正确的方向?

4

2 回答 2

1

glew.h已经包含gl.h并提供 GLX glx.h......然后使用一些宏魔法来弯曲一些符号。我建议你删除这些行

#include <GL/gl.h>
#include <GL/glx.h>

只需使用

#include <GL/glew.h>

在 GLEW 包含原始包含之后,一些宏魔法可能会丢失,最终您会得到一个错误关联的符号,从而导致崩溃。


附带说明:为什么要包含 GLFW 和 GLUT 标头?如果您打算使用裸 GLX,那么您不需要它们也不应该包括它们。

于 2014-02-18T19:30:18.167 回答
1

发现了问题,其解决方案似乎是在调用 glewInit(); 之前添加 'glewExperimental' = GL_TRUE;

于 2014-02-24T16:53:30.333 回答