I am learning OpenGL, and I am trying to draw a rectangle with a texture on it. However, it instead renders a gradient from red in the corners to black at the center. At first, I thought it was like when you specify a color at the vertices of a shape, and it interpolates the color between them. However, there is no vertex at the middle, where is fades to black. My line that sets the color is the only place I can possibly think if that could give a bug like this, but I don’t see any problems with it. The line is “fragColor = texture(ourTexture, texCoord);”. I have confirmed that the texture coordinates are correct, and that the texture file is also correct. Does anybody have any ideas why a bug like this might happen?
There are a few possible reasons. The symptoms suggest that it might be sampling either a 3×3 block of texels or a 2×2 mipmap level, with wrapping at the edges.
One reason it might select the wrong mipmap level (if you’re using mipmaps) is if you’re calculating the texture coordinates within non-uniform control flow (if-else or the ?: operator). The texture function uses implicit derivatives to select mipmap levels, and derivatives are undefined within non-uniform control flow. Also, using modular arithmetic (mod function or % operator) on texture coordinates tends to produce extreme derivatives at the boundary; in that case you need to use e.g. textureGrad or textureLod so that you can supply either correct derivatives or an explicit LoD.
But there are plenty of other possibilies. Ultimately, you need to simplify the code until it stops misbehaving, then determine which change is causing the problem.
Thanks for the response, but I don’t think any of those are the problem. My code is really simple. Here is my vertex shader and my fragment shader, for anyone who wants to see the code. If you want to see my main C++ file, just ask and I can post that to.
#version 330 core
layout (location = 0) in vec3 aPos;
layout (location = 1) in vec3 aColor;
layout (location = 2) in vec2 aTexCoord;
out vec3 ourColor;
out vec2 texCoord;
void main()
{
gl_Position = vec4(aPos, 1.0);
ourColor = aColor;
texCoord = aTexCoord;
}
#version 330 core
in vec3 ourColor;
in vec2 texCoord;
out vec4 fragColor;
uniform sampler2D ourTexture;
void main()
{
fragColor = texture(ourTexture, texCoord);
}
I can’t see any issue there. Although I would have added a layout(location=0) qualifier to fragColor to avoid needing to use glBindFragDataLocation.
So check the client code and/or modify the shader to output debug information (e.g. using fragColor = vec4(texCoord,0,1); to check that the texture coordinates are what you expect them to be).
I checked the texture coordinates, and they were correct. I also used
if (!data) {
std::cout << "FAILED TO LOAD TEXTURE\n";
}
in my main file to check if the texture file was being loaded correctly, and it isn’t. Now I just need to figure out why it isn’t loading and how to fix it.
I fixed it. I just forgot to put the image file in my project folder, so when I tried to load it, it couldn’t find it. I just copied the file into my project folder, and it worked