[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:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值