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.

339 lines
15 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - deferred rendering
  4. *
  5. * NOTE: This example requires raylib OpenGL 3.3 or OpenGL ES 3.0
  6. *
  7. * Example originally created with raylib 4.5, last time updated with raylib 4.5
  8. *
  9. * Example contributed by Justin Andreas Lacoste (@27justin) 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) 2023 Justin Andreas Lacoste (@27justin)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include "rlgl.h"
  19. #include "raymath.h"
  20. #define RLIGHTS_IMPLEMENTATION
  21. #include "rlights.h"
  22. #if defined(PLATFORM_DESKTOP)
  23. #define GLSL_VERSION 330
  24. #else // PLATFORM_ANDROID, PLATFORM_WEB
  25. #define GLSL_VERSION 100
  26. #endif
  27. #include <stdlib.h> // Required for: NULL
  28. #define MAX_CUBES 30
  29. // GBuffer data
  30. typedef struct GBuffer {
  31. unsigned int framebuffer;
  32. unsigned int positionTexture;
  33. unsigned int normalTexture;
  34. unsigned int albedoSpecTexture;
  35. unsigned int depthRenderbuffer;
  36. } GBuffer;
  37. // Deferred mode passes
  38. typedef enum {
  39. DEFERRED_POSITION,
  40. DEFERRED_NORMAL,
  41. DEFERRED_ALBEDO,
  42. DEFERRED_SHADING
  43. } DeferredMode;
  44. //------------------------------------------------------------------------------------
  45. // Program main entry point
  46. //------------------------------------------------------------------------------------
  47. int main(void)
  48. {
  49. // Initialization
  50. // -------------------------------------------------------------------------------------
  51. const int screenWidth = 800;
  52. const int screenHeight = 450;
  53. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - deferred render");
  54. Camera camera = { 0 };
  55. camera.position = (Vector3){ 5.0f, 4.0f, 5.0f }; // Camera position
  56. camera.target = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera looking at point
  57. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  58. camera.fovy = 60.0f; // Camera field-of-view Y
  59. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  60. // Load plane model from a generated mesh
  61. Model model = LoadModelFromMesh(GenMeshPlane(10.0f, 10.0f, 3, 3));
  62. Model cube = LoadModelFromMesh(GenMeshCube(2.0f, 2.0f, 2.0f));
  63. // Load geometry buffer (G-buffer) shader and deferred shader
  64. Shader gbufferShader = LoadShader("resources/shaders/glsl330/gbuffer.vs",
  65. "resources/shaders/glsl330/gbuffer.fs");
  66. Shader deferredShader = LoadShader("resources/shaders/glsl330/deferred_shading.vs",
  67. "resources/shaders/glsl330/deferred_shading.fs");
  68. deferredShader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(deferredShader, "viewPosition");
  69. // Initialize the G-buffer
  70. GBuffer gBuffer = { 0 };
  71. gBuffer.framebuffer = rlLoadFramebuffer();
  72. if (!gBuffer.framebuffer)
  73. {
  74. TraceLog(LOG_WARNING, "Failed to create framebuffer");
  75. exit(1);
  76. }
  77. rlEnableFramebuffer(gBuffer.framebuffer);
  78. // Since we are storing position and normal data in these textures,
  79. // we need to use a floating point format.
  80. gBuffer.positionTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32, 1);
  81. gBuffer.normalTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R32G32B32, 1);
  82. // Albedo (diffuse color) and specular strength can be combined into one texture.
  83. // The color in RGB, and the specular strength in the alpha channel.
  84. gBuffer.albedoSpecTexture = rlLoadTexture(NULL, screenWidth, screenHeight, RL_PIXELFORMAT_UNCOMPRESSED_R8G8B8A8, 1);
  85. // Activate the draw buffers for our framebuffer
  86. rlActiveDrawBuffers(3);
  87. // Now we attach our textures to the framebuffer.
  88. rlFramebufferAttach(gBuffer.framebuffer, gBuffer.positionTexture, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
  89. rlFramebufferAttach(gBuffer.framebuffer, gBuffer.normalTexture, RL_ATTACHMENT_COLOR_CHANNEL1, RL_ATTACHMENT_TEXTURE2D, 0);
  90. rlFramebufferAttach(gBuffer.framebuffer, gBuffer.albedoSpecTexture, RL_ATTACHMENT_COLOR_CHANNEL2, RL_ATTACHMENT_TEXTURE2D, 0);
  91. // Finally we attach the depth buffer.
  92. gBuffer.depthRenderbuffer = rlLoadTextureDepth(screenWidth, screenHeight, true);
  93. rlFramebufferAttach(gBuffer.framebuffer, gBuffer.depthRenderbuffer, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0);
  94. // Make sure our framebuffer is complete.
  95. // NOTE: rlFramebufferComplete() automatically unbinds the framebuffer, so we don't have
  96. // to rlDisableFramebuffer() here.
  97. if (!rlFramebufferComplete(gBuffer.framebuffer))
  98. {
  99. TraceLog(LOG_WARNING, "Framebuffer is not complete");
  100. exit(1);
  101. }
  102. // Now we initialize the sampler2D uniform's in the deferred shader.
  103. // We do this by setting the uniform's value to the color channel slot we earlier
  104. // bound our textures to.
  105. rlEnableShader(deferredShader.id);
  106. rlSetUniformSampler(rlGetLocationUniform(deferredShader.id, "gPosition"), 0);
  107. rlSetUniformSampler(rlGetLocationUniform(deferredShader.id, "gNormal"), 1);
  108. rlSetUniformSampler(rlGetLocationUniform(deferredShader.id, "gAlbedoSpec"), 2);
  109. rlDisableShader();
  110. // Assign out lighting shader to model
  111. model.materials[0].shader = gbufferShader;
  112. cube.materials[0].shader = gbufferShader;
  113. // Create lights
  114. //--------------------------------------------------------------------------------------
  115. Light lights[MAX_LIGHTS] = { 0 };
  116. lights[0] = CreateLight(LIGHT_POINT, (Vector3){ -2, 1, -2 }, Vector3Zero(), YELLOW, deferredShader);
  117. lights[1] = CreateLight(LIGHT_POINT, (Vector3){ 2, 1, 2 }, Vector3Zero(), RED, deferredShader);
  118. lights[2] = CreateLight(LIGHT_POINT, (Vector3){ -2, 1, 2 }, Vector3Zero(), GREEN, deferredShader);
  119. lights[3] = CreateLight(LIGHT_POINT, (Vector3){ 2, 1, -2 }, Vector3Zero(), BLUE, deferredShader);
  120. const float CUBE_SCALE = 0.25;
  121. Vector3 cubePositions[MAX_CUBES] = { 0 };
  122. float cubeRotations[MAX_CUBES] = { 0 };
  123. for (int i = 0; i < MAX_CUBES; i++)
  124. {
  125. cubePositions[i] = (Vector3){
  126. .x = (float)(rand()%10) - 5,
  127. .y = (float)(rand()%5),
  128. .z = (float)(rand()%10) - 5,
  129. };
  130. cubeRotations[i] = (float)(rand()%360);
  131. }
  132. DeferredMode mode = DEFERRED_SHADING;
  133. rlEnableDepthTest();
  134. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  135. //---------------------------------------------------------------------------------------
  136. // Main game loop
  137. while (!WindowShouldClose())
  138. {
  139. // Update
  140. //----------------------------------------------------------------------------------
  141. UpdateCamera(&camera, CAMERA_ORBITAL);
  142. // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
  143. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  144. SetShaderValue(deferredShader, deferredShader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  145. // Check key inputs to enable/disable lights
  146. if (IsKeyPressed(KEY_Y)) { lights[0].enabled = !lights[0].enabled; }
  147. if (IsKeyPressed(KEY_R)) { lights[1].enabled = !lights[1].enabled; }
  148. if (IsKeyPressed(KEY_G)) { lights[2].enabled = !lights[2].enabled; }
  149. if (IsKeyPressed(KEY_B)) { lights[3].enabled = !lights[3].enabled; }
  150. // Check key inputs to switch between G-buffer textures
  151. if (IsKeyPressed(KEY_ONE)) mode = DEFERRED_POSITION;
  152. if (IsKeyPressed(KEY_TWO)) mode = DEFERRED_NORMAL;
  153. if (IsKeyPressed(KEY_THREE)) mode = DEFERRED_ALBEDO;
  154. if (IsKeyPressed(KEY_FOUR)) mode = DEFERRED_SHADING;
  155. // Update light values (actually, only enable/disable them)
  156. for (int i = 0; i < MAX_LIGHTS; i++) UpdateLightValues(deferredShader, lights[i]);
  157. //----------------------------------------------------------------------------------
  158. // Draw
  159. // ---------------------------------------------------------------------------------
  160. BeginDrawing();
  161. ClearBackground(RAYWHITE);
  162. // Draw to the geometry buffer by first activating it
  163. rlEnableFramebuffer(gBuffer.framebuffer);
  164. rlClearScreenBuffers(); // Clear color and depth buffer
  165. rlDisableColorBlend();
  166. BeginMode3D(camera);
  167. // NOTE: We have to use rlEnableShader here. `BeginShaderMode` or thus `rlSetShader`
  168. // will not work, as they won't immediately load the shader program.
  169. rlEnableShader(gbufferShader.id);
  170. // When drawing a model here, make sure that the material's shaders
  171. // are set to the gbuffer shader!
  172. DrawModel(model, Vector3Zero(), 1.0f, WHITE);
  173. DrawModel(cube, (Vector3) { 0.0, 1.0f, 0.0 }, 1.0f, WHITE);
  174. for (int i = 0; i < MAX_CUBES; i++)
  175. {
  176. Vector3 position = cubePositions[i];
  177. DrawModelEx(cube, position, (Vector3) { 1, 1, 1 }, cubeRotations[i], (Vector3) { CUBE_SCALE, CUBE_SCALE, CUBE_SCALE }, WHITE);
  178. }
  179. rlDisableShader();
  180. EndMode3D();
  181. rlEnableColorBlend();
  182. // Go back to the default framebuffer (0) and draw our deferred shading.
  183. rlDisableFramebuffer();
  184. rlClearScreenBuffers(); // Clear color & depth buffer
  185. switch (mode)
  186. {
  187. case DEFERRED_SHADING:
  188. {
  189. BeginMode3D(camera);
  190. rlDisableColorBlend();
  191. rlEnableShader(deferredShader.id);
  192. // Activate our g-buffer textures
  193. // These will now be bound to the sampler2D uniforms `gPosition`, `gNormal`,
  194. // and `gAlbedoSpec`
  195. rlActiveTextureSlot(0);
  196. rlEnableTexture(gBuffer.positionTexture);
  197. rlActiveTextureSlot(1);
  198. rlEnableTexture(gBuffer.normalTexture);
  199. rlActiveTextureSlot(2);
  200. rlEnableTexture(gBuffer.albedoSpecTexture);
  201. // Finally, we draw a fullscreen quad to our default framebuffer
  202. // This will now be shaded using our deferred shader
  203. rlLoadDrawQuad();
  204. rlDisableShader();
  205. rlEnableColorBlend();
  206. EndMode3D();
  207. // As a last step, we now copy over the depth buffer from our g-buffer to the default framebuffer.
  208. rlBindFramebuffer(RL_READ_FRAMEBUFFER, gBuffer.framebuffer);
  209. rlBindFramebuffer(RL_DRAW_FRAMEBUFFER, 0);
  210. rlBlitFramebuffer(0, 0, screenWidth, screenHeight, 0, 0, screenWidth, screenHeight, 0x00000100); // GL_DEPTH_BUFFER_BIT
  211. rlDisableFramebuffer();
  212. // Since our shader is now done and disabled, we can draw our lights in default
  213. // forward rendering
  214. BeginMode3D(camera);
  215. rlEnableShader(rlGetShaderIdDefault());
  216. for(int i = 0; i < MAX_LIGHTS; i++)
  217. {
  218. if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lights[i].color);
  219. else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lights[i].color, 0.3f));
  220. }
  221. rlDisableShader();
  222. EndMode3D();
  223. DrawText("FINAL RESULT", 10, screenHeight - 30, 20, DARKGREEN);
  224. } break;
  225. case DEFERRED_POSITION:
  226. {
  227. DrawTextureRec((Texture2D){
  228. .id = gBuffer.positionTexture,
  229. .width = screenWidth,
  230. .height = screenHeight,
  231. }, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, Vector2Zero(), RAYWHITE);
  232. DrawText("POSITION TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
  233. } break;
  234. case DEFERRED_NORMAL:
  235. {
  236. DrawTextureRec((Texture2D){
  237. .id = gBuffer.normalTexture,
  238. .width = screenWidth,
  239. .height = screenHeight,
  240. }, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, Vector2Zero(), RAYWHITE);
  241. DrawText("NORMAL TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
  242. } break;
  243. case DEFERRED_ALBEDO:
  244. {
  245. DrawTextureRec((Texture2D){
  246. .id = gBuffer.albedoSpecTexture,
  247. .width = screenWidth,
  248. .height = screenHeight,
  249. }, (Rectangle) { 0, 0, (float)screenWidth, (float)-screenHeight }, Vector2Zero(), RAYWHITE);
  250. DrawText("ALBEDO TEXTURE", 10, screenHeight - 30, 20, DARKGREEN);
  251. } break;
  252. default: break;
  253. }
  254. DrawText("Toggle lights keys: [Y][R][G][B]", 10, 40, 20, DARKGRAY);
  255. DrawText("Switch G-buffer textures: [1][2][3][4]", 10, 70, 20, DARKGRAY);
  256. DrawFPS(10, 10);
  257. EndDrawing();
  258. // -----------------------------------------------------------------------------
  259. }
  260. // De-Initialization
  261. //--------------------------------------------------------------------------------------
  262. UnloadModel(model); // Unload the models
  263. UnloadModel(cube);
  264. UnloadShader(deferredShader); // Unload shaders
  265. UnloadShader(gbufferShader);
  266. // Unload geometry buffer and all attached textures
  267. rlUnloadFramebuffer(gBuffer.framebuffer);
  268. rlUnloadTexture(gBuffer.positionTexture);
  269. rlUnloadTexture(gBuffer.normalTexture);
  270. rlUnloadTexture(gBuffer.albedoSpecTexture);
  271. rlUnloadTexture(gBuffer.depthRenderbuffer);
  272. CloseWindow(); // Close window and OpenGL context
  273. //--------------------------------------------------------------------------------------
  274. return 0;
  275. }