我无法使用 sdl2 在 opengl 中创建两个上下文

I can't create two contexts in opengl using sdl2

我需要调试 window,在其中我可以更好地观察场景可能发生的变化,并实时变化,使用 sdl2 和 opengl 3.3 我创建了第二个 window,更改了事件系统使用多个 windows 关闭 window,但是 glContext 有问题,一旦我创建了第二个上下文,就好像第一个上下文似乎存在一样,从而破坏了 [=] 之一的渲染13=],是否可以通过 sdl2 使用多个 opengl 上下文?

this->window=SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_OPENGL | (resize ? SDL_WINDOW_RESIZABLE : SDL_WINDOW_SHOWN));
SDL_GLContext windowContext=SDL_GL_CreateContext(this->window);
this->debug=SDL_CreateWindow("debug", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 600, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
SDL_GLContext debugContext=SDL_GL_CreateContext(this->debug);

这是两个 windows 具有不同上下文的示例:

#include <SDL2/SDL.h>
#include <SDL2/SDL_opengl.h>
#include <stdio.h>

int main(int argc, char **argv) {
    (void)argc, (void)argv;
    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
        fprintf(stderr, "SDL_Init error: %s\n", SDL_GetError());
        return 1;
    }
    SDL_Window *windows[2];
    SDL_GLContext contexts[2];
    for(int i = 0; i != sizeof(windows)/sizeof(windows[0]); ++i) {
        char title[32];
        snprintf(title, sizeof(title), "window%d", i);
        windows[i] = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
                640, 480, SDL_WINDOW_OPENGL);
        if(!windows[i]) {
            fprintf(stderr, "SDL_CreateWindow error: %s\n", SDL_GetError());
            return 1;
        }
        contexts[i] = SDL_GL_CreateContext(windows[i]);
        if(!contexts[i]) {
            fprintf(stderr, "SDL_GL_CreateContext error: %s\n", SDL_GetError());
            return 1;
        }
    }

    int running = 1;
    while(running) {
        SDL_Event ev;
        while(SDL_PollEvent(&ev)) {
            if(ev.type == SDL_KEYDOWN && ev.key.keysym.sym == SDLK_q) {
                running = 0;
                break;
            }
        }

        SDL_GL_MakeCurrent(windows[0], contexts[0]);
        glClearColor(1, 0, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        SDL_GL_SwapWindow(windows[0]);

        SDL_GL_MakeCurrent(windows[1], contexts[1]);
        glClearColor(0, 1, 0, 1);
        glClear(GL_COLOR_BUFFER_BIT);
        SDL_GL_SwapWindow(windows[1]);
    }

    return 0;
}

但每个 window 的单独上下文不是必需的。