models_material_pbr.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. /*******************************************************************************************
  2. *
  3. * raylib [models] example - PBR material
  4. *
  5. * This example has been created using raylib 1.8 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Copyright (c) 2017 Ramon Santamaria (@raysan5)
  9. *
  10. ********************************************************************************************/
  11. #include "raylib.h"
  12. #include "raymath.h"
  13. #include <stdio.h>
  14. #define RLIGHTS_IMPLEMENTATION
  15. #include "rlights.h"
  16. #define CUBEMAP_SIZE 512 // Cubemap texture size
  17. #define IRRADIANCE_SIZE 32 // Irradiance texture size
  18. #define PREFILTERED_SIZE 256 // Prefiltered HDR environment texture size
  19. #define BRDF_SIZE 512 // BRDF LUT texture size
  20. // PBR material loading
  21. static Material LoadMaterialPBR(Color albedo, float metalness, float roughness);
  22. int main(void)
  23. {
  24. // Initialization
  25. //--------------------------------------------------------------------------------------
  26. const int screenWidth = 800;
  27. const int screenHeight = 450;
  28. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  29. InitWindow(screenWidth, screenHeight, "raylib [models] example - pbr material");
  30. // Define the camera to look into our 3d world
  31. Camera camera = { 0 };
  32. camera.position = (Vector3){ 4.0f, 4.0f, 4.0f }; // Camera position
  33. camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
  34. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  35. camera.fovy = 45.0f; // Camera field-of-view Y
  36. camera.type = CAMERA_PERSPECTIVE; // Camera mode type
  37. // Load model and PBR material
  38. Model model = LoadModel("resources/pbr/trooper.obj");
  39. // Mesh tangents are generated... and uploaded to GPU
  40. // NOTE: New VBO for tangents is generated at default location and also binded to mesh VAO
  41. MeshTangents(&model.meshes[0]);
  42. UnloadMaterial(model.materials[0]); // get rid of default material
  43. model.materials[0] = LoadMaterialPBR((Color){ 255, 255, 255, 255 }, 1.0f, 1.0f);
  44. // Create lights
  45. // NOTE: Lights are added to an internal lights pool automatically
  46. 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);
  47. 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);
  48. 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);
  49. 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);
  50. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  51. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  52. //--------------------------------------------------------------------------------------
  53. // Main game loop
  54. while (!WindowShouldClose()) // Detect window close button or ESC key
  55. {
  56. // Update
  57. //----------------------------------------------------------------------------------
  58. UpdateCamera(&camera); // Update camera
  59. // Send to material PBR shader camera view position
  60. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  61. SetShaderValue(model.materials[0].shader, model.materials[0].shader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3);
  62. //----------------------------------------------------------------------------------
  63. // Draw
  64. //----------------------------------------------------------------------------------
  65. BeginDrawing();
  66. ClearBackground(RAYWHITE);
  67. BeginMode3D(camera);
  68. DrawModel(model, Vector3Zero(), 1.0f, WHITE);
  69. DrawGrid(10, 1.0f);
  70. EndMode3D();
  71. DrawFPS(10, 10);
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. // Shaders and textures must be unloaded by user,
  78. // they could be in use by other models
  79. UnloadTexture(model.materials[0].maps[MAP_ALBEDO].texture);
  80. UnloadTexture(model.materials[0].maps[MAP_NORMAL].texture);
  81. UnloadTexture(model.materials[0].maps[MAP_METALNESS].texture);
  82. UnloadTexture(model.materials[0].maps[MAP_ROUGHNESS].texture);
  83. UnloadTexture(model.materials[0].maps[MAP_OCCLUSION].texture);
  84. UnloadTexture(model.materials[0].maps[MAP_IRRADIANCE].texture);
  85. UnloadTexture(model.materials[0].maps[MAP_PREFILTER].texture);
  86. UnloadTexture(model.materials[0].maps[MAP_BRDF].texture);
  87. UnloadShader(model.materials[0].shader);
  88. UnloadModel(model); // Unload model
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. return 0;
  92. }
  93. // Load PBR material (Supports: ALBEDO, NORMAL, METALNESS, ROUGHNESS, AO, EMMISIVE, HEIGHT maps)
  94. // NOTE: PBR shader is loaded inside this function
  95. static Material LoadMaterialPBR(Color albedo, float metalness, float roughness)
  96. {
  97. Material mat = LoadMaterialDefault(); // Initialize material to default
  98. #if defined(PLATFORM_DESKTOP)
  99. mat.shader = LoadShader("resources/shaders/glsl330/pbr.vs", "resources/shaders/glsl330/pbr.fs");
  100. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  101. mat.shader = LoadShader("resources/shaders/glsl100/pbr.vs", "resources/shaders/glsl100/pbr.fs");
  102. #endif
  103. // Get required locations points for PBR material
  104. // NOTE: Those location names must be available and used in the shader code
  105. mat.shader.locs[LOC_MAP_ALBEDO] = GetShaderLocation(mat.shader, "albedo.sampler");
  106. mat.shader.locs[LOC_MAP_METALNESS] = GetShaderLocation(mat.shader, "metalness.sampler");
  107. mat.shader.locs[LOC_MAP_NORMAL] = GetShaderLocation(mat.shader, "normals.sampler");
  108. mat.shader.locs[LOC_MAP_ROUGHNESS] = GetShaderLocation(mat.shader, "roughness.sampler");
  109. mat.shader.locs[LOC_MAP_OCCLUSION] = GetShaderLocation(mat.shader, "occlusion.sampler");
  110. //mat.shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(mat.shader, "emission.sampler");
  111. //mat.shader.locs[LOC_MAP_HEIGHT] = GetShaderLocation(mat.shader, "height.sampler");
  112. mat.shader.locs[LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap");
  113. mat.shader.locs[LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
  114. mat.shader.locs[LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
  115. // Set view matrix location
  116. mat.shader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "matModel");
  117. //mat.shader.locs[LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "view");
  118. mat.shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
  119. // Set PBR standard maps
  120. mat.maps[MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
  121. mat.maps[MAP_NORMAL].texture = LoadTexture("resources/pbr/trooper_normals.png");
  122. mat.maps[MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
  123. mat.maps[MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
  124. mat.maps[MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");
  125. // Load equirectangular to cubemap shader
  126. #if defined(PLATFORM_DESKTOP)
  127. Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs");
  128. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  129. Shader shdrCubemap = LoadShader("resources/shaders/glsl100/cubemap.vs", "resources/shaders/glsl100/cubemap.fs");
  130. #endif
  131. // Load irradiance (GI) calculation shader
  132. #if defined(PLATFORM_DESKTOP)
  133. Shader shdrIrradiance = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/irradiance.fs");
  134. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  135. Shader shdrIrradiance = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/irradiance.fs");
  136. #endif
  137. // Load reflection prefilter calculation shader
  138. #if defined(PLATFORM_DESKTOP)
  139. Shader shdrPrefilter = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/prefilter.fs");
  140. #else
  141. Shader shdrPrefilter = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/prefilter.fs");
  142. #endif
  143. // Load bidirectional reflectance distribution function shader
  144. #if defined(PLATFORM_DESKTOP)
  145. Shader shdrBRDF = LoadShader("resources/shaders/glsl330/brdf.vs", "resources/shaders/glsl330/brdf.fs");
  146. #else
  147. Shader shdrBRDF = LoadShader("resources/shaders/glsl100/brdf.vs", "resources/shaders/glsl100/brdf.fs");
  148. #endif
  149. // Setup required shader locations
  150. SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT);
  151. SetShaderValue(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT);
  152. SetShaderValue(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT);
  153. Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
  154. Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE);
  155. mat.maps[MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE);
  156. mat.maps[MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE);
  157. mat.maps[MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, BRDF_SIZE);
  158. UnloadTexture(cubemap);
  159. UnloadTexture(texHDR);
  160. // Unload already used shaders (to create specific textures)
  161. UnloadShader(shdrCubemap);
  162. UnloadShader(shdrIrradiance);
  163. UnloadShader(shdrPrefilter);
  164. UnloadShader(shdrBRDF);
  165. // Set textures filtering for better quality
  166. SetTextureFilter(mat.maps[MAP_ALBEDO].texture, FILTER_BILINEAR);
  167. SetTextureFilter(mat.maps[MAP_NORMAL].texture, FILTER_BILINEAR);
  168. SetTextureFilter(mat.maps[MAP_METALNESS].texture, FILTER_BILINEAR);
  169. SetTextureFilter(mat.maps[MAP_ROUGHNESS].texture, FILTER_BILINEAR);
  170. SetTextureFilter(mat.maps[MAP_OCCLUSION].texture, FILTER_BILINEAR);
  171. // Enable sample usage in shader for assigned textures
  172. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  173. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  174. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  175. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  176. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  177. int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");
  178. SetShaderValue(mat.shader, renderModeLoc, (int[1]){ 0 }, UNIFORM_INT);
  179. // Set up material properties color
  180. mat.maps[MAP_ALBEDO].color = albedo;
  181. mat.maps[MAP_NORMAL].color = (Color){ 128, 128, 255, 255 };
  182. mat.maps[MAP_METALNESS].value = metalness;
  183. mat.maps[MAP_ROUGHNESS].value = roughness;
  184. mat.maps[MAP_OCCLUSION].value = 1.0f;
  185. mat.maps[MAP_EMISSION].value = 0.5f;
  186. mat.maps[MAP_HEIGHT].value = 0.5f;
  187. return mat;
  188. }