opengl multi threaded

Started by
6 comments, last by nextgengamer 21 years, 5 months ago
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
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).

Kippesoep
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.
quote:Original post by Brother Bob
and a single rendering context can be shared between several threads

not in windows, see msdn.
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.
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.
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
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.

This topic is closed to new replies.

Advertisement