• [SFML] 多个OpenGL上下文


    代码

    #include 
    #include 
    #include 
    #include 
    
    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;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120

    稍微讲一下

    这里创建了一个主窗口(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:

  • 相关阅读:
    写年度总结报告的注意事项
    全新UI简洁又不失美观的短视频去水印微信小程序
    NPM包本地开发
    GE IS420UCSBH1A 自动化控制模块
    闲聊系列之 5-why root cause分析法
    C++ 中的 enum关键字
    go | 切片的长度和容量
    Javaweb之Vue指令案例的详细解析
    1. Springboot集成Mybatis
    face_recognition结合opencv进行多人脸识别
  • 原文地址:https://blog.csdn.net/qq_53530146/article/details/126506726