GLSL Checkerboard Pattern

前端 未结 5 1059
情书的邮戳
情书的邮戳 2021-02-10 03:03

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:55

    Try this function in your fragment shader:

    vec3 checker(in float u, in float v)
    {
      float checkSize = 2;
      float fmodResult = mod(floor(checkSize * u) + floor(checkSize * v), 2.0);
      float fin = max(sign(fmodResult), 0.0);
      return vec3(fin, fin, fin);
    }
    

    Then in main you can call it using :

    vec3 check = checker(fs_vertex_texture.x, fs_vertex_texture.y);
    

    And simply pass x and y you are getting from vertex shader. All you have to do after that is to include it when calculating your vFragColor.

    Keep in mind that you can change chec size simply by modifying checkSize value.

提交回复
热议问题