I can't apply any texture

I have a problem with the textures in my program. I can’t apply a texture on a object. I load the textures, build the object textures, and then apply them, but nothing…

Here is some code to understand :

// This structure describe the element for a texture
typedef struct _TEXTURE
AUX_RGBImageRec* textureImage; // data of the bitmap
int textureIndice; // Reference of the texture
char* textureName; // name of the texture
struct _TEXTURE* next; // next TEXTURE (chain link)

// This function defines the texture object
GLuint* SetParameterTEXTURE()
int cpt = 0;
GLuint* GLTexture;

GLTexture = (GLuint*)malloc(nb_texture * sizeof(GLuint));
glGenTextures(nb_texture, GLTexture);

For each struct TEXTURE in the chain link
glBindTexture(GL_TEXTURE_2D, *(GLTexture + cpt));
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, currentTEXTURE->textureImage->sizeX, currentTEXTURE->textureImage->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, currentTEXTURE->textureImage->data);
currentTEXTURE->textureIndice = cpt;
return GLTexture;

// This function display the object
void DisplayFunc(GLuint* GLTexture)

Get the texture indice of the current element to draw
glBindTexture(GL_TEXTURE_2D, *(GLTexture + textureIndice);

The function that loads the textures works perfectly, such as the mapping (mapping coords are generated by 3DS).
The problem is that there is no texture on the element. When I disable the line “glBindTexture(GL_TEXTURE_2D, *(GLTexture + cpt);” in the SetParameterTEXTURE func, there is only one texture (always the same, the first loaded in fact, but she is well mapped on its element) which is put on every element, although there are 3 or 4 textures in memory.

So did i forget something ?

Thanks to help me.

PS : of course i’ve called glEnable(GL_TEXTURE_2D);

I assume, that you increment cpt for every texture.
And then, you do this:

*(GLTexture + cpt);

This gives the the address to an integer that lies cpt bytes “behind” the array starting adress.
You can make the code correct and readable if do do:

And afterwards, it is very unlikely that the n-th texture index is n, since 0 is reserved and you start with 0:
currentTEXTURE->textureIndice = cpt;

I think it would be correct if you state:
currentTEXTURE->textureIndice = GLTexture[cpt];

Don’t know if there are any other bugs, but these jumped into my eyes. Hope it helps.

glGenTextures return values that are unused for the texture object.
In my case, most of the time (when i have 3 textures), i have :
*(GLTexture + 0) = GLTexture[0] = 1;
*(GLTexture + 1) = GLTexture[1] = 2;
*(GLTexture + 2) = GLTexture[2] = 3;
The structure TEXTURE have an int that save the textureIndice (here it is 0, 1 or 2) so it works.
I have although tried what you tell me (ie currentTEXTURE = GLTexture[cpt]) but it is the same thing.

I had the idea that maybe the Bitmap is too big to be a texture (i got many textures which are 720*512). I’ll tried this evening to see if the creation of the texture works (there is a function for this, glGetTexture… i don’t know anymore).

Dont images always have to have their dimensions a power of 2 to work, unless you use gluBuildMipMaps which automatically scales them to powers of 2.

Resize your images to 512x512 or 256x256 and see if they work.


to Michael:

if you use

int *x;
int i;

and acces it with *(x+i)
will work, cause “C” increases the addr (x) by sizeof(int)*i and not just by i.

(it will work with other types too, even self-made-ones )