Sign in to follow this  
ewan_ko_08

texture loader

Recommended Posts

ewan_ko_08    122
AUX_RGBImageRec *LoadBMP(char *Filename)					// Loads A Bitmap Image
{
	FILE *File=NULL;							// File Handle

	if (!Filename)								// Make Sure A Filename Was Given
	{
		return NULL;							// If Not Return NULL
	}

	File=fopen(Filename,"r");						// Check To See If The File Exists

	if (File)								// Does The File Exist?
	{
		fclose(File);							// Close The Handle
		return auxDIBImageLoad(Filename);				// Load The Bitmap And Return A Pointer
	}

	return NULL;								// If Load Failed Return NULL
}
	

int LoadGLTextures()								// Load Bitmaps And Convert To Textures
{
	AUX_RGBImageRec *TextureImage[1];					// Create Storage Space For The Texture

	memset(TextureImage,0,sizeof(void *)*1);				// Set The Pointer To NULL

	// Load The Bitmap, Check For Errors, If Bitmap's Not Found Quit
	if (TextureImage[0]=LoadBMP("wall.bmp"))
	{
		glGenTextures(1, &texture[0]);					// Create The Texture

		// Typical Texture Generation Using Data From The Bitmap
		glBindTexture(GL_TEXTURE_2D, texture[0]);
	

		// Generate The Texture
		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);	// Linear Filtering
		glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);	// Linear Filtering
	}


	if (TextureImage[0])							// If Texture Exists
	{
		if (TextureImage[0]->data)					// If Texture Image Exists
		{
			free(TextureImage[0]->data);				// Free The Texture Image Memory
		}

		free(TextureImage[0]);						// Free The Image Structure
	}

}
I found this code for texture loader in NeHe's tutorial. How do i load more than one texture using this code?

Share this post


Link to post
Share on other sites
JimmyDeemo    156
Its been a while since i did the NeHe stuff but if i remember rightly you need to look at the
texture[0]
, where it calls
glBindTexture(GL_TEXTURE_2D, texture[0]);
.

That call along with the glGenTextures() call stores the data or at least a reference to how to get it in
texture[0]
. Look to see where else
texture[0]
is used, noteing how its used in rendering.

From that you should realise that you can have a member variable that holds the texture information. Just replace the
texture[0]
in that code. Obvioulsy the may need changing, i think if you return
texture[0]
then that is a reference to the data you need.

I'm just working from memory here so you should probably wait til someone confirms the above.

Share this post


Link to post
Share on other sites
Bob Janova    769
In simple terms, you need to wrap a loop around the loading code, and change the creation of the TextureImage array to
	AUX_RGBImageRec *TextureImage[n];
memset(TextureImage,0,sizeof(void *)*n);

(That void* should be AUX_RGBImageRec* imho, but it will probably work the same.)

Put a for(int i = 0; i < n; i++){ ... } around the rest of the function, and replace [0] with [i] throughout.

You will also need to adjust the definition of texture (not in your snippet) to allow for more than one entry. And changing 'wall.bmp' to be a parameter would make more sense ;). I would pass in an array of strings, calculate n as the length of that array and use textureNames[i] there.

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