Archived

This topic is now archived and is closed to further replies.

n3odymium

Efficient Texture Allocation (NeHe 06)

Recommended Posts

Hi, and thank you for reading my post. I have gotten NeHe''s lesson 6 up and running thanks to the forum community''s help and guidance. Thank you all. If you recall, NeHe''s lesson 06 involves a spinning cube. I have modified his pictures so I can display a spinning 6 sided die in space. I have 6 textures I need to allocate into an array. So far, I''ve been doing it exactly as he has written: *********************************************** if (TextureImage[0]=LoadBMP("Data/Dice1.bmp")) { Status=TRUE; // Set The Status To TRUE glGenTextures(1, &texture[0]); // Create The Texture // Typical Texture Generation Using Data From The Bitmap 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 (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 } *********************************************** For each additional texture (5 more to go) I copy and paste the previous lines, and simply increment the index of TextureImage. The 2nd texture would go into index [1], the 3rd into [2], etc etc. until I have 6 textures loaded into the array. Perhaps my method is a bit ungainly. I was wondering if any of you out there know of a more efficient way to allocate multiple textures rather than the cut-copy-paste method. any info would be greatly appreciated, and as usual, awarded with cigerettes/candy. Thanks for reading! ~stu

Share this post


Link to post
Share on other sites
Di it this way, that should do the trick:

    
for(int i = 0; i < numberOfTextures; i++){
if (TextureImage[0]=LoadBMP(textureNames[i].c_str())){
Status=TRUE; // Set The Status To TRUE

glGenTextures(1, &texture[i]); // Create The Texture


// Typical Texture Generation Using Data From The Bitmap

glBindTexture(GL_TEXTURE_2D, texture[i]);
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 (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

}

//to use a textures afterward, bind it using

glBindTexture(GL_TEXTURE_2D, texture[i]);


So you need to have an index over texture, not TextureImage.

textureNames[] is defined as
std::string textureNames[numberOfTextures];
the textureNames.c_str() just converts a std::string back to a char * (which is used by NeHe's LoadBMP function of I'm not mistaken.


[edited by - rizman on March 6, 2003 4:55:47 AM]

Share this post


Link to post
Share on other sites