首页 文章

上下文共享不能在opengl中使用glfw3和glew,第二个窗口就像上下文一样根本不共享[暂停]

提问于
浏览
0

所以我试图从glfw复制这个上下文共享的例子,在c:https://github.com/glfw/glfw/blob/master/examples/sharing.c中使用glfw,glew和opengl .

除了在每个窗口的上下文中独立设置的背景颜色之外,第二个窗口没有显示任何内容,所有vao和vbo以及着色器程序也独立地绑定在每个上下文中,但主要资源仅在第一个窗口中生成和填充上下文(所有的vbo,vao和着色器程序),这个程序应该在第一个窗口的白色背景上显示一个红色三角形,在第二个窗口的黑色背景上显示一个红色三角形,但如前所述窗口没有显示三角形,即使从第一个窗口的上下文开始绘制它的所有资源都绑定了,代码很大并且包含一些不必要的信息所以我做了一个伪代码表示:

Initialize glfw

Make win and win2 objects

Set win's context to opengl
Create window of win with the parameters that were set
Make opengl use win's context ( glfwMakeContextCurrent(win) )

Initialize and handle glew

//In the win opengl context

//Triangle shape
/// Note: I don't use indicies for this since it's overkill

GLuint vertArrayID;
glGenVertexArrays(1, &vertArrayID);
glBindVertexArray(vertArrayID);

//Triangle position data
static const GLfloat vertex_positions_data[] = {
    -1.0f, -1.0f,
    0.0f, 1.0f,
    1.0f, -1.0f,
};

GLuint vertexPositionBufferID;
glGenBuffers(1, &vertexPositionBufferID);
glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBufferID);
glBufferData(GL_ARRAY_BUFFER, sizeof(vertex_positions_data), vertex_positions_data, GL_STATIC_DRAW);


glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*) 0);

// Shaders
const std::string vs = std::string("#version 330 core\n") +
                       std::string("layout(location = 0) in vec2 vertPos;\n")+
                       std::string("\n")+
                       std::string("void main(){\n")+
                       std::string("gl_Position = vec4(vertPos, 1, 1);\n")+
                       std::string("}\n");

const std::string fs = std::string("#version 330 core\n") +
                       std::string("out vec3 color;\n")+
                       std::string("\n")+
                       std::string("void main(){\n")+
                       std::string("color = vec3(1, 0, 0);\n")+
                       std::string("}\n");

GLuint programID = loadVertexAndFragmentShaders(vs, fs); // Compile link and create the program from the shaders

glUseProgram(programID);

glClearColor(255, 255, 255, 255);

//------------------------------------------------------------------------------------------------------------------

Set win2's context to opengl
Set win2 to share it's context with win
Create the window of win2 with the parameters that were set
Make opengl use win2's context ( glfwMakeContextCurrent(win2) )


//In the win2 opengl context that dosen't have anything bound but has all the data that is shared from win's context ( i think )
//------------------------------------------------------------------------------------------------------------------ 
glBindVertexArray(vertArrayID); // Here was were i discovered the error thanks to the approved answer ( vao's don't get shared between contexts )

glBindBuffer(GL_ARRAY_BUFFER, vertexPositionBufferID);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, (void*) 0);

glUseProgram(programID);

glClearColor(0, 0, 0, 255);
//------------------------------------------------------------------------------------------------------------------

Render win and win2 by using glDrawArrays while on their respective context until both windows are closed

glfwTerminate();

如果你想在这里找到完整的源代码,那就是主源文件的链接:https://gitlab.com/Error1000/MWH/blob/master/src/OpenGLTest.cpp .

附:对不起,如果代码不好,我还在学习一点,对不起我的英语,这不是我的母语,并且有这么大的例子,如果你在伪代码中发现任何问题,请同时查看来源和确保它是代码的问题,而不是我的伪代码表示 .

1 回答

  • 1

    OpenGL上下文共享并不包含所有内容,有些内容不是共享的 .

    根据经验:

    • 实际拥有某种形式的有效载荷(纹理,{顶点,像素,元素}缓冲区对象,显示列表)的每种对象都是共享的 .

    • 与管理状态(顶点数组对象,帧缓冲对象,采样器对象)有关的每种对象都不会被共享 .

    我打赌你的代码假设共享后来的那种对象并且落空了 .

相关问题