您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

196 行
8.9 KiB

  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 eggmund (@eggmund) 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-2023 eggmund (@eggmund) and Ramon Santamaria (@raysan5)
  18. *
  19. ********************************************************************************************/
  20. #include "raylib.h"
  21. #if defined(PLATFORM_DESKTOP)
  22. #define GLSL_VERSION 330
  23. #else // PLATFORM_RPI, 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. //------------------------------------------------------------------------------------
  37. // Program main entry point
  38. //------------------------------------------------------------------------------------
  39. int main(void)
  40. {
  41. // Initialization
  42. //--------------------------------------------------------------------------------------
  43. const int screenWidth = 800;
  44. const int screenHeight = 450;
  45. //SetConfigFlags(FLAG_WINDOW_HIGHDPI);
  46. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia sets");
  47. // Load julia set shader
  48. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  49. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/julia_set.fs", GLSL_VERSION));
  50. // Create a RenderTexture2D to be used for render to texture
  51. RenderTexture2D target = LoadRenderTexture(GetScreenWidth(), GetScreenHeight());
  52. // c constant to use in z^2 + c
  53. float c[2] = { pointsOfInterest[0][0], pointsOfInterest[0][1] };
  54. // Offset and zoom to draw the julia set at. (centered on screen and default size)
  55. float offset[2] = { -(float)GetScreenWidth()/2, -(float)GetScreenHeight()/2 };
  56. float zoom = 1.0f;
  57. Vector2 offsetSpeed = { 0.0f, 0.0f };
  58. // Get variable (uniform) locations on the shader to connect with the program
  59. // NOTE: If uniform variable could not be found in the shader, function returns -1
  60. int cLoc = GetShaderLocation(shader, "c");
  61. int zoomLoc = GetShaderLocation(shader, "zoom");
  62. int offsetLoc = GetShaderLocation(shader, "offset");
  63. // Tell the shader what the screen dimensions, zoom, offset and c are
  64. float screenDims[2] = { (float)GetScreenWidth(), (float)GetScreenHeight() };
  65. SetShaderValue(shader, GetShaderLocation(shader, "screenDims"), screenDims, SHADER_UNIFORM_VEC2);
  66. SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
  67. SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
  68. SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);
  69. int incrementSpeed = 0; // Multiplier of speed to change c value
  70. bool showControls = true; // Show controls
  71. bool pause = false; // Pause animation
  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 (IsKeyPressed(KEY_SPACE)) pause = !pause; // Pause animation (c change)
  96. if (IsKeyPressed(KEY_F1)) showControls = !showControls; // Toggle whether or not to show controls
  97. if (!pause)
  98. {
  99. if (IsKeyPressed(KEY_RIGHT)) incrementSpeed++;
  100. else if (IsKeyPressed(KEY_LEFT)) incrementSpeed--;
  101. // TODO: The idea is to zoom and move around with mouse
  102. // Probably offset movement should be proportional to zoom level
  103. if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
  104. {
  105. if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) zoom += zoom*0.003f;
  106. if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) zoom -= zoom*0.003f;
  107. Vector2 mousePos = GetMousePosition();
  108. offsetSpeed.x = mousePos.x -(float)screenWidth/2;
  109. offsetSpeed.y = mousePos.y -(float)screenHeight/2;
  110. // Slowly move camera to targetOffset
  111. offset[0] += GetFrameTime()*offsetSpeed.x*0.8f;
  112. offset[1] += GetFrameTime()*offsetSpeed.y*0.8f;
  113. }
  114. else offsetSpeed = (Vector2){ 0.0f, 0.0f };
  115. SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
  116. SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);
  117. // Increment c value with time
  118. float amount = GetFrameTime()*incrementSpeed*0.0005f;
  119. c[0] += amount;
  120. c[1] += amount;
  121. SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
  122. }
  123. //----------------------------------------------------------------------------------
  124. // Draw
  125. //----------------------------------------------------------------------------------
  126. // Using a render texture to draw Julia set
  127. BeginTextureMode(target); // Enable drawing to texture
  128. ClearBackground(BLACK); // Clear the render texture
  129. // Draw a rectangle in shader mode to be used as shader canvas
  130. // NOTE: Rectangle uses font white character texture coordinates,
  131. // so shader can not be applied here directly because input vertexTexCoord
  132. // do not represent full screen coordinates (space where want to apply shader)
  133. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), BLACK);
  134. EndTextureMode();
  135. BeginDrawing();
  136. ClearBackground(BLACK); // Clear screen background
  137. // Draw the saved texture and rendered julia set with shader
  138. // NOTE: We do not invert texture on Y, already considered inside shader
  139. BeginShaderMode(shader);
  140. // WARNING: If FLAG_WINDOW_HIGHDPI is enabled, HighDPI monitor scaling should be considered
  141. // when rendering the RenderTexture2D to fit in the HighDPI scaled Window
  142. DrawTextureEx(target.texture, (Vector2){ 0.0f, 0.0f }, 0.0f, 1.0f, WHITE);
  143. EndShaderMode();
  144. if (showControls)
  145. {
  146. DrawText("Press Mouse buttons right/left to zoom in/out and move", 10, 15, 10, RAYWHITE);
  147. DrawText("Press KEY_F1 to toggle these controls", 10, 30, 10, RAYWHITE);
  148. DrawText("Press KEYS [1 - 6] to change point of interest", 10, 45, 10, RAYWHITE);
  149. DrawText("Press KEY_LEFT | KEY_RIGHT to change speed", 10, 60, 10, RAYWHITE);
  150. DrawText("Press KEY_SPACE to pause movement animation", 10, 75, 10, RAYWHITE);
  151. }
  152. EndDrawing();
  153. //----------------------------------------------------------------------------------
  154. }
  155. // De-Initialization
  156. //--------------------------------------------------------------------------------------
  157. UnloadShader(shader); // Unload shader
  158. UnloadRenderTexture(target); // Unload render texture
  159. CloseWindow(); // Close window and OpenGL context
  160. //--------------------------------------------------------------------------------------
  161. return 0;
  162. }