shaders_hybrid_rendering.c 9.1 KB

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