GLSL Pixel Artifact

I test everything but i’m unable to fix it. The blocks has specific properties like specular, ssr. But when i was implementing it i got this error, the background is water and has its own rendering with working specular and ssr.

http://puu.sh/lPS9S/b8c220076f.png
.

I am using Deferred Shading/Lighting, i tested if the error is in the specular/ssr pass but its ok, so its related to the blocks vertex/fragment shader.

Vertex


#version 330 core

in vec3 position;
in vec2 textureCoords;
in vec3 normal;
in vec4 data;//This holds the data about the block(id and bright)

out vec2 pass_textureCoords;
out vec3 surfaceNormal;
out vec4 pass_position;
out vec4 pass_Data;

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform vec3 cameraPos;

void main() {
	vec3 pos = position - cameraPos;
	vec4 worldPosition = vec4(pos, 1.0);
	vec4 positionRelativeToCam = viewMatrix * worldPosition;
	gl_Position = projectionMatrix * positionRelativeToCam;
	pass_textureCoords = textureCoords;
	pass_position = vec4(position, 1.0);
	surfaceNormal = normal;
	pass_Data = data;
}

Fragment


#version 330 core

in vec2 pass_textureCoords;
in vec3 surfaceNormal;
in vec4 pass_position;
in vec4 pass_Data;

out vec4 [5] out_Color;

uniform sampler2D texture0;

void main(void) {
	vec4 data = pass_Data;
	float id = data.x;
	float bright = data.y;

    vec4 textureColour = texture(texture0, pass_textureCoords);
    if(textureColour.a < 0.5)
    	discard;
	out_Color[0] = textureColour;
	out_Color[1] = vec4(pass_position.xyz,0);
	out_Color[2] = vec4(surfaceNormal.xyz,0);
	out_Color[3] = vec4(0.0, 0.0, 0.0, 0.0);
	out_Color[4] = vec4(0.0, 0.0, 0.0, bright);
	if(id == 13 || id == 8){//check if the block needs ssr and add it to the specular/ssr pass
		out_Color[3].r = 1.0;
	}
}

Are you getting self-shadowing? What is SSR? Also, you really shouldn’t be storing position in the G-buffer. Just reconstruct it from depth.

SSR = Screen Space Reflections, and this is the first time that i implement Deferred Shading so for more easy way i use texture to store data, probably change it later. The problem is that the specular isn’t applying correctly in the shader, i assign one color for one effect, in this case Red means Specular and reflections, but for some strange reason is flickering when it applies to the pixel. Other object doesn’t do this, like water, only this.

That’s all float data, interpolated between vertices by the shader pipeline. Maybe a floating point precision issue? For a quick&dirty test, try to compare with delta, like:


	if(abs(id - 13) < 0.01 || abs(id - 8) < 0.01){//check if the block needs ssr and add it to the specular/ssr pass
		out_Color[3].r = 1.0;
	}

I add the delta compare and is fixed, so it’s related to float precision, there is a better way to fix it?

http://puu.sh/lQB59/4ed2db0113.png

I believe it’s always a good habit to compare (==) floats with an error epsilon.

You should set ID output as a flat int instead, so you get an uninterpolated integer ID. That would eliminate any precision issues.

What @theagentd said is correct, another (quick) solution is to do something like:

int id = int(data.x + 0.5f);

I’m fairly certain this is a bad practice however.

Edit: clarification.

For correctness with negative numbers, use int(round(data.x)). For only positive numbers, that works perfectly fine.

Thanks for the help and have a good day! ;D. Btw: I’m using the @thedanisaur solution.