0

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.

eliaspr
  • 302
  • 3
  • 15
  • 1
    Edit in a [mcve]. – genpfault Oct 13 '17 at 16:46
  • 1
    I don't know that the problem with the example is. It's minimal because the three lines that generate the problem are shown and everything around them does not matter. It is complete because the problem occurs in the example, and I'ts verifiable since the code didn't work for me since I started debugging two days ago. – eliaspr Oct 13 '17 at 16:50
  • @Professor901 *usually, a standard requirement for MCVE source code is that of enabling people to 1) copy-and-paste the code into a file 2) compile it with a oneliner (provided by you) 3) execute and test the program.* The link you provided is **ok**. – Patrick Trentin Oct 13 '17 at 22:06

2 Answers2

2

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.

0

If after creating the texture of size, say, 2000x1000, glGetError() returns 0, probably the problem lies in your rendering code. Also you can check texture size of texture via

int w=0;
glGetTexLevelParameteriv(GL_TEXTURE_2D, 0, GL_TEXTURE_WIDTH, &w); 

though it's not supported everywhere.

olha
  • 2,132
  • 1
  • 18
  • 39