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.

194 lines
10 KiB

7 years ago
6 years ago
  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. #define RLIGHTS_IMPLEMENTATION
  14. #include "rlights.h"
  15. #define CUBEMAP_SIZE 512 // Cubemap texture size
  16. #define IRRADIANCE_SIZE 32 // Irradiance texture size
  17. #define PREFILTERED_SIZE 256 // Prefiltered HDR environment texture size
  18. #define BRDF_SIZE 512 // BRDF LUT texture size
  19. // PBR material loading
  20. static Material LoadMaterialPBR(Color albedo, float metalness, float roughness);
  21. int main()
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. int screenWidth = 800;
  26. int screenHeight = 450;
  27. SetConfigFlags(FLAG_MSAA_4X_HINT); // Enable Multi Sampling Anti Aliasing 4x (if available)
  28. InitWindow(screenWidth, screenHeight, "raylib [models] example - pbr material");
  29. // Define the camera to look into our 3d world
  30. Camera camera = {{ 4.0f, 4.0f, 4.0f }, { 0.0f, 0.5f, 0.0f }, { 0.0f, 1.0f, 0.0f }, 45.0f, 0 };
  31. // Load model and PBR material
  32. Model model = LoadModel("resources/pbr/trooper.obj");
  33. model.material = LoadMaterialPBR((Color){ 255, 255, 255, 255 }, 1.0f, 1.0f);
  34. // Define lights attributes
  35. // NOTE: Shader is passed to every light on creation to define shader bindings internally
  36. Light lights[MAX_LIGHTS] = {
  37. CreateLight(LIGHT_POINT, (Vector3){ LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 255, 0, 0, 255 }, model.material.shader),
  38. CreateLight(LIGHT_POINT, (Vector3){ 0.0f, LIGHT_HEIGHT, LIGHT_DISTANCE }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 0, 255, 0, 255 }, model.material.shader),
  39. CreateLight(LIGHT_POINT, (Vector3){ -LIGHT_DISTANCE, LIGHT_HEIGHT, 0.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, (Color){ 0, 0, 255, 255 }, model.material.shader),
  40. 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.material.shader)
  41. };
  42. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  43. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  44. //--------------------------------------------------------------------------------------
  45. // Main game loop
  46. while (!WindowShouldClose()) // Detect window close button or ESC key
  47. {
  48. // Update
  49. //----------------------------------------------------------------------------------
  50. UpdateCamera(&camera); // Update camera
  51. // Send to material PBR shader camera view position
  52. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  53. SetShaderValue(model.material.shader, model.material.shader.locs[LOC_VECTOR_VIEW], cameraPos, 3);
  54. //----------------------------------------------------------------------------------
  55. // Draw
  56. //----------------------------------------------------------------------------------
  57. BeginDrawing();
  58. ClearBackground(RAYWHITE);
  59. Begin3dMode(camera);
  60. DrawModel(model, Vector3Zero(), 1.0f, WHITE);
  61. DrawGrid(10, 1.0f);
  62. End3dMode();
  63. DrawFPS(10, 10);
  64. EndDrawing();
  65. //----------------------------------------------------------------------------------
  66. }
  67. // De-Initialization
  68. //--------------------------------------------------------------------------------------
  69. UnloadModel(model); // Unload skybox model
  70. CloseWindow(); // Close window and OpenGL context
  71. //--------------------------------------------------------------------------------------
  72. return 0;
  73. }
  74. // Load PBR material (Supports: ALBEDO, NORMAL, METALNESS, ROUGHNESS, AO, EMMISIVE, HEIGHT maps)
  75. // NOTE: PBR shader is loaded inside this function
  76. static Material LoadMaterialPBR(Color albedo, float metalness, float roughness)
  77. {
  78. Material mat = { 0 }; // NOTE: All maps textures are set to { 0 }
  79. #define PATH_PBR_VS "resources/shaders/pbr.vs" // Path to physically based rendering vertex shader
  80. #define PATH_PBR_FS "resources/shaders/pbr.fs" // Path to physically based rendering fragment shader
  81. mat.shader = LoadShader(PATH_PBR_VS, PATH_PBR_FS);
  82. // Get required locations points for PBR material
  83. // NOTE: Those location names must be available and used in the shader code
  84. mat.shader.locs[LOC_MAP_ALBEDO] = GetShaderLocation(mat.shader, "albedo.sampler");
  85. mat.shader.locs[LOC_MAP_METALNESS] = GetShaderLocation(mat.shader, "metalness.sampler");
  86. mat.shader.locs[LOC_MAP_NORMAL] = GetShaderLocation(mat.shader, "normals.sampler");
  87. mat.shader.locs[LOC_MAP_ROUGHNESS] = GetShaderLocation(mat.shader, "roughness.sampler");
  88. mat.shader.locs[LOC_MAP_OCCLUSION] = GetShaderLocation(mat.shader, "occlusion.sampler");
  89. //mat.shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(mat.shader, "emission.sampler");
  90. //mat.shader.locs[LOC_MAP_HEIGHT] = GetShaderLocation(mat.shader, "height.sampler");
  91. mat.shader.locs[LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap");
  92. mat.shader.locs[LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
  93. mat.shader.locs[LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
  94. // Set view matrix location
  95. mat.shader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "mMatrix");
  96. mat.shader.locs[LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "view");
  97. mat.shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
  98. // Set PBR standard maps
  99. mat.maps[MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
  100. mat.maps[MAP_NORMAL].texture = LoadTexture("resources/pbr/trooper_normals.png");
  101. mat.maps[MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
  102. mat.maps[MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
  103. mat.maps[MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");
  104. // Set environment maps
  105. #define PATH_CUBEMAP_VS "resources/shaders/cubemap.vs" // Path to equirectangular to cubemap vertex shader
  106. #define PATH_CUBEMAP_FS "resources/shaders/cubemap.fs" // Path to equirectangular to cubemap fragment shader
  107. #define PATH_SKYBOX_VS "resources/shaders/skybox.vs" // Path to skybox vertex shader
  108. #define PATH_IRRADIANCE_FS "resources/shaders/irradiance.fs" // Path to irradiance (GI) calculation fragment shader
  109. #define PATH_PREFILTER_FS "resources/shaders/prefilter.fs" // Path to reflection prefilter calculation fragment shader
  110. #define PATH_BRDF_VS "resources/shaders/brdf.vs" // Path to bidirectional reflectance distribution function vertex shader
  111. #define PATH_BRDF_FS "resources/shaders/brdf.fs" // Path to bidirectional reflectance distribution function fragment shader
  112. Shader shdrCubemap = LoadShader(PATH_CUBEMAP_VS, PATH_CUBEMAP_FS);
  113. Shader shdrIrradiance = LoadShader(PATH_SKYBOX_VS, PATH_IRRADIANCE_FS);
  114. Shader shdrPrefilter = LoadShader(PATH_SKYBOX_VS, PATH_PREFILTER_FS);
  115. Shader shdrBRDF = LoadShader(PATH_BRDF_VS, PATH_BRDF_FS);
  116. // Setup required shader locations
  117. SetShaderValuei(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, 1);
  118. SetShaderValuei(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, 1);
  119. SetShaderValuei(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, 1);
  120. Texture2D texHDR = LoadTexture("resources/pinetree.hdr");
  121. Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE);
  122. mat.maps[MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE);
  123. mat.maps[MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE);
  124. mat.maps[MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, cubemap, BRDF_SIZE);
  125. UnloadTexture(cubemap);
  126. UnloadTexture(texHDR);
  127. // Unload already used shaders (to create specific textures)
  128. UnloadShader(shdrCubemap);
  129. UnloadShader(shdrIrradiance);
  130. UnloadShader(shdrPrefilter);
  131. UnloadShader(shdrBRDF);
  132. // Set textures filtering for better quality
  133. SetTextureFilter(mat.maps[MAP_ALBEDO].texture, FILTER_BILINEAR);
  134. SetTextureFilter(mat.maps[MAP_NORMAL].texture, FILTER_BILINEAR);
  135. SetTextureFilter(mat.maps[MAP_METALNESS].texture, FILTER_BILINEAR);
  136. SetTextureFilter(mat.maps[MAP_ROUGHNESS].texture, FILTER_BILINEAR);
  137. SetTextureFilter(mat.maps[MAP_OCCLUSION].texture, FILTER_BILINEAR);
  138. // Enable sample usage in shader for assigned textures
  139. SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, 1);
  140. SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, 1);
  141. SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, 1);
  142. SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, 1);
  143. SetShaderValuei(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, 1);
  144. int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");
  145. SetShaderValuei(mat.shader, renderModeLoc, (int[1]){ 0 }, 1);
  146. // Set up material properties color
  147. mat.maps[MAP_ALBEDO].color = albedo;
  148. mat.maps[MAP_NORMAL].color = (Color){ 128, 128, 255, 255 };
  149. mat.maps[MAP_METALNESS].value = metalness;
  150. mat.maps[MAP_ROUGHNESS].value = roughness;
  151. mat.maps[MAP_OCCLUSION].value = 1.0f;
  152. mat.maps[MAP_EMISSION].value = 0.5f;
  153. mat.maps[MAP_HEIGHT].value = 0.5f;
  154. return mat;
  155. }