25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

258 lines
8.7 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. #include <stddef.h>
  33. #include <stdint.h>
  34. #if defined(PLATFORM_DESKTOP)
  35. #define GLSL_VERSION 330
  36. #else // PLATFORM_RPI, 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. // Spot data
  42. typedef struct {
  43. Vector2 pos;
  44. Vector2 vel;
  45. float inner;
  46. float radius;
  47. // Shader locations
  48. unsigned int posLoc;
  49. unsigned int innerLoc;
  50. unsigned int radiusLoc;
  51. } Spot;
  52. // Stars in the star field have a position and velocity
  53. typedef struct Star {
  54. Vector2 pos;
  55. Vector2 vel;
  56. } Star;
  57. void UpdateStar(Star *s);
  58. void ResetStar(Star *s);
  59. //------------------------------------------------------------------------------------
  60. // Program main entry point
  61. //------------------------------------------------------------------------------------
  62. int main(void)
  63. {
  64. // Initialization
  65. //--------------------------------------------------------------------------------------
  66. const int screenWidth = 800;
  67. const int screenHeight = 450;
  68. InitWindow(screenWidth, screenHeight, "raylib - shader spotlight");
  69. HideCursor();
  70. Texture texRay = LoadTexture("resources/raysan.png");
  71. Star stars[MAX_STARS] = { 0 };
  72. for (int n = 0; n < MAX_STARS; n++) ResetStar(&stars[n]);
  73. // Progress all the stars on, so they don't all start in the centre
  74. for (int m = 0; m < screenWidth/2.0; m++)
  75. {
  76. for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
  77. }
  78. int frameCounter = 0;
  79. // Use default vert shader
  80. Shader shdrSpot = LoadShader(0, TextFormat("resources/shaders/glsl%i/spotlight.fs", GLSL_VERSION));
  81. // Get the locations of spots in the shader
  82. Spot spots[MAX_SPOTS];
  83. for (int i = 0; i < MAX_SPOTS; i++)
  84. {
  85. char posName[32] = "spots[x].pos\0";
  86. char innerName[32] = "spots[x].inner\0";
  87. char radiusName[32] = "spots[x].radius\0";
  88. posName[6] = '0' + i;
  89. innerName[6] = '0' + i;
  90. radiusName[6] = '0' + i;
  91. spots[i].posLoc = GetShaderLocation(shdrSpot, posName);
  92. spots[i].innerLoc = GetShaderLocation(shdrSpot, innerName);
  93. spots[i].radiusLoc = GetShaderLocation(shdrSpot, radiusName);
  94. }
  95. // Tell the shader how wide the screen is so we can have
  96. // a pitch black half and a dimly lit half.
  97. unsigned int wLoc = GetShaderLocation(shdrSpot, "screenWidth");
  98. float sw = (float)GetScreenWidth();
  99. SetShaderValue(shdrSpot, wLoc, &sw, SHADER_UNIFORM_FLOAT);
  100. // Randomize the locations and velocities of the spotlights
  101. // and initialize the shader locations
  102. for (int i = 0; i < MAX_SPOTS; i++)
  103. {
  104. spots[i].pos.x = (float)GetRandomValue(64, screenWidth - 64);
  105. spots[i].pos.y = (float)GetRandomValue(64, screenHeight - 64);
  106. spots[i].vel = (Vector2){ 0, 0 };
  107. while ((fabs(spots[i].vel.x) + fabs(spots[i].vel.y)) < 2)
  108. {
  109. spots[i].vel.x = GetRandomValue(-400, 40) / 10.0f;
  110. spots[i].vel.y = GetRandomValue(-400, 40) / 10.0f;
  111. }
  112. spots[i].inner = 28.0f * (i + 1);
  113. spots[i].radius = 48.0f * (i + 1);
  114. SetShaderValue(shdrSpot, spots[i].posLoc, &spots[i].pos.x, SHADER_UNIFORM_VEC2);
  115. SetShaderValue(shdrSpot, spots[i].innerLoc, &spots[i].inner, SHADER_UNIFORM_FLOAT);
  116. SetShaderValue(shdrSpot, spots[i].radiusLoc, &spots[i].radius, SHADER_UNIFORM_FLOAT);
  117. }
  118. SetTargetFPS(60); // Set to run at 60 frames-per-second
  119. //--------------------------------------------------------------------------------------
  120. // Main game loop
  121. while (!WindowShouldClose()) // Detect window close button or ESC key
  122. {
  123. // Update
  124. //----------------------------------------------------------------------------------
  125. frameCounter++;
  126. // Move the stars, resetting them if the go offscreen
  127. for (int n = 0; n < MAX_STARS; n++) UpdateStar(&stars[n]);
  128. // Update the spots, send them to the shader
  129. for (int i = 0; i < MAX_SPOTS; i++)
  130. {
  131. if (i == 0)
  132. {
  133. Vector2 mp = GetMousePosition();
  134. spots[i].pos.x = mp.x;
  135. spots[i].pos.y = screenHeight - mp.y;
  136. }
  137. else
  138. {
  139. spots[i].pos.x += spots[i].vel.x;
  140. spots[i].pos.y += spots[i].vel.y;
  141. if (spots[i].pos.x < 64) spots[i].vel.x = -spots[i].vel.x;
  142. if (spots[i].pos.x > (screenWidth - 64)) spots[i].vel.x = -spots[i].vel.x;
  143. if (spots[i].pos.y < 64) spots[i].vel.y = -spots[i].vel.y;
  144. if (spots[i].pos.y > (screenHeight - 64)) spots[i].vel.y = -spots[i].vel.y;
  145. }
  146. SetShaderValue(shdrSpot, spots[i].posLoc, &spots[i].pos.x, SHADER_UNIFORM_VEC2);
  147. }
  148. // Draw
  149. //----------------------------------------------------------------------------------
  150. BeginDrawing();
  151. ClearBackground(DARKBLUE);
  152. // Draw stars and bobs
  153. for (int n = 0; n < MAX_STARS; n++)
  154. {
  155. // Single pixel is just too small these days!
  156. DrawRectangle((int)stars[n].pos.x, (int)stars[n].pos.y, 2, 2, WHITE);
  157. }
  158. for (int i = 0; i < 16; i++)
  159. {
  160. DrawTexture(texRay,
  161. (int)((screenWidth/2.0f) + cos((frameCounter + i*8)/51.45f)*(screenWidth/2.2f) - 32),
  162. (int)((screenHeight/2.0f) + sin((frameCounter + i*8)/17.87f)*(screenHeight/4.2f)), WHITE);
  163. }
  164. // Draw spot lights
  165. BeginShaderMode(shdrSpot);
  166. // Instead of a blank rectangle you could render here
  167. // a render texture of the full screen used to do screen
  168. // scaling (slight adjustment to shader would be required
  169. // to actually pay attention to the colour!)
  170. DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
  171. EndShaderMode();
  172. DrawFPS(10, 10);
  173. DrawText("Move the mouse!", 10, 30, 20, GREEN);
  174. DrawText("Pitch Black", (int)(screenWidth*0.2f), screenHeight/2, 20, GREEN);
  175. DrawText("Dark", (int)(screenWidth*.66f), screenHeight/2, 20, GREEN);
  176. EndDrawing();
  177. //----------------------------------------------------------------------------------
  178. }
  179. // De-Initialization
  180. //--------------------------------------------------------------------------------------
  181. UnloadTexture(texRay);
  182. UnloadShader(shdrSpot);
  183. CloseWindow(); // Close window and OpenGL context
  184. //--------------------------------------------------------------------------------------
  185. return 0;
  186. }
  187. void ResetStar(Star *s)
  188. {
  189. s->pos = (Vector2){ GetScreenWidth()/2.0f, GetScreenHeight()/2.0f };
  190. do
  191. {
  192. s->vel.x = (float)GetRandomValue(-1000, 1000)/100.0f;
  193. s->vel.y = (float)GetRandomValue(-1000, 1000)/100.0f;
  194. } while (!(fabs(s->vel.x) + (fabs(s->vel.y) > 1)));
  195. s->pos = Vector2Add(s->pos, Vector2Multiply(s->vel, (Vector2){ 8.0f, 8.0f }));
  196. }
  197. void UpdateStar(Star *s)
  198. {
  199. s->pos = Vector2Add(s->pos, s->vel);
  200. if ((s->pos.x < 0) || (s->pos.x > GetScreenWidth()) ||
  201. (s->pos.y < 0) || (s->pos.y > GetScreenHeight()))
  202. {
  203. ResetStar(s);
  204. }
  205. }