shaders_julia_set.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Render julia sets using a shader.
  4. *
  5. * NOTE: This example requires raylib OpenGL 3.3 or ES2 versions for shaders support,
  6. * OpenGL 1.1 does not support shaders, recompile raylib to OpenGL 3.3 version.
  7. *
  8. * NOTE: Shaders used in this example are #version 330 (OpenGL 3.3).
  9. *
  10. * This example has been created using raylib 2.5 (www.raylib.com)
  11. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  12. *
  13. * Author: eggmund (https://github.com/eggmund)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #include <string.h> // For memcpy
  18. // Speed when using auto
  19. const float AUTO_SPEED = 0.0005;
  20. // A few good julia sets
  21. const float POINTS_OF_INTEREST[6][2] =
  22. {
  23. {-0.348827, 0.607167},
  24. {-0.786268, 0.169728},
  25. {-0.8, 0.156},
  26. {0.285, 0.0},
  27. {-0.835, -0.2321},
  28. {-0.70176, -0.3842},
  29. };
  30. int main()
  31. {
  32. // Initialization
  33. //--------------------------------------------------------------------------------------
  34. int screenWidth = 1280;
  35. int screenHeight = 720;
  36. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia set renderer");
  37. // If julia set is rendered for this frame.
  38. bool rendered = false;
  39. bool showControls = true;
  40. // Multiplier of speed to change c value. Set to 3 to start off with.
  41. int incrementSpeed = 3;
  42. // Offset and zoom to draw the julia set at. (centered on screen and 1.6 times smaller)
  43. float offset[2] = { -(float)screenWidth/2, -(float)screenHeight/2 };
  44. float zoom = 1.6;
  45. // c constant to use in z^2 + c
  46. float c[2];
  47. // Copy a point of interest into the c variable. 4 bytes per float (32 bits).
  48. memcpy(c, &POINTS_OF_INTEREST[0], 8);
  49. // Load julia set shader
  50. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  51. Shader shader = LoadShader(0, "resources/shaders/glsl330/julia_shader.fs");
  52. // Get variable (uniform) location on the shader to connect with the program
  53. // NOTE: If uniform variable could not be found in the shader, function returns -1
  54. // The location of c will be stored since we will need to change this whenever c changes
  55. int cLoc = GetShaderLocation(shader, "c");
  56. // Tell the shader what the screen dimensions, zoom, offset and c are
  57. float screenDims[2] = { (float)screenWidth, (float)screenHeight };
  58. SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), screenDims, UNIFORM_VEC2);
  59. SetShaderValue(shader, GetShaderLocation(shader, "zoom"), &zoom, UNIFORM_FLOAT);
  60. SetShaderValue(shader, GetShaderLocation(shader, "offset"), offset, UNIFORM_VEC2);
  61. SetShaderValue(shader, cLoc, c, UNIFORM_VEC2);
  62. // Create a RenderTexture2D to be used for render to texture
  63. RenderTexture2D target = LoadRenderTexture(screenWidth, screenHeight);
  64. SetTargetFPS(60); // Set the window to run at 60 frames-per-second
  65. //--------------------------------------------------------------------------------------
  66. // Main game loop
  67. while (!WindowShouldClose()) // Detect window close button or ESC key
  68. {
  69. // Update
  70. //----------------------------------------------------------------------------------
  71. // Get input
  72. //----------------------------------------------------------------------------------
  73. // Press 0 - 4 to reset c to a point of interest.
  74. if (IsKeyPressed(KEY_ONE) || IsKeyPressed(KEY_TWO) || IsKeyPressed(KEY_THREE) || IsKeyPressed(KEY_FOUR) || IsKeyPressed(KEY_FIVE) || IsKeyPressed(KEY_SIX))
  75. {
  76. if (IsKeyPressed(KEY_ONE))
  77. {
  78. memcpy(c, &POINTS_OF_INTEREST[0], 8);
  79. }
  80. else if (IsKeyPressed(KEY_TWO))
  81. {
  82. memcpy(c, &POINTS_OF_INTEREST[1], 8);
  83. }
  84. else if (IsKeyPressed(KEY_THREE))
  85. {
  86. memcpy(c, &POINTS_OF_INTEREST[2], 8);
  87. }
  88. else if (IsKeyPressed(KEY_FOUR))
  89. {
  90. memcpy(c, &POINTS_OF_INTEREST[3], 8);
  91. }
  92. else if (IsKeyPressed(KEY_FIVE))
  93. {
  94. memcpy(c, &POINTS_OF_INTEREST[4], 8);
  95. }
  96. else if (IsKeyPressed(KEY_SIX))
  97. {
  98. memcpy(c, &POINTS_OF_INTEREST[5], 8);
  99. }
  100. SetShaderValue(shader, cLoc, c, UNIFORM_VEC2);
  101. rendered = false; // c value has changed, so render the set again.
  102. }
  103. // Press "r" to stop changing c
  104. if (IsKeyPressed(KEY_R))
  105. {
  106. incrementSpeed = 0;
  107. }
  108. // Toggle whether or not to show controls
  109. if (IsKeyPressed(KEY_H))
  110. {
  111. showControls = !showControls;
  112. }
  113. // Scroll to change c increment speed.
  114. int mouseMv = GetMouseWheelMove(); // Get the amount the mouse has moved this frame
  115. if (mouseMv != 0)
  116. {
  117. if (IsKeyDown(KEY_LEFT_SHIFT))
  118. {
  119. incrementSpeed += mouseMv * 10;
  120. }
  121. else
  122. {
  123. incrementSpeed += mouseMv;
  124. }
  125. rendered = false;
  126. }
  127. if (incrementSpeed != 0)
  128. {
  129. float amount = GetFrameTime() * incrementSpeed * AUTO_SPEED;
  130. c[0] += amount;
  131. c[1] += amount;
  132. // Update the c value in the shader.
  133. SetShaderValue(shader, cLoc, c, UNIFORM_VEC2);
  134. rendered = false;
  135. }
  136. //----------------------------------------------------------------------------------
  137. // Draw
  138. //----------------------------------------------------------------------------------
  139. BeginDrawing();
  140. ClearBackground(BLACK); // Clear the screen of the previous frame.
  141. // If the c value has changed, redraw the julia set using the shader, onto the render texture.
  142. if (!rendered)
  143. {
  144. BeginTextureMode(target); // Enable drawing to texture
  145. ClearBackground(BLACK); // Clear the last frame drawn on the texture.
  146. // Draw a rectangle in shader mode. This acts as a canvas for the shader to draw on.
  147. BeginShaderMode(shader);
  148. DrawRectangle(0, 0, screenWidth, screenHeight, BLACK);
  149. EndShaderMode();
  150. EndTextureMode();
  151. rendered = true; // The set is now rendered, so do not compute it again until it next changes.
  152. }
  153. // Draw the saved texture (rendered julia set).
  154. DrawTextureRec(target.texture, (Rectangle){ 0, 0, target.texture.width, target.texture.height }, (Vector2){ 0, 0 }, WHITE);
  155. // Print information.
  156. DrawText( FormatText("cx: %f\ncy: %f\nspeed: %d", c[0], c[1], incrementSpeed), 10, 10, 20, RAYWHITE );
  157. if (showControls)
  158. {
  159. DrawText("Press keys 1 - 6 to change point of interest.", 10, screenHeight - 88, 20, RAYWHITE);
  160. DrawText("Use the scroll wheel to auto increment the c value. Hold shift while scrolling to increase speed by 10.", 10, screenHeight - 66, 20, RAYWHITE);
  161. DrawText("Press 'r' to reset speed.", 10, screenHeight - 44, 20, RAYWHITE);
  162. DrawText("Press 'h' to hide these controls.", 10, screenHeight - 22, 20, RAYWHITE);
  163. }
  164. EndDrawing();
  165. //----------------------------------------------------------------------------------
  166. }
  167. // De-Initialization
  168. //--------------------------------------------------------------------------------------
  169. UnloadShader(shader); // Unload shader
  170. UnloadRenderTexture(target); // Unload render texture
  171. CloseWindow(); // Close window and OpenGL context
  172. //--------------------------------------------------------------------------------------
  173. return 0;
  174. }