I have been struggling with getting textures to work via loading various .BMP, .JPG, etc. files. The images were being displayed with a “phase” problem. What I mean is that if there was a vertical line in the image, it would be displayed diagonally. Also the colors were messed up. It was as if the RGB codes got out of phase (i.e. there was an extra code per line that was throwing each successive line off). Actually only some files had the problem, others displayed correctly. After massive debugging struggles what appears to be happening is that if the .BMP or .JPG file has a width length or a height length that doesn’t end on a multiple of 4 bytes, the display is off (e.g. a width of 3 pixels has the problem as that is 9 bytes, where as a width of 4 pixels (12 bytes) doesn’t have the problem. It seems weird that OpenGL would have a bug like this so I am sending this to question my understanding. Please enlighten.
I am running under Windows 10 and am using OpenGL version 3.1 (I am using this version as it is the last that supports glGenList() and enables wglUseFontOutlines() to work, making it trivial to have 3D fonts)
The relevant code setting up the texture is:
glBindTexture(GL_TEXTURE_2D, colorTexture); glTexImage2D(GL_TEXTURE_2D, 0, GL_SRGB8, textureWidth, textureHeight, 0, (channels == 3) ? GL_RGB : GL_RGBA, GL_UNSIGNED_BYTE, td_ptr); glGenerateMipmap(GL_TEXTURE_2D); glBindBuffer(GL_ARRAY_BUFFER, texCoordBuffer); glBufferData(GL_ARRAY_BUFFER, numVertices * 2 * sizeof(texture), texture, GL_STATIC_DRAW); glVertexAttribPointer(texCoordAttribute, 2, GL_FLOAT, GL_FALSE, 0, 0); glEnableVertexAttribArray(texCoordAttribute);
The shader uses baseColor = texture(colorTexture, interpolated.textCoord); in what I think is very straight forward
Also I am not sure what the difference between GL_SRGB8 and GL_RGB8 is?