您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

167 行
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_RPI, 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 mode type
  50. };
  51. SetCameraMode(camera, CAMERA_ORBITAL);
  52. SetTargetFPS(60);
  53. //--------------------------------------------------------------------------------------
  54. // Main game loop
  55. while (!WindowShouldClose()) // Detect window close button or ESC key
  56. {
  57. // Update
  58. //----------------------------------------------------------------------------------
  59. UpdateCamera(&camera);
  60. //----------------------------------------------------------------------------------
  61. // Draw
  62. //----------------------------------------------------------------------------------
  63. // Draw into our custom render texture (framebuffer)
  64. BeginTextureMode(target);
  65. ClearBackground(WHITE);
  66. BeginMode3D(camera);
  67. BeginShaderMode(shader);
  68. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, RED);
  69. DrawCubeV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, PURPLE);
  70. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, DARKGREEN);
  71. DrawCubeV((Vector3) { 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, YELLOW);
  72. DrawGrid(10, 1.0f);
  73. EndShaderMode();
  74. EndMode3D();
  75. EndTextureMode();
  76. // Draw into screen our custom render texture
  77. BeginDrawing();
  78. ClearBackground(RAYWHITE);
  79. DrawTextureRec(target.texture, (Rectangle) { 0, 0, screenWidth, -screenHeight }, (Vector2) { 0, 0 }, WHITE);
  80. DrawFPS(10, 10);
  81. EndDrawing();
  82. //----------------------------------------------------------------------------------
  83. }
  84. // De-Initialization
  85. //--------------------------------------------------------------------------------------
  86. UnloadRenderTextureDepthTex(target);
  87. UnloadShader(shader);
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }
  92. //------------------------------------------------------------------------------------
  93. // Define custom functions required for the example
  94. //------------------------------------------------------------------------------------
  95. // Load custom render texture, create a writable depth texture buffer
  96. RenderTexture2D LoadRenderTextureDepthTex(int width, int height)
  97. {
  98. RenderTexture2D target = { 0 };
  99. target.id = rlLoadFramebuffer(width, height); // Load an empty framebuffer
  100. if (target.id > 0)
  101. {
  102. rlEnableFramebuffer(target.id);
  103. // Create color texture (default to RGBA)
  104. target.texture.id = rlLoadTexture(0, width, height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
  105. target.texture.width = width;
  106. target.texture.height = height;
  107. target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
  108. target.texture.mipmaps = 1;
  109. // Create depth texture buffer (instead of raylib default renderbuffer)
  110. target.depth.id = rlLoadTextureDepth(width, height, false);
  111. target.depth.width = width;
  112. target.depth.height = height;
  113. target.depth.format = 19; //DEPTH_COMPONENT_24BIT?
  114. target.depth.mipmaps = 1;
  115. // Attach color texture and depth texture to FBO
  116. rlFramebufferAttach(target.id, target.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
  117. rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
  118. // Check if fbo is complete with attachments (valid)
  119. if (rlFramebufferComplete(target.id)) TRACELOG(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", target.id);
  120. rlDisableFramebuffer();
  121. }
  122. else TRACELOG(LOG_WARNING, "FBO: Framebuffer object can not be created");
  123. return target;
  124. }
  125. // Unload render texture from GPU memory (VRAM)
  126. void UnloadRenderTextureDepthTex(RenderTexture2D target)
  127. {
  128. if (target.id > 0)
  129. {
  130. // Color texture attached to FBO is deleted
  131. rlUnloadTexture(target.texture.id);
  132. rlUnloadTexture(target.depth.id);
  133. // NOTE: Depth texture is automatically
  134. // queried and deleted before deleting framebuffer
  135. rlUnloadFramebuffer(target.id);
  136. }
  137. }