我正在尝试在Linux中设置一个使用OpenGL 3.2核心配置文件和GLEW的非常基本的程序。我在article的帮助下尝试了这一点。
这是我的代码:
#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生成的函数的第一次调用。这里有些不对劲,但我似乎无法弄清楚是什么以及为什么。
有人能指出我正确的方向吗?
答案 0 :(得分:1)
glew.h
已包含gl.h
且GLX可用glx.h
...然后使用一些宏魔术来弯曲某些符号。我建议你删除这些行
#include <GL/gl.h>
#include <GL/glx.h>
然后使用
#include <GL/glew.h>
在GLEW包含原始包含后,一些宏魔法可能会丢失,并最终导致错误关联的符号导致崩溃。
旁注:为什么要包含GLFW和GLUT标题?如果您打算使用裸GLX,那么您不需要它们,也不应该包含它们。
答案 1 :(得分:1)
发现问题,其解决方案似乎是在调用glewInit()之前添加'glewExperimental'= GL_TRUE;