上下文创建失败

凯文

我正在学习OpenGL,所以我正在尝试绘制2D图像。首先,将所有内容放入C ++程序的主要功能中。工作正常(绘制了2个三角形)。

我决定通过制作单独的类来使程序更清晰,但是...现在它不再起作用了。当我要制作OpenGL上下文时,它会失败。当我显示错误时,我得到:

Failed creating OpenGL context at version requested

在本教程中,我正在阅读,他们说这个错误最有可能是由于您的图形卡不支持OpenGL版本,但是如果是这种情况,那么当我将所有代码都放入其中时,它将不起作用主要程序。

这是出错的部分(上下文创建):

bool OpenGL_Scene::initializeWindow() {
    // Initialize the SDL
    if(SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        cout << "Error while initializing the SDL : " << SDL_GetError() << endl;
        SDL_Quit();

        return false;
    }

    // Configure OpenGL

    // Use version 3.1
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); // OpenGL 3.x
    SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); // OpenGL x.1 --> OpenGL 3.1

    // Double buffering
    SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Use double buffering (0 to not use it)
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);  // Buffer has a depth of 24 bits

    // Make the window
    this->window = SDL_CreateWindow(this->windowTitle.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, this->windowWidth, this->windowHeight, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL); // SDL_WINDOW_OPENGL necessary to specify that the window will have an OpenGL context attached to it.

    if(this->window == 0) // Initialization failed
    {
        cout << "Error while creating the window : " << SDL_GetError() << endl;
        SDL_Quit();

        return false;
    }

    // Make the OpenGL context given the SDL window
    this->OpenGL_Context = SDL_GL_CreateContext(this->window);

    // Make sure the creation of the context succeeded. If not the problem is probably that the version of OpenGL isn't supported by the graphics card.
    if(this->OpenGL_Context == 0)
    {
        cout << "Could not create the OpenGL context : " << SDL_GetError() << endl;

        SDL_DestroyWindow(window);
        SDL_Quit();
        return false;
    }

    return true;
};

在最后几行中显示错误,因此程序导致:

Could not create the OpenGL context : Failed creating OpenGL context at version requested

我进行了很多搜索以找到解决方案,然后找到了这个:SO主题

因此,在上面的代码中,我尝试了:

// Configure OpenGL

// Use version 3.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); // OpenGL 3.x
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); // OpenGL x.1 --> OpenGL 3.1

// Double buffering
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);      // ADDING THIS AVOIDS THAT THE CONTEXT COULD NOT BE CREATED BUT THEN WHEN WE DRAW SOMETHING WE DON'T SEE ANYTHING

SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Use double buffering (0 to not use it)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);  // Buffer has a depth of 24 bits

现在,当我使用此额外的行运行程序时,将创建上下文(没有错误OpenGL_Context != 0),但是没有绘制任何内容我省略了我正在绘制的代码部分,因为该代码以前可以工作,并且我没有做任何更改。

有人知道可能是什么问题吗?

PS:我正在使用Macbook Pro(OS X Yosemite(10.10.4)),并且我的图形卡是NVIDIA GeForce GT 650M 1024 MB

编辑:我尝试调试绘制三角形的代码,但我确实看不到错误(主要是因为我真的是OpenGL的新手)。在下面可以找到绘图代码。请注意,由于不再需要,我不再初始化GLEW(根据评论)。

void OpenGL_Scene::mainLoop() {
    bool end = false;

    // Make vertices (punten) in a table
    // !!! WARNING : Use 1 table for ALL vertices !!! Don't use a separate table for each of the forms, this would slow down the program because you have to send each of the tables to OpenGL !!!

    float vertices[] = {-0.5, -0.5,   0.0, 0.5,   0.5, -0.5,      // 3 Points for first triangle --> (-0.5, -0.5) , (0.0, 0.5) and (0.5, -0.5)  (All in (x, y) --> 2D)
                    -0.8, -0.8,   -0.3, -0.8,   -0.8, -0.3};  // 3 Points for second triangle

    // Before we start drawing, clear the screen
    glClear(GL_COLOR_BUFFER_BIT);

    glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, vertices);
    glEnableVertexAttribArray(0); // Activate the table we passed to OpenGL using the identifier (index) passed to OpenGL (in this case 0)

    // Now that OpenGL knows which vertices it has to display we are going to specify what it has to do with these vertices
    glDrawArrays(GL_TRIANGLES, 0, 6); // Draw both triangles

    glDisableVertexAttribArray(0);   // Because it isn't necessary anymore
    SDL_GL_SwapWindow(this->window); // Refresh the screen

    while(!end) {
        // Listen to events and play with them
        SDL_WaitEvent(&this->events); // Will wait for an event and assign it to "events" variable

        if(this->events.window.event == SDL_WINDOWEVENT_HIDDEN)
            cout << "The user has hidden the window !" << endl;
        else if(this->events.window.event == SDL_WINDOWEVENT_CLOSE) {
            cout << "The user closed the window !" << endl;

            glClear(GL_COLOR_BUFFER_BIT); // Clear the screen
            SDL_GL_SwapWindow(this->window);    // Refresh the window

            end = true;
        }  
    }
    // Quitting the SDL and OpenGL properly is done by the destructor
}

因此,现在执行项目时,首先创建窗口(成功),然后调用mainLoop过程(从上方)。我使用调试器跨过绘图代码,所有过程都被调用(glClear,...),但是窗口中什么都没有出现(它保持黑色)。

凯文

我发现了错误。在初始化SDL之后,我正在初始化OpenGL,这就是为什么在不强制进入“ Core Profile”的情况下无法创建上下文的原因。

“ initializeWindow”中的正确顺序为:

// Configure OpenGL

// Use version 3.1
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); // OpenGL 3.x
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); // OpenGL x.1 --> OpenGL 3.1

// Double buffering
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); // Use double buffering (0 to not use it)
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);  // Buffer has a depth of 24 bits

// Initialize the SDL
if(SDL_Init(SDL_INIT_VIDEO) < 0)
{
    cout << "Error while initializing the SDL : " << SDL_GetError() << endl;
    SDL_Quit();

    return false;
}

这样做可以成功创建上下文。

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

上下文初始化失败

来自分类Dev

IWebDriver的Specflow上下文注入失败

来自分类Dev

Kubernetes部署因上下文而失败

来自分类Dev

IWebDriver的Specflow上下文注入失败

来自分类Dev

GLFW无法创建窗口:“ GLX:创建上下文失败:GLXBadFBConfig”

来自分类Dev

PyOpenCL:gl共享上下文创建失败(溢出错误)

来自分类Dev

@MockBean似乎重新运行上下文的创建和失败afterMigrate.sql

来自分类Dev

由于属性文件位置类型,应用程序或集成测试 Spring 上下文创建失败

来自分类Dev

Spark 创建新的 Spark 会话/上下文并从失败中恢复

来自分类Dev

GLFW无法创建4.3上下文

来自分类Dev

创建ThreadLocal EF上下文

来自分类Dev

在函数内创建RSpec上下文

来自分类Dev

如何创建上下文无关的语法?

来自分类Dev

在特定上下文中创建资源

来自分类Dev

创建动态中心上下文

来自分类Dev

创建ThreadLocal EF上下文

来自分类Dev

点燃上下文创建时出错

来自分类Dev

失败-上下文路径中已部署的应用程序/但上下文无法启动

来自分类Dev

isGooglePlayServicesAvailable的上下文(上下文上下文)

来自分类Dev

创建不属于Code First上下文上下文的表的外键

来自分类Dev

Spock:如何获取失败测试的上下文?

来自分类Dev

Swift受管对象上下文保存失败

来自分类Dev

Azure App Service(Mobile)上的db上下文失败

来自分类Dev

来自EntityFramework上下文的SaveChanges()静默失败

来自分类Dev

严重:由于先前的错误,上下文[/ example]启动失败

来自分类Dev

由于财产非法价值而失败:上下文

来自分类Dev

上下文初始化失败的春天

来自分类Dev

具有多个上下文的Spark作业失败

来自分类Dev

上下文初始化失败Spring MVC错误

Related 相关文章

热门标签

归档