Search code examples
arraysglslshader

shadertoy GLSL - creating a large matrix and displaying it on the screen


I have a palette of 64 colors. I need to create a 512*512 table and write the color indexes in the palette into it, and then display everything on the screen. The problem is that glsl does not support two-dimensional arrays, and it is impossible to save a table between frames


Solution

  • The closest thing you can do is create a separate buffer and only use a part of it. here's an example buffer A:

    void mainImage( out vec4 fragColor, in vec2 fragCoord )
    {
        if(any(greaterThan(fragCoord, vec2(512)))) return;
        fragCoord -= .5;
        fragColor = vec4(mod(fragCoord.x,2.), 0, 0, 1); // generate a color at point.
    }
    

    then in the main shader, you can can access a pixel with:

    // vec2 p; // p.x and p.y in range(0, 512)
    texture(iChannel0, p/iResolution.xy);
    

    If you are using openGL instead of shadertoy, you can use a texture 2d instead.