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.

254 line
8.7 KiB

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