【问题标题】:Context creation fails上下文创建失败
【发布时间】:2015-07-21 12:12:29
【问题描述】:

我正在学习 OpenGL,所以我正在尝试绘制 2D 图像。 我首先将所有内容都放在我的 C++ 程序的 main 函数中。效果很好(绘制了 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 topic

所以在上面的代码中我尝试了:

// 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,因为它不是必需的(根据 cmets)。

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,...),但窗口中没有出现任何内容(它保持黑色)。

【问题讨论】:

  • 它是否适用于任何其他版本?尝试 3.2 或 3.3。 OpenGL 3.1 是一个“特殊”的 OpenGL 版本,它从 OpenGL 3.0 规范中删除了许多已弃用的功能。
  • 我尝试了 3.2 和 3.3,但它们显示出同样的问题
  • 您添加到代码中的行强制在“核心配置文件”中创建 OpenGL 上下文。默认情况下,可能没有选择“已弃用的配置文件”行,但您的驱动程序无法创建已弃用的配置文件。但是,我不知道为什么您的新代码不起作用。尝试在别处搜索错误。
  • @stgatilov 谢谢!由于代码一直运行到上下文创建,我正在寻找上下文创建之前的部分中的错误。但是在initializeWindow 被调用之前发生的唯一事情是我的“OpenGL_Scene”类的构造函数。所以我没有得到它可能是错误的地方,因为我的构造函数非常简单:OpenGL_Scene::OpenGL_Scene(string windowTitle, int windowHeight, int windowWidth):windowTitle(windowTitle), windowHeight(windowHeight), windowWidth(windowWidth) {};
  • this。这就是你必须/应该使用 GLEW 的原因。

标签: c++ opengl


【解决方案1】:

我发现了错误。我在初始化 SDL 后正在初始化 OpenGL,这就是为什么如果不强制进入“核心配置文件”就无法创建上下文的原因。

“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;
}

这样就成功创建了上下文。

【讨论】:

    猜你喜欢
    • 2011-07-13
    • 2013-02-25
    • 1970-01-01
    • 1970-01-01
    • 2020-07-15
    • 2013-01-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-05
    相关资源
    最近更新 更多