Sign in to follow this  

O' LoadGLTextures() where art thou?

This topic is 4862 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

In NeHe tutorial #6, NeHe presents us with something quite like the following:
int LoadGLTextures()										// Load Bitmaps And Convert To Textures
{
	int Status=FALSE;										// Status Indicator
	AUX_RGBImageRec *TextureImage[3];						// Create Storage Space For The Texture
	memset(TextureImage,0,sizeof(void *)*3);				// Set The Pointer To NULL

	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
	if ((TextureImage[0]=LoadBMP("Playership2.bmp")) &&
		(TextureImage[1]=LoadBMP("Playership1_mask.bmp")) &&
		(TextureImage[2]=LoadBMP("stars.bmp")))
	{
		Status=TRUE;										// Set The Status To TRUE
		glGenTextures(3, &texture[0]);						// Create The Texture

		for (int loop=0; loop<3; loop++)					// Loop Through All 5 Textures
		{
			glBindTexture(GL_TEXTURE_2D, texture[loop]);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
			glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
			glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[loop]->sizeX, TextureImage[loop]->sizeY,
				0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
		}
	}

	for (int loop=0; loop<3; loop++)						// Loop Through All 5 Textures
	{
		if (TextureImage[loop])						// If Texture Exists
		{
			if (TextureImage[loop]->data)				// If Texture Image Exists
			{
				free(TextureImage[loop]->data);			// Free The Texture Image Memory
			}
			free(TextureImage[loop]);				// Free The Image Structure
		}
	}

	return Status;											// Return The Status
}

It's nice, it works brilliantly, but I would like to do something a bit different. You see, NeHe calls LoadGLTextures() in his InitGL() function. That's nice, cause it gets it outta the way right at the beginning. However, I would like to encapsulate LoadGLTextures() into another function and simply pass my filename as one of the parameters (amongst others) of this new function. I would then like to call this new function dynamically in my main game code (something like EnemyShip()) whenever I feel the need to. And I would like to dynamically load textures only when the function is called (as my enemies may change and their textures change with them). My question is, would the dynamic loading of textures serve a significant performance hit? I assume NeHe called the function in his Init for performance and debugging reasons and not because of any necessary hierarchy dictated by the GL spec. Please correct me if I'm wrong.

Share this post


Link to post
Share on other sites
Two things really,
1) loading textures during gameplay really does have a performance hit. If you need to do that (streaming data for instance) then you should have a worker thread that asynchronously loads it.

2) Generally, for most games atleast, you load all of the textures at level load time (think init, except every time a level is completed, you do it again). This is perhaps the best choice, for most people, especially if you are new to this field. You can get into more complex streaming solutions later on when you have a better grasp on the fundamentals.

Share this post


Link to post
Share on other sites
As Washu said, I load all of my textures for a level at the start, loading and converting textures on the fly during game play would cripple your speed, not to mention the errors that could occur if you don't correctly manage the freeing of textures and handling of file not found errors.

Share this post


Link to post
Share on other sites
Thanks for the tips. I implemented it exactly as I imagined it and it works, but if its a significant performance hit, I'll just go with your reccomendation. I actually ran into a problem with errors right off the bat. I have a message box pop-up whenever it doesn't find the file I point to, but because it was being called within my main game loop, it just kept popping and popping, halting the program entirely. I fixed that with a simple global boolean error flag.

Anyway, thanks for the feedback. I'm trying to push the limits of my admittedly meager skills with GL and I appreciate your help.

Share this post


Link to post
Share on other sites
You could look into streaming data during the game, it will reduce the game speed, but would still, in most cases, maintain a playable speed, but would be a good choice if you need to read data during the game instead of during level loading. Altough streaming isn't "that" easy, it would still perhaps be worth a look. :)

Streaming is mainly usefull for multiplayer games: let's say you preload the textures of all the player models that are used in the server, so now imagine someone joined the server, you could either try loading his model + texture right away or stream it, making it take longer but still providing the user with a playable game.

[Edited by - Alura on August 25, 2004 5:39:19 PM]

Share this post


Link to post
Share on other sites

This topic is 4862 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.

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