因此,我试图在 c++ 中使用 glfw、glew 和 opengl 从 glfw 复制这个上下文共享示例:https ://github.com/glfw/glfw/blob/master/examples/sharing.c 。
除了在每个窗口的上下文中独立设置的背景颜色外,第二个窗口不显示任何内容,所有 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 。
PS对不起,如果代码不好我还在学习一点,也对不起我的英语,它不是我的母语并且有这么大的例子,如果你发现伪代码有任何问题,也请检查源并确保这是代码问题,而不是我的伪代码表示。