shaders_depth_rendering.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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) and reviewed by Ramon Santamaria (@raysan5)
  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. //--------------------------------------------------------------------------------------
  25. // Module Functions Declaration
  26. //--------------------------------------------------------------------------------------
  27. // Load custom render texture with depth texture attached
  28. static RenderTexture2D LoadRenderTextureDepthTex(int width, int height);
  29. // Unload render texture from GPU memory (VRAM)
  30. static void UnloadRenderTextureDepthTex(RenderTexture2D target);
  31. //------------------------------------------------------------------------------------
  32. // Program main entry point
  33. //------------------------------------------------------------------------------------
  34. int main(void)
  35. {
  36. // Initialization
  37. //--------------------------------------------------------------------------------------
  38. const int screenWidth = 800;
  39. const int screenHeight = 450;
  40. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - depth rendering");
  41. // Define the camera to look into our 3d world
  42. Camera camera = { 0 };
  43. camera.position = (Vector3){ 4.0f, 1.0f, 5.0f };
  44. camera.target = (Vector3){ 0.0f, 0.0f, 0.0f };
  45. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f };
  46. camera.fovy = 45.0f;
  47. camera.projection = CAMERA_PERSPECTIVE;
  48. // Load render texture with a depth texture attached
  49. RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
  50. // Load depth shader and get depth texture shader location
  51. Shader depthShader = LoadShader(0, TextFormat("resources/shaders/glsl%i/depth_render.fs", GLSL_VERSION));
  52. int depthLoc = GetShaderLocation(depthShader, "depthTexture");
  53. int flipTextureLoc = GetShaderLocation(depthShader, "flipY");
  54. SetShaderValue(depthShader, flipTextureLoc, (int[]){ 1 }, SHADER_UNIFORM_INT); // Flip Y texture
  55. // Load scene models
  56. Model cube = LoadModelFromMesh(GenMeshCube(1.0f, 1.0f, 1.0f));
  57. Model floor = LoadModelFromMesh(GenMeshPlane(20.0f, 20.0f, 1, 1));
  58. DisableCursor(); // Limit cursor to relative movement inside the window
  59. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  60. //--------------------------------------------------------------------------------------
  61. // Main game loop
  62. while (!WindowShouldClose()) // Detect window close button or ESC key
  63. {
  64. // Update
  65. //----------------------------------------------------------------------------------
  66. UpdateCamera(&camera, CAMERA_FREE);
  67. //----------------------------------------------------------------------------------
  68. // Draw
  69. //----------------------------------------------------------------------------------
  70. BeginTextureMode(target);
  71. ClearBackground(WHITE);
  72. BeginMode3D(camera);
  73. DrawModel(cube, (Vector3){ 0.0f, 0.0f, 0.0f }, 3.0f, YELLOW);
  74. DrawModel(floor, (Vector3){ 10.0f, 0.0f, 2.0f }, 2.0f, RED);
  75. EndMode3D();
  76. EndTextureMode();
  77. // Draw into screen (main framebuffer)
  78. BeginDrawing();
  79. ClearBackground(RAYWHITE);
  80. BeginShaderMode(depthShader);
  81. SetShaderValueTexture(depthShader, depthLoc, target.depth);
  82. DrawTexture(target.depth, 0, 0, WHITE);
  83. EndShaderMode();
  84. DrawRectangle( 10, 10, 320, 93, Fade(SKYBLUE, 0.5f));
  85. DrawRectangleLines( 10, 10, 320, 93, BLUE);
  86. DrawText("Camera Controls:", 20, 20, 10, BLACK);
  87. DrawText("- WASD to move", 40, 40, 10, DARKGRAY);
  88. DrawText("- Mouse Wheel Pressed to Pan", 40, 60, 10, DARKGRAY);
  89. DrawText("- Z to zoom to (0, 0, 0)", 40, 80, 10, DARKGRAY);
  90. EndDrawing();
  91. //----------------------------------------------------------------------------------
  92. }
  93. // De-Initialization
  94. //--------------------------------------------------------------------------------------
  95. UnloadModel(cube); // Unload model
  96. UnloadModel(floor); // Unload model
  97. UnloadRenderTextureDepthTex(target);
  98. UnloadShader(depthShader); // Unload shader
  99. CloseWindow(); // Close window and OpenGL context
  100. //--------------------------------------------------------------------------------------
  101. return 0;
  102. }
  103. //--------------------------------------------------------------------------------------
  104. // Module Functions Definition
  105. //--------------------------------------------------------------------------------------
  106. // Load custom render texture, create a writable depth texture buffer
  107. static RenderTexture2D LoadRenderTextureDepthTex(int width, int height)
  108. {
  109. RenderTexture2D target = { 0 };
  110. target.id = rlLoadFramebuffer(); // Load an empty framebuffer
  111. if (target.id > 0)
  112. {
  113. rlEnableFramebuffer(target.id);
  114. // Create color texture (default to RGBA)
  115. target.texture.id = rlLoadTexture(0, width, height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
  116. target.texture.width = width;
  117. target.texture.height = height;
  118. target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
  119. target.texture.mipmaps = 1;
  120. // Create depth texture buffer (instead of raylib default renderbuffer)
  121. target.depth.id = rlLoadTextureDepth(width, height, false);
  122. target.depth.width = width;
  123. target.depth.height = height;
  124. target.depth.format = 19; // DEPTH_COMPONENT_24BIT: Not defined in raylib
  125. target.depth.mipmaps = 1;
  126. // Attach color texture and depth texture to FBO
  127. rlFramebufferAttach(target.id, target.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
  128. rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
  129. // Check if fbo is complete with attachments (valid)
  130. if (rlFramebufferComplete(target.id)) TRACELOG(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", target.id);
  131. rlDisableFramebuffer();
  132. }
  133. else TRACELOG(LOG_WARNING, "FBO: Framebuffer object can not be created");
  134. return target;
  135. }
  136. // Unload render texture from GPU memory (VRAM)
  137. void UnloadRenderTextureDepthTex(RenderTexture2D target)
  138. {
  139. if (target.id > 0)
  140. {
  141. // Color texture attached to FBO is deleted
  142. rlUnloadTexture(target.texture.id);
  143. rlUnloadTexture(target.depth.id);
  144. // NOTE: Depth texture is automatically
  145. // queried and deleted before deleting framebuffer
  146. rlUnloadFramebuffer(target.id);
  147. }
  148. }