summaryrefslogtreecommitdiffhomepage
path: root/examples/shaders
diff options
context:
space:
mode:
authorraysan5 <[email protected]>2020-03-25 18:39:21 +0100
committerraysan5 <[email protected]>2020-03-25 18:39:21 +0100
commit74339b9fdcfd57350b861b3e7b1c5b0495e92cb2 (patch)
treec741e2d715af7ef2979234596bdc1b76a6a75013 /examples/shaders
parent2dbcef218ca5ddf4fa0c7857ac80947af0da16d5 (diff)
downloadraylib-74339b9fdcfd57350b861b3e7b1c5b0495e92cb2.tar.gz
raylib-74339b9fdcfd57350b861b3e7b1c5b0495e92cb2.zip
[example] Review shaders_spotlight to work on GLSL 100
Diffstat (limited to 'examples/shaders')
-rw-r--r--examples/shaders/resources/shaders/glsl100/spotlight.fs56
-rw-r--r--examples/shaders/resources/shaders/glsl330/spotlight.fs29
-rw-r--r--examples/shaders/shaders_spotlight.pngbin0 -> 120371 bytes
3 files changed, 42 insertions, 43 deletions
diff --git a/examples/shaders/resources/shaders/glsl100/spotlight.fs b/examples/shaders/resources/shaders/glsl100/spotlight.fs
index 65612753..c1202610 100644
--- a/examples/shaders/resources/shaders/glsl100/spotlight.fs
+++ b/examples/shaders/resources/shaders/glsl100/spotlight.fs
@@ -2,8 +2,7 @@
precision mediump float;
-#define MAX_SPOTS 3
-
+#define MAX_SPOTS 3
struct Spot {
vec2 pos; // window coords of spot
@@ -11,27 +10,27 @@ struct Spot {
float radius; // alpha fades out to this radius
};
-uniform Spot spots[MAX_SPOTS]; // Spotlight positions array
-uniform float screenWidth; // Width of the screen
+uniform Spot spots[MAX_SPOTS]; // Spotlight positions array
+uniform float screenWidth; // Width of the screen
void main()
{
-
float alpha = 1.0;
- // get the position of the current fragment (screen coordinates!)
+
+ // Get the position of the current fragment (screen coordinates!)
vec2 pos = vec2(gl_FragCoord.x, gl_FragCoord.y);
-
- // find out which spotlight is nearest
- float d = 65000.0; // some high value
- int fi = -1;
+ // Find out which spotlight is nearest
+ float d = 65000.0; // some high value
+ int fi = -1; // found index
for (int i = 0; i < MAX_SPOTS; i++)
{
for (int j = 0; j < MAX_SPOTS; j++)
{
float dj = distance(pos, spots[j].pos) - spots[j].radius + spots[i].radius;
- if (d > dj )
+
+ if (d > dj)
{
d = dj;
fi = i;
@@ -41,22 +40,31 @@ void main()
// d now equals distance to nearest spot...
// allowing for the different radii of all spotlights
- if (fi != -1) {
-
- if (d > spots[fi].radius)
+ if (fi == 0)
+ {
+ if (d > spots[0].radius) alpha = 1.0;
+ else
{
- alpha = 1.0;
+ if (d < spots[0].inner) alpha = 0.0;
+ else alpha = (d - spots[0].inner)/(spots[0].radius - spots[0].inner);
}
+ }
+ else if (fi == 1)
+ {
+ if (d > spots[1].radius) alpha = 1.0;
else
{
- if (d < spots[fi].inner)
- {
- alpha = 0.0;
- }
- else
- {
- alpha = (d - spots[fi].inner) / (spots[fi].radius - spots[fi].inner);
- }
+ if (d < spots[1].inner) alpha = 0.0;
+ else alpha = (d - spots[1].inner)/(spots[1].radius - spots[1].inner);
+ }
+ }
+ else if (fi == 2)
+ {
+ if (d > spots[2].radius) alpha = 1.0;
+ else
+ {
+ if (d < spots[2].inner) alpha = 0.0;
+ else alpha = (d - spots[2].inner)/(spots[2].radius - spots[2].inner);
}
}
@@ -65,5 +73,5 @@ void main()
if ((pos.x > screenWidth/2.0) && (alpha > 0.9)) alpha = 0.9;
// could make the black out colour user definable...
- gl_FragColor = vec4( 0, 0, 0, alpha);
+ gl_FragColor = vec4(0, 0, 0, alpha);
}
diff --git a/examples/shaders/resources/shaders/glsl330/spotlight.fs b/examples/shaders/resources/shaders/glsl330/spotlight.fs
index fe53ad61..97a4377b 100644
--- a/examples/shaders/resources/shaders/glsl330/spotlight.fs
+++ b/examples/shaders/resources/shaders/glsl330/spotlight.fs
@@ -9,7 +9,7 @@ out vec4 finalColor;
// NOTE: Add here your custom variables
-#define MAX_SPOTS 3
+#define MAX_SPOTS 3
struct Spot {
vec2 pos; // window coords of spot
@@ -25,19 +25,19 @@ void main()
float alpha = 1.0;
// Get the position of the current fragment (screen coordinates!)
-
vec2 pos = vec2(gl_FragCoord.x, gl_FragCoord.y);
// Find out which spotlight is nearest
- float d = 65000; // some high value
- int fi = -1; // found index
+ float d = 65000; // some high value
+ int fi = -1; // found index
for (int i = 0; i < MAX_SPOTS; i++)
{
for (int j = 0; j < MAX_SPOTS; j++)
{
float dj = distance(pos, spots[j].pos) - spots[j].radius + spots[i].radius;
- if (d > dj )
+
+ if (d > dj)
{
d = dj;
fi = i;
@@ -47,22 +47,13 @@ void main()
// d now equals distance to nearest spot...
// allowing for the different radii of all spotlights
- if (fi != -1) {
-
- if (d > spots[fi].radius)
- {
- alpha = 1.0;
- }
+ if (fi != -1)
+ {
+ if (d > spots[fi].radius) alpha = 1.0;
else
{
- if (d < spots[fi].inner)
- {
- alpha = 0.0;
- }
- else
- {
- alpha = (d - spots[fi].inner) / (spots[fi].radius - spots[fi].inner);
- }
+ if (d < spots[fi].inner) alpha = 0.0;
+ else alpha = (d - spots[fi].inner) / (spots[fi].radius - spots[fi].inner);
}
}
diff --git a/examples/shaders/shaders_spotlight.png b/examples/shaders/shaders_spotlight.png
new file mode 100644
index 00000000..424a56c7
--- /dev/null
+++ b/examples/shaders/shaders_spotlight.png
Binary files differ