gpt4 book ai didi

c++ - EGL 在第一次 opengl 函数调用时崩溃

转载 作者:搜寻专家 更新时间:2023-10-31 01:31:35 24 4
gpt4 key购买 nike

我在带有最新驱动程序的 arch linux 上使用 nvidia 1060 gtx 卡。但是 ELG 根本不起作用,实际上它在第一次调用 opengl 函数时崩溃,例如 glGenBuffers()。使用 GLFW 创建上下文时一切正常。但是这里需要不依赖于 x-server 的存在。

以下代码是使用编译的:gcc testegl.cpp -lGLEW -lGL -lEGL_nvidia -lEGL -lstdc++ -o testegl

输出是:

  • 开始...
  • 段错误(核心已转储)

testegl.cpp:

#include <EGL/egl.h>
#include <GL/glew.h>
#include <exception>
#include <string>
#include <iostream>

class GLEGLContext {
private:
EGLDisplay mDisplay;
EGLSurface mSurface;
EGLContext mContext;

public:
GLEGLContext ();
~GLEGLContext ();
GLEGLContext (const GLEGLContext& ) = delete;
GLEGLContext& operator = (const GLEGLContext&) = delete;

public:
virtual void makeCurrent ();
virtual void releaseCurrent ();
};

GLEGLContext::GLEGLContext () {
const EGLint configAttribs[] = {
EGL_SURFACE_TYPE, EGL_PBUFFER_BIT,
EGL_BLUE_SIZE, 8,
EGL_GREEN_SIZE, 8,
EGL_RED_SIZE, 8,
EGL_DEPTH_SIZE, 0,
EGL_RENDERABLE_TYPE, EGL_OPENGL_BIT,
EGL_NONE
};

const EGLint pbufferAttribs[] = {
EGL_WIDTH, 1920,
EGL_HEIGHT, 1080,
EGL_NONE,
};

mDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
if (mDisplay == EGL_NO_DISPLAY)
throw StdException(__PRETTY_FUNCTION__, "eglGetDisplay() failed");

EGLint major, minor;
if (!eglInitialize(mDisplay, &major, &minor))
throw StdException(__PRETTY_FUNCTION__, "eglInitialize() failed");

EGLint numConfigs;
EGLConfig eglCfg;
if (!eglChooseConfig(mDisplay, configAttribs, &eglCfg, 1, &numConfigs))
throw StdException(__PRETTY_FUNCTION__, "eglChooseConfig() failed");

mSurface = eglCreatePbufferSurface(mDisplay, eglCfg, pbufferAttribs);
if (mSurface == EGL_NO_SURFACE)
throw StdException(__PRETTY_FUNCTION__, "eglCreatePbufferSurface() failed");

if (!eglBindAPI(EGL_OPENGL_API))
throw StdException(__PRETTY_FUNCTION__, "eglBindAPI() failed");

mContext = eglCreateContext(mDisplay, eglCfg, EGL_NO_CONTEXT, NULL);
if (mContext == EGL_NO_CONTEXT)
throw StdException(__PRETTY_FUNCTION__, "eglCreateContext() failed");

makeCurrent();
}

GLEGLContext::~GLEGLContext () {
eglTerminate(mDisplay);
}

void GLEGLContext::makeCurrent () {
if (!eglMakeCurrent(mDisplay, mSurface, mSurface, mContext))
throw StdException(__PRETTY_FUNCTION__, "eglMakeCurrent() create failed");
}

void GLEGLContext::releaseCurrent () {
if (!eglMakeCurrent(mDisplay, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT))
throw StdException(__PRETTY_FUNCTION__, "eglMakeCurrent() release failed");
}

int main(int argc, char** argv) {
GLEGLContext ctx;
ctx.makeCurrent();
std::cout << "Starting..." << std::endl;
GLuint vertexBuffer = 0;
glGenBuffers(1, &vertexBuffer);
std::cout << "done" << std::endl;
ctx.releaseCurrent();

return 0;
}

最佳答案

你必须初始化GLEW .调用glewInit在您创建 OpenGL 上下文之后:

if ( glewInit() != GLEW_OK )
return;

请注意,glewInit如果成功则返回 GLEW_OKglewInit为 OpenGL 函数初始化函数指针。如果您尝试通过未初始化的函数指针调用函数,则会发生段错误。

另请参阅 Undefined reference when using glew and mingw? 的答案

关于c++ - EGL 在第一次 opengl 函数调用时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45482422/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com