(Demo) Simple 2D grid in GLSL


Simple 2D grid in GLSL



Here is a demo that renders a simple 2D grid in a GLSL shader. This demo should run on any OpenGL 2.1 capable platform (Windows, Linux, macOS and Raspberry Pi).

The demo is also available in the full code sample pack in the misc/grid-2d/ folder.

The vertex shader:

#version 120
varying vec4 v_uv;

void main()
{
  gl_Position = gl_Vertex;
  v_uv = gl_MultiTexCoord0;
}

 
The fragment shader:

#version 120
varying vec4 v_uv;
uniform vec4 params;

// adapted from https://thebookofshaders.com/edit.php#10/ikeda-simple-grid.frag

float grid(vec2 st, float res)
{
  vec2 grid = fract(st*res);
  return (step(res,grid.x) * step(res,grid.y));
}

void main()
{
  vec2 grid_uv = v_uv.xy * params.x; // scale
  float x = grid(grid_uv, params.y); // resolution
  gl_FragColor.rgb = vec3(0.5) * x;  
  gl_FragColor.a = 1.0;
}


Simple 2D grid in GLSL





Leave a Comment

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