loading multiple textures

Hi Me again

Quick and Painless:

I know lots of you are familiar with this:

int LoadGLTextures()
{
int Status=FALSE;

AUX_RGBImageRec *TextureImage[1];

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

This is ok if you want to load 1 texture, but what if I want to load more? To be more precise, what if the number of textures i want to load is stored in some int variable?
int textures=5;
AUX_RGBImageRec *TextureImage[textures];
That doesn´t work…

Any ideas?
Thanks
?

this code seems very weird… I never would think of something that looks like that if it’s about loading textures. what image file format do you want do load?

You’ll need to use dynamic memory allocation. Read up on it in your favorite C or C++ book.

The “weird” code belongs to Nehe´s tutorials (#6)

I´ve searched for dinamyc or variable arrays and all I get is this:

TextureImage[];


TextureImage[3];

But that doesn´t work. Maybe for the AUX_RGB preceding it. The compiler´s error reads like: “expecting a constant”
Hope someone knows what I´m talking about
Thanks
?

First we don’t know every tutor out there by heart, while nehe is a good resourse the first lessons are examples, just to show how to do something. But those first examples are limited in that maybe not the correct way to do something beyond what it demostrates.

I think the problem is that example is setup to load one texture and there is more to it then increasing the size of the texture array.

Maybe this will help:

TextureImage[0]; // is for storing the BMP file before it is loading into openGL’s texture memory space, so this is really a temp storage and you could use this same variable over and over.

GLuint texture[1]; // This is the real place the texture is going to be stored, currently has space for one texture.

glGenTextures(1, &texture[0]); // Here is where we create our texture space.

glBindTexture(GL_TEXTURE_2D, texture[0]); // What type of texture we are creating.

glTexImage2D(GL_TEXTURE_2D, 0, 3, TextureImage[0]->sizeX, TextureImage[0]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, TextureImage[0]->data); // Copy our image into the texture space.

To load multiple textures you would:

LoadBMP into TextureImage[0]

Create a texture space and copy the TextureImage into texture[?].

Clear the TextureImage, per example in NEHE’s code and load the next BMP file.

Keep repeating until all of the files needed have been loaded.

Note later NEHE goes go into loading multiple images, I forget which lesson.

I have code for loading multiple images for a file list, but I do not use the glaux library since it is for the windows only platform.

[This message has been edited by nexusone (edited 08-06-2003).]

If you are going to allocate memory for an array on the function stack, which is what you are doing when you do something like int array[10], then the size of the array HAS to be a constant. If that number can be changed at runtime you need to use dynamic memory allocation to allocate the array on the heap.

Example:

int size = GetSize();
int *pArray = new int[size];

// use pArray

// when done with the array do this:
delete [] pArray;

BYTE Texture[]={TEXTURE1,TEXTURE2,ECT, ECT};

Originally posted by c0d3Junk3Y:
BYTE Texture[]={TEXTURE1,TEXTURE2,ECT, ECT};

That’s still an array with a static size. You can’t dynamically specify the number of elements in the brackets at runtime with the above code.

That´s exactly my point.

I´ve number of textures to be used, stored in some variable…

An in the other hand the compiler asks for a constant

HELP!!!

?

I´ve number of textures to be used, stored in some variable…

An in the other hand the compiler asks for a constant

The answer was already posted, twice, by Deiussum and myself. No need to get alarmed; dynamic memory allocation and pointers are not an easy thing to grasp at first, and take a little while to master.

Originally posted by Titus:
[b]That´s exactly my point.

I´ve number of textures to be used, stored in some variable…

An in the other hand the compiler asks for a constant

HELP!!!

?[/b]

Why are you looking to store the textures twice and take up so much memory? When you can load them into the texture memory?

Per the NEHE example TextureImage is the image stored outside of openGL, so you will still have to copy it into openGL texture storage space and I gave you the correct answer, if it is that you want to load multiple textures into openGL’s texture store.

[This message has been edited by nexusone (edited 08-06-2003).]

Multiple texture loading routine I wrote:

void Load_card_images(void)
{

int i;

char filename[32];

image_t temp_image;

glEnable( GL_TEXTURE_2D );

glPixelStorei( GL_UNPACK_ALIGNMENT,1 );
glGenTextures( CARD_TEXTURES, deck_store_textures ); // Here I create space for 52 card textures

for( i=0; i < CARD_TEXTURES; i++) // I loop here to load the textures from files
{
glBindTexture( GL_TEXTURE_2D, deck_store_textures[i] );

glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
strcpy( filename, “images/” );
strncat( filename, CardList[i], 32 - strlen(filename));
tgaLoad( filename, &temp_image, TGA_FREE | TGA_LOW_QUALITY ); // could replace this with loadBMP + a few other commands would be needed. look something like this TextureImage[0]=LoadBMP(filename)
}

}

Originally posted by JanHH:
this code seems very weird… I never would think of something that looks like that if it’s about loading textures. what image file format do you want do load?

I think the question is different from the concept of multi_texture in opengl. The question could be understood as “How to load several textures independently in OpenGL”.
Is what I think correct?

Sorry about my poor English!

Originally posted by nexusone:
[b]Multiple texture loading routine I wrote:

[quote]

// Code cut for space
glGenTextures( CARD_TEXTURES, deck_store_textures ); // Here I create space for 52 card textures

[/b][/QUOTE]

I think the line of code I left above is ultimately where Titus is having problems. It’s not that line per-se, but how do you allocate memory for the deck_store_textures variable above. Your comment is somewhat misleading in that you say you create space for 52 textures. You don’t really. You create 52 texture id’s, but you are not creating any space in deck_store_textures. The memory has to be allocated before you call this function.

You can either create that memory on the stack, whereby you need to have a constant number like so:

GLuint deck_store_textures[52];

Or if you’re not sure how many textures you need exactly, you need to do it dynamically on the heap.

int nNumTextures = GetNumberOfTexturesSomehow();
GLuint deck_store_textures = new GLuint[nNumTextures];

[This message has been edited by Deiussum (edited 08-07-2003).]

[This message has been edited by Deiussum (edited 08-07-2003).]

Yes, you are correct that is only creates the texture id’s(names), what is not in my code is the part where I call glTexImage2D, which correct me if I am wrong copy’s our image data into the texture memory.

So the point I was trying to get accross to titus that in the example from NEHE, the variable that he was using is just to store the image file until it is converted into a
texture data by glTexImage2D and asigned a id from glGenTextures.

Other then allocating memory to load a image from the file does not openGL manage it once it is loaded into it’s texture space?

[This message has been edited by nexusone (edited 08-07-2003).]

Yeah, that’s correct. Once you’ve uploaded the image data with glTexImage2D, you don’t need to keep it around. So in the original post, he wouldn’t need to keep an array of AUX_RGBImageRecs, but he would still need to keep an array of texture id’s.

In your example, it sounds like you are just keeping an array of texture IDs for a deck of cards, so it will always be 52 textures. In that case a constant is just fine. If it were some game like blackjack where you could have multiple decks, and you wanted to let the user decide how many decks were used, you’d need to use dynamic allocation. I think it’s something like this that Titus is getting hung up on. I could be wrong, though.