Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

232 lignes
12 KiB

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