我正在尝试在 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 生成的函数的第一次调用。这里出了点问题,但我似乎无法弄清楚是什么以及为什么。
谁能指出我正确的方向?