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.

117 lines
5.0 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  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. * Example originally created with raylib 2.0, last time updated with raylib 4.2
  9. *
  10. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  11. * BSD-like license that allows static linking with closed source software
  12. *
  13. * Copyright (c) 2018-2024 Ramon Santamaria (@raysan5)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #if defined(PLATFORM_DESKTOP)
  18. #define GLSL_VERSION 330
  19. #else // PLATFORM_ANDROID, PLATFORM_WEB -> Not supported at this moment
  20. #define GLSL_VERSION 100
  21. #endif
  22. //------------------------------------------------------------------------------------
  23. // Program main entry point
  24. //------------------------------------------------------------------------------------
  25. int main(void)
  26. {
  27. // Initialization
  28. //--------------------------------------------------------------------------------------
  29. const int screenWidth = 800;
  30. const int screenHeight = 450;
  31. SetConfigFlags(FLAG_WINDOW_RESIZABLE);
  32. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - raymarching shapes");
  33. Camera camera = { 0 };
  34. camera.position = (Vector3){ 2.5f, 2.5f, 3.0f }; // Camera position
  35. camera.target = (Vector3){ 0.0f, 0.0f, 0.7f }; // Camera looking at point
  36. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  37. camera.fovy = 65.0f; // Camera field-of-view Y
  38. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  39. // Load raymarching shader
  40. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  41. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/raymarching.fs", GLSL_VERSION));
  42. // Get shader locations for required uniforms
  43. int viewEyeLoc = GetShaderLocation(shader, "viewEye");
  44. int viewCenterLoc = GetShaderLocation(shader, "viewCenter");
  45. int runTimeLoc = GetShaderLocation(shader, "runTime");
  46. int resolutionLoc = GetShaderLocation(shader, "resolution");
  47. float resolution[2] = { (float)screenWidth, (float)screenHeight };
  48. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  49. float runTime = 0.0f;
  50. DisableCursor(); // Limit cursor to relative movement inside the window
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. UpdateCamera(&camera, CAMERA_FIRST_PERSON);
  59. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  60. float cameraTarget[3] = { camera.target.x, camera.target.y, camera.target.z };
  61. float deltaTime = GetFrameTime();
  62. runTime += deltaTime;
  63. // Set shader required uniform values
  64. SetShaderValue(shader, viewEyeLoc, cameraPos, SHADER_UNIFORM_VEC3);
  65. SetShaderValue(shader, viewCenterLoc, cameraTarget, SHADER_UNIFORM_VEC3);
  66. SetShaderValue(shader, runTimeLoc, &runTime, SHADER_UNIFORM_FLOAT);
  67. // Check if screen is resized
  68. if (IsWindowResized())
  69. {
  70. resolution[0] = (float)GetScreenWidth();
  71. resolution[1] = (float)GetScreenHeight();
  72. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  73. }
  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, GetScreenWidth(), GetScreenHeight(), WHITE);
  83. EndShaderMode();
  84. DrawText("(c) Raymarching shader by Iñigo Quilez. MIT License.", GetScreenWidth() - 280, GetScreenHeight() - 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. }