c++openglglfwglewfreeimage

OpenGL texture does not work with resolutions greater than 1x1


I'm currently developing a GUI system for a 3D game I'm working on. I'm using instanced rendering for a pretty good compromise of speed and memory usage. Everything works well so far, I'm able to display multiple colored rectangles on the screen (up to 200k@60fps).

Now, I'm trying to add textures to be able to display characters, icons and framebuffer contents. That's where my problem occurs. I'm using FreeImage as the image loading library. The code which loads the image file and creates a OpenGL texture object is copied straight from another project.

When I try to display the texture, I only see a black rectangle on the screen. Note, that the code worked fine in the old project. The only case in which I see a colored box, is when I set the with and height passed to glTexImage2D to 1:

glGenTextures(1, &textureID);
glBindTexture(GL_TEXTURE_2D, textureID);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 1, 0, GL_BGRA, GL_UNSIGNED_BYTE, (void*)bits)

Everything other than that does not work. There are no error codes generated by glGetError(). I've also tried generating a random texture, which does not work either which sizes above 1x1:

float* data = new float[4 * 2 * 2 * 4]{
    1.0f,0.3f,0.5f,1.0f,
    0.0f,0.7f,0.5f,1.0f,
    0.0f,0.5f,0.2f,1.0f,
    0.0f,0.0f,0.0f,1.0f
};
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, 1, 2, 0, GL_RGBA, GL_FLOAT, (void*)data);

I'm using GLFW for the window and GLEW to enable using modern OpenGL. My IDE is VS2017 and I'm running Windows 10 64bit with the lates nVidia drivers for my GTX1060 (GL 4.6.0, 387.92). It would be nice if anyone could help since I've been searching my mistake for the last few days.

Complete texture loading code is available here. If any more information or code is needed, please leave a comment.


Solution

  • I see two problems here

    FreeImage_GetBits() function will return a pointer to the data stored in a FreeImage-internal format. Where each scanline is usually aligned to a 4-bytes boundary. This will work fine while you have power-of-two textures. However, for NPOT textures you will need to specify the correct unpack alignment for OpenGL.

    Use can use following function

    FreeImage_ConvertToRawBits() which returns non aligned bits and will work for NPOT textures.

    Second problem could be not specifying filtering mode for mipmaps try adding following lines after your call to glTexImage2D()

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

    There is one more this you are using internal format which is 3rd parameter as GL_RGBA so you are telling opengl to interprete pixel data as RGBA but you are giving 7th parameter as GL_BGRA so you are telling the data I am giving in pixel array is aligned line BGRA is this really what you are meaning ? check that also.