1
\$\begingroup\$

Update: OK, not being able to see the textures loaded by FreeImage was just one of the common mistakes when using modern OpenGL. My texture loading code did not set GL_TEXTURE_WRAP_S/T and GL_TEXTURE_MIN/MAG_FILTER, and didn't have any mipmaps either: Textures "don't work" when I don't specify any texture parameters. Is this a driver bug or intended behavior?

May then the random colors I'm getting with the testing code be caused by the texture being too small? Maybe If I hardcode a 64x64 red square I will get same results in both cases. It is still weird that with float it works at 2x2 size.


I hit a wall trying to rewrite old sprite rendering code based on SDL to use modern OpenGL directly.

I'm loading images using FreeImage library and I already confirmed that the pixel data in memory is as expected. But at rendering I was getting no textures. So I harcoded a 2x2 image as a single dimensional array to pass to glTexImage2D and found something weird. When using float, result is as expected but when using char or uint8_t I get random colors.

Test code with floats:

        float pixels [] = {1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f,
                           1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 0.0f};

        glGenTextures(1, &mRedSquareTex);
        glBindTexture(GL_TEXTURE_2D, mRedSquareTex);
        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_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_FLOAT, pixels);

enter image description here

Test code with uint8_t:

        uint8_t pixels [] = {255, 0, 0, 255, 0, 0,
                             255, 0, 0, 255, 0, 0};

        glGenTextures(1, &mRedSquareTex);
        glBindTexture(GL_TEXTURE_2D, mRedSquareTex);
        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_MIN_FILTER, GL_LINEAR);
        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
        glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, 2, 2, 0, GL_RGB, GL_UNSIGNED_BYTE, pixels);

enter image description here

I think that if I walk pixel by pixel the data I'm loading using FreeImage and write them to a float array, I can finally see the textures loaded from disk. But It would be nice to just be able to use them as they are. As I'm not sure about the channels ordering of images loaded with FreeImage I'm avoiding the alpha channel for now, until I confirm the channels order, but in theory, as long as I avoid the alpha channel, I must be able to see the texture drawn, maybe with the wrong colors.

Shaders don't do anything special yet. The Vertex Shader just pass the textures coordinates as they are and the Fragment Shader just call texture().

I'm testing this in a machine with a NVidia GT 640, driver version 340.93, Ubuntu 14.04 64 bits.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.