【发布时间】:2019-05-10 14:56:17
【问题描述】:
所以我试图用 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。
附:对不起,如果代码不好我还在学习一点,也对不起我的英语,它不是我的母语,并且有这么大的例子,如果你发现伪代码有任何问题,请检查源代码和确保这是代码的问题,而不是我的伪代码表示。
【问题讨论】:
标签: c++ opengl glfw glew openglcontext