I am currently attempting to load a png image using libpng, and then convert it into an OpenGL texture that is then drawn.
Now, first of all, to do a quick test to see if any textures would get drawn a simple test quad, I made this texture:
glGenTextures(1, &textureHandle);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, textureHandle);
glTexStorage2D(GL_TEXTURE_2D, 1, GL_RGB8, 4, 4);
const GLubyte myTexture[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, 4, 4, GL_RGB, GL_UNSIGNED_BYTE, myTexture);
Just a white texture that is then drawn on my quad. This works perfectly. Therefore it seems to indicate that the fragment shader and vertex shader is working correctly, as well as the rest of my drawing logic.
For anyone interested, this is the main loop that does the drawing using a texture:
glClearColor(1, 0, 0, 1);
glClear(GL_COLOR_BUFFER_BIT);
glUseProgram(theProgram);
GLuint samplerLoc = glGetUniformLocation(theProgram, "tex");
if (samplerLoc == -1)
printf("Location name could not be found...\n");
glUniform1i(samplerLoc, 0);
glBindBuffer(GL_ARRAY_BUFFER, vertexBuffer);
glEnableVertexAttribArray(0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(0, 4, GL_FLOAT, GL_FALSE, 0, 0);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(16 * sizeof(float)));
glDrawArrays(GL_TRIANGLE_FAN, 0, 4);
Now, the function I use to load an OpenGL texture from a png using libpng is this one: OpenGL Texture Load Function
The reason I've provided a link is so that this post does not become impossible to read :)
Basically, the part of my code where I load the texture and bind it is this:
GLuint myImage = png_texture_load("niks.png", &width, &height);
glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, myImage);
GLuint mySampler;
glGenSamplers(1, &mySampler);
glBindSampler(0, mySampler);
This happens right before the main loop.
It's also worth noting that I've done extensive debugging using the glGetError function, and it returns no errors at all. Additionally, I've stepped through the png_texture_load function and it returns the OpenGL texture successfully.
Also, the image I'm loading is a power of 2. It has dimensions of 64x64.
I just seem to be at a complete loss as to why, then, it's just a black texture when my own text texture shows up just fine?
EDIT
This is the part of the libpng image function that creates a texture from the loaded data:
// Generate the OpenGL texture object
GLuint texture;
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexImage2D(GL_TEXTURE_2D, 0, format, temp_width, temp_height, 0, format, GL_UNSIGNED_BYTE, image_data);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, temp_width, temp_height, GL_RGB, GL_UNSIGNED_BYTE, image_data);