glGenTexture problem (or something else...)


I’ve some code loading a 2D texture as this :

void load_texture () {
GLfloat *params;
GLubyte *raw_bitmap;
//here is the problem
if (!file_name) Form1->file_name= “test.dcm”; //A: like this it works
//return; //B: black screen
else file_name= “test.dcm”; //B: doesn’t work even if I do this (so it’s not a path problem)
raw_bitmap = chargementImage(&Image_Width,&Image_Height); //file_name is global

if (raw_bitmap) {
if (allocated)
glDeleteTextures(1, texture_id);
allocated= true;
if (glIsTexture(texture_id[0])==GL_TRUE)
glDeleteTextures(1, texture_id);
glGenTextures (1, texture_id);
glBindTexture (GL_TEXTURE_2D, texture_id[0]);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, Image_Width, Image_Height, 0, GL_RGBA, GL_UNSIGNED_BYTE, raw_bitmap);
free (raw_bitmap);
allocated= false;
void draw_texture (int number) {
float tempX= -0.5f, tempY=-0.5f;
int i;

glPushMatrix ();
glColor3f (1.0, 1.0, 1.0);
glBindTexture (GL_TEXTURE_2D, texture_id[number]);
glTranslatef(panX0+panX, panY0+panY, 0.);
glBegin (GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(tempX, tempY, 0.);
glTexCoord2f(1.0f, 0.0f); glVertex3f(tempX+1.0F, tempY, 0.);
glTexCoord2f(1.0f, 1.0f); glVertex3f(tempX+1.0F, tempY+1.0F, 0.);
glTexCoord2f(0.0f, 1.0f); glVertex3f(tempX, tempY+1.0F, 0.);
glEnd ();
glColor3f (1.0, 0.0, 0.0);
if (C1.finished) {
glBegin (GL_LINE_LOOP);
for (i=0; i<C1.nbr; ++i)
glEnd ();
} else
for (i=1; i<C1.nbr; ++i) {
glBegin (GL_LINES);
glColor3f (0.0, 1.0, 0.0);
for (i=0; i<C1.nbr; ++i) {
glBegin (GL_QUADS);
glEnd ();
glPopMatrix ( );

(C++ Builder 5)

I’d like to load my texture or change it with the BCB File selection component but I can’t get it working!
Altough it works fine when I load the texture giving the name of the file manually on the first pass (cfr //here is the problem).

When I trace the error I see that glGenTexture doesn’t allow a name when I don’t do it manually at the first o_O

I resume the 2 ways and the weird problem :
A. Init the file_name var with the relative path of my image :

  1. init my prog
  2. load texture giving the “filename” manually (as none has been selected yet : initialisation)
  3. texture_id has 1 after glGenTexture (ok!)
  4. it works (image on the screen)
  5. when I try to select a file after it, it gives me a white screen (my white quad)

B. try to not do it with the initialisation but when i select an image :

  1. init my prog (file_name is NULL)
  2. loading the texture returns NULL as there are no file_name given (this time)
    3. texture_id has 0 after glGenTexture (normal)
    4. my screen is black (it would be a white quad o_O I don’t understand this)
    5. try to select an image
    6. texture_id has 0 after glGenTexture (weird problem as nothing change but the time when I set file_name - even if i put it manually at this moment so it’s not a path problem)
    7. My screen is white (my quad but no pic on it)

Anyone has an idea? You’re welcome…


[This message has been edited by acerb (edited 03-19-2003).]

Please, tell me if you read this post but don’t see anything or if I have to change my technique or if i’m not clear with my explanations… but say something =/ (I really need this)


[This message has been edited by acerb (edited 03-20-2003).]


Quite a mess you’ve got there, don’t you? It’s hard to tell really, but it seems like the texture loading works only on first time you call it, after that it propably returns a null texture.

First of all, remove this line:

if (glIsTexture(texture_id[0])=GL_TRUE) glDeleteTextures(1, texture_id);

It seems unnecessary, if a texture has been allocated, it’s been wiped out by the previous lines anyway. In addition, on the first round you delete some random texture, cause you can’t have anything reasonable in your texture_id yet. If it’s initialized to 0, for example, you may try to delete the default texture.

Maybe there’s a problem with the chargementImage call, don’t know. And I’m not sure if just calling glTexImage is enough, I think you have to set the at least the warping parameters (with glTexParameter or something like that) for textures to work, but you seem to be getting some images so… Oh, but you do set some params in the drawTexture procedure. It would be a better idea to do that at the same time with glTexImage, but at least do it AFTER you’ve bound the texture, those things are associated with the texture objects.

What else… Your texture loading function always loads the new texture to texture_id[0], so make sure you aren’t trying to draw any others…

I hope some of these things makes it work!


Oh, and “hardly” means “not really”. Maybe that’s why you weren’t getting any help No offense, I’m not a native english speaker either.