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.

166 lines
7.0 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Depth buffer writing
  4. *
  5. * Example originally created with raylib 4.2, last time updated with raylib 4.2
  6. *
  7. * Example contributed by Buğra Alptekin Sarı (@BugraAlptekinSari) and reviewed by Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2022-2023 Buğra Alptekin Sarı (@BugraAlptekinSari)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "rlgl.h"
  17. #if defined(PLATFORM_DESKTOP)
  18. #define GLSL_VERSION 330
  19. #else // PLATFORM_ANDROID, PLATFORM_WEB
  20. #define GLSL_VERSION 100
  21. #endif
  22. //------------------------------------------------------------------------------------
  23. // Declare custom functions required for the example
  24. //------------------------------------------------------------------------------------
  25. // Load custom render texture, create a writable depth texture buffer
  26. static RenderTexture2D LoadRenderTextureDepthTex(int width, int height);
  27. // Unload render texture from GPU memory (VRAM)
  28. static void UnloadRenderTextureDepthTex(RenderTexture2D target);
  29. //------------------------------------------------------------------------------------
  30. // Program main entry point
  31. //------------------------------------------------------------------------------------
  32. int main(void)
  33. {
  34. // Initialization
  35. //--------------------------------------------------------------------------------------
  36. const int screenWidth = 800;
  37. const int screenHeight = 450;
  38. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - write depth buffer");
  39. // The shader inverts the depth buffer by writing into it by `gl_FragDepth = 1 - gl_FragCoord.z;`
  40. Shader shader = LoadShader(0, TextFormat("resources/shaders/glsl%i/write_depth.fs", GLSL_VERSION));
  41. // Use Customized function to create writable depth texture buffer
  42. RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
  43. // Define the camera to look into our 3d world
  44. Camera camera = {
  45. .position = (Vector3){ 2.0f, 2.0f, 3.0f }, // Camera position
  46. .target = (Vector3){ 0.0f, 0.5f, 0.0f }, // Camera looking at point
  47. .up = (Vector3){ 0.0f, 1.0f, 0.0f }, // Camera up vector (rotation towards target)
  48. .fovy = 45.0f, // Camera field-of-view Y
  49. .projection = CAMERA_PERSPECTIVE // Camera projection type
  50. };
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. UpdateCamera(&camera, CAMERA_ORBITAL);
  59. //----------------------------------------------------------------------------------
  60. // Draw
  61. //----------------------------------------------------------------------------------
  62. // Draw into our custom render texture (framebuffer)
  63. BeginTextureMode(target);
  64. ClearBackground(WHITE);
  65. BeginMode3D(camera);
  66. BeginShaderMode(shader);
  67. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, RED);
  68. DrawCubeV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, PURPLE);
  69. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, DARKGREEN);
  70. DrawCubeV((Vector3) { 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, YELLOW);
  71. DrawGrid(10, 1.0f);
  72. EndShaderMode();
  73. EndMode3D();
  74. EndTextureMode();
  75. // Draw into screen our custom render texture
  76. BeginDrawing();
  77. ClearBackground(RAYWHITE);
  78. DrawTextureRec(target.texture, (Rectangle) { 0, 0, screenWidth, -screenHeight }, (Vector2) { 0, 0 }, WHITE);
  79. DrawFPS(10, 10);
  80. EndDrawing();
  81. //----------------------------------------------------------------------------------
  82. }
  83. // De-Initialization
  84. //--------------------------------------------------------------------------------------
  85. UnloadRenderTextureDepthTex(target);
  86. UnloadShader(shader);
  87. CloseWindow(); // Close window and OpenGL context
  88. //--------------------------------------------------------------------------------------
  89. return 0;
  90. }
  91. //------------------------------------------------------------------------------------
  92. // Define custom functions required for the example
  93. //------------------------------------------------------------------------------------
  94. // Load custom render texture, create a writable depth texture buffer
  95. RenderTexture2D LoadRenderTextureDepthTex(int width, int height)
  96. {
  97. RenderTexture2D target = { 0 };
  98. target.id = rlLoadFramebuffer(width, height); // 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 buffer (instead of raylib default renderbuffer)
  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?
  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. }
  124. // Unload render texture from GPU memory (VRAM)
  125. void UnloadRenderTextureDepthTex(RenderTexture2D target)
  126. {
  127. if (target.id > 0)
  128. {
  129. // Color texture attached to FBO is deleted
  130. rlUnloadTexture(target.texture.id);
  131. rlUnloadTexture(target.depth.id);
  132. // NOTE: Depth texture is automatically
  133. // queried and deleted before deleting framebuffer
  134. rlUnloadFramebuffer(target.id);
  135. }
  136. }