GLSL Checkerboard Pattern

前端 未结 5 1802
余生分开走
余生分开走 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:39

    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.

提交回复
热议问题