選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

543 行
28 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - PBR material
  4. *
  5. * NOTE: This example requires raylib OpenGL 3.3 for shaders support and only #version 330
  6. * is currently supported. OpenGL ES 2.0 platforms are not supported at the moment.
  7. *
  8. * This example has been created using raylib 1.8 (www.raylib.com)
  9. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  10. *
  11. * Copyright (c) 2017 Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "raymath.h"
  16. #include "rlgl.h"
  17. #include <stdio.h>
  18. #define RLIGHTS_IMPLEMENTATION
  19. #include "rlights.h"
  20. #if defined(PLATFORM_DESKTOP)
  21. #define GLSL_VERSION 330
  22. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  23. #define GLSL_VERSION 100
  24. #endif
  25. #define CUBEMAP_SIZE 1024 // Cubemap texture size
  26. #define IRRADIANCE_SIZE 32 // Irradiance texture size
  27. #define PREFILTERED_SIZE 256 // Prefiltered HDR environment texture size
  28. #define BRDF_SIZE 512 // BRDF LUT texture size
  29. #define LIGHT_DISTANCE 1000.0f
  30. #define LIGHT_HEIGHT 1.0f
  31. // PBR texture maps generation
  32. static TextureCubemap GenTextureCubemap(Shader shader, Texture2D panorama, int size, int format); // Generate cubemap (6 faces) from equirectangular (panorama) texture
  33. static TextureCubemap GenTextureIrradiance(Shader shader, TextureCubemap cubemap, int size); // Generate irradiance cubemap using cubemap texture
  34. static TextureCubemap GenTexturePrefilter(Shader shader, TextureCubemap cubemap, int size); // Generate prefilter cubemap using cubemap texture
  35. static Texture2D GenTextureBRDF(Shader shader, int size); // Generate a generic BRDF texture
  36. // PBR material loading
  37. static Material LoadMaterialPBR(Color albedo, float metalness, float roughness);
  38. int main(void)
  39. {
  40. // Initialization
  41. //--------------------------------------------------------------------------------------
  42. const int screenWidth = 800;
  43. const int screenHeight = 450;
  44. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  45. InitWindow(screenWidth, screenHeight, "raylib [models] example - pbr material");
  46. // Define the camera to look into our 3d world
  47. Camera camera = { 0 };
  48. camera.position = (Vector3){ 4.0f, 4.0f, 4.0f }; // Camera position
  49. camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
  50. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  51. camera.fovy = 45.0f; // Camera field-of-view Y
  52. camera.projection = CAMERA_PERSPECTIVE; // Camera mode type
  53. // Load model and PBR material
  54. Model model = LoadModel("resources/pbr/trooper.obj");
  55. // Mesh tangents are generated... and uploaded to GPU
  56. // NOTE: New VBO for tangents is generated at default location and also binded to mesh VAO
  57. //MeshTangents(&model.meshes[0]);
  58. model.materials[0] = LoadMaterialPBR((Color){ 255, 255, 255, 255 }, 1.0f, 1.0f);
  59. // Create lights
  60. // NOTE: Lights are added to an internal lights pool automatically
  61. CreateLight(LIGHT_POINT, (Vector3){ LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 255, 0, 0, 255 }, model.materials[0].shader);
  62. CreateLight(LIGHT_POINT, (Vector3){ 0.0f, LIGHT_HEIGHT, LIGHT_DISTANCE }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 0, 255, 0, 255 }, model.materials[0].shader);
  63. CreateLight(LIGHT_POINT, (Vector3){ -LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 0, 0, 255, 255 }, model.materials[0].shader);
  64. CreateLight(LIGHT_DIRECTIONAL, (Vector3){ 0.0f, LIGHT_HEIGHT*2.0f, -LIGHT_DISTANCE }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 255, 0, 255, 255 }, model.materials[0].shader);
  65. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  66. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  67. //--------------------------------------------------------------------------------------
  68. // Main game loop
  69. while (!WindowShouldClose()) // Detect window close button or ESC key
  70. {
  71. // Update
  72. //----------------------------------------------------------------------------------
  73. UpdateCamera(&camera); // Update camera
  74. // Send to material PBR shader camera view position
  75. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  76. SetShaderValue(model.materials[0].shader, model.materials[0].shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  77. //----------------------------------------------------------------------------------
  78. // Draw
  79. //----------------------------------------------------------------------------------
  80. BeginDrawing();
  81. ClearBackground(RAYWHITE);
  82. BeginMode3D(camera);
  83. DrawModel(model, Vector3Zero(), 1.0f, WHITE);
  84. DrawGrid(10, 1.0f);
  85. EndMode3D();
  86. DrawFPS(10, 10);
  87. EndDrawing();
  88. //----------------------------------------------------------------------------------
  89. }
  90. // De-Initialization
  91. //--------------------------------------------------------------------------------------
  92. UnloadMaterial(model.materials[0]); // Unload material: shader and textures
  93. UnloadModel(model); // Unload model
  94. CloseWindow(); // Close window and OpenGL context
  95. //--------------------------------------------------------------------------------------
  96. return 0;
  97. }
  98. // Load PBR material (Supports: ALBEDO, NORMAL, METALNESS, ROUGHNESS, AO, EMMISIVE, HEIGHT maps)
  99. // NOTE: PBR shader is loaded inside this function
  100. static Material LoadMaterialPBR(Color albedo, float metalness, float roughness)
  101. {
  102. Material mat = LoadMaterialDefault(); // Initialize material to default
  103. // Load PBR shader (requires several maps)
  104. mat.shader = LoadShader(TextFormat("resources/shaders/glsl%i/pbr.vs", GLSL_VERSION),
  105. TextFormat("resources/shaders/glsl%i/pbr.fs", GLSL_VERSION));
  106. // Get required locations points for PBR material
  107. // NOTE: Those location names must be available and used in the shader code
  108. mat.shader.locs[SHADER_LOC_MAP_ALBEDO] = GetShaderLocation(mat.shader, "albedo.sampler");
  109. mat.shader.locs[SHADER_LOC_MAP_METALNESS] = GetShaderLocation(mat.shader, "metalness.sampler");
  110. mat.shader.locs[SHADER_LOC_MAP_NORMAL] = GetShaderLocation(mat.shader, "normals.sampler");
  111. mat.shader.locs[SHADER_LOC_MAP_ROUGHNESS] = GetShaderLocation(mat.shader, "roughness.sampler");
  112. mat.shader.locs[SHADER_LOC_MAP_OCCLUSION] = GetShaderLocation(mat.shader, "occlusion.sampler");
  113. //mat.shader.locs[SHADER_LOC_MAP_EMISSION] = GetShaderLocation(mat.shader, "emission.sampler");
  114. //mat.shader.locs[SHADER_LOC_MAP_HEIGHT] = GetShaderLocation(mat.shader, "height.sampler");
  115. mat.shader.locs[SHADER_LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap");
  116. mat.shader.locs[SHADER_LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
  117. mat.shader.locs[SHADER_LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
  118. // Set view matrix location
  119. mat.shader.locs[SHADER_LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "matModel");
  120. //mat.shader.locs[SHADER_LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "view");
  121. mat.shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
  122. // Set PBR standard maps
  123. mat.maps[MATERIAL_MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
  124. mat.maps[MATERIAL_MAP_NORMAL].texture = LoadTexture("resources/pbr/trooper_normals.png");
  125. mat.maps[MATERIAL_MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
  126. mat.maps[MATERIAL_MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
  127. mat.maps[MATERIAL_MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");
  128. // Set textures filtering for better quality
  129. SetTextureFilter(mat.maps[MATERIAL_MAP_ALBEDO].texture, FILTER_BILINEAR);
  130. SetTextureFilter(mat.maps[MATERIAL_MAP_NORMAL].texture, FILTER_BILINEAR);
  131. SetTextureFilter(mat.maps[MATERIAL_MAP_METALNESS].texture, FILTER_BILINEAR);
  132. SetTextureFilter(mat.maps[MATERIAL_MAP_ROUGHNESS].texture, FILTER_BILINEAR);
  133. SetTextureFilter(mat.maps[MATERIAL_MAP_OCCLUSION].texture, FILTER_BILINEAR);
  134. // Enable sample usage in shader for assigned textures
  135. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, SHADER_UNIFORM_INT);
  136. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, SHADER_UNIFORM_INT);
  137. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, SHADER_UNIFORM_INT);
  138. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, SHADER_UNIFORM_INT);
  139. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, SHADER_UNIFORM_INT);
  140. int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");
  141. SetShaderValue(mat.shader, renderModeLoc, (int[1]){ 0 }, SHADER_UNIFORM_INT);
  142. // Set up material properties color
  143. mat.maps[MATERIAL_MAP_ALBEDO].color = albedo;
  144. mat.maps[MATERIAL_MAP_NORMAL].color = (Color){ 128, 128, 255, 255 };
  145. mat.maps[MATERIAL_MAP_METALNESS].value = metalness;
  146. mat.maps[MATERIAL_MAP_ROUGHNESS].value = roughness;
  147. mat.maps[MATERIAL_MAP_OCCLUSION].value = 1.0f;
  148. mat.maps[MATERIAL_MAP_EMISSION].value = 0.5f;
  149. mat.maps[MATERIAL_MAP_HEIGHT].value = 0.5f;
  150. // Generate cubemap from panorama texture
  151. //--------------------------------------------------------------------------------------------------------
  152. Texture2D panorama = LoadTexture("resources/dresden_square_2k.hdr");
  153. // Load equirectangular to cubemap shader
  154. Shader shdrCubemap = LoadShader(TextFormat("resources/shaders/glsl%i/pbr.vs", GLSL_VERSION),
  155. TextFormat("resources/shaders/glsl%i/pbr.fs", GLSL_VERSION));
  156. SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, SHADER_UNIFORM_INT);
  157. TextureCubemap cubemap = GenTextureCubemap(shdrCubemap, panorama, CUBEMAP_SIZE, PIXELFORMAT_UNCOMPRESSED_R32G32B32);
  158. UnloadTexture(panorama);
  159. UnloadShader(shdrCubemap);
  160. //--------------------------------------------------------------------------------------------------------
  161. // Generate irradiance map from cubemap texture
  162. //--------------------------------------------------------------------------------------------------------
  163. // Load irradiance (GI) calculation shader
  164. Shader shdrIrradiance = LoadShader(TextFormat("resources/shaders/glsl%i/skybox.vs", GLSL_VERSION),
  165. TextFormat("resources/shaders/glsl%i/irradiance.fs", GLSL_VERSION));
  166. SetShaderValue(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, SHADER_UNIFORM_INT);
  167. mat.maps[MATERIAL_MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE);
  168. UnloadShader(shdrIrradiance);
  169. //--------------------------------------------------------------------------------------------------------
  170. // Generate prefilter map from cubemap texture
  171. //--------------------------------------------------------------------------------------------------------
  172. // Load reflection prefilter calculation shader
  173. Shader shdrPrefilter = LoadShader(TextFormat("resources/shaders/glsl%i/skybox.vs", GLSL_VERSION),
  174. TextFormat("resources/shaders/glsl%i/prefilter.fs", GLSL_VERSION));
  175. SetShaderValue(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, SHADER_UNIFORM_INT);
  176. mat.maps[MATERIAL_MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE);
  177. UnloadTexture(cubemap);
  178. UnloadShader(shdrPrefilter);
  179. //--------------------------------------------------------------------------------------------------------
  180. // Generate BRDF (bidirectional reflectance distribution function) texture (using shader)
  181. //--------------------------------------------------------------------------------------------------------
  182. Shader shdrBRDF = LoadShader(TextFormat("resources/shaders/glsl%i/brdf.vs", GLSL_VERSION),
  183. TextFormat("resources/shaders/glsl%i/brdf.fs", GLSL_VERSION));
  184. mat.maps[MATERIAL_MAP_BRDG].texture = GenTextureBRDF(shdrBRDF, BRDF_SIZE);
  185. UnloadShader(shdrBRDF);
  186. //--------------------------------------------------------------------------------------------------------
  187. return mat;
  188. }
  189. // Texture maps generation (PBR)
  190. //-------------------------------------------------------------------------------------------
  191. // Generate cubemap texture from HDR texture
  192. static TextureCubemap GenTextureCubemap(Shader shader, Texture2D panorama, int size, int format)
  193. {
  194. TextureCubemap cubemap = { 0 };
  195. rlDisableBackfaceCulling(); // Disable backface culling to render inside the cube
  196. // STEP 1: Setup framebuffer
  197. //------------------------------------------------------------------------------------------
  198. unsigned int rbo = rlLoadTextureDepth(size, size, true);
  199. cubemap.id = rlLoadTextureCubemap(NULL, size, format);
  200. unsigned int fbo = rlLoadFramebuffer(size, size);
  201. rlFramebufferAttach(fbo, rbo, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0);
  202. rlFramebufferAttach(fbo, cubemap.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X, 0);
  203. // Check if framebuffer is complete with attachments (valid)
  204. if (rlFramebufferComplete(fbo)) TraceLog(LOG_INFO, "FBO: [ID %i] Framebuffer object created successfully", fbo);
  205. //------------------------------------------------------------------------------------------
  206. // STEP 2: Draw to framebuffer
  207. //------------------------------------------------------------------------------------------
  208. // NOTE: Shader is used to convert HDR equirectangular environment map to cubemap equivalent (6 faces)
  209. rlEnableShader(shader.id);
  210. // Define projection matrix and send it to shader
  211. Matrix matFboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
  212. rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_PROJECTION], matFboProjection);
  213. // Define view matrix for every side of the cubemap
  214. Matrix fboViews[6] = {
  215. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  216. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  217. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }),
  218. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }),
  219. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  220. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f })
  221. };
  222. rlViewport(0, 0, size, size); // Set viewport to current fbo dimensions
  223. // Activate and enable texture for drawing to cubemap faces
  224. rlActiveTextureSlot(0);
  225. rlEnableTexture(panorama.id);
  226. for (int i = 0; i < 6; i++)
  227. {
  228. // Set the view matrix for the current cube face
  229. rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_VIEW], fboViews[i]);
  230. // Select the current cubemap face attachment for the fbo
  231. // WARNING: This function by default enables->attach->disables fbo!!!
  232. rlFramebufferAttach(fbo, cubemap.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X + i, 0);
  233. rlEnableFramebuffer(fbo);
  234. // Load and draw a cube, it uses the current enabled texture
  235. rlClearScreenBuffers();
  236. rlLoadDrawCube();
  237. // ALTERNATIVE: Try to use internal batch system to draw the cube instead of rlLoadDrawCube
  238. // for some reason this method does not work, maybe due to cube triangles definition? normals pointing out?
  239. // TODO: Investigate this issue...
  240. //rlSetTexture(panorama.id); // WARNING: It must be called after enabling current framebuffer if using internal batch system!
  241. //rlClearScreenBuffers();
  242. //DrawCubeV(Vector3Zero(), Vector3One(), WHITE);
  243. //rlDrawRenderBatchActive();
  244. }
  245. //------------------------------------------------------------------------------------------
  246. // STEP 3: Unload framebuffer and reset state
  247. //------------------------------------------------------------------------------------------
  248. rlDisableShader(); // Unbind shader
  249. rlDisableTexture(); // Unbind texture
  250. rlDisableFramebuffer(); // Unbind framebuffer
  251. rlUnloadFramebuffer(fbo); // Unload framebuffer (and automatically attached depth texture/renderbuffer)
  252. // Reset viewport dimensions to default
  253. rlViewport(0, 0, rlGetFramebufferWidth(), rlGetFramebufferHeight());
  254. rlEnableBackfaceCulling();
  255. //------------------------------------------------------------------------------------------
  256. cubemap.width = size;
  257. cubemap.height = size;
  258. cubemap.mipmaps = 1;
  259. cubemap.format = PIXELFORMAT_UNCOMPRESSED_R32G32B32;
  260. return cubemap;
  261. }
  262. // Generate irradiance texture using cubemap data
  263. static TextureCubemap GenTextureIrradiance(Shader shader, TextureCubemap cubemap, int size)
  264. {
  265. TextureCubemap irradiance = { 0 };
  266. rlDisableBackfaceCulling(); // Disable backface culling to render inside the cube
  267. // STEP 1: Setup framebuffer
  268. //------------------------------------------------------------------------------------------
  269. unsigned int rbo = rlLoadTextureDepth(size, size, true);
  270. irradiance.id = rlLoadTextureCubemap(NULL, size, PIXELFORMAT_UNCOMPRESSED_R32G32B32);
  271. unsigned int fbo = rlLoadFramebuffer(size, size);
  272. rlFramebufferAttach(fbo, rbo, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0);
  273. rlFramebufferAttach(fbo, cubemap.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X, 0);
  274. //------------------------------------------------------------------------------------------
  275. // STEP 2: Draw to framebuffer
  276. //------------------------------------------------------------------------------------------
  277. // NOTE: Shader is used to solve diffuse integral by convolution to create an irradiance cubemap
  278. rlEnableShader(shader.id);
  279. // Define projection matrix and send it to shader
  280. Matrix matFboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
  281. rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_PROJECTION], matFboProjection);
  282. // Define view matrix for every side of the cubemap
  283. Matrix fboViews[6] = {
  284. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  285. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  286. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }),
  287. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }),
  288. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  289. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f })
  290. };
  291. rlActiveTextureSlot(0);
  292. rlEnableTextureCubemap(cubemap.id);
  293. rlViewport(0, 0, size, size); // Set viewport to current fbo dimensions
  294. for (int i = 0; i < 6; i++)
  295. {
  296. rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_VIEW], fboViews[i]);
  297. rlFramebufferAttach(fbo, irradiance.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X + i, 0);
  298. rlEnableFramebuffer(fbo);
  299. rlClearScreenBuffers();
  300. rlLoadDrawCube();
  301. }
  302. //------------------------------------------------------------------------------------------
  303. // STEP 3: Unload framebuffer and reset state
  304. //------------------------------------------------------------------------------------------
  305. rlDisableShader(); // Unbind shader
  306. rlDisableTexture(); // Unbind texture
  307. rlDisableFramebuffer(); // Unbind framebuffer
  308. rlUnloadFramebuffer(fbo); // Unload framebuffer (and automatically attached depth texture/renderbuffer)
  309. // Reset viewport dimensions to default
  310. rlViewport(0, 0, rlGetFramebufferWidth(), rlGetFramebufferHeight());
  311. rlEnableBackfaceCulling();
  312. //------------------------------------------------------------------------------------------
  313. irradiance.width = size;
  314. irradiance.height = size;
  315. irradiance.mipmaps = 1;
  316. irradiance.format = PIXELFORMAT_UNCOMPRESSED_R32G32B32;
  317. return irradiance;
  318. }
  319. // Generate prefilter texture using cubemap data
  320. static TextureCubemap GenTexturePrefilter(Shader shader, TextureCubemap cubemap, int size)
  321. {
  322. TextureCubemap prefilter = { 0 };
  323. rlDisableBackfaceCulling(); // Disable backface culling to render inside the cube
  324. // STEP 1: Setup framebuffer
  325. //------------------------------------------------------------------------------------------
  326. unsigned int rbo = rlLoadTextureDepth(size, size, true);
  327. prefilter.id = rlLoadTextureCubemap(NULL, size, PIXELFORMAT_UNCOMPRESSED_R32G32B32);
  328. rlTextureParameters(prefilter.id, RL_TEXTURE_MIN_FILTER, RL_TEXTURE_FILTER_MIP_LINEAR);
  329. unsigned int fbo = rlLoadFramebuffer(size, size);
  330. rlFramebufferAttach(fbo, rbo, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0);
  331. rlFramebufferAttach(fbo, cubemap.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X, 0);
  332. //------------------------------------------------------------------------------------------
  333. // Generate mipmaps for the prefiltered HDR texture
  334. //glGenerateMipmap(GL_TEXTURE_CUBE_MAP); // TODO!
  335. // STEP 2: Draw to framebuffer
  336. //------------------------------------------------------------------------------------------
  337. // NOTE: Shader is used to prefilter HDR and store data into mipmap levels
  338. // Define projection matrix and send it to shader
  339. Matrix fboProjection = MatrixPerspective(90.0*DEG2RAD, 1.0, RL_CULL_DISTANCE_NEAR, RL_CULL_DISTANCE_FAR);
  340. rlEnableShader(shader.id);
  341. rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_PROJECTION], fboProjection);
  342. // Define view matrix for every side of the cubemap
  343. Matrix fboViews[6] = {
  344. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  345. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ -1.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  346. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }),
  347. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }),
  348. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, 1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f }),
  349. MatrixLookAt((Vector3){ 0.0f, 0.0f, 0.0f }, (Vector3){ 0.0f, 0.0f, -1.0f }, (Vector3){ 0.0f, -1.0f, 0.0f })
  350. };
  351. rlActiveTextureSlot(0);
  352. rlEnableTextureCubemap(cubemap.id);
  353. // TODO: Locations should be taken out of this function... too shader dependant...
  354. int roughnessLoc = rlGetLocationUniform(shader.id, "roughness");
  355. rlEnableFramebuffer(fbo);
  356. #define MAX_MIPMAP_LEVELS 5 // Max number of prefilter texture mipmaps
  357. for (int mip = 0; mip < MAX_MIPMAP_LEVELS; mip++)
  358. {
  359. // Resize framebuffer according to mip-level size.
  360. unsigned int mipWidth = size*(int)powf(0.5f, (float)mip);
  361. unsigned int mipHeight = size*(int)powf(0.5f, (float)mip);
  362. rlViewport(0, 0, mipWidth, mipHeight);
  363. //glBindRenderbuffer(GL_RENDERBUFFER, rbo);
  364. //glRenderbufferStorage(GL_RENDERBUFFER, GL_DEPTH_COMPONENT24, mipWidth, mipHeight);
  365. float roughness = (float)mip/(float)(MAX_MIPMAP_LEVELS - 1);
  366. rlSetUniform(roughnessLoc, &roughness, SHADER_UNIFORM_FLOAT, 1);
  367. for (int i = 0; i < 6; i++)
  368. {
  369. rlSetUniformMatrix(shader.locs[SHADER_LOC_MATRIX_VIEW], fboViews[i]);
  370. rlFramebufferAttach(fbo, prefilter.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_CUBEMAP_POSITIVE_X + i, mip);
  371. rlClearScreenBuffers();
  372. rlLoadDrawCube();
  373. }
  374. }
  375. //------------------------------------------------------------------------------------------
  376. // STEP 3: Unload framebuffer and reset state
  377. //------------------------------------------------------------------------------------------
  378. rlDisableShader(); // Unbind shader
  379. rlDisableTexture(); // Unbind texture
  380. rlDisableFramebuffer(); // Unbind framebuffer
  381. rlUnloadFramebuffer(fbo); // Unload framebuffer (and automatically attached depth texture/renderbuffer)
  382. // Reset viewport dimensions to default
  383. rlViewport(0, 0, rlGetFramebufferWidth(), rlGetFramebufferHeight());
  384. rlEnableBackfaceCulling();
  385. //------------------------------------------------------------------------------------------
  386. prefilter.width = size;
  387. prefilter.height = size;
  388. prefilter.mipmaps = MAX_MIPMAP_LEVELS;
  389. prefilter.format = PIXELFORMAT_UNCOMPRESSED_R32G32B32;
  390. return prefilter;
  391. }
  392. // Generate BRDF texture using cubemap data
  393. // TODO: Review implementation: https://github.com/HectorMF/BRDFGenerator
  394. static Texture2D GenTextureBRDF(Shader shader, int size)
  395. {
  396. Texture2D brdf = { 0 };
  397. // STEP 1: Setup framebuffer
  398. //------------------------------------------------------------------------------------------
  399. unsigned int rbo = rlLoadTextureDepth(size, size, true);
  400. brdf.id = rlLoadTexture(NULL, size, size, PIXELFORMAT_UNCOMPRESSED_R32G32B32, 1);
  401. unsigned int fbo = rlLoadFramebuffer(size, size);
  402. rlFramebufferAttach(fbo, rbo, RL_ATTACHMENT_DEPTH, RL_ATTACHMENT_RENDERBUFFER, 0);
  403. rlFramebufferAttach(fbo, brdf.id, RL_ATTACHMENT_COLOR_CHANNEL0, RL_ATTACHMENT_TEXTURE2D, 0);
  404. //------------------------------------------------------------------------------------------
  405. // STEP 2: Draw to framebuffer
  406. //------------------------------------------------------------------------------------------
  407. // NOTE: Render BRDF LUT into a quad using FBO
  408. rlEnableShader(shader.id);
  409. rlViewport(0, 0, size, size);
  410. rlEnableFramebuffer(fbo);
  411. rlClearScreenBuffers();
  412. rlLoadDrawQuad();
  413. //------------------------------------------------------------------------------------------
  414. // STEP 3: Unload framebuffer and reset state
  415. //------------------------------------------------------------------------------------------
  416. rlDisableShader(); // Unbind shader
  417. rlDisableTexture(); // Unbind texture
  418. rlDisableFramebuffer(); // Unbind framebuffer
  419. rlUnloadFramebuffer(fbo); // Unload framebuffer (and automatically attached depth texture/renderbuffer)
  420. // Reset viewport dimensions to default
  421. rlViewport(0, 0, rlGetFramebufferWidth(), rlGetFramebufferHeight());
  422. //------------------------------------------------------------------------------------------
  423. brdf.width = size;
  424. brdf.height = size;
  425. brdf.mipmaps = 1;
  426. brdf.format = PIXELFORMAT_UNCOMPRESSED_R32G32B32;
  427. return brdf;
  428. }