Checkerboard template GLSL

I want to shadow the ATV with checkers:

F (P) = [sex (Px) + gender (Py)] mod 2.

My quad:

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

Vertex Shader File:

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

And the fragment shader file:

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

But I get this:

alt text

It seems the mod doe function is not working, or maybe something else ... Any help?

+4
source share
3 answers

It’s better to calculate this effect in the fragment shader, something like this:

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

program fragment =>

 #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

+14
source

What your code does is calculate the coefficient 4 times (once for each vertex, since it is a vertex shader code), and then interpolate these values ​​(since it is written into a variable variable) and then output this variable as a color in the fragment shader.

So it does not work. You need to do this calculation directly in the fragment shader. You can get the position of a fragment using the built-in variable gl_FragCoord in the fragment shader.

+3
source

Try this feature 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 basically you can call it using:

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

And just pass the x and y that you get from the vertex shader. All you have to do is enable it when calculating vFragColor.

Remember that you can resize a check by simply changing the value of checkSize.

+2
source

Source: https://habr.com/ru/post/1335590/


All Articles