Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.
 1 void main(void)
 2 {
 3     vec2 pos = mod(gl_FragCoord.xy, vec2(50.0)) - vec2(25.0);
 4     float dist_squared = dot(pos, pos);
 5 
 6     gl_FragColor = (dist_squared < 400.0) 
 7         ? vec4(.90, .90, .90, 1.0)
 8         : vec4(.20, .20, .40, 1.0);
 9 }

taken from http://people.freedesktop.org/~idr/OpenGL_tutorials/03-fragment-intro.html

Now, this looks really trivial and simple, but my problem is with the mod function.

This function is taking 2 vec2 as inputs but is supposed to take just 2 atomic arguments according to the official documentation, also this function makes an implicit use of the floor function that only accepts, again, 1 atomic argument.

Can someone explain this to me step by step and point out what I'm not getting here?

It's some kind of OpenGL trick? OpenGL Math trick? in the GLSL docs i always find and explicit reference to the type accepted by the function and vec2 it's not there.

share|improve this question
add comment

1 Answer

up vote 5 down vote accepted

The first line with "mod" makes sure that coordinates are always in the range [-25;25), repeating all the time ("mod" is for repeating and without the "-vec2(25)" at the end, coordinates would be in the [0;50) range).

"mod" is often used in this manner to repeat procedurally generated content at no cost.

The reason you won't see explicitly defined types in GLSL docs is because this function works with genType (float/vec2/vec3/vec4) types. By the way, the tutorial you provided includes that:

Like many functions in GLSL, step has parameters and return values with the type genType. This is shorthand for float, vec2, vec3, and vec4.

Graph of the function: set "fmod(x#2)" here as the first graph and click on "Draw" to see what it looks like.

Here's a graph that might load slower but requires no input: mod(x,2) on Wolfram Alpha

share|improve this answer
    
thanks for pointing out, i was just focusing right on the shader itself that caught my attention in the first place. –  user827992 Sep 1 '12 at 20:11
    
so the purpose of mod is to create and round the numbers at this 2 boundaries, 0 and 50 ? –  user827992 Sep 1 '12 at 22:41
    
I'm not sure if "round" is the right word for this. In any case, I updated the answer to include a graph of the function. –  snake5 Sep 2 '12 at 5:07
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.