shaders_write_depth.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Depth buffer 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-2024 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. // Declare custom functions required for the example
  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 - write depth buffer");
  41. // The shader inverts the depth buffer by writing into it by `gl_FragDepth = 1 - gl_FragCoord.z;`
  42. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/write_depth.fs", GLSL_VERSION));
  43. // Use Customized function to create writable depth texture buffer
  44. RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
  45. // Define the camera to look into our 3d world
  46. Camera camera = {
  47. .position = (Vector3){ 2.0f, 2.0f, 3.0f }, // Camera position
  48. .target = (Vector3){ 0.0f, 0.5f, 0.0f }, // Camera looking at point
  49. .up = (Vector3){ 0.0f, 1.0f, 0.0f }, // Camera up vector (rotation towards target)
  50. .fovy = 45.0f, // Camera field-of-view Y
  51. .projection = CAMERA_PERSPECTIVE // Camera projection type
  52. };
  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_ORBITAL);
  61. //----------------------------------------------------------------------------------
  62. // Draw
  63. //----------------------------------------------------------------------------------
  64. // Draw into our custom render texture (framebuffer)
  65. BeginTextureMode(target);
  66. ClearBackground(WHITE);
  67. BeginMode3D(camera);
  68. BeginShaderMode(shader);
  69. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, RED);
  70. DrawCubeV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, PURPLE);
  71. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, DARKGREEN);
  72. DrawCubeV((Vector3) { 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, YELLOW);
  73. DrawGrid(10, 1.0f);
  74. EndShaderMode();
  75. EndMode3D();
  76. EndTextureMode();
  77. // Draw into screen our custom render texture
  78. BeginDrawing();
  79. ClearBackground(RAYWHITE);
  80. DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);
  81. DrawFPS(10, 10);
  82. EndDrawing();
  83. //----------------------------------------------------------------------------------
  84. }
  85. // De-Initialization
  86. //--------------------------------------------------------------------------------------
  87. UnloadRenderTextureDepthTex(target);
  88. UnloadShader(shader);
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. return 0;
  92. }
  93. //------------------------------------------------------------------------------------
  94. // Define custom functions required for the example
  95. //------------------------------------------------------------------------------------
  96. // Load custom render texture, create a writable depth texture buffer
  97. RenderTexture2D LoadRenderTextureDepthTex(int width, int height)
  98. {
  99. RenderTexture2D target = { 0 };
  100. target.id = rlLoadFramebuffer(); // Load an empty framebuffer
  101. if (target.id > 0)
  102. {
  103. rlEnableFramebuffer(target.id);
  104. // Create color texture (default to RGBA)
  105. target.texture.id = rlLoadTexture(0, width, height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
  106. target.texture.width = width;
  107. target.texture.height = height;
  108. target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
  109. target.texture.mipmaps = 1;
  110. // Create depth texture buffer (instead of raylib default renderbuffer)
  111. target.depth.id = rlLoadTextureDepth(width, height, false);
  112. target.depth.width = width;
  113. target.depth.height = height;
  114. target.depth.format = 19; //DEPTH_COMPONENT_24BIT?
  115. target.depth.mipmaps = 1;
  116. // Attach color texture and depth texture to FBO
  117. rlFramebufferAttach(target.id, target.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
  118. rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
  119. // Check if fbo is complete with attachments (valid)
  120. if (rlFramebufferComplete(target.id)) TRACELOG(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", target.id);
  121. rlDisableFramebuffer();
  122. }
  123. else TRACELOG(LOG_WARNING, "FBO: Framebuffer object can not be created");
  124. return target;
  125. }
  126. // Unload render texture from GPU memory (VRAM)
  127. void UnloadRenderTextureDepthTex(RenderTexture2D target)
  128. {
  129. if (target.id > 0)
  130. {
  131. // Color texture attached to FBO is deleted
  132. rlUnloadTexture(target.texture.id);
  133. rlUnloadTexture(target.depth.id);
  134. // NOTE: Depth texture is automatically
  135. // queried and deleted before deleting framebuffer
  136. rlUnloadFramebuffer(target.id);
  137. }
  138. }