上下文共享在 opengl 中与 glfw 和 glew 不起作用,第二个 window 表现得好像上下文根本没有共享
Context sharing not working in opengl with glfw3 and glew, second window acting like the context isn't shared at all
所以我试图用 c++ 中的 glfw、glew 和 opengl 复制这个来自 glfw 的上下文共享示例:https://github.com/glfw/glfw/blob/master/examples/sharing.c。
第二个 window 除了在每个 window 上下文中独立设置的背景颜色外没有显示任何内容,所有 vao 和 vbo 以及着色器程序也在每个上下文中绑定独立地,但主要资源仅在第一个 window 的上下文(即所有 vbo、vao 和着色器程序)中生成和填充,该程序应该在第一个 window 和第二个 window 黑色背景上的红色三角形,但如前所述,第二个 window 没有显示三角形,即使绘制它的所有资源都已绑定在从第一个 window 的上下文这样做之前,代码非常大并且包含一些不必要的信息所以我做了一个伪代码表示:
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();
如果你想要完整的源代码,这里是主源文件的 link:https://gitlab.com/Error1000/MWH/blob/master/src/OpenGLTest.cpp.
P.S。对不起,如果代码不好,我还在学习,也对不起我的英语,它不是我的母语,而且有这么大的例子,如果你在伪代码中发现任何问题,也请检查源代码和确保这是代码的问题,而不是我的伪代码表示。
OpenGL 上下文共享并不是包罗万象,有些东西是不共享的。
根据经验:
实际上包含某种形式的有效负载(纹理、{vertex,pixel,element} 缓冲区对象、显示列表)的每种对象都是共享的。
每种与管理状态相关的对象(顶点数组对象、帧缓冲区对象、采样器对象)不共享。
我敢打赌,您的代码假定共享后一种对象,但不符合这一点。
所以我试图用 c++ 中的 glfw、glew 和 opengl 复制这个来自 glfw 的上下文共享示例:https://github.com/glfw/glfw/blob/master/examples/sharing.c。
第二个 window 除了在每个 window 上下文中独立设置的背景颜色外没有显示任何内容,所有 vao 和 vbo 以及着色器程序也在每个上下文中绑定独立地,但主要资源仅在第一个 window 的上下文(即所有 vbo、vao 和着色器程序)中生成和填充,该程序应该在第一个 window 和第二个 window 黑色背景上的红色三角形,但如前所述,第二个 window 没有显示三角形,即使绘制它的所有资源都已绑定在从第一个 window 的上下文这样做之前,代码非常大并且包含一些不必要的信息所以我做了一个伪代码表示:
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();
如果你想要完整的源代码,这里是主源文件的 link:https://gitlab.com/Error1000/MWH/blob/master/src/OpenGLTest.cpp.
P.S。对不起,如果代码不好,我还在学习,也对不起我的英语,它不是我的母语,而且有这么大的例子,如果你在伪代码中发现任何问题,也请检查源代码和确保这是代码的问题,而不是我的伪代码表示。
OpenGL 上下文共享并不是包罗万象,有些东西是不共享的。
根据经验:
实际上包含某种形式的有效负载(纹理、{vertex,pixel,element} 缓冲区对象、显示列表)的每种对象都是共享的。
每种与管理状态相关的对象(顶点数组对象、帧缓冲区对象、采样器对象)不共享。
我敢打赌,您的代码假定共享后一种对象,但不符合这一点。