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.

113 lines
4.7 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
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. * 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, TextFormat("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, SHADER_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. // Update
  51. //----------------------------------------------------------------------------------
  52. UpdateCamera(&camera); // Update camera
  53. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  54. float cameraTarget[3] = { camera.target.x, camera.target.y, camera.target.z };
  55. float deltaTime = GetFrameTime();
  56. runTime += deltaTime;
  57. // Set shader required uniform values
  58. SetShaderValue(shader, viewEyeLoc, cameraPos, SHADER_UNIFORM_VEC3);
  59. SetShaderValue(shader, viewCenterLoc, cameraTarget, SHADER_UNIFORM_VEC3);
  60. SetShaderValue(shader, runTimeLoc, &runTime, SHADER_UNIFORM_FLOAT);
  61. // Check if screen is resized
  62. if (IsWindowResized())
  63. {
  64. screenWidth = GetScreenWidth();
  65. screenHeight = GetScreenHeight();
  66. float resolution[2] = { (float)screenWidth, (float)screenHeight };
  67. SetShaderValue(shader, resolutionLoc, resolution, SHADER_UNIFORM_VEC2);
  68. }
  69. //----------------------------------------------------------------------------------
  70. // Draw
  71. //----------------------------------------------------------------------------------
  72. BeginDrawing();
  73. ClearBackground(RAYWHITE);
  74. // We only draw a white full-screen rectangle,
  75. // frame is generated in shader using raymarching
  76. BeginShaderMode(shader);
  77. DrawRectangle(0, 0, screenWidth, screenHeight, WHITE);
  78. EndShaderMode();
  79. DrawText("(c) Raymarching shader by Iñigo Quilez. MIT License.", screenWidth - 280, screenHeight - 20, 10, BLACK);
  80. EndDrawing();
  81. //----------------------------------------------------------------------------------
  82. }
  83. // De-Initialization
  84. //--------------------------------------------------------------------------------------
  85. UnloadShader(shader); // Unload shader
  86. CloseWindow(); // Close window and OpenGL context
  87. //--------------------------------------------------------------------------------------
  88. return 0;
  89. }