shaders_depth_writing.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - depth writing
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 4.2, last time updated with raylib 4.2
  8. *
  9. * Example contributed by Buğra Alptekin Sarı (@BugraAlptekinSari) 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) 2022-2025 Buğra Alptekin Sarı (@BugraAlptekinSari)
  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, create a writable depth texture buffer
  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 writing");
  41. // Define the camera to look into our 3d world
  42. Camera camera = {
  43. .position = (Vector3){ 2.0f, 2.0f, 3.0f }, // Camera position
  44. .target = (Vector3){ 0.0f, 0.5f, 0.0f }, // Camera looking at point
  45. .up = (Vector3){ 0.0f, 1.0f, 0.0f }, // Camera up vector (rotation towards target)
  46. .fovy = 45.0f, // Camera field-of-view Y
  47. .projection = CAMERA_PERSPECTIVE // Camera projection type
  48. };
  49. // Load custom render texture with writable depth texture buffer
  50. RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
  51. // Load depth writing shader
  52. // NOTE: The shader inverts the depth buffer by writing into it by `gl_FragDepth = 1 - gl_FragCoord.z;`
  53. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/depth_write.fs", GLSL_VERSION));
  54. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  55. //--------------------------------------------------------------------------------------
  56. // Main game loop
  57. while (!WindowShouldClose()) // Detect window close button or ESC key
  58. {
  59. // Update
  60. //----------------------------------------------------------------------------------
  61. UpdateCamera(&camera, CAMERA_ORBITAL);
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. // Draw into our custom render texture
  66. BeginTextureMode(target);
  67. ClearBackground(WHITE);
  68. BeginMode3D(camera);
  69. BeginShaderMode(shader);
  70. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, RED);
  71. DrawCubeV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, PURPLE);
  72. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, DARKGREEN);
  73. DrawCubeV((Vector3) { 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, YELLOW);
  74. DrawGrid(10, 1.0f);
  75. EndShaderMode();
  76. EndMode3D();
  77. EndTextureMode();
  78. // Draw into screen our custom render texture
  79. BeginDrawing();
  80. ClearBackground(RAYWHITE);
  81. DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);
  82. DrawFPS(10, 10);
  83. EndDrawing();
  84. //----------------------------------------------------------------------------------
  85. }
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. UnloadRenderTextureDepthTex(target);
  89. UnloadShader(shader);
  90. CloseWindow(); // Close window and OpenGL context
  91. //--------------------------------------------------------------------------------------
  92. return 0;
  93. }
  94. //--------------------------------------------------------------------------------------
  95. // Module Functions Definition
  96. //--------------------------------------------------------------------------------------
  97. // Load custom render texture, create a writable depth texture buffer
  98. static RenderTexture2D LoadRenderTextureDepthTex(int width, int height)
  99. {
  100. RenderTexture2D target = { 0 };
  101. target.id = rlLoadFramebuffer(); // Load an empty framebuffer
  102. if (target.id > 0)
  103. {
  104. rlEnableFramebuffer(target.id);
  105. // Create color texture (default to RGBA)
  106. target.texture.id = rlLoadTexture(0, width, height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
  107. target.texture.width = width;
  108. target.texture.height = height;
  109. target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
  110. target.texture.mipmaps = 1;
  111. // Create depth texture buffer (instead of raylib default renderbuffer)
  112. target.depth.id = rlLoadTextureDepth(width, height, false);
  113. target.depth.width = width;
  114. target.depth.height = height;
  115. target.depth.format = 19; // DEPTH_COMPONENT_24BIT: Not defined in raylib
  116. target.depth.mipmaps = 1;
  117. // Attach color texture and depth texture to FBO
  118. rlFramebufferAttach(target.id, target.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
  119. rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
  120. // Check if fbo is complete with attachments (valid)
  121. if (rlFramebufferComplete(target.id)) TRACELOG(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", target.id);
  122. rlDisableFramebuffer();
  123. }
  124. else TRACELOG(LOG_WARNING, "FBO: Framebuffer object can not be created");
  125. return target;
  126. }
  127. // Unload render texture from GPU memory (VRAM)
  128. void UnloadRenderTextureDepthTex(RenderTexture2D target)
  129. {
  130. if (target.id > 0)
  131. {
  132. // Color texture attached to FBO is deleted
  133. rlUnloadTexture(target.texture.id);
  134. rlUnloadTexture(target.depth.id);
  135. // NOTE: Depth texture is automatically
  136. // queried and deleted before deleting framebuffer
  137. rlUnloadFramebuffer(target.id);
  138. }
  139. }