上下文共享在带有glfw3和glew的opengl中不起作用,根本不共享像上下文一样的第二个窗口

时间:2018-12-09 14:22:31

标签: c++ opengl glfw glew openglcontext

因此,我试图用c ++在https://github.com/glfw/glfw/blob/master/examples/sharing.c中用glfw,glew和opengl复制来自glfw的上下文共享示例。

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

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

P.S。抱歉,如果代码不好,我仍在学习,也要对我的英语感到抱歉,这不是我的母语,并且有这么大的示例,如果您在伪代码中发现任何问题,请同时检查源代码和确保这是代码问题,而不是我的伪代码表示形式。

1 个答案:

答案 0 :(得分:1)

OpenGL上下文共享不包含所有内容,某些内容不共享。

根据经验:

  • 实际上包含某种形式的有效负载的每种对象(纹理,{顶点,像素,元素}缓冲对象,显示列表)都可以共享。

  • 与状态管理相关的每种对象(顶点数组对象,帧缓冲对象,采样器对象)均共享。

我敢打赌,您的代码是假定共享后一种对象,并且落在那上面。