So i was trying to replicate this example of context sharing from glfw with glfw, glew and opengl in c++: https://github.com/glfw/glfw/blob/master/examples/sharing.c.
The second window isn't showing anything except the background color which was set independently in each window's context, all the vao's and vbo's and shader program's were also bound in each context independently, but the main resources were generated and populated only in the first window's context( that beign all the vbo's, vao's and shader programs), this program is supposed to show a red triangle on a white background in the first window and a red triangle on a black background on the second window, but as stated before the second window isn't showing the triangle even though all the resources to draw it were bound before doing so from the first window's context, the code is quite big and contains some unnecessary info so i made a pseudo code representation:
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();
If you want the full source here's the link to the main source file: https://gitlab.com/Error1000/MWH/blob/master/src/OpenGLTest.cpp.
P.S. Sorry if the code is bad I'm still learning a bit, also sorry for my English, it's not my native language and for having such a large example, also if you find any problems in the pseudo code, please also check the source and make sure that it's a problem with the code and not my pseudo code representation.
OpenGL context sharing is not encompassing everything, some things are not shared.
As a rule of thumb:
every kind of object that actually holds some form of payload (textures, {vertex,pixel,element} buffer objects, display lists) are shared.
every kind of object that's concerned with managing state (vertex array objects, framebuffer objects, sampler objects) are not shared.
I'd wager your code is assuming the sharing of the later kind of objects and falls over that.