1

I'm having some problems trying to load images with DevIL and creating textures in OpenGL. When a friend of mine tested my program on his Windows, all the rects, whom were supposed to contain textures, were white, without any image (texture). The problem seems to occur only on Window XP, Windows Vista and Windows 7, but not in every Windows PC: my Windows XP runs the program without problems.

Maybe there're some missing DLLs or files (improbable), or something that don't let the image to be loaded or used as a texture. By the other hand, the program runs fine on *UNIX systems.

This is the code I'm using to load an image and generate a texture:

void Image::load(const char* filename)
{
    ILuint ilimg;

    ilGenImages(1, &ilimg);
    ilBindImage(ilimg);

    if (!ilLoadImage(filename))
        throw ImageLoadError;

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

    bpp = ilGetInteger(IL_IMAGE_BPP);
    width = ilGetInteger(IL_IMAGE_WIDTH);
    height = ilGetInteger(IL_IMAGE_HEIGHT);
    format = ilGetInteger(IL_IMAGE_FORMAT);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);

    glTexImage2D(GL_TEXTURE_2D, 0, bpp, width, height, 0, format,
        GL_UNSIGNED_BYTE, ilGetData());

    ilDeleteImages(1, &ilimg);
}

This is the code that draws a rect with the texture applied:

void Rect::show()
{
    glPushAttrib(GL_CURRENT_BIT);

    glEnable(GL_TEXTURE_2D);
    glColor4f(1.0, 1.0, 1.0, opacity);
    glBindTexture(GL_TEXTURE_2D, texture->get_image());

    glBegin(GL_POLYGON);
        glTexCoord2i(0, 0); glVertex2f(x, y);
        glTexCoord2i(1, 0); glVertex2f(x+width, y);
        glTexCoord2i(1, 1); glVertex2f(x+width, y+height);
        glTexCoord2i(0, 1); glVertex2f(x, y+height);
    glEnd();

    glDisable(GL_TEXTURE_2D);

    glPopAttrib();
}

If you need some other code that I don't mentioned, ask me and i'll post it.

genpfault
  • 51,148
  • 11
  • 85
  • 139
  • I would guess, there is a path problem - try to print out getcwd and check if there exist really an image. – Constantin Dec 21 '10 at 00:12
  • If the DLLs were missing there would be runtime error and it would complain it couldn't find "ATIOGLX.dll" or something to that effect depending on your video card and drivers. – EnabrenTane Dec 21 '10 at 00:22
  • The default opengl driver that comes with windows systems is just a software driver that wraps GDI. This driver doesn't support a lot of features and as such frequently makes white textures if it fails to load them properly. Are you sure these systems have a 3rd party video driver installed (which is the only way to get the hw OpenGL drivers for the card installed) – Chris Becke Dec 21 '10 at 06:08

2 Answers2

0
glTexImage2D(target, level, internal format, width, height, border, format, type, data);

The internal format parameter of glTexImage is not bits per pixel. Actually its numerical value is not related to the format at all. OpenGL defines only specific values to be valid, among them also four with numeric relation, but just as a mnemonic: 1 (shorthand for GL_LUMINANCE), 2 (GL_LUMINANCE_ALPHA), 3 (GL_RGB), 4 (GL_RGBA). There are also a number of other format tokens, but their numeric value is arbitrarily choosen.

Also the bpp for the image data is used to find the format and type parameters. You'll need to implement a LUT or switch-case structure for that.

datenwolf
  • 159,371
  • 13
  • 185
  • 298
-1

The issue you are experiencing sounds like an implementation version issue. Probably a "correct" but less "nice" implementation isn't letting you get away with something subtly incorrect. Or on the other hand the implementation may have a subtle bug and isn't accepting your valid code.

I might try swapping

glColor4f(1.0, 1.0, 1.0, opacity);
glBindTexture(GL_TEXTURE_2D, texture->get_image());

to

glBindTexture(GL_TEXTURE_2D, texture->get_image());
glColor4f(1.0, 1.0, 1.0, opacity);

and declaring vertex before texcoord like.

glVertex2f(x, y);
glTexCoord2i(0, 0);

Short of that if you do not already have "The Red Book" consult http://glprogramming.com/red/chapter09.html

EnabrenTane
  • 7,428
  • 2
  • 26
  • 44
  • Neither of those suggestions is usefull or correct. The funtion glVertex always comes after setting all the other attibutes. glColor doesn't interact with the texture binding at all – should be obvious from the fact that glColor can be called per vertex and influences the texture very far in the pipeline, when the texture environment is evaluated. Yes there are issues with some implementations, like the ATI/AMD one, but that's not like this one. – datenwolf Dec 21 '10 at 00:53