diff --git a/examples/shaders/resources/shaders/glsl100/spotlight.fs b/examples/shaders/resources/shaders/glsl100/spotlight.fs index 204b89a72..65612753f 100644 --- a/examples/shaders/resources/shaders/glsl100/spotlight.fs +++ b/examples/shaders/resources/shaders/glsl100/spotlight.fs @@ -2,50 +2,67 @@ precision mediump float; -#define MAX_SPOTS 4 -#define RADIUS 256.0 -#define INNER 200.0 +#define MAX_SPOTS 3 -// Inputs -// array of spotlight positions -uniform vec2 spots[MAX_SPOTS]; -uniform float screenWidth; // width of the screen +struct Spot { + vec2 pos; // window coords of spot + float inner; // inner fully transparent centre radius + float radius; // alpha fades out to this radius +}; + +uniform Spot spots[MAX_SPOTS]; // Spotlight positions array +uniform float screenWidth; // Width of the screen void main() { - float alpha; + 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.0; // some high value - float di = 0.0; + int fi = -1; for (int i = 0; i < MAX_SPOTS; i++) { - di = distance(pos, spots[i]); - if (d > di) d = di; + for (int j = 0; j < MAX_SPOTS; j++) + { + float dj = distance(pos, spots[j].pos) - spots[j].radius + spots[i].radius; + if (d > dj ) + { + d = dj; + fi = i; + } + } } // d now equals distance to nearest spot... - if (d > RADIUS) { - alpha = 1.0; - } else { - if (d < INNER) { - alpha = 0.0; - } else { - alpha = (d - INNER) / (RADIUS - INNER); + // allowing for the different radii of all spotlights + 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); + } } } - // right hand side of screen is dimly lit, could make the - // threshold value user definable. - if (pos.x > screenWidth/2.0 && alpha > 0.9) { - alpha = 0.9; - } + // Right hand side of screen is dimly lit, + // could make the threshold value user definable + 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); diff --git a/examples/shaders/resources/shaders/glsl330/spotlight.fs b/examples/shaders/resources/shaders/glsl330/spotlight.fs index f20d92a80..fe53ad61f 100644 --- a/examples/shaders/resources/shaders/glsl330/spotlight.fs +++ b/examples/shaders/resources/shaders/glsl330/spotlight.fs @@ -9,36 +9,61 @@ out vec4 finalColor; // NOTE: Add here your custom variables -#define MAX_SPOTS 2 -#define RADIUS 128 -#define INNER 96 +#define MAX_SPOTS 3 -uniform vec2 spots[MAX_SPOTS]; // Spotlight positions array +struct Spot { + vec2 pos; // window coords of spot + float inner; // inner fully transparent centre radius + float radius; // alpha fades out to this radius +}; + +uniform Spot spots[MAX_SPOTS]; // Spotlight positions array uniform float screenWidth; // Width of the screen void main() { - float alpha = 0.0; + 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 - float di = 0; + int fi = -1; // found index for (int i = 0; i < MAX_SPOTS; i++) { - di = distance(pos, spots[i]); - if (d > di) d = di; + for (int j = 0; j < MAX_SPOTS; j++) + { + float dj = distance(pos, spots[j].pos) - spots[j].radius + spots[i].radius; + if (d > dj ) + { + d = dj; + fi = i; + } + } } // d now equals distance to nearest spot... - if (d > RADIUS) alpha = 1.0; - else - { - if (d < INNER) alpha = 0.0; - else alpha = (d - INNER)/(RADIUS - INNER); + // allowing for the different radii of all spotlights + 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); + } + } } // Right hand side of screen is dimly lit, diff --git a/examples/shaders/shaders_spotlight.c b/examples/shaders/shaders_spotlight.c index 5955d96cf..944f3d588 100644 --- a/examples/shaders/shaders_spotlight.c +++ b/examples/shaders/shaders_spotlight.c @@ -39,9 +39,24 @@ #define GLSL_VERSION 100 #endif -#define MAXSPOT 2 + +#define MAXSPOT 3 // NB must be the same as define in shader #define numStars 400 + +// Spot data +typedef struct { + Vector2 pos; + Vector2 vel; + float inner; + float radius; + + // Shader locations + unsigned int posLoc; + unsigned int innerLoc; + unsigned int radiusLoc; +} Spot; + // Stars in the star field have a position and velocity typedef struct Star { Vector2 pos; @@ -59,6 +74,7 @@ int main(void) const int screenHeight = 450; InitWindow(screenWidth, screenHeight, "raylib - shader spotlight"); + HideCursor(); Texture texRay = LoadTexture("resources/raysan.png"); @@ -74,20 +90,32 @@ int main(void) int frameCounter = 0; - unsigned int spotLoc[MAXSPOT]; // shader locations - - Vector2 spotPos[MAXSPOT]; // position and velocity - Vector2 spotVel[MAXSPOT]; + // Use default vert shader Shader spotShader = LoadShader(0, FormatText("resources/shaders/glsl%i/spotlight.fs", GLSL_VERSION)); // Get the locations of spots in the shader - char spotName[32] = "spots[x]\0"; + Spot spots[MAXSPOT]; +/* + unsigned int posLoc; + unsigned int innerLoc; + unsigned int radiusLoc; +*/ for (int i = 0; i < MAXSPOT; i++) { - spotName[6] = '0' + i; - spotLoc[i] = GetShaderLocation(spotShader, spotName); + char posName[32] = "spots[x].pos\0"; + char innerName[32] = "spots[x].inner\0"; + char radiusName[32] = "spots[x].radius\0"; + + posName[6] = '0' + i; + innerName[6] = '0' + i; + radiusName[6] = '0' + i; + + spots[i].posLoc = GetShaderLocation(spotShader, posName); + spots[i].innerLoc = GetShaderLocation(spotShader, innerName); + spots[i].radiusLoc = GetShaderLocation(spotShader, radiusName); + } // tell the shader how wide the screen is so we can have @@ -99,17 +127,26 @@ int main(void) } // randomise the locations and velocities of the spotlights + // and initialise the shader locations for (int i = 0; i < MAXSPOT; i++) { - spotPos[i].x = GetRandomValue(64, screenWidth - 64); - spotPos[i].y = GetRandomValue(64, screenHeight - 64); - spotVel[i] = (Vector2){ 0, 0 }; + + spots[i].pos.x = GetRandomValue(64, screenWidth - 64); + spots[i].pos.y = GetRandomValue(64, screenHeight - 64); + spots[i].vel = (Vector2){ 0, 0 }; - while ((fabs(spotVel[i].x) + fabs(spotVel[i].y)) < 2) + while ((fabs(spots[i].vel.x) + fabs(spots[i].vel.y)) < 2) { - spotVel[i].x = GetRandomValue(-40, 40)/10.0; - spotVel[i].y = GetRandomValue(-40, 40)/10.0; + spots[i].vel.x = GetRandomValue(-40, 40)/10.0; + spots[i].vel.y = GetRandomValue(-40, 40)/10.0; } + + spots[i].inner = 28 * (i + 1); + spots[i].radius = 48 * (i + 1); + + SetShaderValue(spotShader, spots[i].posLoc, &spots[i].pos.x, UNIFORM_VEC2); + SetShaderValue(spotShader, spots[i].innerLoc, &spots[i].inner, UNIFORM_FLOAT); + SetShaderValue(spotShader, spots[i].radiusLoc, &spots[i].radius, UNIFORM_FLOAT); } SetTargetFPS(60); // Set to run at 60 frames-per-second @@ -128,15 +165,21 @@ int main(void) // Update the spots, send them to the shader for (int i = 0; i < MAXSPOT; i++) { - spotPos[i].x += spotVel[i].x; - spotPos[i].y += spotVel[i].y; - - if (spotPos[i].x < 64) spotVel[i].x = -spotVel[i].x; - if (spotPos[i].x > screenWidth - 64) spotVel[i].x = -spotVel[i].x; - if (spotPos[i].y < 64) spotVel[i].y = -spotVel[i].y; - if (spotPos[i].y > screenHeight - 64) spotVel[i].y = -spotVel[i].y; + if ( i == 0 ) { + Vector2 mp = GetMousePosition(); + spots[i].pos.x = mp.x; + spots[i].pos.y = screenHeight - mp.y; + } else { + spots[i].pos.x += spots[i].vel.x; + spots[i].pos.y += spots[i].vel.y; + + if (spots[i].pos.x < 64) spots[i].vel.x = -spots[i].vel.x; + if (spots[i].pos.x > screenWidth - 64) spots[i].vel.x = -spots[i].vel.x; + if (spots[i].pos.y < 64) spots[i].vel.y = -spots[i].vel.y; + if (spots[i].pos.y > screenHeight - 64) spots[i].vel.y = -spots[i].vel.y; + } - SetShaderValue(spotShader, spotLoc[i], &spotPos[i].x, UNIFORM_VEC2); + SetShaderValue(spotShader, spots[i].posLoc, &spots[i].pos.x, UNIFORM_VEC2); } // Draw @@ -170,6 +213,11 @@ int main(void) EndShaderMode(); DrawFPS(10, 10); + + DrawText("Move the mouse!", 10, 30, 20, GREEN); + DrawText("Pitch Black", screenWidth * .2, screenHeight / 2, 20, GREEN); + DrawText("Dark", screenWidth * .66, screenHeight / 2, 20, GREEN); + EndDrawing(); //----------------------------------------------------------------------------------