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