shaders_spotlight_rendering.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - spotlight rendering
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 2.5, last time updated with raylib 3.7
  8. *
  9. * Example contributed by Chris Camacho (@chriscamacho) and reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2019-2025 Chris Camacho (@chriscamacho) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************
  17. *
  18. * The shader makes alpha holes in the forground to give the appearance of a top
  19. * down look at a spotlight casting a pool of light...
  20. *
  21. * The right hand side of the screen there is just enough light to see whats
  22. * going on without the spot light, great for a stealth type game where you
  23. * have to avoid the spotlights
  24. *
  25. * The left hand side of the screen is in pitch dark except for where the spotlights are
  26. *
  27. * Although this example doesn't scale like the letterbox example, you could integrate
  28. * the two techniques, but by scaling the actual colour of the render texture rather
  29. * than using alpha as a mask
  30. *
  31. ********************************************************************************************/
  32. #include "raylib.h"
  33. #include "raymath.h"
  34. #if defined(PLATFORM_DESKTOP)
  35. #define GLSL_VERSION 330
  36. #else // PLATFORM_ANDROID, PLATFORM_WEB
  37. #define GLSL_VERSION 100
  38. #endif
  39. #define MAX_SPOTS 3 // NOTE: It must be the same as define in shader
  40. #define MAX_STARS 400
  41. //----------------------------------------------------------------------------------
  42. // Types and Structures Definition
  43. //----------------------------------------------------------------------------------
  44. // Spot data
  45. typedef struct Spot {
  46. Vector2 position;
  47. Vector2 speed;
  48. float inner;
  49. float radius;
  50. // Shader locations
  51. unsigned int positionLoc;
  52. unsigned int innerLoc;
  53. unsigned int radiusLoc;
  54. } Spot;
  55. // Stars in the star field have a position and velocity
  56. typedef struct Star {
  57. Vector2 position;
  58. Vector2 speed;
  59. } Star;
  60. //--------------------------------------------------------------------------------------
  61. // Module Functions Declaration
  62. //--------------------------------------------------------------------------------------
  63. static void UpdateStar(Star *star);
  64. static void ResetStar(Star *star);
  65. //------------------------------------------------------------------------------------
  66. // Program main entry point
  67. //------------------------------------------------------------------------------------
  68. int main(void)
  69. {
  70. // Initialization
  71. //--------------------------------------------------------------------------------------
  72. const int screenWidth = 800;
  73. const int screenHeight = 450;
  74. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - spotlight rendering");
  75. HideCursor();
  76. Texture texRay = LoadTexture("resources/raysan.png");
  77. Star stars[MAX_STARS] = { 0 };
  78. for (int n = 0; n < MAX_STARS; n++) ResetStar(&stars[n]);
  79. // Progress all the stars on, so they don't all start in the centre
  80. for (int m = 0; m < screenWidth/2.0; m++)
  81. {
  82. for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
  83. }
  84. int frameCounter = 0;
  85. // Use default vert shader
  86. Shader shdrSpot = LoadShader(0, TextFormat("resources/shaders/glsl%i/spotlight.fs", GLSL_VERSION));
  87. // Get the locations of spots in the shader
  88. Spot spots[MAX_SPOTS];
  89. for (int i = 0; i < MAX_SPOTS; i++)
  90. {
  91. char posName[32] = "spots[x].pos\0";
  92. char innerName[32] = "spots[x].inner\0";
  93. char radiusName[32] = "spots[x].radius\0";
  94. posName[6] = '0' + i;
  95. innerName[6] = '0' + i;
  96. radiusName[6] = '0' + i;
  97. spots[i].positionLoc = GetShaderLocation(shdrSpot, posName);
  98. spots[i].innerLoc = GetShaderLocation(shdrSpot, innerName);
  99. spots[i].radiusLoc = GetShaderLocation(shdrSpot, radiusName);
  100. }
  101. // Tell the shader how wide the screen is so we can have
  102. // a pitch black half and a dimly lit half
  103. unsigned int wLoc = GetShaderLocation(shdrSpot, "screenWidth");
  104. float sw = (float)GetScreenWidth();
  105. SetShaderValue(shdrSpot, wLoc, &sw, SHADER_UNIFORM_FLOAT);
  106. // Randomize the locations and velocities of the spotlights
  107. // and initialize the shader locations
  108. for (int i = 0; i < MAX_SPOTS; i++)
  109. {
  110. spots[i].position.x = (float)GetRandomValue(64, screenWidth - 64);
  111. spots[i].position.y = (float)GetRandomValue(64, screenHeight - 64);
  112. spots[i].speed = (Vector2){ 0, 0 };
  113. while ((fabs(spots[i].speed.x) + fabs(spots[i].speed.y)) < 2)
  114. {
  115. spots[i].speed.x = GetRandomValue(-400, 40)/25.0f;
  116. spots[i].speed.y = GetRandomValue(-400, 40)/25.0f;
  117. }
  118. spots[i].inner = 28.0f*(i + 1);
  119. spots[i].radius = 48.0f*(i + 1);
  120. SetShaderValue(shdrSpot, spots[i].positionLoc, &spots[i].position.x, SHADER_UNIFORM_VEC2);
  121. SetShaderValue(shdrSpot, spots[i].innerLoc, &spots[i].inner, SHADER_UNIFORM_FLOAT);
  122. SetShaderValue(shdrSpot, spots[i].radiusLoc, &spots[i].radius, SHADER_UNIFORM_FLOAT);
  123. }
  124. SetTargetFPS(60); // Set to run at 60 frames-per-second
  125. //--------------------------------------------------------------------------------------
  126. // Main game loop
  127. while (!WindowShouldClose()) // Detect window close button or ESC key
  128. {
  129. // Update
  130. //----------------------------------------------------------------------------------
  131. frameCounter++;
  132. // Move the stars, resetting them if the go offscreen
  133. for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
  134. // Update the spots, send them to the shader
  135. for (int i = 0; i < MAX_SPOTS; i++)
  136. {
  137. if (i == 0)
  138. {
  139. Vector2 mp = GetMousePosition();
  140. spots[i].position.x = mp.x;
  141. spots[i].position.y = screenHeight - mp.y;
  142. }
  143. else
  144. {
  145. spots[i].position.x += spots[i].speed.x;
  146. spots[i].position.y += spots[i].speed.y;
  147. if (spots[i].position.x < 64) spots[i].speed.x = -spots[i].speed.x;
  148. if (spots[i].position.x > (screenWidth - 64)) spots[i].speed.x = -spots[i].speed.x;
  149. if (spots[i].position.y < 64) spots[i].speed.y = -spots[i].speed.y;
  150. if (spots[i].position.y > (screenHeight - 64)) spots[i].speed.y = -spots[i].speed.y;
  151. }
  152. SetShaderValue(shdrSpot, spots[i].positionLoc, &spots[i].position.x, SHADER_UNIFORM_VEC2);
  153. }
  154. // Draw
  155. //----------------------------------------------------------------------------------
  156. BeginDrawing();
  157. ClearBackground(DARKBLUE);
  158. // Draw stars and bobs
  159. for (int n = 0; n < MAX_STARS; n++)
  160. {
  161. // Single pixel is just too small these days!
  162. DrawRectangle((int)stars[n].position.x, (int)stars[n].position.y, 2, 2, WHITE);
  163. }
  164. for (int i = 0; i < 16; i++)
  165. {
  166. DrawTexture(texRay,
  167. (int)((screenWidth/2.0f) + cos((frameCounter + i*8)/51.45f)*(screenWidth/2.2f) - 32),
  168. (int)((screenHeight/2.0f) + sin((frameCounter + i*8)/17.87f)*(screenHeight/4.2f)), WHITE);
  169. }
  170. // Draw spot lights
  171. BeginShaderMode(shdrSpot);
  172. // Instead of a blank rectangle you could render here
  173. // a render texture of the full screen used to do screen
  174. // scaling (slight adjustment to shader would be required
  175. // to actually pay attention to the colour!)
  176. DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
  177. EndShaderMode();
  178. DrawFPS(10, 10);
  179. DrawText("Move the mouse!", 10, 30, 20, GREEN);
  180. DrawText("Pitch Black", (int)(screenWidth*0.2f), screenHeight/2, 20, GREEN);
  181. DrawText("Dark", (int)(screenWidth*.66f), screenHeight/2, 20, GREEN);
  182. EndDrawing();
  183. //----------------------------------------------------------------------------------
  184. }
  185. // De-Initialization
  186. //--------------------------------------------------------------------------------------
  187. UnloadTexture(texRay);
  188. UnloadShader(shdrSpot);
  189. CloseWindow(); // Close window and OpenGL context
  190. //--------------------------------------------------------------------------------------
  191. return 0;
  192. }
  193. //--------------------------------------------------------------------------------------
  194. // Module Functions Definition
  195. //--------------------------------------------------------------------------------------
  196. static void ResetStar(Star *star)
  197. {
  198. star->position = (Vector2){ GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
  199. star->speed.x = (float)GetRandomValue(-1000, 1000)/100.0f;
  200. star->speed.y = (float)GetRandomValue(-1000, 1000)/100.0f;
  201. while (!(fabs(star->speed.x) + (fabs(star->speed.y) > 1)))
  202. {
  203. star->speed.x = (float)GetRandomValue(-1000, 1000)/100.0f;
  204. star->speed.y = (float)GetRandomValue(-1000, 1000)/100.0f;
  205. }
  206. star->position = Vector2Add(star->position, Vector2Multiply(star->speed, (Vector2){ 8.0f, 8.0f }));
  207. }
  208. static void UpdateStar(Star *star)
  209. {
  210. star->position = Vector2Add(star->position, star->speed);
  211. if ((star->position.x < 0) || (star->position.x > GetScreenWidth()) ||
  212. (star->position.y < 0) || (star->position.y > GetScreenHeight())) ResetStar(star);
  213. }