[SFML] 多个OpenGL上下文

代码

#include <iostream>
#include <gl/glew.h>
#include <SFML/Graphics.hpp>
#include <windows.h>

int main()
{
    auto getInstance = []()
    {
        return (HINSTANCE)GetModuleHandle(nullptr);
    };

    auto debug = [](GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
    {
        std::cout << message << std::endl;
    };

    const char* vsource =
    "#version 330 core\n"
    "layout(location=0)in vec3 a_position;"
    "void main()"
    "{gl_Position = vec4(a_position,1.0);}";
    const char* fsource =
    "#version 330 core\n"
    "out vec4 o_color;"
    "void main()"
    "{o_color = vec4(0.0,0.0,1.0,1.0);}";
    const float ver[] =
    {
        0.0f,0.5f,0.0f,
        -0.5f,-0.5f,0.0f,
        0.5f,-0.5f,0.0,
    };

    sf::RenderWindow main_window(sf::VideoMode(640,480),"Multi Contexts");
    sf::Event main_event;

    sf::WindowHandle main_handle = main_window.getSystemHandle();
    HWND sub_window_handle_1 = CreateWindowEx(WS_EX_WINDOWEDGE,TEXT("static"),TEXT(""),WS_VISIBLE|WS_CHILD,0,0,320,480,main_handle,nullptr,getInstance(),nullptr);
    sf::RenderWindow sub_window_1(sub_window_handle_1);
    sf::ContextSettings cs(8,8,8,3,3,sf::ContextSettings::Attribute::Core|sf::ContextSettings::Attribute::Debug);
    HWND sub_window_handle_2 = CreateWindowEx(WS_EX_WINDOWEDGE,TEXT("static"),TEXT(""),WS_VISIBLE|WS_CHILD,320,0,320,480,main_handle,nullptr,getInstance(),nullptr);
    sf::RenderWindow sub_window_2(sub_window_handle_2,cs);

    sub_window_2.setActive(true);
    std::cout << (glewInit() == GLEW_OK) << std::endl;
    glDebugMessageCallback(debug,nullptr);

    GLuint vs = glCreateShader(GL_VERTEX_SHADER);
    GLuint fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(vs,1,&vsource,nullptr);
    glShaderSource(fs,1,&fsource,nullptr);
    glCompileShader(vs);
    glCompileShader(fs);
    GLuint program = glCreateProgram();
    glAttachShader(program,vs);
    glAttachShader(program,fs);
    glLinkProgram(program);
    glDeleteShader(vs);
    glDeleteShader(fs);

    GLuint vao;
    glGenVertexArrays(1,&vao);
    glBindVertexArray(vao);
    GLuint vbo;
    glGenBuffers(1,&vbo);
    glBindBuffer(GL_ARRAY_BUFFER,vbo);
    glBufferData(GL_ARRAY_BUFFER,sizeof(ver),ver,GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,3 * sizeof(float),nullptr);
    glBindVertexArray(0);
    glBindBuffer(GL_ARRAY_BUFFER,0);
    glDeleteBuffers(1,&vbo);

    //sub_window_2.setActive(false);

    sf::RectangleShape rect(sf::Vector2f(160,240));
    rect.setFillColor(sf::Color::Red);
    rect.setPosition(sf::Vector2f(80,120));

    while(main_window.isOpen())
    {
        while (main_window.pollEvent(main_event))
        {
            if(main_event.type == sf::Event::Closed)
            {
                main_window.close();
            }
            else if(main_event.type == sf::Event::KeyPressed)
            {
                if(main_event.key.code == sf::Keyboard::Key::Escape)
                {
                    main_window.close();
                }
            }
        }
        
        /*main_window.clear();
        main_window.display();*/

        sub_window_1.clear();
        sub_window_1.draw(rect);
        sub_window_1.display();

        sub_window_2.setActive(true);
        glClear(GL_COLOR_BUFFER_BIT);
        glUseProgram(program);
        glBindVertexArray(vao);
        glDrawArrays(GL_TRIANGLES,0,3);
        glBindVertexArray(0);
        sub_window_2.setActive(false);
        sub_window_2.display();
    }

    sub_window_2.setActive(true);
    glDeleteVertexArrays(1,&vao);
    glDeleteProgram(program);
    sub_window_2.setActive(false);
    return 0;
}

稍微讲一下

这里创建了一个主窗口(main_window),之后再主窗口内创建了两个小窗口(sub_window),一个是用默认上下文的,另一个是OpenGL3.3的上下文。
窗口创建可以看[C++] [SFML] 基于Win32的SFML程序

效果

在这里插入图片描述


但是最后出现了奇怪的日志,表示没法启用上下文,让人困惑

Failed to activate OpenGL context:
Failed to activate the window’s context
Failed to activate OpenGL context:
Failed to activate the window’s context
Failed to activate OpenGL context:
Failed to activate the window’s context
Failed to activate OpenGL context:
Failed to activate the window’s context
Failed to activate OpenGL context:
Failed to activate the window’s context
Failed to activate OpenGL context:
Failed to activate OpenGL context:

相关文章

学习编程是顺着互联网的发展潮流,是一件好事。新手如何学习...
IT行业是什么工作做什么?IT行业的工作有:产品策划类、页面...
女生学Java好就业吗?女生适合学Java编程吗?目前有不少女生...
Can’t connect to local MySQL server through socket \'/v...
oracle基本命令 一、登录操作 1.管理员登录 # 管理员登录 ...
一、背景 因为项目中需要通北京网络,所以需要连vpn,但是服...