Sign in to follow this  
Aidamina

[CLOSED]Loading textures on the flow

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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 help

void 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);


Share this post


Link to post
Share on other sites
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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
You would just need to set a boolean to true when the loading has finished and check that every frame in the rendering code, and exit the rendering thread if the boolean is true. Then you will be able to create the textures as normal. You might want to pause after finishing loading and creating the textures (using a normal loop) to give the rendering thread time to finish.

Share this post


Link to post
Share on other sites
Just load the image data into an array as you normally do before you do the following (or something similar)

glGenTextures(1, &texture[0]);
glBindTexture(GL_TEXTURE_2D, texture[0]);

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX,
TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data);


glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR)


If you don't know how to load the texture data you can look here and here.
Hope this helps.

Share this post


Link to post
Share on other sites
If you are just doing a loading sceen, you probably don't care about constantly redrawing the sceen. You could just do this:

for (each item that has to be loaded) {
load_item();
draw_loading_sceen(progress);
}

why waste cpu cycles when constant redrawing is not needed?

Share this post


Link to post
Share on other sites
Its not a loading screen its a loading scene.
It renders a opengl scene while loading.
Later i hope to extend this thread to make the textures load dynamically if their needed, without any loading times/screens, while the game is runnings.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this