Skip to content

Instantly share code, notes, and snippets.

@BeRo1985
Last active March 17, 2017 02:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BeRo1985/51682cee0bece6630861 to your computer and use it in GitHub Desktop.
Save BeRo1985/51682cee0bece6630861 to your computer and use it in GitHub Desktop.
GLSL n-rook anti-aliasing - based on the idea from http://mlab.uiah.fi/~kkallio/antialiasing/EdgeFlagAA.pdf
// n-rook anti-aliasing - based on the idea from http://mlab.uiah.fi/~kkallio/antialiasing/
// GLSL implementation by Benjamin 'BeRo' Rosseaux
// licensed under CC0 1.0 Universal (CC0 1.0) Public Domain Dedication ( http://creativecommons.org/publicdomain/zero/1.0/ )
#version 150
#define nrookSamples 8
uniform vec2 resolution;
vec4 realMainContent(vec2 p){
vec4 c = vec4(0.);
// ...
return c;
}
void main(){
#if nrookSamples == 2
const float nrook[2] = float[2](1.0, 0.0);
const float nrookInverseScale = 0.5;
#define antialiased
#elif nrookSamples == 4
const float nrook[4] = float[4](1.0, 3.0, 0.0, 2.0);
const float nrookInverseScale = 0.25;
#define antialiased
#elif nrookSamples == 8
const float nrook[8] = float[8](5.0, 0.0, 3.0, 6.0, 1.0, 4.0, 7.0, 2.0);
const float nrookInverseScale = 0.125;
#define antialiased
#elif nrookSamples == 16
const float nrook[16] = float[16](1.0, 8.0, 4.0, 15.0, 11.0, 2.0, 6.0, 14.0, 10.0, 3.0, 7.0, 12.0, 0.0, 9.0, 5.0, 13.0);
const float nrookInverseScale = 0.0625;
#define antialiased
#elif nrookSamples == 32
const float nrook[32] = float[32](28.0, 13.0, 6.0, 23.0, 0.0, 17.0, 10.0, 27.0, 4.0, 21.0, 14.0, 31.0, 8.0, 25.0, 18.0, 3.0, 12.0, 29.0, 22.0, 7.0, 16.0, 1.0, 26.0, 11.0, 20.0, 5.0, 30.0, 15.0, 24.0, 9.0, 2.0, 19.0);
const float nrookInverseScale = 0.03125;
#define antialiased
#elif nrookSamples == 64
const float nrook[64] = float[64](35.0, 41.0, 5.0, 20.0, 7.0, 25.0, 46.0, 60.0, 47.0, 61.0, 49.0, 15.0, 0.0, 10.0, 29.0, 1.0, 33.0, 6.0, 4.0, 18.0, 57.0, 40.0, 30.0, 24.0, 2.0, 8.0, 62.0, 44.0, 53.0, 63.0, 27.0, 31.0, 37.0, 26.0, 22.0, 51.0, 19.0, 38.0, 28.0, 32.0, 16.0, 9.0, 12.0, 52.0, 39.0, 48.0, 58.0, 59.0, 56.0, 54.0, 34.0, 13.0, 55.0, 3.0, 50.0, 42.0, 45.0, 21.0, 36.0, 23.0, 17.0, 11.0, 43.0, 14.0);
const float nrookInverseScale = 0.015625;
#define antialiased
#else
#undef antialiased
#endif
#ifdef antialiased
vec4 c = vec4(0.);
for(int i = 0; i < nrookSamples; i++){
c += realMainContent(((gl_FragCoord.xy + ((vec2(nrook[i], float(i)) * nrookInverseScale) - vec2(0.5))) / resolution.xy));
}
gl_FragColor = c / float(nrookSamples);
#else
gl_FragColor = realMainContent(gl_FragCoord.xy / resolution.xy);
#endif
}
@Joffrey
Copy link

Joffrey commented Mar 17, 2017

Great !
But I am somehow lost, what should we insert in the realMainContent function ?
Do you have a complete sample ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment