Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

210 lignes
9.1 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Hybrid Rendering
  4. *
  5. * Example complexity rating: [] 4/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. #include "math.h" // Used for tan()
  20. #include "raymath.h" // Used to calculate camera Direction
  21. #if defined(PLATFORM_DESKTOP)
  22. #define GLSL_VERSION 330
  23. #else // PLATFORM_ANDROID, PLATFORM_WEB
  24. #define GLSL_VERSION 100
  25. #endif
  26. //------------------------------------------------------------------------------------
  27. // Declare custom functions required for the example
  28. //------------------------------------------------------------------------------------
  29. // Load custom render texture, create a writable depth texture buffer
  30. static RenderTexture2D LoadRenderTextureDepthTex(int width, int height);
  31. // Unload render texture from GPU memory (VRAM)
  32. static void UnloadRenderTextureDepthTex(RenderTexture2D target);
  33. //------------------------------------------------------------------------------------
  34. // Declare custom Structs
  35. //------------------------------------------------------------------------------------
  36. typedef struct {
  37. unsigned int camPos, camDir, screenCenter;
  38. }RayLocs ;
  39. //------------------------------------------------------------------------------------
  40. // Program main entry point
  41. //------------------------------------------------------------------------------------
  42. int main(void)
  43. {
  44. // Initialization
  45. //--------------------------------------------------------------------------------------
  46. const int screenWidth = 800;
  47. const int screenHeight = 450;
  48. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - write depth buffer");
  49. // This Shader calculates pixel depth and color using raymarch
  50. Shader shdrRaymarch = LoadShader(0, TextFormat("resources/shaders/glsl%i/hybrid_raymarch.fs", GLSL_VERSION));
  51. // This Shader is a standard rasterization fragment shader with the addition of depth writing
  52. // You are required to write depth for all shaders if one shader does it
  53. Shader shdrRaster = LoadShader(0, TextFormat("resources/shaders/glsl%i/hybrid_raster.fs", GLSL_VERSION));
  54. // Declare Struct used to store camera locs.
  55. RayLocs marchLocs = {0};
  56. // Fill the struct with shader locs.
  57. marchLocs.camPos = GetShaderLocation(shdrRaymarch, "camPos");
  58. marchLocs.camDir = GetShaderLocation(shdrRaymarch, "camDir");
  59. marchLocs.screenCenter = GetShaderLocation(shdrRaymarch, "screenCenter");
  60. // Transfer screenCenter position to shader. Which is used to calculate ray direction.
  61. Vector2 screenCenter = {.x = screenWidth/2.0f, .y = screenHeight/2.0f};
  62. SetShaderValue(shdrRaymarch, marchLocs.screenCenter , &screenCenter , SHADER_UNIFORM_VEC2);
  63. // Use Customized function to create writable depth texture buffer
  64. RenderTexture2D target = LoadRenderTextureDepthTex(screenWidth, screenHeight);
  65. // Define the camera to look into our 3d world
  66. Camera camera = {
  67. .position = (Vector3){ 0.5f, 1.0f, 1.5f }, // Camera position
  68. .target = (Vector3){ 0.0f, 0.5f, 0.0f }, // Camera looking at point
  69. .up = (Vector3){ 0.0f, 1.0f, 0.0f }, // Camera up vector (rotation towards target)
  70. .fovy = 45.0f, // Camera field-of-view Y
  71. .projection = CAMERA_PERSPECTIVE // Camera projection type
  72. };
  73. // Camera FOV is pre-calculated in the camera Distance.
  74. float camDist = 1.0f/(tanf(camera.fovy*0.5f*DEG2RAD));
  75. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  76. //--------------------------------------------------------------------------------------
  77. // Main game loop
  78. while (!WindowShouldClose()) // Detect window close button or ESC key
  79. {
  80. // Update
  81. //----------------------------------------------------------------------------------
  82. UpdateCamera(&camera, CAMERA_ORBITAL);
  83. // Update Camera Postion in the ray march shader.
  84. SetShaderValue(shdrRaymarch, marchLocs.camPos, &(camera.position), RL_SHADER_UNIFORM_VEC3);
  85. // Update Camera Looking Vector. Vector length determines FOV.
  86. Vector3 camDir = Vector3Scale( Vector3Normalize( Vector3Subtract(camera.target, camera.position)) , camDist);
  87. SetShaderValue(shdrRaymarch, marchLocs.camDir, &(camDir), RL_SHADER_UNIFORM_VEC3);
  88. //----------------------------------------------------------------------------------
  89. // Draw
  90. //----------------------------------------------------------------------------------
  91. // Draw into our custom render texture (framebuffer)
  92. BeginTextureMode(target);
  93. ClearBackground(WHITE);
  94. // Raymarch Scene
  95. rlEnableDepthTest(); //Manually enable Depth Test to handle multiple rendering methods.
  96. BeginShaderMode(shdrRaymarch);
  97. DrawRectangleRec((Rectangle){0,0, (float)screenWidth, (float)screenHeight},WHITE);
  98. EndShaderMode();
  99. // Rasterize Scene
  100. BeginMode3D(camera);
  101. BeginShaderMode(shdrRaster);
  102. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, RED);
  103. DrawCubeV((Vector3){ 0.0f, 0.5f, 1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, PURPLE);
  104. DrawCubeWiresV((Vector3){ 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, DARKGREEN);
  105. DrawCubeV((Vector3) { 0.0f, 0.5f, -1.0f }, (Vector3){ 1.0f, 1.0f, 1.0f }, YELLOW);
  106. DrawGrid(10, 1.0f);
  107. EndShaderMode();
  108. EndMode3D();
  109. EndTextureMode();
  110. // Draw into screen our custom render texture
  111. BeginDrawing();
  112. ClearBackground(RAYWHITE);
  113. DrawTextureRec(target.texture, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, (Vector2) { 0, 0 }, WHITE);
  114. DrawFPS(10, 10);
  115. EndDrawing();
  116. //----------------------------------------------------------------------------------
  117. }
  118. // De-Initialization
  119. //--------------------------------------------------------------------------------------
  120. UnloadRenderTextureDepthTex(target);
  121. UnloadShader(shdrRaymarch);
  122. UnloadShader(shdrRaster);
  123. CloseWindow(); // Close window and OpenGL context
  124. //--------------------------------------------------------------------------------------
  125. return 0;
  126. }
  127. //------------------------------------------------------------------------------------
  128. // Define custom functions required for the example
  129. //------------------------------------------------------------------------------------
  130. // Load custom render texture, create a writable depth texture buffer
  131. RenderTexture2D LoadRenderTextureDepthTex(int width, int height)
  132. {
  133. RenderTexture2D target = { 0 };
  134. target.id = rlLoadFramebuffer(); // Load an empty framebuffer
  135. if (target.id > 0)
  136. {
  137. rlEnableFramebuffer(target.id);
  138. // Create color texture (default to RGBA)
  139. target.texture.id = rlLoadTexture(0, width, height, PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
  140. target.texture.width = width;
  141. target.texture.height = height;
  142. target.texture.format = PIXELFORMAT_UNCOMPRESSED_R8G8B8A8;
  143. target.texture.mipmaps = 1;
  144. // Create depth texture buffer (instead of raylib default renderbuffer)
  145. target.depth.id = rlLoadTextureDepth(width, height, false);
  146. target.depth.width = width;
  147. target.depth.height = height;
  148. target.depth.format = 19; //DEPTH_COMPONENT_24BIT?
  149. target.depth.mipmaps = 1;
  150. // Attach color texture and depth texture to FBO
  151. rlFramebufferAttach(target.id, target.texture.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
  152. rlFramebufferAttach(target.id, target.depth.id, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_TEXTURE2D, 0);
  153. // Check if fbo is complete with attachments (valid)
  154. if (rlFramebufferComplete(target.id)) TRACELOG(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", target.id);
  155. rlDisableFramebuffer();
  156. }
  157. else TRACELOG(LOG_WARNING, "FBO: Framebuffer object can not be created");
  158. return target;
  159. }
  160. // Unload render texture from GPU memory (VRAM)
  161. void UnloadRenderTextureDepthTex(RenderTexture2D target)
  162. {
  163. if (target.id > 0)
  164. {
  165. // Color texture attached to FBO is deleted
  166. rlUnloadTexture(target.texture.id);
  167. rlUnloadTexture(target.depth.id);
  168. // NOTE: Depth texture is automatically
  169. // queried and deleted before deleting framebuffer
  170. rlUnloadFramebuffer(target.id);
  171. }
  172. }