I have been banging my head on this for several hours, I am sure that this is something simple, but I just canβt get the result. I had to modify this code a bit because I created a small library to encapsulate OpenGL calls, but the following is an accurate description of the state of affairs.
I use the following vertex shader:
#version 330 in vec4 position; in vec2 uv; out vec2 varying_uv; void main(void) { gl_Position = position; varying_uv = uv; }
And the following fragment shader:
#version 330 in vec2 varying_uv; uniform sampler2D base_texture; out vec4 fragment_colour; void main(void) { fragment_colour = texture2D(base_texture, varying_uv); }
Both shaders are compiled, and the program links are no problem.
In my initialization section, I load one texture as follows:
// Check for errors. kt::kits::open_gl::Core<QString>::throw_on_error(); // Load an image. QImage image("G:/test_image.png"); image = image.convertToFormat(QImage::Format_RGB888); if(!image.isNull()) { // Load up a single texture. glGenTextures(1, &Texture); glBindTexture(GL_TEXTURE_2D, Texture); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB8, image.width(), image.height(), 0, GL_RGB, GL_UNSIGNED_BYTE, image.constBits()); glBindTexture(GL_TEXTURE_2D, 0); } // Check for errors. kt::kits::open_gl::Core<QString>::throw_on_error();
You will notice that I use Qt to load textures. Calls :: throw_on_error () check for errors in OpenGL (by calling Error ()) and throw an exception if it occurs. There are no OpenGL errors in this code, and the image uploaded using Qt is valid.
Drawing is performed as follows:
// Clear previous. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); // Use our program. glUseProgram(GLProgram); // Bind the vertex array. glBindVertexArray(GLVertexArray); /* ------------------ Setting active texture here ------------------- */ // Tell the shader which textures are which. kt::kits::open_gl::gl_int tAddr = glGetUniformLocation(GLProgram, "base_texture"); glUniform1i(tAddr, 0); // Activate the texture Texture(0) as texture 0. glActiveTexture(GL_TEXTURE0 + 0); glBindTexture(GL_TEXTURE_2D, Texture); /* ------------------------------------------------------------------ */ // Draw vertex array as triangles. glDrawArrays(GL_TRIANGLES, 0, 4); glBindVertexArray(0); glUseProgram(0); // Detect errors. kt::kits::open_gl::Core<QString>::throw_on_error();
Similarly, no OpenGL errors occur, and the triangle is drawn to scroll. However, it looks like this:

I had a problem with my texture coordinates. So, I displayed the following image, using s as the "red" component, and t as the "green" component:

The texture coordinates look correct, but I still get a black triangle of fate. What am I doing wrong?