Want to store textures, access them later

…but it’s not working. The texture won’t display properly and I get an error when I try to delete it later, or even with a glIsEnabled(GL_TEXTURE_2D) check. I’ve tried numerous permutations of the basic commands but I haven’t gotten anything to work. If I make, draw, and delete within the same function, everything is fine; but when I do it separately it doesn’t work. So if anybody could help me that would be great. Anyway, first I create the texture, where sq is the (power of 2) array of GLubytes:

void MakeTexture()
{
    int tex_name;
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size, size,
                 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, sq);
    glEnable(GL_TEXTURE_2D);
    glGenTextures(1, &tex_name);
    glBindTexture(GL_TEXTURE_2D,tex_name);
    glDisable(GL_TEXTURE_2D);
}

After this function call other things might be drawn, including other textures. However I get failure even when this is the only thing I try to draw.

void ShowTexture()
{
    glColor4f(1.0, 1.0, 1.0, 1.0);
    glEnable(GL_TEXTURE_2D);
    glBindTexture(GL_TEXTURE_2D,GetTexName());
    glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);

    glPushMatrix();
    glBegin(GL_POLYGON); 
        for (i=0; i<num_sides; ++i)
        {
           glTexCoord2f(...);
           glVertex2f(...);
           //... The texture mapping is not itself the problem, it works in other circumstances.
        }
    glEnd();
    glPopMatrix();
    glDisable(GL_TEXTURE_2D);
}

The geometry is completely determined by the glColor() call, e.g. is white in this case. But if I omit that it’s still white. And the environment variable is GL_REPLACE anyway.

void CleanTexture()
{
    //Every one of the following statements individually makes the program crash:
    glIsEnabled(GL_TEXTURE_2D); //As used in an if statement
    glEnable(GL_TEXTURE_2D);
    glIsTexture(tex_name); //As used in an if statement
    glDeleteTextures(1, &tex_name);
}

So by the time the program gets to CleanTexture(), the state machine is apparently broken. What is going on? What’s the right way to create a texture, store it, and retrieve it later? Thank you…

Matt

You need to Gen and Bind the texture before calling glTexImage2D:

void MakeTexture()
{
    int tex_name;

    glGenTextures(1, &tex_name);
    glBindTexture(GL_TEXTURE_2D,tex_name);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, size, size,
                 0, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, sq);
    glEnable(GL_TEXTURE_2D);
    glDisable(GL_TEXTURE_2D); // get rid of this
}

If sq is an array of unsigned bytes use GL_UNSIGNED_BYTE for the format, not GL_UNSIGNED_INT_8_8_8_8. Get rid of your glDisable (GL_TEXTURE_2D) as well. Your pixel store and parameters can move to generation time (pixel store before teximage, parameters after binding), only the texenv needs to be done at draw time.

Also double check that your GetTexName function returns the right value (the same as the value you got from glGenTextures).

Thanks a lot mhagain, your suggestion and some other rearranging got the stimulus to appear properly. But I’m still getting the same crash–even glIsTexture, glIsEnabled, fail. What does it take to break these? The only thing that happens between the mapping and the (attempted) deletion is

    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

Got it! I was executing the GL commands described above from a worker thread. I keep forgetting you can’t do that. (I’m not sure why not, either. I’m working within Qt so maybe that has something to do with it.) Anyway, thanks for the help mhagain.

Matt