GLSL shaders: black dots

By , last updated October 1, 2016

Recall our previous post about stretched terrain bug in the game. Shader that fixed the problem gave although an unpleasant bug in the form of many black dots with newer graphic drivers. The black dots are usually a result of a NAN error (not a number) and normally related to bad geometry, a bad shader, light or camera.

In our case it was the variable “varying float v” that cased the problem. Variable is a float, but was used like an integer. Compiler didn’t complain so it was not so obvious that this cast gave such results. The solution was to use a float like a float. So here are new shaders (without black dots):

Vertex shader

varying float v;
varying float xcoord,ycoord,zcoord;

void main ()
{
	//Texture Coordinates
	xcoord = gl_Vertex.x;
	zcoord = gl_Vertex.z;
	ycoord = gl_Vertex.y ;
	
	// projection1. y is largest normal component
	// so use x and z to sample texture
	gl_TexCoord[0] = vec4(xcoord,zcoord,0,0); //first projection
	// projection2. x is largest normal component
	// so use z and y to sample texture
	gl_TexCoord[1] = vec4(zcoord,ycoord,0,0); //second projection
	// projection3. z is largest normal component
	// so use x and y to sample texture
	gl_TexCoord[2] = vec4(xcoord,ycoord,0,0); //third projection
	
   //gl_Normal is the vertex's normal vector. 
   //We compare it's absolute values with each other to find which projection to use
	float x = abs(gl_Normal.x);
	float y = abs(gl_Normal.y);
	float z = abs(gl_Normal.z);
 
    v = 0.0;
	if(x > y && x > z)
	{
		v = 1.0; 
	}
	if(z > y && z > x)
	{
		v = -1.0;
	} 
		
	gl_Position = ftransform();
}

Fragment shader

varying float v;
uniform sampler2D myTexture0;
uniform sampler2D myTexture1;
uniform sampler2D myTexture2;
void main (void)
{
	vec4 color = texture2D( myTexture0, gl_TexCoord[0].st);

	if ( v > 0.0)
	{
		color = texture2D( myTexture1, gl_TexCoord[1].st);
	}
	if ( v < 0.0 )
	{
		color = texture2D( myTexture2, gl_TexCoord[2].st);
	}
	
	gl_FragColor = color;
}

Senior Software Engineer developing all kinds of stuff.

Comments

  1. Black dots | Monsters are coming - [...] bugs. We have some newer hardware at our workplace and it showed some unexpected bugs in shaders. Here’s what…

Leave a Reply


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

*