shaders_raymarching.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Raymarching shapes generation
  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 2.0 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2018 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #if defined(PLATFORM_DESKTOP)
  16. #define GLSL_VERSION 330
  17. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB -> Not supported at this moment
  18. #define GLSL_VERSION 100
  19. #endif
  20. int main(void)
  21. {
  22. // Initialization
  23. //--------------------------------------------------------------------------------------
  24. int screenWidth = 800;
  25. int screenHeight = 450;
  26. SetConfigFlags(FLAG_WINDOW_RESIZABLE);
  27. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - raymarching shapes");
  28. Camera camera = { 0 };
  29. camera.position = (Vector3){ 2.5f, 2.5f, 3.0f }; // Camera position
  30. camera.target = (Vector3){ 0.0f, 0.0f, 0.7f }; // Camera looking at point
  31. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  32. camera.fovy = 65.0f; // Camera field-of-view Y
  33. SetCameraMode(camera, CAMERA_FREE); // Set camera mode
  34. // Load raymarching shader
  35. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  36. Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/raymarching.fs", GLSL_VERSION));
  37. // Get shader locations for required uniforms
  38. int viewEyeLoc = GetShaderLocation(shader, "viewEye");
  39. int viewCenterLoc = GetShaderLocation(shader, "viewCenter");
  40. int runTimeLoc = GetShaderLocation(shader, "runTime");
  41. int resolutionLoc = GetShaderLocation(shader, "resolution");
  42. float resolution[2] = { (float)screenWidth, (float)screenHeight };
  43. SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2);
  44. float runTime = 0.0f;
  45. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  46. //--------------------------------------------------------------------------------------
  47. // Main game loop
  48. while (!WindowShouldClose()) // Detect window close button or ESC key
  49. {
  50. // Check if screen is resized
  51. //----------------------------------------------------------------------------------
  52. if(IsWindowResized())
  53. {
  54. screenWidth = GetScreenWidth();
  55. screenHeight = GetScreenHeight();
  56. float resolution[2] = { (float)screenWidth, (float)screenHeight };
  57. SetShaderValue(shader, resolutionLoc, resolution, UNIFORM_VEC2);
  58. }
  59. // Update
  60. //----------------------------------------------------------------------------------
  61. UpdateCamera(&camera); // Update camera
  62. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  63. float cameraTarget[3] = { camera.target.x, camera.target.y, camera.target.z };
  64. float deltaTime = GetFrameTime();
  65. runTime += deltaTime;
  66. // Set shader required uniform values
  67. SetShaderValue(shader, viewEyeLoc, cameraPos, UNIFORM_VEC3);
  68. SetShaderValue(shader, viewCenterLoc, cameraTarget, UNIFORM_VEC3);
  69. SetShaderValue(shader, runTimeLoc, &runTime, UNIFORM_FLOAT);
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginDrawing();
  74. ClearBackground(RAYWHITE);
  75. // We only draw a white full-screen rectangle,
  76. // frame is generated in shader using raymarching
  77. BeginShaderMode(shader);
  78. DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
  79. EndShaderMode();
  80. DrawText("(c) Raymarching shader by Iñigo Quilez. MIT License.", screenWidth - 280, screenHeight - 20, 10, BLACK);
  81. EndDrawing();
  82. //----------------------------------------------------------------------------------
  83. }
  84. // De-Initialization
  85. //--------------------------------------------------------------------------------------
  86. UnloadShader(shader); // Unload shader
  87. CloseWindow(); // Close window and OpenGL context
  88. //--------------------------------------------------------------------------------------
  89. return 0;
  90. }