shaders_depth_rendering.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - depth rendering
  4. *
  5. * Example complexity rating: [★★★☆] 3/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by Luís Almeida (@luis605)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Luís Almeida (@luis605)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "rlgl.h"
  19. #if defined(PLATFORM_DESKTOP)
  20. #define GLSL_VERSION 330
  21. #else // PLATFORM_ANDROID, PLATFORM_WEB
  22. #define GLSL_VERSION 100
  23. #endif
  24. RenderTexture2D LoadRenderTextureWithDepth(int width, int height);
  25. //------------------------------------------------------------------------------------
  26. // Program main entry point
  27. //------------------------------------------------------------------------------------
  28. int main(void)
  29. {
  30. // Initialization
  31. //--------------------------------------------------------------------------------------
  32. const int screenWidth = 800;
  33. const int screenHeight = 450;
  34. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - depth rendering");
  35. // Init camera
  36. Camera camera = { 0 };
  37. camera.position = (Vector3){ 4.0f, 1.0f, 5.0f };
  38. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  39. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  40. camera.fovy = 45.0f;
  41. camera.projection = CAMERA_PERSPECTIVE;
  42. // Load an empty render texture with a depth texture
  43. RenderTexture2D target = LoadRenderTextureWithDepth(screenWidth, screenHeight);
  44. // Load depth shader and get depth texture shader location
  45. Shader depthShader = LoadShader(0, TextFormat("resources/shaders/glsl%i/depth.fs", GLSL_VERSION));
  46. int depthLoc = GetShaderLocation(depthShader, "depthTexture");
  47. int flipTextureLoc = GetShaderLocation(depthShader, "flipY");
  48. SetShaderValue(depthShader, flipTextureLoc, (int[]){ 1 }, SHADER_UNIFORM_INT); // Flip Y texture
  49. // Load models
  50. Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  51. Model floor = LoadModelFromMesh(GenMeshPlane(20.0f, 20.0f, 1, 1));
  52. DisableCursor(); // Limit cursor to relative movement inside the window
  53. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  54. //--------------------------------------------------------------------------------------
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. // Update
  59. //----------------------------------------------------------------------------------
  60. UpdateCamera(&camera, CAMERA_FREE);
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. BeginTextureMode(target);
  65. ClearBackground(WHITE);
  66. BeginMode3D(camera);
  67. DrawModel(cube, (Vector3){ 0.0f, 0.0f, 0.0f }, 3.0f, YELLOW);
  68. DrawModel(floor, (Vector3){ 10.0f, 0.0f, 2.0f }, 2.0f, RED);
  69. EndMode3D();
  70. EndTextureMode();
  71. BeginDrawing();
  72. BeginShaderMode(depthShader);
  73. SetShaderValueTexture(depthShader, depthLoc, target.depth);
  74. DrawTexture(target.depth, 0, 0, WHITE);
  75. EndShaderMode();
  76. DrawRectangle( 10, 10, 320, 93, Fade(SKYBLUE, 0.5f));
  77. DrawRectangleLines( 10, 10, 320, 93, BLUE);
  78. DrawText("Camera Controls:", 20, 20, 10, BLACK);
  79. DrawText("- WASD to move", 40, 40, 10, DARKGRAY);
  80. DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, DARKGRAY);
  81. DrawText("- Z to zoom to (0, 0, 0)", 40, 80, 10, DARKGRAY);
  82. EndDrawing();
  83. //----------------------------------------------------------------------------------
  84. }
  85. // De-Initialization
  86. //--------------------------------------------------------------------------------------
  87. UnloadModel(cube); // Unload model
  88. UnloadModel(floor); // Unload model
  89. UnloadRenderTexture(target); // Unload render texture
  90. UnloadShader(depthShader); // Unload shader
  91. CloseWindow(); // Close window and OpenGL context
  92. //--------------------------------------------------------------------------------------
  93. return 0;
  94. }
  95. RenderTexture2D LoadRenderTextureWithDepth(int width, int height)
  96. {
  97. RenderTexture2D target = {0};
  98. target.id = rlLoadFramebuffer(); // Load an empty framebuffer
  99. if (target.id > 0)
  100. {
  101. rlEnableFramebuffer(target.id);
  102. // Create color texture (default to RGBA)
  103. target.texture.id = rlLoadTexture(0, width, height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
  104. target.texture.width = width;
  105. target.texture.height = height;
  106. target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
  107. target.texture.mipmaps = 1;
  108. // Create depth texture
  109. target.depth.id = rlLoadTextureDepth(width, height, false);
  110. target.depth.width = width;
  111. target.depth.height = height;
  112. target.depth.format = 19; //DEPTH_COMPONENT_24BIT? THIS DOESN'T EXIST IN RAYLIB
  113. target.depth.mipmaps = 1;
  114. // Attach color texture and depth texture to FBO
  115. rlFramebufferAttach(target.id, target.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
  116. rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
  117. // Check if fbo is complete with attachments (valid)
  118. if (rlFramebufferComplete(target.id)) TRACELOG(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", target.id);
  119. rlDisableFramebuffer();
  120. }
  121. else TRACELOG(LOG_WARNING, "FBO: Framebuffer object can not be created");
  122. return target;
  123. }