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.

116 lines
4.9 KiB

6 years ago
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-2022 Ramon Santamaria (@raysan5)
  14. *
  15. ********************************************************************************************/
  16. #include "raylib.h"
  17. #if defined(PLATFORM_DESKTOP)
  18. #define GLSL_VERSION 330
  19. #else // PLATFORM_RPI, 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. SetCameraMode(camera, CAMERA_FREE); // Set camera mode
  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. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  51. //--------------------------------------------------------------------------------------
  52. // Main game loop
  53. while (!WindowShouldClose()) // Detect window close button or ESC key
  54. {
  55. // Update
  56. //----------------------------------------------------------------------------------
  57. UpdateCamera(&camera);
  58. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  59. float cameraTarget[3] = { camera.target.x, camera.target.y, camera.target.z };
  60. float deltaTime = GetFrameTime();
  61. runTime += deltaTime;
  62. // Set shader required uniform values
  63. SetShaderValue(shader, viewEyeLoc, cameraPos, SHADER_UNIFORM_VEC3);
  64. SetShaderValue(shader, viewCenterLoc, cameraTarget, SHADER_UNIFORM_VEC3);
  65. SetShaderValue(shader, runTimeLoc, &runTime, SHADER_UNIFORM_FLOAT);
  66. // Check if screen is resized
  67. if (IsWindowResized())
  68. {
  69. float resolution[2] = { (float)GetScreenWidth(), (float)GetScreenHeight() };
  70. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  71. }
  72. //----------------------------------------------------------------------------------
  73. // Draw
  74. //----------------------------------------------------------------------------------
  75. BeginDrawing();
  76. ClearBackground(RAYWHITE);
  77. // We only draw a white full-screen rectangle,
  78. // frame is generated in shader using raymarching
  79. BeginShaderMode(shader);
  80. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), WHITE);
  81. EndShaderMode();
  82. DrawText("(c) Raymarching shader by Iñigo Quilez. MIT License.", GetScreenWidth() - 280, GetScreenHeight() - 20, 10, BLACK);
  83. EndDrawing();
  84. //----------------------------------------------------------------------------------
  85. }
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. UnloadShader(shader); // Unload shader
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. return 0;
  92. }