OpenGL, Shader Model 3.3 Texturing: Black Textures?

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:

Texturing, not as expected.

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:

Coloring using st coordinates.

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

+4
source share
2 answers

I think this may be due to the incomplete initialization of your texture object.

Try running the MIN and MAG texture filter

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

In addition, I would suggest checking the size of the texture. If it is not 2, you need to set the wrap mode to CLAMP_TO_EDGE

 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_CLAMP_TO_EDGE); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_CLAMP_TO_EDGE); 

Black textures are often associated with this problem, a very common problem.

Ciao

+9
source

In your fragment shader you write an individual target

  fragment_colour = texture2D(base_texture, varying_uv); 

If it was not gl_FragColor or gl_FragData[…] , did you set the data location with the indicated fragments correctly?

+2
source

Source: https://habr.com/ru/post/1390408/


All Articles