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.

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