7
\$\begingroup\$

I'm using SFML 2.1, it's much more straightforward for me so I can jump directly to learning the shading language.

I'm trying to do something similar to conway's game of life. I already learned that I will need to use 2 textures since I need to read and write at the same time.

My questions:

  • How do I read neighbor pixels ? Do I need to pass a vec2 from the vertex shader to the fragment shader like this ? :

vertex shader:

out vec2 pixelpos;
void main(){
    pixelpos = gl_Position;
}

fragment shader:

in sampler2d mytexture;
in vec2 pixelpos;
void main(){
    if(mytexture[pixelpos.x][pixelpos.y].r > 0.5)
    {}
}
  • How do I write to a texture ?
\$\endgroup\$
  • \$\begingroup\$ Read neighbour pixels by sampling the input texture with offsetted texture coordinates. Yes, you can use the pixelpos variable to know from the fragment shader, which fragment you are currently on. There is also a FragCoord constant you can use, which is similar, if I am not mistaken. You write to a texture just by outputting a fragment. If your output texture is properly bound along with its framebuffer object, it should work out of the box (this is done before the draw call that will run your shader). \$\endgroup\$ – Grimshaw Nov 13 '14 at 11:34
  • \$\begingroup\$ Can you please clarify what are you trying to achieve, I don't know what conway's game of life is. \$\endgroup\$ – concept3d Nov 13 '14 at 12:08
  • \$\begingroup\$ patater.com/life.html patater.com/life although, I don't want to do exactly that, I just want to alter that algorithm to do something very different, like a virus spreading... \$\endgroup\$ – jokoon Nov 13 '14 at 13:24
  • \$\begingroup\$ One question per question please :) \$\endgroup\$ – Kromster Nov 13 '14 at 13:32
  • 1
    \$\begingroup\$ Feel free to poke around on omino.com/experiments/webgl/life.html on chrome or safari. I've implemented conway's life using shaders in WebGL. \$\endgroup\$ – david van brink Nov 13 '14 at 19:57
3
\$\begingroup\$

I did something similar, but with edge detection algorithm. I would use the following setup:

  • 2 frame buffers with one texture each.
  • a vertex buffer/mesh with a full screen rectangle (-1,-1 -> 1,1) with the texture cords going from 0,0 to the texture size.
  • one shader computing the game of life

The two frame buffers are used alternating, since they can not be read and written to. (At least that is not efficient.)

The vertex shader is basically a pass through shader:

#version 400

in vec3 aVertex;    
in vec2 aTexCoord;

out vec2 vTexCoord;
out vec3 vNormal;

void main()
{   
    vTexCoord   = aTexCoord;   
    gl_Position = vec4(aVertex, 1);
}

The fragment shader then uses texelFetch to get the individual values from the texture. For example something like this:

uniform sampler2D uTexture;

in vec2 vTexCoord;

out vec4 oFragColor;

void main() 
{
   bool alive = texelFetch(uTexture, ivec2(vTexCoord)).r < 0.7;
   float s = 0;
   for (int i=0; i<3; i++)
   {
        for (int j=0; j<3; j++) 
        { 
            if (i != 1 && j != 1)
            {
                s = s + texelFetch(uTexture, ivec2(vTexCoord) + ivec2(i-1,j-1), 0).r;
            }
        }
    }

    if (alive)
    {
        if (s < 1.5) 
        {
            oFragColor = vec4(0, 0, 0, 1);
        }
        else if (s > 1.5 && s < 3.5)
        {
            oFragColor = vec4(1, 1, 1, 1);
        }
        else
        {
            oFragColor = vec4(0, 0, 0, 1);
        }
    }
    else 
    {
        if (s > 1.5 && s < 3.5)
        {
            oFragColor = vec4(1, 1, 1, 1);
        }
        else
        {
            oFragColor = vec4(0, 0, 0, 1);
        }
    }    
}
\$\endgroup\$
  • \$\begingroup\$ Hi, thanks for this example, it is great! I understand the GLSL side of things pretty well, can you you give some guidance how the attributes are set? \$\endgroup\$ – Austin Jul 13 '15 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.