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.

111 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
6 years ago
  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. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - raymarching shapes");
  31. Camera camera = { 0 };
  32. camera.position = (Vector3){ 2.5f, 2.5f, 3.0f }; // Camera position
  33. camera.target = (Vector3){ 0.0f, 0.0f, 0.7f }; // Camera looking at point
  34. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  35. camera.fovy = 65.0f; // Camera field-of-view Y
  36. SetCameraMode(camera, CAMERA_FREE); // Set camera mode
  37. // Load raymarching shader
  38. // NOTE: Defining 0 (NULL) for vertex shader forces usage of internal default vertex shader
  39. Shader shader = LoadShader(0, FormatText("resources/shaders/glsl%i/raymarching.fs", GLSL_VERSION));
  40. // Get shader locations for required uniforms
  41. int viewEyeLoc = GetShaderLocation(shader, "viewEye");
  42. int viewCenterLoc = GetShaderLocation(shader, "viewCenter");
  43. int viewUpLoc = GetShaderLocation(shader, "viewUp");
  44. int deltaTimeLoc = GetShaderLocation(shader, "deltaTime");
  45. int runTimeLoc = GetShaderLocation(shader, "runTime");
  46. int resolutionLoc = GetShaderLocation(shader, "resolution");
  47. float resolution[2] = { screenWidth, screenHeight };
  48. SetShaderValue(shader, resolutionLoc, resolution, 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); // Update 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 cameraUp[3] = { camera.up.x, camera.up.y, camera.up.z };
  61. float deltaTime = GetFrameTime();
  62. runTime += deltaTime;
  63. // Set shader required uniform values
  64. SetShaderValue(shader, viewEyeLoc, cameraPos, UNIFORM_VEC3);
  65. SetShaderValue(shader, viewCenterLoc, cameraTarget, UNIFORM_VEC3);
  66. SetShaderValue(shader, viewUpLoc, cameraUp, UNIFORM_VEC3);
  67. SetShaderValue(shader, deltaTimeLoc, &deltaTime, UNIFORM_FLOAT);
  68. SetShaderValue(shader, runTimeLoc, &runTime, UNIFORM_FLOAT);
  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, GRAY);
  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. }