You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

255 lines
8.9 KiB

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