raylib/examples/shaders/shaders_spotlight.c

256 lines
8.9 KiB
C
Raw Normal View History

/*******************************************************************************************
*
* raylib [shaders] example - Simple shader mask
*
2022-07-20 02:28:37 +03:00
* Example originally created with raylib 2.5, last time updated with raylib 3.7
*
2022-07-20 02:28:37 +03:00
* Example contributed by Chris Camacho (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
*
2022-07-20 02:28:37 +03:00
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
2024-01-02 22:58:12 +03:00
* Copyright (c) 2019-2024 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5)
*
********************************************************************************************
*
* The shader makes alpha holes in the forground to give the appearance of a top
* down look at a spotlight casting a pool of light...
2021-04-22 19:55:24 +03:00
*
* The right hand side of the screen there is just enough light to see whats
* going on without the spot light, great for a stealth type game where you
* have to avoid the spotlights.
2021-04-22 19:55:24 +03:00
*
* The left hand side of the screen is in pitch dark except for where the spotlights are.
2021-04-22 19:55:24 +03:00
*
* Although this example doesn't scale like the letterbox example, you could integrate
* the two techniques, but by scaling the actual colour of the render texture rather
* than using alpha as a mask.
*
********************************************************************************************/
#include "raylib.h"
2023-02-14 22:00:51 +03:00
#include "raymath.h"
#if defined(PLATFORM_DESKTOP)
#define GLSL_VERSION 330
#else // PLATFORM_ANDROID, PLATFORM_WEB
#define GLSL_VERSION 100
#endif
2020-12-23 22:30:00 +03:00
#define MAX_SPOTS 3 // NOTE: It must be the same as define in shader
#define MAX_STARS 400
// Spot data
2023-02-14 22:00:51 +03:00
typedef struct Spot {
Vector2 position;
Vector2 speed;
float inner;
float radius;
2021-04-22 19:55:24 +03:00
// Shader locations
2023-02-14 22:00:51 +03:00
unsigned int positionLoc;
unsigned int innerLoc;
unsigned int radiusLoc;
} Spot;
// Stars in the star field have a position and velocity
typedef struct Star {
2023-02-14 22:00:51 +03:00
Vector2 position;
Vector2 speed;
} Star;
2023-02-14 22:00:51 +03:00
static void UpdateStar(Star *s);
static void ResetStar(Star *s);
2022-06-21 20:53:18 +03:00
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
2023-02-14 22:00:51 +03:00
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - shader spotlight");
HideCursor();
Texture texRay = LoadTexture("resources/raysan.png");
2021-04-22 19:55:24 +03:00
2020-12-23 22:30:00 +03:00
Star stars[MAX_STARS] = { 0 };
2020-12-23 22:30:00 +03:00
for (int n = 0; n < MAX_STARS; n++) ResetStar(&stars[n]);
// Progress all the stars on, so they don't all start in the centre
2021-04-22 19:55:24 +03:00
for (int m = 0; m < screenWidth/2.0; m++)
{
2020-12-23 22:30:00 +03:00
for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
}
int frameCounter = 0;
2021-04-22 19:55:24 +03:00
// Use default vert shader
2020-12-23 22:30:00 +03:00
Shader shdrSpot = LoadShader(0, TextFormat("resources/shaders/glsl%i/spotlight.fs", GLSL_VERSION));
2021-04-22 19:55:24 +03:00
2020-12-23 22:30:00 +03:00
// Get the locations of spots in the shader
Spot spots[MAX_SPOTS];
2021-04-22 19:55:24 +03:00
for (int i = 0; i < MAX_SPOTS; i++)
{
2020-12-23 22:30:00 +03:00
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;
2021-04-22 19:55:24 +03:00
2023-02-14 22:00:51 +03:00
spots[i].positionLoc = GetShaderLocation(shdrSpot, posName);
2020-12-23 22:30:00 +03:00
spots[i].innerLoc = GetShaderLocation(shdrSpot, innerName);
spots[i].radiusLoc = GetShaderLocation(shdrSpot, radiusName);
2021-04-22 19:55:24 +03:00
2020-12-23 22:30:00 +03:00
}
2021-04-22 19:55:24 +03:00
2020-12-23 22:30:00 +03:00
// Tell the shader how wide the screen is so we can have
// a pitch black half and a dimly lit half.
unsigned int wLoc = GetShaderLocation(shdrSpot, "screenWidth");
float sw = (float)GetScreenWidth();
SetShaderValue(shdrSpot, wLoc, &sw, SHADER_UNIFORM_FLOAT);
2020-12-23 22:30:00 +03:00
// Randomize the locations and velocities of the spotlights
// and initialize the shader locations
2020-12-23 22:30:00 +03:00
for (int i = 0; i < MAX_SPOTS; i++)
{
2023-02-14 22:00:51 +03:00
spots[i].position.x = (float)GetRandomValue(64, screenWidth - 64);
spots[i].position.y = (float)GetRandomValue(64, screenHeight - 64);
spots[i].speed = (Vector2){ 0, 0 };
2021-04-22 19:55:24 +03:00
2023-02-14 22:00:51 +03:00
while ((fabs(spots[i].speed.x) + fabs(spots[i].speed.y)) < 2)
{
2023-02-14 22:00:51 +03:00
spots[i].speed.x = GetRandomValue(-400, 40) / 10.0f;
spots[i].speed.y = GetRandomValue(-400, 40) / 10.0f;
2020-12-23 22:30:00 +03:00
}
2021-04-22 19:55:24 +03:00
spots[i].inner = 28.0f * (i + 1);
spots[i].radius = 48.0f * (i + 1);
2021-04-22 19:55:24 +03:00
2023-02-14 22:00:51 +03:00
SetShaderValue(shdrSpot, spots[i].positionLoc, &spots[i].position.x, SHADER_UNIFORM_VEC2);
SetShaderValue(shdrSpot, spots[i].innerLoc, &spots[i].inner, SHADER_UNIFORM_FLOAT);
SetShaderValue(shdrSpot, spots[i].radiusLoc, &spots[i].radius, SHADER_UNIFORM_FLOAT);
2020-12-23 22:30:00 +03:00
}
SetTargetFPS(60); // Set to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
2021-04-22 19:55:24 +03:00
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
frameCounter++;
2020-12-23 22:30:00 +03:00
// Move the stars, resetting them if the go offscreen
for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
2020-12-23 22:30:00 +03:00
// Update the spots, send them to the shader
for (int i = 0; i < MAX_SPOTS; i++)
{
2020-12-23 22:30:00 +03:00
if (i == 0)
{
Vector2 mp = GetMousePosition();
2023-02-14 22:00:51 +03:00
spots[i].position.x = mp.x;
spots[i].position.y = screenHeight - mp.y;
2020-12-23 22:30:00 +03:00
}
else
{
2023-02-14 22:00:51 +03:00
spots[i].position.x += spots[i].speed.x;
spots[i].position.y += spots[i].speed.y;
2021-04-22 19:55:24 +03:00
2023-02-14 22:00:51 +03:00
if (spots[i].position.x < 64) spots[i].speed.x = -spots[i].speed.x;
if (spots[i].position.x > (screenWidth - 64)) spots[i].speed.x = -spots[i].speed.x;
if (spots[i].position.y < 64) spots[i].speed.y = -spots[i].speed.y;
if (spots[i].position.y > (screenHeight - 64)) spots[i].speed.y = -spots[i].speed.y;
2020-12-23 22:30:00 +03:00
}
2021-04-22 19:55:24 +03:00
2023-02-14 22:00:51 +03:00
SetShaderValue(shdrSpot, spots[i].positionLoc, &spots[i].position.x, SHADER_UNIFORM_VEC2);
2020-12-23 22:30:00 +03:00
}
2021-04-22 19:55:24 +03:00
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(DARKBLUE);
2020-12-23 22:30:00 +03:00
// Draw stars and bobs
for (int n = 0; n < MAX_STARS; n++)
{
2020-12-23 22:30:00 +03:00
// Single pixel is just too small these days!
2023-02-14 22:00:51 +03:00
DrawRectangle((int)stars[n].position.x, (int)stars[n].position.y, 2, 2, WHITE);
}
for (int i = 0; i < 16; i++)
{
DrawTexture(texRay,
2021-10-25 11:21:16 +03:00
(int)((screenWidth/2.0f) + cos((frameCounter + i*8)/51.45f)*(screenWidth/2.2f) - 32),
(int)((screenHeight/2.0f) + sin((frameCounter + i*8)/17.87f)*(screenHeight/4.2f)), WHITE);
}
2020-12-23 22:30:00 +03:00
// Draw spot lights
BeginShaderMode(shdrSpot);
// Instead of a blank rectangle you could render here
// a render texture of the full screen used to do screen
// scaling (slight adjustment to shader would be required
// to actually pay attention to the colour!)
DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
EndShaderMode();
DrawFPS(10, 10);
2021-04-22 19:55:24 +03:00
DrawText("Move the mouse!", 10, 30, 20, GREEN);
2021-10-25 11:21:16 +03:00
DrawText("Pitch Black", (int)(screenWidth*0.2f), screenHeight/2, 20, GREEN);
DrawText("Dark", (int)(screenWidth*.66f), screenHeight/2, 20, GREEN);
2021-04-22 19:55:24 +03:00
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadTexture(texRay);
2020-12-23 22:30:00 +03:00
UnloadShader(shdrSpot);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
2023-02-14 22:00:51 +03:00
static void ResetStar(Star *s)
{
2023-02-14 22:00:51 +03:00
s->position = (Vector2){ GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
2021-04-22 19:55:24 +03:00
do
{
2023-02-14 22:00:51 +03:00
s->speed.x = (float)GetRandomValue(-1000, 1000)/100.0f;
s->speed.y = (float)GetRandomValue(-1000, 1000)/100.0f;
2021-04-22 19:55:24 +03:00
2023-02-14 22:00:51 +03:00
} while (!(fabs(s->speed.x) + (fabs(s->speed.y) > 1)));
2021-04-22 19:55:24 +03:00
2023-02-14 22:00:51 +03:00
s->position = Vector2Add(s->position, Vector2Multiply(s->speed, (Vector2){ 8.0f, 8.0f }));
}
2023-02-14 22:00:51 +03:00
static void UpdateStar(Star *s)
{
2023-02-14 22:00:51 +03:00
s->position = Vector2Add(s->position, s->speed);
2021-04-22 19:55:24 +03:00
2023-02-14 22:00:51 +03:00
if ((s->position.x < 0) || (s->position.x > GetScreenWidth()) ||
(s->position.y < 0) || (s->position.y > GetScreenHeight()))
{
ResetStar(s);
}
}