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.

202 lines
9.2 KiB

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