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.

193 lines
8.6 KiB

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