GlGenTextures keeps returing 0's

12,484

Solution 1

Try calling glGetError. It should tell you in more detail what went wrong. In general, if an OpenGL function fails, the first thing you do should be to ask OpenGL why it failed. It knows, because it just tried to execute the function.

It's much harder for us to guess at what might have gone wrong.

Solution 2

You probably are calling glGenTextures before creating the OpenGL context, and that will generate a GL error. Don't try to create textures before you've initialized OpenGL.

Solution 3

I had this problem, and glGetError() was returning 0.

In my case it was caused by calling glGenTextures(...) on a different thread to the one the GL context was created on (because I was loading the textures asynchronously). Calling it from the main thread after the async load made glGenTextures(...) start working again.

Share:
12,484
jmasterx
Author by

jmasterx

Hi

Updated on June 09, 2022

Comments

  • jmasterx
    jmasterx almost 2 years

    I'm trying to generate textures like so:

    #define checkImageWidth 64
    #define checkImageHeight 64
    static GLubyte checkImage[checkImageHeight][checkImageWidth][4];
    static GLubyte otherImage[checkImageHeight][checkImageWidth][4];
    
    static GLuint texName[2];
    
    void makeCheckImages(void)
    {
        int i, j, c;
    
        for (i = 0; i < checkImageHeight; i++) {
            for (j = 0; j < checkImageWidth; j++) {
                c = ((((i&0x8)==0)^((j&0x8))==0))*255;
                checkImage[i][j][0] = (GLubyte) c;
                checkImage[i][j][1] = (GLubyte) c;
                checkImage[i][j][2] = (GLubyte) c;
                checkImage[i][j][3] = (GLubyte) 255;
                c = ((((i&0x10)==0)^((j&0x10))==0))*255;
                otherImage[i][j][0] = (GLubyte) c;
                otherImage[i][j][1] = (GLubyte) 0;
                otherImage[i][j][2] = (GLubyte) 0;
                otherImage[i][j][3] = (GLubyte) 255;
            }
        }
    }
    void init(void)
    {    
        glClearColor (1.0, 0.0, 0.0, 0.0);
        glShadeModel(GL_FLAT);
        glEnable(GL_DEPTH_TEST);
    
        makeCheckImages();
        glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
    
        glGenTextures(2, texName);
        glBindTexture(GL_TEXTURE_2D, texName[0]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
            GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
            GL_NEAREST);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth,
            checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE,
            checkImage);
    
        glBindTexture(GL_TEXTURE_2D, texName[1]);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
            GL_NEAREST);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
            GL_NEAREST);
        glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);   
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, checkImageWidth, 
            checkImageHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, 
            otherImage);
        glEnable(GL_TEXTURE_2D);
    
        engineGL.current.tex = texName[1];
    }
    

    But when I check the values of texName[0] and [1] they are both 0, I do not understand why, what am I doing wrong. Thanks.

    • josesuero
      josesuero almost 14 years
      Is that the only function that fails?
    • jmasterx
      jmasterx almost 14 years
      No, other things are failing too, I'm calling init() after setting up my context...
    • Jerry Coffin
      Jerry Coffin almost 14 years
      have you verified that you're succeeding in setting up your context?
    • josesuero
      josesuero almost 14 years
      If other functions are failing too, then write the shortest code snippet you can in which a function fails, so you can narrow down where the error is introduced.
  • josesuero
    josesuero almost 14 years
    Use gluErrorString() to find out. :) opengl.org/wiki/…
  • josesuero
    josesuero almost 14 years
    The (documentation)[opengl.org/sdk/docs/man/xhtml/glGenTextures.‌​xml] mentiones that "GL_INVALID_OPERATION is generated if glGenTextures is executed between the execution of glBegin and the corresponding execution of glEnd.". Does that help you?
  • jmasterx
    jmasterx almost 14 years
    Yea I had seen this but I don't do any sort of rendering before calling init() so im confused.
  • jmasterx
    jmasterx almost 14 years
    Yes, that was 1 of my issues, I was also calling wglmakecurrent in my render loop which was causing trouble