Sign in to follow this  

tutorial 6(texture mapping)

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

Tutorial 6 shows you how to use texture mapping for one texture. Where is there info on how to load multiple textures into the textureimage[0] array? I seem to be lost trying to figure this out any ideas please? Thank you.

Share this post


Link to post
Share on other sites
Textures used by your program need to stay in the memory they started in once you load it into OpenGL. In order to load multiple textures, you need to have them all stored in your program's memory individually. You'll probably have to dynamically allocate arrays to do this.

You can keep track of different textures using the "textures" parameter of glGenTextures. Simply call glBindTexture with that parameter to switch the currently-used texture. The "textures" parameter of glGenTextures will basically act as a GL-savvy ID number for the texture.

Share this post


Link to post
Share on other sites
It's not that much harder than in tutorial 6. I believe in later tutorials, he expands it and shows you how to do it, but in any case, here is the code.

GLuint texture[5]; //Creat our variable for the texture
//in this case, we have 5 textures,
//but you can change that number
//according to how many you have
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 LoadTextures()
{
int Status = FALSE;
int loop;
AUX_RGBImageRec *TextureImage[5];
memset(TextureImage, 0, sizeof(void *)*5);
if ( (TextureImage[0]=LoadBMP("Data/Font.bmp"))&&
(TextureImage[1]=LoadBMP("Data/Field.bmp"))&&
(TextureImage[2]=LoadBMP("Data/asteroid1.bmp"))&&
(TextureImage[3]=LoadBMP("Data/asteroidm.bmp"))&&
(TextureImage[4]=LoadBMP("Data/weapon.bmp")))

{
Status=TRUE;

glGenTextures(5, &texture[0]); // Create The Texture
for(loop=0;loop<5;loop++)
{
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_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, TextureImage[loop]->sizeX,
TextureImage[loop]->sizeY, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[loop]->data);
}
for(loop=0;loop<5;loop++)
{
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;
}





Keep in mind, that any time you want to add a new texture, you must
1. Change the code that initiallizes the variable, for example, if we had wanted to load six textures, the code would read
GLuint texture[6];

2. Change all the numbers in the following lines to the number of new textures.


AUX_RGBImageRec *TextureImage[5];

memset(TextureImage, 0, sizeof(void *)*5);

glGenTextures(5, &texture[0]);

for(loop=0;loop<5;loop++)

for(loop=0;loop<5;loop++)






3. Add the line that shows where the texture is stored, for example, if you wanted to add a sixth texture, that was stored in the data folder name "green.bmp", you would change

if ( (TextureImage[0]=LoadBMP("Data/Font.bmp"))&&
(TextureImage[1]=LoadBMP("Data/Field.bmp"))&&
(TextureImage[2]=LoadBMP("Data/asteroid1.bmp"))&&
(TextureImage[3]=LoadBMP("Data/asteroidm.bmp"))&&
(TextureImage[4]=LoadBMP("Data/weapon.bmp")))





to

if ( (TextureImage[0]=LoadBMP("Data/Font.bmp"))&&
(TextureImage[1]=LoadBMP("Data/Field.bmp"))&&
(TextureImage[2]=LoadBMP("Data/asteroid1.bmp"))&&
(TextureImage[3]=LoadBMP("Data/asteroidm.bmp"))&&
(TextureImage[4]=LoadBMP("Data/weapon.bmp"))&&
(TextureImage[5]=LoadBMP("Data/green.bmp")))






Hope that helps.
Levi

Share this post


Link to post
Share on other sites

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