shaders_raymarching.c 5.0 KB

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