I wrote my own mix function, complete with really obvious variable names, so when I get confused again in the future I can look back on this and be like "Oh, that's what that was."
vec4 my_mix(vec4 x, vec4 y, float a) {
vec4 x_contribution = x * (1.0 - a);
vec4 y_contribution = y * a;
return x_contribution + y_contribution;
}
void main(void) {
vec4 white_layer = vec4(1.0, 1.0, 1.0, 1.0);
vec4 black_layer = vec4(0.0, 0.0, 0.0, 1.0);
float amount_of_second_layer = 0.5;
//gl_FragColor = mix(white_layer, black_layer, amount_of_second_layer);
gl_FragColor = my_mix(white_layer, black_layer, amount_of_second_layer);
}
vec4 x_contribution = x * (1.0 - a);
vec4 y_contribution = y * a;
return x_contribution + y_contribution;
}
void main(void) {
vec4 white_layer = vec4(1.0, 1.0, 1.0, 1.0);
vec4 black_layer = vec4(0.0, 0.0, 0.0, 1.0);
float amount_of_second_layer = 0.5;
//gl_FragColor = mix(white_layer, black_layer, amount_of_second_layer);
gl_FragColor = my_mix(white_layer, black_layer, amount_of_second_layer);
}
Although I'm having a lot of fun working with this kind of fun and it is so incredibly exciting seeing the possibilities of this, it's also really hard being the n00b. The absolute n00b. I look on people's code online and they're like "Oh I'm an absolute beginner at this stuff" and even they are using functions that I've never used before. It's just been a while since I've been this n00b at something.
But I love it anyway :)
// Edit
The declaration for clamp, vec4 is:
vec4 clamp(vec4 x, vec4 minVal, vec4 maxVal)
How does GLSL judge if x is "bigger" than minVal? Does it judge solely on magnitude? Does it compare, component by component?
Answer to my own question, they do it component by component
So here would be code to implement clamp:
vec4 my_clamp(vec4 x, vec4 minVal, vec4 maxVal) {
float x_comp = x.x < minVal.x ? minVal.x : x.x > maxVal.x ? maxVal.x : x.x;
float y_comp = x.y < minVal.y ? minVal.y : x.y > maxVal.y ? maxVal.y : x.y;
float z_comp = x.z < minVal.z ? minVal.z : x.z > maxVal.z ? maxVal.z : x.z;
float w_comp = x.w < minVal.w ? minVal.w : x.w > maxVal.w ? maxVal.w : x.w;
return vec4(x_comp, y_comp, z_comp, w_comp);
}
float x_comp = x.x < minVal.x ? minVal.x : x.x > maxVal.x ? maxVal.x : x.x;
float y_comp = x.y < minVal.y ? minVal.y : x.y > maxVal.y ? maxVal.y : x.y;
float z_comp = x.z < minVal.z ? minVal.z : x.z > maxVal.z ? maxVal.z : x.z;
float w_comp = x.w < minVal.w ? minVal.w : x.w > maxVal.w ? maxVal.w : x.w;
return vec4(x_comp, y_comp, z_comp, w_comp);
}
Ugly code sorry