Skip to main content
GameDev.net gamedev.net
🔒 Locked

[ Resolved ] how to load a texture in a thread ?

Started by cbastien Jun 18, 2007 at 6:34 AM 4 replies 700+ views
Original Post
cbastien
cbastien
hello, My goal is to have two threads. One to handle the main program, and the other one to load textures. But i fail to apply the texture i had loaded to the triangle i draw. here the first thread :

		glutInit(&iii, c);
		glutInitDisplayMode(GLUT_DEPTH | GLUT_DOUBLE | GLUT_RGBA);
		glutInitWindowPosition(100,100);
		glutInitWindowSize(320,320);
		glutCreateWindow("test");	

		glutDisplayFunc(window_display);
		glutIdleFunc(window_display);
		glutReshapeFunc(window_reshape);
		glutMainLoop();	



and the second thread :

GLubyte Texture[16] =
{
	0xCC,0xCC,0xCC,0xCC, 0xCC,0xCC,0xCC,0xCC,
		0xCC,0xCC,0xCC,0xCC, 0xCC,0xCC,0xCC,0xCC
};

	glClearColor(.5,.5,.5,0);  	//Change la couleur du fond
	glEnable(GL_DEPTH_TEST); 	//Active le depth test
	glEnable(GL_TEXTURE_2D); 	//Active le texturing

	glGenTextures(1, &g_Texture[num_texture] );
	
        glBindTexture( GL_TEXTURE_2D, g_Texture[num_texture] );

	glTexImage2D (
		GL_TEXTURE_2D, 	//Type : texture 2D
		0, 	//Mipmap : aucun
		4, 	//Couleurs : 4
		2, 	//Largeur : 2
		2, 	//Hauteur : 2
		0, 	//Largeur du bord : 0
		GL_RGBA, 	//Format : RGBA
		GL_UNSIGNED_BYTE, 	//Type des couleurs
		Texture 	//Addresse de l'image
		); 	
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);



if i put everything in the same thread, the program work well, but when i put the texture loading's part in the second thread, the triangle stay white, i suppose there is a problem on how to share the texture. Thanks in advance for your help ! [Edited by - cbastien on June 18, 2007 7:49:35 AM]
Brother Bob
Brother Bob
The rendering contex can only be active in one thread at a time, which is by default the one you create it in. Therefore, your second thread doesn't have an active rendering context, and so any GL-call is ignored.
cbastien
cbastien
ok, so the solution is just to load the texture in the second thread and to apply it in the first thread (the one with the glcontext) ?


i mean, in the second thread i just call the dds image load function ( i use *.dds texture). i will try that. hope i'll keep a good framerate with this method.


thx !
nmi
nmi
In the main thread (the one with an opengl context) create the textures, but do not upload the data (set it to null). Then map those textures into memory. Hand over the memory location to the second thread (texture loader thread). This needs to be synchronized. The texture thread can then just write the texture data into the memory mapped texture. When it is finished, the main thread is notified to unmap the texture. This needs to be synchronized as well.
cbastien
cbastien
yeah, that was exactly what i had in mind to avoid to have a white texture during the loading of the texture.

Apply the texture when the loading is finished, thanks to synchronization.


thank you
cbastien
cbastien
hi again,

i made a little program to test everything : a spin traingle !

when you push a key it load a texture and apply it to the triangle.

mono-thread program : the triangle stop rotating during one second, the time the computer need to download the 11Mb texture.

two-thread program : it is perfect !



thanks again Brother Bob and nmi !

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.