本文介绍了GLSL棋盘图案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用方格阴影四边形:

i want to shade the quad with checkers:

我的四边形是:

glBegin(GL_QUADS);
  glVertex3f(0,0,0.0);
  glVertex3f(4,0,0.0);
  glVertex3f(4,4,0.0);
  glVertex3f(0,4, 0.0);
glEnd();

顶点着色器文件:

varying float factor;
float x,y;
void main(){
  x=floor(gl_Position.x);
  y=floor(gl_Position.y);
  factor = mod((x+y),2.0);
}

片段着色器文件为:

varying float factor;
void main(){
  gl_FragColor = vec4(factor,factor,factor,1.0);
}

但是我得到了这个:

似乎mod函数无法正常工作,或者还有其他功能……有帮助吗?

It seems that the mod function doeasn't work or maybe somthing else...Any help?

推荐答案

最好在片段着色器中计算这种效果,如下所示:

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

顶点程序=>

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 );
}

片段程序=>

#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;
}

输出=>

祝你好运!

这篇关于GLSL棋盘图案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 11:23