119 行
5.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Raymarching shapes generation
  4. *
  5. * Example complexity rating: [] 4/4
  6. *
  7. * NOTE: This example requires raylib OpenGL 3.3 for shaders support and only #version 330
  8. * is currently supported. OpenGL ES 2.0 platforms are not supported at the moment.
  9. *
  10. * Example originally created with raylib 2.0, last time updated with raylib 4.2
  11. *
  12. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  13. * BSD-like license that allows static linking with closed source software
  14. *
  15. * Copyright (c) 2018-2025 Ramon Santamaria (@raysan5)
  16. *
  17. ********************************************************************************************/
  18. #include "raylib.h"
  19. #if defined(PLATFORM_DESKTOP)
  20. #define GLSL_VERSION 330
  21. #else // PLATFORM_ANDROID, PLATFORM_WEB -> Not supported at this moment
  22. #define GLSL_VERSION 100
  23. #endif
  24. //------------------------------------------------------------------------------------
  25. // Program main entry point
  26. //------------------------------------------------------------------------------------
  27. int main(void)
  28. {
  29. // Initialization
  30. //--------------------------------------------------------------------------------------
  31. const int screenWidth = 800;
  32. const int screenHeight = 450;
  33. SetConfigFlags(FLAG_WINDOW_RESIZABLE);
  34. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - raymarching shapes");
  35. Camera camera = { 0 };
  36. camera.position = (Vector3){ 2.5f, 2.5f, 3.0f }; // Camera position
  37. camera.target = (Vector3){ 0.0f, 0.0f, 0.7f }; // Camera looking at point
  38. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  39. camera.fovy = 65.0f; // Camera field-of-view Y
  40. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  41. // Load raymarching 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/raymarching.fs", GLSL_VERSION));
  44. // Get shader locations for required uniforms
  45. int viewEyeLoc = GetShaderLocation(shader, "viewEye");
  46. int viewCenterLoc = GetShaderLocation(shader, "viewCenter");
  47. int runTimeLoc = GetShaderLocation(shader, "runTime");
  48. int resolutionLoc = GetShaderLocation(shader, "resolution");
  49. float resolution[2] = { (float)screenWidth, (float)screenHeight };
  50. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  51. float runTime = 0.0f;
  52. DisableCursor(); // Limit cursor to relative movement inside the window
  53. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  54. //--------------------------------------------------------------------------------------
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. // Update
  59. //----------------------------------------------------------------------------------
  60. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  61. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  62. float cameraTarget[3] = { camera.target.x, camera.target.y, camera.target.z };
  63. float deltaTime = GetFrameTime();
  64. runTime += deltaTime;
  65. // Set shader required uniform values
  66. SetShaderValue(shader, viewEyeLoc, cameraPos, SHADER_UNIFORM_VEC3);
  67. SetShaderValue(shader, viewCenterLoc, cameraTarget, SHADER_UNIFORM_VEC3);
  68. SetShaderValue(shader, runTimeLoc, &runTime, SHADER_UNIFORM_FLOAT);
  69. // Check if screen is resized
  70. if (IsWindowResized())
  71. {
  72. resolution[0] = (float)GetScreenWidth();
  73. resolution[1] = (float)GetScreenHeight();
  74. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  75. }
  76. //----------------------------------------------------------------------------------
  77. // Draw
  78. //----------------------------------------------------------------------------------
  79. BeginDrawing();
  80. ClearBackground(RAYWHITE);
  81. // We only draw a white full-screen rectangle,
  82. // frame is generated in shader using raymarching
  83. BeginShaderMode(shader);
  84. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), WHITE);
  85. EndShaderMode();
  86. DrawText("(c) Raymarching shader by Iñigo Quilez. MIT License.", GetScreenWidth() - 280, GetScreenHeight() - 20, 10, BLACK);
  87. EndDrawing();
  88. //----------------------------------------------------------------------------------
  89. }
  90. // De-Initialization
  91. //--------------------------------------------------------------------------------------
  92. UnloadShader(shader); // Unload shader
  93. CloseWindow(); // Close window and OpenGL context
  94. //--------------------------------------------------------------------------------------
  95. return 0;
  96. }