Downloading GLFW 3.0 Resources from OpenGL

I began to penetrate the slightly overwhelming scene of loading OpenGL resources on a separate thread, so the main thread can continue to render the object. Upon entering, I noticed that GLFW released an updated version a month ago with easier context management.

However, with glfwMakeContextCurrent (), I could not make this possible. In the boot thread, I use this function, and after its completion, I add it again, so the main thread then gets the context for future use. This does not allow me to create and compile shaders or any other OpenGL-related creatures.

UPDATED:

What needs to be done so that I can use GLFW in this situation? Since GLFW is portable, I would like to use code that includes it. I do not know the necessary steps to prepare a thread that supports the GLFW API.

Like this blog post, I need to create two threads with an OpenGL context (not the same context; D), and then share the information. However, the instructions given are platform specific. How can I use GLFW so that the steps in the example are as platform-independent as possible?

+8
multithreading opengl glfw
source share
1 answer

Use the share parameter on glfwCreateWindow() :

 #include <GL/glew.h> #include <GLFW/glfw3.h> #include <chrono> #include <thread> #include <atomic> // reload shared VBO with random data every second void MyThread( GLFWwindow* win, GLuint vbo, std::atomic< bool >& running ) { glfwMakeContextCurrent( win ); glewInit(); while( running ) { float temp[ 512 ]; for( size_t i = 0; i < 512; i+=2 ) { temp[ i+0 ] = static_cast< float >( rand() % 600 ); temp[ i+1 ] = static_cast< float >( rand() % 600 ); } glBindBuffer( GL_ARRAY_BUFFER, vbo ); glBufferData( GL_ARRAY_BUFFER, sizeof( float ) * 512, temp, GL_DYNAMIC_DRAW ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); // oddly important, might need to be glFinish() glFlush(); std::this_thread::sleep_for( std::chrono::milliseconds( 1000 ) ); } } int main( int argc, char** argv ) { if( !glfwInit() ) return -1; glfwWindowHint( GLFW_VISIBLE, GL_FALSE ); GLFWwindow* threadWin = glfwCreateWindow( 1, 1, "Thread Window", NULL, NULL ); glfwWindowHint( GLFW_VISIBLE, GL_TRUE ); GLFWwindow* window = glfwCreateWindow( 600, 600, "Hello World", NULL, threadWin ); glfwMakeContextCurrent( window ); glewInit(); // load shared VBO with dummy data float temp[ 512 ] = { 0 }; GLuint vbo; glGenBuffers( 1, &vbo ); glBindBuffer( GL_ARRAY_BUFFER, vbo ); glBufferData( GL_ARRAY_BUFFER, sizeof( float ) * 512, temp, GL_DYNAMIC_DRAW ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); std::atomic< bool > running( true ); std::thread aThread( MyThread, threadWin, vbo, std::ref( running ) ); while( !glfwWindowShouldClose( window ) ) { glfwPollEvents(); glClear( GL_COLOR_BUFFER_BIT ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glOrtho( 0, 600, 0, 600, -1, 1 ); glMatrixMode( GL_MODELVIEW ); glLoadIdentity(); glEnableClientState( GL_VERTEX_ARRAY ); glBindBuffer( GL_ARRAY_BUFFER, vbo ); glVertexPointer( 2, GL_FLOAT, 0, 0 ); glColor3ub( 255, 0, 0 ); glDrawArrays( GL_LINES, 0, 256 ); glBindBuffer( GL_ARRAY_BUFFER, 0 ); glDisableClientState( GL_VERTEX_ARRAY ); std::this_thread::sleep_for( std::chrono::milliseconds( 10 ) ); glfwSwapBuffers( window ); } running = false; aThread.join(); glfwTerminate(); return 0; } 
+16
source share

All Articles