GLSL Checkerboard Pattern

前端 未结 5 1798
余生分开走
余生分开走 2021-02-10 02:56

i want to shade the quad with checkers:

f(P)=[floor(Px)+floor(Py)]mod2.

My quad is:

glBegin(GL_QUADS);    
  glVerte         


        
5条回答
  •  醉梦人生
    2021-02-10 03:35

    It is better to calculate this effect in fragment shader, something like that:

    vertex program =>

    varying vec2 texCoord;
    
    void main(void)
    {
       gl_Position = vec4( gl_Vertex.xy, 0.0, 1.0 );
       gl_Position = sign( gl_Position );
    
       texCoord = (vec2( gl_Position.x, gl_Position.y ) 
                 + vec2( 1.0 ) ) / vec2( 2.0 );      
    }
    

    fragment program =>

    #extension GL_EXT_gpu_shader4 : enable
    uniform sampler2D Texture0;
    varying vec2 texCoord;
    
    void main(void)
    {
        ivec2 size = textureSize2D(Texture0,0);
        float total = floor(texCoord.x*float(size.x)) +
                      floor(texCoord.y*float(size.y));
        bool isEven = mod(total,2.0)==0.0;
        vec4 col1 = vec4(0.0,0.0,0.0,1.0);
        vec4 col2 = vec4(1.0,1.0,1.0,1.0);
        gl_FragColor = (isEven)? col1:col2;
    }
    

    Output =>

    alt text

    Good luck!

提交回复
热议问题