glGenTextures() function

I can’t get texturing to work. I believe the problem may have something to do with glGenTextures, because it doesn’t return the correct name of the texture and this apparently signals failure.

So I picked my own values as a texture ID, but I couldn’t get texturing to work.
How can I resolve this problem?

Thank you in advance.
Hanuni

Not enought information, post your code so we can see what is going on.

Originally posted by hanuni:
[b]I can’t get texturing to work. I believe the problem may have something to do with glGenTextures, because it doesn’t return the correct name of the texture and this apparently signals failure.

So I picked my own values as a texture ID, but I couldn’t get texturing to work.
How can I resolve this problem?

Thank you in advance.
Hanuni[/b]

At first, I tried to get the texture name using glGenTextures() func.

glGenTextures(1, &texture); // the value of texture not changed.
glBindTexture(GL_TEXTURE_2D, texture);

But, it didn’t return correct one.
So, I modified the code as follows:

texture = 1234; //arbitrary set
glBindTexture(GL_TEXTURE_2D, texture); // the texture object of given name(1234) not created

What’s the problem?
Best,
Hanuni

Do you have a rendering context at the point where you generate texture names and create your textures?

In addition, this is my reference function, but it doesn’t return correct texture name.

GLuint /* O - Texture object or 0 on error */
TextureLoad(char filename, / I - Bitmap file to load /
GLboolean alpha, /
I - Generate alpha for bitmap /
GLenum minfilter, /
I - Minification filter /
GLenum magfilter, /
I - Magnification filter /
GLenum wrap) /
I - Repeat or clamp /
{
int i; /
Looping var */
BITMAPINFO info; / Bitmap information */
GLubyte bits; / Bitmap RGB pixels */
GLubyte ptr; / Pointer into bit buffer */
GLubyte rgba; / RGBA pixel buffer */
GLubyte rgbaptr; / Pointer into RGBA buffer /
GLubyte temp; /
Swapping variable /
GLenum type; /
Texture type /
GLuint texture; /
Texture object */

/* Try loading the bitmap file... */
bits = LoadDIBitmap(filename, &info);
if (bits == (GLubyte *)0)
    return;// (0);

#ifdef WIN32 /* This already done by non-WIN32 LoadDIBitmap /
/

* Convert the bitmap data from BGR to RGB. Since texture images
* must be a power of two, and since we can figure that we won’t
* have a texture image as small as 2x2 pixels, we ignore any
* alignment concerns…
*/

for (i = info->bmiHeader.biWidth * info->bmiHeader.biHeight, ptr = bits;
     i > 0;
 i --, ptr += 3)
    {
/* Swap red and blue */
temp   = ptr[0];
ptr[0] = ptr[2];
ptr[2] = temp;
}

#endif /* WIN32 */

/* Figure out the type of texture... */
if (info->bmiHeader.biHeight == 1)
    type = GL_TEXTURE_1D;
else
    type = GL_TEXTURE_2D;

/* Create and bind a texture object */
glGenTextures(1, &texture);
glBindTexture(type, texture);

/* Set texture parameters */
glTexParameteri(type, GL_TEXTURE_MAG_FILTER, magfilter);
glTexParameteri(type, GL_TEXTURE_MIN_FILTER, minfilter);
glTexParameteri(type, GL_TEXTURE_WRAP_S, wrap);
glTexParameteri(type, GL_TEXTURE_WRAP_T, wrap);
glTexEnvi(type, GL_TEXTURE_ENV_MODE, GL_MODULATE);

if (alpha)
    {
/* Create and use an RGBA image... */
    rgba = malloc(info->bmiHeader.biWidth * info->bmiHeader.biHeight * 4);

    for (i = info->bmiHeader.biWidth * info->bmiHeader.biHeight,
         rgbaptr = rgba, ptr = bits;
         i > 0;
     i --, rgbaptr += 4, ptr += 3)
    {
        rgbaptr[0] = ptr[0];
        rgbaptr[1] = ptr[1];
        rgbaptr[2] = ptr[2];
        rgbaptr[3] = (ptr[0] + ptr[1] + ptr[2]) / 3;
    }

    /*
     * Set texture image; if the minification filter uses mip-mapping
     * then use gluBuild2D/1DMipmaps() to load the texture...
     */

    if (minfilter == GL_LINEAR | | minfilter == GL_NEAREST)
        glTexImage2D(type, 0, 4, info->bmiHeader.biWidth,
                     info->bmiHeader.biHeight, 0, GL_RGBA,
                     GL_UNSIGNED_BYTE, rgba);
    else if (type == GL_TEXTURE_1D)
        gluBuild1DMipmaps(type, 4, info->bmiHeader.biWidth,
                          GL_RGBA, GL_UNSIGNED_BYTE, rgba);
    else  
        gluBuild2DMipmaps(type, 3, info->bmiHeader.biWidth,
                          info->bmiHeader.biHeight, GL_RGBA,
                          GL_UNSIGNED_BYTE, rgba);
    /* Free the RGBA buffer */
free(rgba);
}
else
    {
    /*
     * Set texture image; if the minification filter uses mip-mapping
     * then use gluBuild2D/1DMipmaps() to load the texture...
     */

    if (minfilter == GL_LINEAR | | minfilter == GL_NEAREST)
        glTexImage2D(type, 0, 3, info->bmiHeader.biWidth,
                     info->bmiHeader.biHeight, 0, GL_RGB,
                     GL_UNSIGNED_BYTE, bits);
    else if (type == GL_TEXTURE_1D)
        gluBuild1DMipmaps(type, 3, info->bmiHeader.biWidth,
                          GL_RGB, GL_UNSIGNED_BYTE, bits);
    else  
        gluBuild2DMipmaps(type, 3, info->bmiHeader.biWidth,
                          info->bmiHeader.biHeight, GL_RGB,
                          GL_UNSIGNED_BYTE, bits);
    }

/* Free the bitmap and return... */
free(info);
free(bits);

 return texture;

}

As Bob said, do you have a rendering context at the time you try to load the textures? You must have enabled a window to use OpenGL before you can use any OpenGL functions. If you are creating your window with the Win32 API, this means doing it after the wglCreateContext stuff. If you’re using glut, you have to do it after glutCreateWindow.

If you’re sure you have a rendering context at the time you load textures, try using glGetError right after glGenTextures and see if there are any errors returned.

glGetError() returns GL_INVALID_OPERATION.

how can I have the rendering context?
(I’m using glut functions.)

Thanks
any other advice?

Hanuni

You have a rendering context after you have called glutCreateWindow. Make sure every OpenGL call is performed after you have created the window.

You’re right.

Thank you for your help.