I have just encountered this issues and I do not know how to look at it. I guess that I have an intuition of how it might work but I want to know if some of you can give me an answer.
So, i have a function wave
that returns a float.
Now, with this code:
vec2 p = ( gl_FragCoord.xy / resolution.xy );
float a;
int steps = 6;
for(int i = 1; i < steps; i++)
{
a = wave(p, i, steps, 0.125, float(steps/2), .7, time / 2., touch);
}
gl_FragColor = vec4(vec3(a*p.x, a*p.y, a * (1. - p.x) ), 1. );
Will render well, drawing a wave behind another, starting with the farthest to the closest and brightest .
But, if I do this:
vec2 p = ( gl_FragCoord.xy / resolution.xy );
float a;
int steps = 6;
a = wave(p, 1, steps, 0.125, float(steps/2), .7, time / 2., touch);
a = wave(p, 2, steps, 0.125, float(steps/2), .7, time / 2., touch);
a = wave(p, 3, steps, 0.125, float(steps/2), .7, time / 2., touch);
a = wave(p, 4, steps, 0.125, float(steps/2), .7, time / 2., touch);
a = wave(p, 5, steps, 0.125, float(steps/2), .7, time / 2., touch);
gl_FragColor = vec4(vec3(a*p.x, a*p.y, a * (1. - p.x) ), 1. );
It will draw only the closest and also the last wave , ignoring the others.
My experience tells me that in most circumstances and programming languages the above two are somewhat equivalent. Can you tell me what is the difference here?
And the wave function:
float wave(vec2 p, int depth, int scale,
float amp, float freq, float wh,
float move, vec2 modif)
{
float a;
float fi = float(depth);
float div_height = 35.;
float m_div = 200./fi;
float m_order_h = fi * .09;
float m_height = wh - m_order_h;
float m_paralax = fi / float(scale);
float m_scale = fi / div_height;
float m_m_var = 50.*fi;
float m_wave = amp*cos(move + m_paralax + p.x*freq);
float s = floor(m_div*(p.x) + m_m_var + move);
float ns = noise2(vec2(s) + modif);
if (p.y < ns*m_scale + m_height + m_wave)
{
a = fi/float(scale);
}
return a;
}