25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.

130 satır
5.4 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Hot reloading
  4. *
  5. * NOTE: This example requires raylib OpenGL 3.3 for shaders support and only #version 330
  6. * is currently supported. OpenGL ES 2.0 platforms are not supported at the moment.
  7. *
  8. * This example has been created using raylib 3.0 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2020 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "rlgl.h"
  16. #include <time.h> // Required for: localtime(), asctime()
  17. #if defined(PLATFORM_DESKTOP)
  18. #define GLSL_VERSION 330
  19. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  20. #define GLSL_VERSION 100
  21. #endif
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. int screenWidth = 800;
  27. int screenHeight = 450;
  28. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - hot reloading");
  29. const char *fragShaderFileName = "resources/shaders/glsl%i/reload.fs";
  30. long fragShaderFileModTime = GetFileModTime(TextFormat(fragShaderFileName, GLSL_VERSION));
  31. // Load raymarching shader
  32. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  33. Shader shader = LoadShader(0, TextFormat(fragShaderFileName, GLSL_VERSION));
  34. // Get shader locations for required uniforms
  35. int resolutionLoc = GetShaderLocation(shader, "resolution");
  36. int mouseLoc = GetShaderLocation(shader, "mouse");
  37. int timeLoc = GetShaderLocation(shader, "time");
  38. float resolution[2] = { (float)screenWidth, (float)screenHeight };
  39. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  40. float totalTime = 0.0f;
  41. bool shaderAutoReloading = false;
  42. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  43. //--------------------------------------------------------------------------------------
  44. // Main game loop
  45. while (!WindowShouldClose()) // Detect window close button or ESC key
  46. {
  47. // Update
  48. //----------------------------------------------------------------------------------
  49. totalTime += GetFrameTime();
  50. Vector2 mouse = GetMousePosition();
  51. float mousePos[2] = { mouse.x, mouse.y };
  52. // Set shader required uniform values
  53. SetShaderValue(shader, timeLoc, &totalTime, SHADER_UNIFORM_FLOAT);
  54. SetShaderValue(shader, mouseLoc, mousePos, SHADER_UNIFORM_VEC2);
  55. // Hot shader reloading
  56. if (shaderAutoReloading || (IsMouseButtonPressed(MOUSE_LEFT_BUTTON)))
  57. {
  58. long currentFragShaderModTime = GetFileModTime(TextFormat(fragShaderFileName, GLSL_VERSION));
  59. // Check if shader file has been modified
  60. if (currentFragShaderModTime != fragShaderFileModTime)
  61. {
  62. // Try reloading updated shader
  63. Shader updatedShader = LoadShader(0, TextFormat(fragShaderFileName, GLSL_VERSION));
  64. if (updatedShader.id != rlGetShaderDefault().id) // It was correctly loaded
  65. {
  66. UnloadShader(shader);
  67. shader = updatedShader;
  68. // Get shader locations for required uniforms
  69. resolutionLoc = GetShaderLocation(shader, "resolution");
  70. mouseLoc = GetShaderLocation(shader, "mouse");
  71. timeLoc = GetShaderLocation(shader, "time");
  72. // Reset required uniforms
  73. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  74. }
  75. fragShaderFileModTime = currentFragShaderModTime;
  76. }
  77. }
  78. if (IsKeyPressed(KEY_A)) shaderAutoReloading = !shaderAutoReloading;
  79. //----------------------------------------------------------------------------------
  80. // Draw
  81. //----------------------------------------------------------------------------------
  82. BeginDrawing();
  83. ClearBackground(RAYWHITE);
  84. // We only draw a white full-screen rectangle, frame is generated in shader
  85. BeginShaderMode(shader);
  86. DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
  87. EndShaderMode();
  88. DrawText(TextFormat("PRESS [A] to TOGGLE SHADER AUTOLOADING: %s",
  89. shaderAutoReloading? "AUTO" : "MANUAL"), 10, 10, 10, shaderAutoReloading? RED : BLACK);
  90. if (!shaderAutoReloading) DrawText("MOUSE CLICK to SHADER RE-LOADING", 10, 30, 10, BLACK);
  91. DrawText(TextFormat("Shader last modification: %s", asctime(localtime(&fragShaderFileModTime))), 10, 430, 10, BLACK);
  92. EndDrawing();
  93. //----------------------------------------------------------------------------------
  94. }
  95. // De-Initialization
  96. //--------------------------------------------------------------------------------------
  97. UnloadShader(shader); // Unload shader
  98. CloseWindow(); // Close window and OpenGL context
  99. //--------------------------------------------------------------------------------------
  100. return 0;
  101. }