Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

nextgengamer

OpenGL opengl multi threaded

This topic is 5737 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi All, I''ve got a small problem Ive been working on for hours trying to solve. I''m trying to render stuff in a thread other then my main one. I''m using the win32 stuff _beginthread() my thread function right now is just : void LoadBar(void *pData) { bool blocalDie = false; while(!blocalDie) { EnterCriticalSection(&cs); // Clear the color Buffer glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glBindTexture(GL_TEXTURE_2D, ((Game*)pData)->texs[1]); ((Game*)pData)->StartOrtho(); glBegin(GL_QUADS); glTexCoord2f(0.0f, 0.0f); glVertex2f(0.0f, 0.0f); glTexCoord2f(0.0f, 1.0f); glVertex2f(0.0f, 100.0f); glTexCoord2f(1.0f, 1.0f); glVertex2f(100.0f, 100.0f); glTexCoord2f(1.0f, 0.0f); glVertex2f(100.0f, 0.0f); glEnd(); ((Game*)pData)->EndOrtho(); // Swap the Buffers SwapBuffers(((Game*)pData)->Hdc); if (((Game*)pData)->percentLoaded == 100) blocalDie = true; LeaveCriticalSection(&cs); } } This exact section of code works on the normal thread but on the other thread it doesn''t render anything. I dont crash it just runs the main thread and does what it needs to do. However, when debugging I can get into this function and see that it is being called. I''m just wondering if there is anything I need to know about running a thread like this in OpenGL. I am concerend because when I use glGetFloatv(GL_COLOR_CLEAR_VALUE) in the main thread it returns 0 but in the second thread it says a non initialized number making me think the thread knows nothing about the OpenGL rendering context. Well if anyone has done this before or knows my problem I would appreciate any help. Thanks, Nextgengamer www.celestialwake.com

Share this post


Link to post
Share on other sites
Advertisement
OpenGL is a state machine. That state is maintained on a per-thread basis. If you do OpenGL rendering using a thread, then that thread must also have created the rendering context. Oh, and AFAIK, there''s no need for critical sections surrounding OpenGL calls (although your other code might need it).

Share this post


Link to post
Share on other sites
quote:

That state is maintained on a per-thread basis


Not really. States is maintained per rendering context. A thread can have several rendering contexts, and a single rendering context can be shared between several threads (not at the same time though). To render in another thread, you must deactivate the RC in the old thread, and activate it in the new one, since a RC can be active in only one thread at a time. Have a look at wglMakeCurrent.

Share this post


Link to post
Share on other sites
quote:
Original post by Brother Bob
and a single rendering context can be shared between several threads

not in windows, see msdn.

Share this post


Link to post
Share on other sites
Please show me where. It says "You cannot make a rendering context current to multiple threads", but I cannot see where it says I can''t move it between different threads.

Share this post


Link to post
Share on other sites
Guess you''re right. Just found a document on MSDN saying I can''t move a RC from one thread to another. It''s strange, cause I know I tried it before (long time ago), and I can swear I got it working. But maybe I didn''t... don''t really remember.

Anyways, if I''m wrong, I apologize.

Share this post


Link to post
Share on other sites
Thank you everyone for the responses I have worked around most of the stuff by creating new OpenGL rendering Contexts for each thread. However, I am trying to make a thread that does my loading in the background and I need to get access to my other threads context to set up the textures. I tried the wglMakeCurrent() releasing the context on the main thread and then having the secondary thread pick it up and wgl function returns to me successful but it seems I have lost anything the context had on it before.

Nextgengamer

Share this post


Link to post
Share on other sites
You should create the texture in your opengl thread. You can still load it from disk in the other thread (since that takes significantly longer than loading it to video memory)...

If I had my way, I''d have all of you shot!


codeka.com - Just click it.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!