So I’m seeing some baffling behavior from my GLSL shaders. They do the bog-standard thing where you send in the texture coordinate as a vertex attribute to the vertex shader, and then pass it on through to the fragment shader, which then looks up the appropriate color in a bound texture. However, if I add 0.5 to the texture coordinate in the vertex shader, I see what looks like Z-fighting noise in the rendered output. No idea what might be causing this; I’m quite confident that there are not actually two triangles in the same place here. It also doesn’t replicate if I add 0.49, or 0.51; just 0.5. Anyone ever seen something like this…?
Here’s the vertex shader:
attribute vec3 i_position;
attribute vec2 i_texCoord;
attribute float i_heightScale;
varying vec2 texCoord;
uniform sampler2D i_heightmapTexture;
uniform float i_globalHeightScale;
uniform vec2 i_topLeft;
uniform vec2 i_bottomRight;
void main()
{
texCoord = i_topLeft + i_texCoord * (i_bottomRight - i_topLeft);
float heightmapSample = texture2D(i_heightmapTexture, texCoord).r;
float localHeight = heightmapSample * i_heightScale * i_globalHeightScale;
// Added this line to explicitly demonstrate the problem. I originally saw it when
// the input values above resulted in adding particular values to the texture coordinate.
texCoord.y = i_texCoord.y + 0.50;
gl_Position = gl_ProjectionMatrix * gl_ModelViewMatrix * vec4(i_position.x, localHeight, i_position.z, 1.0);
}
And the fragment shader:
varying vec2 texCoord;
uniform sampler2D i_colormapTexture;
void main()
{
gl_FragColor = texture2D(i_colormapTexture, texCoord);
}