shaders_mandelbrot_set.c 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - mandelbrot set
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  8. * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version
  9. *
  10. * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3)
  11. *
  12. * Example originally created with raylib 5.6, last time updated with raylib 5.6
  13. *
  14. * Example contributed by Jordi Santonja (@JordSant)
  15. * Based on previous work by Josh Colclough (@joshcol9232)
  16. *
  17. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  18. * BSD-like license that allows static linking with closed source software
  19. *
  20. * Copyright (c) 2025 Jordi Santonja (@JordSant)
  21. *
  22. ********************************************************************************************/
  23. #include "raylib.h"
  24. #include <math.h>
  25. #if defined(PLATFORM_DESKTOP)
  26. #define GLSL_VERSION 330
  27. #else // PLATFORM_ANDROID, PLATFORM_WEB
  28. #define GLSL_VERSION 100
  29. #endif
  30. // A few good interesting places
  31. const float pointsOfInterest[6][3] =
  32. {
  33. { -1.76826775f, -0.00422996283f, 28435.9238f },
  34. { 0.322004497f, -0.0357099883f, 56499.7266f },
  35. { -0.748880744f, -0.0562955774f, 9237.59082f },
  36. { -1.78385007f, -0.0156200649f, 14599.5283f },
  37. { -0.0985441282f, -0.924688697f, 26259.8535f },
  38. { 0.317785531f, -0.0322612226f, 29297.9258f },
  39. };
  40. const int screenWidth = 800;
  41. const int screenHeight = 450;
  42. const float zoomSpeed = 1.01f;
  43. const float offsetSpeedMul = 2.0f;
  44. const float startingZoom = 0.6f;
  45. const float startingOffset[2] = { -0.5f, 0.0f };
  46. //------------------------------------------------------------------------------------
  47. // Program main entry point
  48. //------------------------------------------------------------------------------------
  49. int main(void)
  50. {
  51. // Initialization
  52. //--------------------------------------------------------------------------------------
  53. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - mandelbrot set");
  54. // Load mandelbrot set shader
  55. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  56. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/mandelbrot_set.fs", GLSL_VERSION));
  57. // Create a RenderTexture2D to be used for render to texture
  58. RenderTexture2D target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
  59. // Offset and zoom to draw the mandelbrot set at. (centered on screen and default size)
  60. float offset[2] = { startingOffset[0], startingOffset[1] };
  61. float zoom = startingZoom;
  62. // Depending on the zoom the mximum number of iterations must be adapted to get more detail as we zzoom in
  63. // The solution is not perfect, so a control has been added to increase/decrease the number of iterations with UP/DOWN keys
  64. #if defined(PLATFORM_DESKTOP)
  65. int maxIterations = 333;
  66. float maxIterationsMultiplier = 166.5f;
  67. #else
  68. int maxIterations = 43;
  69. float maxIterationsMultiplier = 22.0f;
  70. #endif
  71. // Get variable (uniform) locations on the shader to connect with the program
  72. // NOTE: If uniform variable could not be found in the shader, function returns -1
  73. int zoomLoc = GetShaderLocation(shader, "zoom");
  74. int offsetLoc = GetShaderLocation(shader, "offset");
  75. int maxIterationsLoc = GetShaderLocation(shader, "maxIterations");
  76. // Upload the shader uniform values!
  77. SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
  78. SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);
  79. SetShaderValue(shader, maxIterationsLoc, &maxIterations, SHADER_UNIFORM_INT);
  80. bool showControls = true; // Show controls
  81. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  82. //--------------------------------------------------------------------------------------
  83. // Main game loop
  84. while (!WindowShouldClose()) // Detect window close button or ESC key
  85. {
  86. // Update
  87. //----------------------------------------------------------------------------------
  88. bool updateShader = false;
  89. // Press [1 - 6] to reset c to a point of interest
  90. if (IsKeyPressed(KEY_ONE) ||
  91. IsKeyPressed(KEY_TWO) ||
  92. IsKeyPressed(KEY_THREE) ||
  93. IsKeyPressed(KEY_FOUR) ||
  94. IsKeyPressed(KEY_FIVE) ||
  95. IsKeyPressed(KEY_SIX))
  96. {
  97. int interestIndex = 0;
  98. if (IsKeyPressed(KEY_ONE)) interestIndex = 0;
  99. else if (IsKeyPressed(KEY_TWO)) interestIndex = 1;
  100. else if (IsKeyPressed(KEY_THREE)) interestIndex = 2;
  101. else if (IsKeyPressed(KEY_FOUR)) interestIndex = 3;
  102. else if (IsKeyPressed(KEY_FIVE)) interestIndex = 4;
  103. else if (IsKeyPressed(KEY_SIX)) interestIndex = 5;
  104. offset[0] = pointsOfInterest[interestIndex][0];
  105. offset[1] = pointsOfInterest[interestIndex][1];
  106. zoom = pointsOfInterest[interestIndex][2];
  107. updateShader = true;
  108. }
  109. // If "R" is pressed, reset zoom and offset
  110. if (IsKeyPressed(KEY_R))
  111. {
  112. offset[0] = startingOffset[0];
  113. offset[1] = startingOffset[1];
  114. zoom = startingZoom;
  115. updateShader = true;
  116. }
  117. if (IsKeyPressed(KEY_F1)) showControls = !showControls; // Toggle whether or not to show controls
  118. // Change number of max iterations with UP and DOWN keys
  119. // WARNING: Increasing the number of max iterations greatly impacts performance
  120. if (IsKeyPressed(KEY_UP))
  121. {
  122. maxIterationsMultiplier *= 1.4f;
  123. updateShader = true;
  124. }
  125. else if (IsKeyPressed(KEY_DOWN))
  126. {
  127. maxIterationsMultiplier /= 1.4f;
  128. updateShader = true;
  129. }
  130. // If either left or right button is pressed, zoom in/out
  131. if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
  132. {
  133. // Change zoom. If Mouse left -> zoom in. Mouse right -> zoom out
  134. zoom *= IsMouseButtonDown(MOUSE_BUTTON_LEFT)? zoomSpeed : (1.0f/zoomSpeed);
  135. const Vector2 mousePos = GetMousePosition();
  136. Vector2 offsetVelocity;
  137. // Find the velocity at which to change the camera. Take the distance of the mouse
  138. // From the center of the screen as the direction, and adjust magnitude based on the current zoom
  139. offsetVelocity.x = (mousePos.x/(float)screenWidth - 0.5f)*offsetSpeedMul/zoom;
  140. offsetVelocity.y = (mousePos.y/(float)screenHeight - 0.5f)*offsetSpeedMul/zoom;
  141. // Apply move velocity to camera
  142. offset[0] += GetFrameTime()*offsetVelocity.x;
  143. offset[1] += GetFrameTime()*offsetVelocity.y;
  144. updateShader = true;
  145. }
  146. // In case a parameter has been changed, update the shader values
  147. if (updateShader)
  148. {
  149. // As we zoom in, increase the number of max iterations to get more detail
  150. // Aproximate formula, but it works-ish
  151. maxIterations = (int)(sqrtf(2.0f*sqrtf(fabsf(1.0f - sqrtf(37.5f*zoom))))*maxIterationsMultiplier);
  152. // Update the shader uniform values!
  153. SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
  154. SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);
  155. SetShaderValue(shader, maxIterationsLoc, &maxIterations, SHADER_UNIFORM_INT);
  156. }
  157. //----------------------------------------------------------------------------------
  158. // Draw
  159. //----------------------------------------------------------------------------------
  160. // Using a render texture to draw Mandelbrot set
  161. BeginTextureMode(target); // Enable drawing to texture
  162. ClearBackground(BLACK); // Clear the render texture
  163. // Draw a rectangle in shader mode to be used as shader canvas
  164. // NOTE: Rectangle uses font white character texture coordinates,
  165. // So shader can not be applied here directly because input vertexTexCoord
  166. // Do not represent full screen coordinates (space where want to apply shader)
  167. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK);
  168. EndTextureMode();
  169. BeginDrawing();
  170. ClearBackground(BLACK); // Clear screen background
  171. // Draw the saved texture and rendered mandelbrot set with shader
  172. // NOTE: We do not invert texture on Y, already considered inside shader
  173. BeginShaderMode(shader);
  174. // WARNING: If FLAG_WINDOW_HIGHDPI is enabled, HighDPI monitor scaling should be considered
  175. // When rendering the RenderTexture2D to fit in the HighDPI scaled Window
  176. DrawTextureEx(target.texture, (Vector2){ 0.0f, 0.0f }, 0.0f, 1.0f, WHITE);
  177. EndShaderMode();
  178. if (showControls)
  179. {
  180. DrawText("Press Mouse buttons right/left to zoom in/out and move", 10, 15, 10, RAYWHITE);
  181. DrawText("Press F1 to toggle these controls", 10, 30, 10, RAYWHITE);
  182. DrawText("Press [1 - 6] to change point of interest", 10, 45, 10, RAYWHITE);
  183. DrawText("Press UP | DOWN to change number of iterations", 10, 60, 10, RAYWHITE);
  184. DrawText("Press R to recenter the camera", 10, 75, 10, RAYWHITE);
  185. }
  186. EndDrawing();
  187. //----------------------------------------------------------------------------------
  188. }
  189. // De-Initialization
  190. //--------------------------------------------------------------------------------------
  191. UnloadShader(shader); // Unload shader
  192. UnloadRenderTexture(target); // Unload render texture
  193. CloseWindow(); // Close window and OpenGL context
  194. //--------------------------------------------------------------------------------------
  195. return 0;
  196. }