shaders_julia_set.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Julia sets
  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 2.5, last time updated with raylib 4.0
  13. *
  14. * Example contributed by Josh Colclough (@joshcol9232) and reviewed by Ramon Santamaria (@raysan5)
  15. *
  16. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  17. * BSD-like license that allows static linking with closed source software
  18. *
  19. * Copyright (c) 2019-2025 Josh Colclough (@joshcol9232) and Ramon Santamaria (@raysan5)
  20. *
  21. ********************************************************************************************/
  22. #include "raylib.h"
  23. #if defined(PLATFORM_DESKTOP)
  24. #define GLSL_VERSION 330
  25. #else // PLATFORM_ANDROID, PLATFORM_WEB
  26. #define GLSL_VERSION 100
  27. #endif
  28. // A few good julia sets
  29. const float pointsOfInterest[6][2] =
  30. {
  31. { -0.348827f, 0.607167f },
  32. { -0.786268f, 0.169728f },
  33. { -0.8f, 0.156f },
  34. { 0.285f, 0.0f },
  35. { -0.835f, -0.2321f },
  36. { -0.70176f, -0.3842f },
  37. };
  38. const int screenWidth = 800;
  39. const int screenHeight = 450;
  40. const float zoomSpeed = 1.01f;
  41. const float offsetSpeedMul = 2.0f;
  42. const float startingZoom = 0.75f;
  43. //------------------------------------------------------------------------------------
  44. // Program main entry point
  45. //------------------------------------------------------------------------------------
  46. int main(void)
  47. {
  48. // Initialization
  49. //--------------------------------------------------------------------------------------
  50. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia sets");
  51. // Load julia set shader
  52. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  53. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/julia_set.fs", GLSL_VERSION));
  54. // Create a RenderTexture2D to be used for render to texture
  55. RenderTexture2D target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
  56. // c constant to use in z^2 + c
  57. float c[2] = { pointsOfInterest[0][0], pointsOfInterest[0][1] };
  58. // Offset and zoom to draw the julia set at. (centered on screen and default size)
  59. float offset[2] = { 0.0f, 0.0f };
  60. float zoom = startingZoom;
  61. // Get variable (uniform) locations on the shader to connect with the program
  62. // NOTE: If uniform variable could not be found in the shader, function returns -1
  63. int cLoc = GetShaderLocation(shader, "c");
  64. int zoomLoc = GetShaderLocation(shader, "zoom");
  65. int offsetLoc = GetShaderLocation(shader, "offset");
  66. // Upload the shader uniform values!
  67. SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
  68. SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
  69. SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);
  70. int incrementSpeed = 0; // Multiplier of speed to change c value
  71. bool showControls = true; // Show controls
  72. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  73. //--------------------------------------------------------------------------------------
  74. // Main game loop
  75. while (!WindowShouldClose()) // Detect window close button or ESC key
  76. {
  77. // Update
  78. //----------------------------------------------------------------------------------
  79. // Press [1 - 6] to reset c to a point of interest
  80. if (IsKeyPressed(KEY_ONE) ||
  81. IsKeyPressed(KEY_TWO) ||
  82. IsKeyPressed(KEY_THREE) ||
  83. IsKeyPressed(KEY_FOUR) ||
  84. IsKeyPressed(KEY_FIVE) ||
  85. IsKeyPressed(KEY_SIX))
  86. {
  87. if (IsKeyPressed(KEY_ONE)) c[0] = pointsOfInterest[0][0], c[1] = pointsOfInterest[0][1];
  88. else if (IsKeyPressed(KEY_TWO)) c[0] = pointsOfInterest[1][0], c[1] = pointsOfInterest[1][1];
  89. else if (IsKeyPressed(KEY_THREE)) c[0] = pointsOfInterest[2][0], c[1] = pointsOfInterest[2][1];
  90. else if (IsKeyPressed(KEY_FOUR)) c[0] = pointsOfInterest[3][0], c[1] = pointsOfInterest[3][1];
  91. else if (IsKeyPressed(KEY_FIVE)) c[0] = pointsOfInterest[4][0], c[1] = pointsOfInterest[4][1];
  92. else if (IsKeyPressed(KEY_SIX)) c[0] = pointsOfInterest[5][0], c[1] = pointsOfInterest[5][1];
  93. SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
  94. }
  95. // If "R" is pressed, reset zoom and offset.
  96. if (IsKeyPressed(KEY_R))
  97. {
  98. zoom = startingZoom;
  99. offset[0] = 0.0f;
  100. offset[1] = 0.0f;
  101. SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
  102. SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);
  103. }
  104. if (IsKeyPressed(KEY_SPACE)) incrementSpeed = 0; // Pause animation (c change)
  105. if (IsKeyPressed(KEY_F1)) showControls = !showControls; // Toggle whether or not to show controls
  106. if (IsKeyPressed(KEY_RIGHT)) incrementSpeed++;
  107. else if (IsKeyPressed(KEY_LEFT)) incrementSpeed--;
  108. // If either left or right button is pressed, zoom in/out.
  109. if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
  110. {
  111. // Change zoom. If Mouse left -> zoom in. Mouse right -> zoom out.
  112. zoom *= IsMouseButtonDown(MOUSE_BUTTON_LEFT)? zoomSpeed : 1.0f/zoomSpeed;
  113. const Vector2 mousePos = GetMousePosition();
  114. Vector2 offsetVelocity;
  115. // Find the velocity at which to change the camera. Take the distance of the mouse
  116. // from the center of the screen as the direction, and adjust magnitude based on
  117. // the current zoom.
  118. offsetVelocity.x = (mousePos.x/(float)screenWidth - 0.5f)*offsetSpeedMul/zoom;
  119. offsetVelocity.y = (mousePos.y/(float)screenHeight - 0.5f)*offsetSpeedMul/zoom;
  120. // Apply move velocity to camera
  121. offset[0] += GetFrameTime()*offsetVelocity.x;
  122. offset[1] += GetFrameTime()*offsetVelocity.y;
  123. // Update the shader uniform values!
  124. SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
  125. SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);
  126. }
  127. // Increment c value with time
  128. const float dc = GetFrameTime()*(float)incrementSpeed*0.0005f;
  129. c[0] += dc;
  130. c[1] += dc;
  131. SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
  132. //----------------------------------------------------------------------------------
  133. // Draw
  134. //----------------------------------------------------------------------------------
  135. // Using a render texture to draw Julia set
  136. BeginTextureMode(target); // Enable drawing to texture
  137. ClearBackground(BLACK); // Clear the render texture
  138. // Draw a rectangle in shader mode to be used as shader canvas
  139. // NOTE: Rectangle uses font white character texture coordinates,
  140. // so shader can not be applied here directly because input vertexTexCoord
  141. // do not represent full screen coordinates (space where want to apply shader)
  142. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK);
  143. EndTextureMode();
  144. BeginDrawing();
  145. ClearBackground(BLACK); // Clear screen background
  146. // Draw the saved texture and rendered julia set with shader
  147. // NOTE: We do not invert texture on Y, already considered inside shader
  148. BeginShaderMode(shader);
  149. // WARNING: If FLAG_WINDOW_HIGHDPI is enabled, HighDPI monitor scaling should be considered
  150. // when rendering the RenderTexture2D to fit in the HighDPI scaled Window
  151. DrawTextureEx(target.texture, (Vector2){ 0.0f, 0.0f }, 0.0f, 1.0f, WHITE);
  152. EndShaderMode();
  153. if (showControls)
  154. {
  155. DrawText("Press Mouse buttons right/left to zoom in/out and move", 10, 15, 10, RAYWHITE);
  156. DrawText("Press KEY_F1 to toggle these controls", 10, 30, 10, RAYWHITE);
  157. DrawText("Press KEYS [1 - 6] to change point of interest", 10, 45, 10, RAYWHITE);
  158. DrawText("Press KEY_LEFT | KEY_RIGHT to change speed", 10, 60, 10, RAYWHITE);
  159. DrawText("Press KEY_SPACE to stop movement animation", 10, 75, 10, RAYWHITE);
  160. DrawText("Press KEY_R to recenter the camera", 10, 90, 10, RAYWHITE);
  161. }
  162. EndDrawing();
  163. //----------------------------------------------------------------------------------
  164. }
  165. // De-Initialization
  166. //--------------------------------------------------------------------------------------
  167. UnloadShader(shader); // Unload shader
  168. UnloadRenderTexture(target); // Unload render texture
  169. CloseWindow(); // Close window and OpenGL context
  170. //--------------------------------------------------------------------------------------
  171. return 0;
  172. }