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.

207 lines
9.1 KiB

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