(Demo) Checkerboard in GLSL




GeeXLab GLSL checkerboard demo



If you need to draw a checkerboard pattern using a pixel shader, here is a GLSL program that does the job perfectly.

You can play with the shader in the GeeXLab demo (live coding is enabled for the FRAME script and for the GLSL shaders). Download the demo, unzip it somewhere and load the main.xml file into GeeXLab. This demo has been coded with GeeXLab 0.27.4.0 (it should run with previous versions of GeeXLab but I’m not sure…).

Vertex shader

#version 120
void main()
{
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;		
  gl_TexCoord[0] = gl_MultiTexCoord0;
}

Pixel shader

#version 120
uniform vec2 texsize;
uniform float checker_size;
 
// Adapted from: https://github.com/mattdesl/glsl-checker
 
float checker(vec2 uv, float repeats) 
{
  float cx = floor(repeats * uv.x);
  float cy = floor(repeats * uv.y); 
  float result = mod(cx + cy, 2.0);
  return sign(result);
}
 
void main (void)
{
  vec2 uv = gl_TexCoord[0].xy;
  uv.x *= texsize.x / texsize.y;
  float c = mix(1.0, 0.0, checker(uv, checker_size));
  gl_FragColor = vec4(c, c, c, 1.0);  
}




Leave a Comment

Your email address will not be published. Required fields are marked *