spotlight example, each spot has own radius, mouse countrol (#1148)
NB glsl100 shader needs testing on "bare metal" Co-authored-by: codifies <nospam@antispam.com>
This commit is contained in:
parent
e07281f8bd
commit
2dbcef218c
3 changed files with 148 additions and 58 deletions
|
@ -2,50 +2,67 @@
|
||||||
|
|
||||||
precision mediump float;
|
precision mediump float;
|
||||||
|
|
||||||
#define MAX_SPOTS 4
|
#define MAX_SPOTS 3
|
||||||
#define RADIUS 256.0
|
|
||||||
#define INNER 200.0
|
|
||||||
|
|
||||||
// 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()
|
void main()
|
||||||
{
|
{
|
||||||
|
|
||||||
float alpha;
|
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);
|
vec2 pos = vec2(gl_FragCoord.x, gl_FragCoord.y);
|
||||||
|
|
||||||
|
|
||||||
// find out which spotlight is nearest
|
// find out which spotlight is nearest
|
||||||
float d = 65000.0; // some high value
|
float d = 65000.0; // some high value
|
||||||
float di = 0.0;
|
int fi = -1;
|
||||||
|
|
||||||
for (int i = 0; i < MAX_SPOTS; i++)
|
for (int i = 0; i < MAX_SPOTS; i++)
|
||||||
{
|
{
|
||||||
di = distance(pos, spots[i]);
|
for (int j = 0; j < MAX_SPOTS; j++)
|
||||||
if (d > di) d = di;
|
{
|
||||||
|
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...
|
// d now equals distance to nearest spot...
|
||||||
if (d > RADIUS) {
|
// allowing for the different radii of all spotlights
|
||||||
alpha = 1.0;
|
if (fi != -1) {
|
||||||
} else {
|
|
||||||
if (d < INNER) {
|
if (d > spots[fi].radius)
|
||||||
alpha = 0.0;
|
{
|
||||||
} else {
|
alpha = 1.0;
|
||||||
alpha = (d - INNER) / (RADIUS - INNER);
|
}
|
||||||
|
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
|
// Right hand side of screen is dimly lit,
|
||||||
// threshold value user definable.
|
// could make the threshold value user definable
|
||||||
if (pos.x > screenWidth/2.0 && alpha > 0.9) {
|
if ((pos.x > screenWidth/2.0) && (alpha > 0.9)) alpha = 0.9;
|
||||||
alpha = 0.9;
|
|
||||||
}
|
|
||||||
|
|
||||||
// could make the black out colour user definable...
|
// could make the black out colour user definable...
|
||||||
gl_FragColor = vec4( 0, 0, 0, alpha);
|
gl_FragColor = vec4( 0, 0, 0, alpha);
|
||||||
|
|
|
@ -9,36 +9,61 @@ out vec4 finalColor;
|
||||||
|
|
||||||
// NOTE: Add here your custom variables
|
// NOTE: Add here your custom variables
|
||||||
|
|
||||||
#define MAX_SPOTS 2
|
#define MAX_SPOTS 3
|
||||||
#define RADIUS 128
|
|
||||||
#define INNER 96
|
|
||||||
|
|
||||||
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
|
uniform float screenWidth; // Width of the screen
|
||||||
|
|
||||||
void main()
|
void main()
|
||||||
{
|
{
|
||||||
float alpha = 0.0;
|
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);
|
vec2 pos = vec2(gl_FragCoord.x, gl_FragCoord.y);
|
||||||
|
|
||||||
// Find out which spotlight is nearest
|
// Find out which spotlight is nearest
|
||||||
float d = 65000; // some high value
|
float d = 65000; // some high value
|
||||||
float di = 0;
|
int fi = -1; // found index
|
||||||
|
|
||||||
for (int i = 0; i < MAX_SPOTS; i++)
|
for (int i = 0; i < MAX_SPOTS; i++)
|
||||||
{
|
{
|
||||||
di = distance(pos, spots[i]);
|
for (int j = 0; j < MAX_SPOTS; j++)
|
||||||
if (d > di) d = di;
|
{
|
||||||
|
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...
|
// d now equals distance to nearest spot...
|
||||||
if (d > RADIUS) alpha = 1.0;
|
// allowing for the different radii of all spotlights
|
||||||
else
|
if (fi != -1) {
|
||||||
{
|
|
||||||
if (d < INNER) alpha = 0.0;
|
if (d > spots[fi].radius)
|
||||||
else alpha = (d - INNER)/(RADIUS - INNER);
|
{
|
||||||
|
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,
|
// Right hand side of screen is dimly lit,
|
||||||
|
|
|
@ -39,9 +39,24 @@
|
||||||
#define GLSL_VERSION 100
|
#define GLSL_VERSION 100
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define MAXSPOT 2
|
|
||||||
|
#define MAXSPOT 3 // NB must be the same as define in shader
|
||||||
#define numStars 400
|
#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
|
// Stars in the star field have a position and velocity
|
||||||
typedef struct Star {
|
typedef struct Star {
|
||||||
Vector2 pos;
|
Vector2 pos;
|
||||||
|
@ -59,6 +74,7 @@ int main(void)
|
||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib - shader spotlight");
|
InitWindow(screenWidth, screenHeight, "raylib - shader spotlight");
|
||||||
|
HideCursor();
|
||||||
|
|
||||||
Texture texRay = LoadTexture("resources/raysan.png");
|
Texture texRay = LoadTexture("resources/raysan.png");
|
||||||
|
|
||||||
|
@ -74,20 +90,32 @@ int main(void)
|
||||||
|
|
||||||
int frameCounter = 0;
|
int frameCounter = 0;
|
||||||
|
|
||||||
unsigned int spotLoc[MAXSPOT]; // shader locations
|
|
||||||
|
|
||||||
Vector2 spotPos[MAXSPOT]; // position and velocity
|
|
||||||
Vector2 spotVel[MAXSPOT];
|
|
||||||
|
|
||||||
// Use default vert shader
|
// Use default vert shader
|
||||||
Shader spotShader = LoadShader(0, FormatText("resources/shaders/glsl%i/spotlight.fs", GLSL_VERSION));
|
Shader spotShader = LoadShader(0, FormatText("resources/shaders/glsl%i/spotlight.fs", GLSL_VERSION));
|
||||||
|
|
||||||
// Get the locations of spots in the shader
|
// 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++)
|
for (int i = 0; i < MAXSPOT; i++)
|
||||||
{
|
{
|
||||||
spotName[6] = '0' + i;
|
char posName[32] = "spots[x].pos\0";
|
||||||
spotLoc[i] = GetShaderLocation(spotShader, spotName);
|
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
|
// 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
|
// randomise the locations and velocities of the spotlights
|
||||||
|
// and initialise the shader locations
|
||||||
for (int i = 0; i < MAXSPOT; i++)
|
for (int i = 0; i < MAXSPOT; i++)
|
||||||
{
|
{
|
||||||
spotPos[i].x = GetRandomValue(64, screenWidth - 64);
|
|
||||||
spotPos[i].y = GetRandomValue(64, screenHeight - 64);
|
spots[i].pos.x = GetRandomValue(64, screenWidth - 64);
|
||||||
spotVel[i] = (Vector2){ 0, 0 };
|
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;
|
spots[i].vel.x = GetRandomValue(-40, 40)/10.0;
|
||||||
spotVel[i].y = 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
|
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
|
// Update the spots, send them to the shader
|
||||||
for (int i = 0; i < MAXSPOT; i++)
|
for (int i = 0; i < MAXSPOT; i++)
|
||||||
{
|
{
|
||||||
spotPos[i].x += spotVel[i].x;
|
if ( i == 0 ) {
|
||||||
spotPos[i].y += spotVel[i].y;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
if (spotPos[i].x < 64) spotVel[i].x = -spotVel[i].x;
|
SetShaderValue(spotShader, spots[i].posLoc, &spots[i].pos.x, UNIFORM_VEC2);
|
||||||
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;
|
|
||||||
|
|
||||||
SetShaderValue(spotShader, spotLoc[i], &spotPos[i].x, UNIFORM_VEC2);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Draw
|
// Draw
|
||||||
|
@ -170,6 +213,11 @@ int main(void)
|
||||||
EndShaderMode();
|
EndShaderMode();
|
||||||
|
|
||||||
DrawFPS(10, 10);
|
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();
|
EndDrawing();
|
||||||
//----------------------------------------------------------------------------------
|
//----------------------------------------------------------------------------------
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue