Review some shaders to work on GLSL 100

Tested on Raspberry Pi... Just note that platform is very limited by GPU...
This commit is contained in:
raysan5 2019-12-04 19:52:53 +01:00
parent ff499fe57d
commit 3aad221b1e
4 changed files with 40 additions and 23 deletions

View File

@ -7,8 +7,8 @@ varying vec2 fragTexCoord;
varying vec4 fragColor;
// Custom variables
#define PI 3.14159265358979323846
uniform float uTime = 0.0;
const float PI = 3.14159265358979323846;
uniform float uTime;
float divisions = 5.0;
float angle = 0.0;
@ -19,9 +19,9 @@ vec2 VectorRotateTime(vec2 v, float speed)
float localTime = fract(time); // The time domain this works on is 1 sec.
if ((localTime >= 0.0) && (localTime < 0.25)) angle = 0.0;
else if ((localTime >= 0.25) && (localTime < 0.50)) angle = PI/4*sin(2*PI*localTime - PI/2);
else if ((localTime >= 0.25) && (localTime < 0.50)) angle = PI/4.0*sin(2.0*PI*localTime - PI/2.0);
else if ((localTime >= 0.50) && (localTime < 0.75)) angle = PI*0.25;
else if ((localTime >= 0.75) && (localTime < 1.00)) angle = PI/4*sin(2*PI*localTime);
else if ((localTime >= 0.75) && (localTime < 1.00)) angle = PI/4.0*sin(2.0*PI*localTime);
// Rotate vector by angle
v -= 0.5;

View File

@ -39,20 +39,22 @@ void main()
{
vec4 color = vec4(1.0);
float scale = 1000.0; // Makes 100x100 square grid. Change this variable to make a smaller or larger grid.
int value = int(scale*floor(fragTexCoord.y*scale) + floor(fragTexCoord.x*scale)); // Group pixels into boxes representing integer values
if ((value == 0) || (value == 1) || (value == 2)) gl_FragColor = vec4(1.0);
else
float value = scale*floor(fragTexCoord.y*scale) + floor(fragTexCoord.x*scale); // Group pixels into boxes representing integer values
int valuei = int(value);
//if ((valuei == 0) || (valuei == 1) || (valuei == 2)) gl_FragColor = vec4(1.0);
//else
{
for (int i = 2; (i < max(2, sqrt(value) + 1)); i++)
//for (int i = 2; (i < int(max(2.0, sqrt(value) + 1.0))); i++)
// NOTE: On GLSL 100 for loops are restricted and loop condition must be a constant
// Tested on RPI, it seems loops are limited around 60 iteractions
for (int i = 2; i < 48; i++)
{
if ((value - i*floor(value/i)) == 0)
if ((value - float(i)*floor(value/float(i))) <= 0.0)
{
color = Colorizer(float(i), scale);
gl_FragColor = Colorizer(float(i), scale);
//break; // Uncomment to color by the largest factor instead
}
}
gl_FragColor = color;
}
}

View File

@ -11,7 +11,9 @@ uniform vec2 c; // c.x = real, c.y = imaginary component. Equati
uniform vec2 offset; // Offset of the scale.
uniform float zoom; // Zoom of the scale.
const int MAX_ITERATIONS = 255; // Max iterations to do.
// NOTE: Maximum number of shader for-loop iterations depend on GPU,
// for example, on RasperryPi for this examply only supports up to 60
const int MAX_ITERATIONS = 48; // Max iterations to do
// Square a complex number
vec2 ComplexSquare(vec2 z)
@ -56,21 +58,22 @@ void main()
// NOTE: fragTexCoord already comes as normalized screen coordinates but offset must be normalized before scaling and zoom
vec2 z = vec2((fragTexCoord.x + offset.x/screenDims.x)*2.5/zoom, (fragTexCoord.y + offset.y/screenDims.y)*1.5/zoom);
int iterations = 0;
for (iterations = 0; iterations < MAX_ITERATIONS; iterations++)
int iter = 0;
for (int iterations = 0; iterations < 60; iterations++)
{
z = ComplexSquare(z) + c; // Iterate function
if (dot(z, z) > 4.0) break;
iter = iterations;
}
// Another few iterations decreases errors in the smoothing calculation.
// See http://linas.org/art-gallery/escape/escape.html for more information.
z = ComplexSquare(z) + c;
z = ComplexSquare(z) + c;
// This last part smooths the color (again see link above).
float smoothVal = float(iterations) + 1.0 - (log(log(length(z)))/log(2.0));
float smoothVal = float(iter) + 1.0 - (log(log(length(z)))/log(2.0));
// Normalize the value so it is between 0 and 1.
float norm = smoothVal/float(MAX_ITERATIONS);

View File

@ -15,15 +15,27 @@ uniform ivec3 palette[colors];
void main()
{
// Texel color fetching from texture sampler
vec4 texelColor = texture2D(texture0, fragTexCoord) * fragColor;
vec4 texelColor = texture2D(texture0, fragTexCoord)*fragColor;
// Convert the (normalized) texel color RED component (GB would work, too)
// to the palette index by scaling up from [0, 1] to [0, 255].
int index = int(texelColor.r * 255.0);
ivec3 color = palette[index];
int index = int(texelColor.r*255.0);
ivec3 color = ivec3(0);
// NOTE: On GLSL 100 we are not allowed to index a uniform array by a variable value,
// a constantmust be used, so this logic...
if (index == 0) color = palette[0];
else if (index == 1) color = palette[1];
else if (index == 2) color = palette[2];
else if (index == 3) color = palette[3];
else if (index == 4) color = palette[4];
else if (index == 5) color = palette[5];
else if (index == 6) color = palette[6];
else if (index == 7) color = palette[7];
// Calculate final fragment color. Note that the palette color components
// are defined in the range [0, 255] and need to be normalized to [0, 1]
// for OpenGL to work.
gl_FragColor = vec4(color / 255.0, texelColor.a);
gl_FragColor = vec4(float(color.x)/255.0, float(color.y)/255.0, float(color.z)/255.0, texelColor.a);
}