[CLOSED]Loading textures on the flow

Started by
15 comments, last by Aidamina 18 years, 10 months ago
I got a problem with textureloading: I build my textureloading based on Nehe lesson 33 (Loading TGA files compressed/uncompressed) When i make my LoadGLTextures(); Call in InitGL(); This give no problems. But if i want to load textures on the flow. (Loading it while a scene is rendered it isnt working. I created a second thread called Load(); which does exactly the same call to LoadGLTextures();. But for some reason it doesnt load the textures and just shows up blank. ( Yes i checked if the thread is running!) I think it should be possible to load the textures outside the InitGL(); Call Any suggestions [Edited by - Aidamina on May 29, 2005 1:30:31 PM]
-->My Site<--
Advertisement
Can you show some code?
____________________________________________________________Programmers Resource Central
Here's the thread code

// This is the thread that is suppose to load the textures //but it isnt working(textures shown white, but return no error)//Please helpvoid Load(){		if (!LoadGLTextures())											{	status = "Textures - > Failed!"	;        return;	        }}


Here's the call to the thread
It's in WinMain()
DWORD ThreadID=1;HANDLE loadingthread  = CreateThread( NULL, 0, (LPTHREAD_START_ROUTINE)Load, NULL, CREATE_SUSPENDED, &ThreadID);			ResumeThread(loadingthread);


-->My Site<--
Found out that a direct call after the createwindowex() thingie to Load(); makes it work, but i want it to load as a thread.

Maybe the thread initialisation is wrong??
-->My Site<--
Big note that I had trouble with - multithreaded OpenGL is not recommended. IIRC using the same rendering context across multiple threads causes problems. Or at least when you're using them at the same time. You have to re-SetCurrent() in each thread, and IIRC rendering at the same time/loading textures while rendering/whatever can cause problems. I think you should think of a different approach to whatever problem you're trying to solve. There may be another solution besides loading textures on the fly.

Hope it helps!
- fyhuang [ site ]
One thing that would be helpful is to know exactly where the texture load fails. There's two parts really: Loading the texture into RAM (which never even touches the GFX card) and blitting that information to your cards memory. If your process is failing on the first part, then I would suspect a broken pointer or overwriting a global variable or some other such nonsense. If it's failing on the second part, then it could be any number of things, such as possibly calling gluBuild2DMipmaps() between calls to glBegin() and glEnd(), or trying to overwite an existing texture while it's in use.

We would need a bit more of the source code to be sure, (specifically your LoadGLTextures()) but I would do some debugging to find out exactly where the problem occurs.
// The user formerly known as Tojiro67445, formerly known as Toji [smile]
From the MSDN...
Quote:
A thread can have one current rendering context. A process can have multiple rendering contexts by means of multithreading. A thread must set a current rendering context before calling any OpenGL functions. Otherwise, all OpenGL calls are ignored.

A rendering context can be current to only one thread at a time. You cannot make a rendering context current to multiple threads.

An application can perform multithread drawing by making different rendering contexts current to different threads, supplying each thread with its own rendering context and device context.

You have to create a second opengl context for the second thread. But then, the textures won't be shared across threads. I believe there is an extension to let you share textures between contexts, but I don't know what it is offhand. You might want to try having the loading thread set up a pointer to the pixel data or something, and then have the main thread do the openGL calls.
Quote:Original post by fyhuang
There may be another solution besides loading textures on the fly.

Separate thread could probably be used for _loading/decompressing image data from file_. Once this is done, let the thread register this 'buffer' of processed data with texture manager or whatever, and have the main thread occasionally check if there's any such buffers prepared to have textures built from them. This way you can also introduce 'throttling' (the manager doesn't build more than x textures during the single check) so there isn't something like ten different threads trying to build ten different textures over the course of 1-2 frames because new object appeared in the view...
Well the texture loading and rendering at the same time is actually for a loading screen.

It renders the loading screen and the other thread loads the textures needed for the game itself, if you know what i mean. Isnt there an easy way to get arround that?

Creating a buffer isnt very efficient and a lock can occur
-->My Site<--
You could load all of the texture data while rendering things, and then when everything is loaded stop rendering and call the opengl functions that you need to create the textures.
____________________________________________________________Programmers Resource Central

This topic is closed to new replies.

Advertisement