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.

237 lines
12 KiB

7 years ago
6 years ago
  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 512 // 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. UnloadMaterial(model.materials[0]); // get rid of default material
  46. model.materials[0] = LoadMaterialPBR((Color){ 255, 255, 255, 255 }, 1.0f, 1.0f);
  47. // Create lights
  48. // NOTE: Lights are added to an internal lights pool automatically
  49. 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);
  50. 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);
  51. 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);
  52. 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);
  53. SetCameraMode(camera, CAMERA_ORBITAL); // Set an orbital camera mode
  54. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  55. //--------------------------------------------------------------------------------------
  56. // Main game loop
  57. while (!WindowShouldClose()) // Detect window close button or ESC key
  58. {
  59. // Update
  60. //----------------------------------------------------------------------------------
  61. UpdateCamera(&camera); // Update camera
  62. // Send to material PBR shader camera view position
  63. float cameraPos[3] = { camera.position.x, camera.position.y, camera.position.z };
  64. SetShaderValue(model.materials[0].shader, model.materials[0].shader.locs[LOC_VECTOR_VIEW], cameraPos, UNIFORM_VEC3);
  65. //----------------------------------------------------------------------------------
  66. // Draw
  67. //----------------------------------------------------------------------------------
  68. BeginDrawing();
  69. ClearBackground(RAYWHITE);
  70. BeginMode3D(camera);
  71. DrawModel(model, Vector3Zero(), 1.0f, WHITE);
  72. DrawGrid(10, 1.0f);
  73. EndMode3D();
  74. DrawFPS(10, 10);
  75. EndDrawing();
  76. //----------------------------------------------------------------------------------
  77. }
  78. // De-Initialization
  79. //--------------------------------------------------------------------------------------
  80. // Shaders and textures must be unloaded by user,
  81. // they could be in use by other models
  82. UnloadTexture(model.materials[0].maps[MAP_ALBEDO].texture);
  83. UnloadTexture(model.materials[0].maps[MAP_NORMAL].texture);
  84. UnloadTexture(model.materials[0].maps[MAP_METALNESS].texture);
  85. UnloadTexture(model.materials[0].maps[MAP_ROUGHNESS].texture);
  86. UnloadTexture(model.materials[0].maps[MAP_OCCLUSION].texture);
  87. UnloadTexture(model.materials[0].maps[MAP_IRRADIANCE].texture);
  88. UnloadTexture(model.materials[0].maps[MAP_PREFILTER].texture);
  89. UnloadTexture(model.materials[0].maps[MAP_BRDF].texture);
  90. UnloadShader(model.materials[0].shader);
  91. UnloadModel(model); // Unload model
  92. CloseWindow(); // Close window and OpenGL context
  93. //--------------------------------------------------------------------------------------
  94. return 0;
  95. }
  96. // Load PBR material (Supports: ALBEDO, NORMAL, METALNESS, ROUGHNESS, AO, EMMISIVE, HEIGHT maps)
  97. // NOTE: PBR shader is loaded inside this function
  98. static Material LoadMaterialPBR(Color albedo, float metalness, float roughness)
  99. {
  100. Material mat = LoadMaterialDefault(); // Initialize material to default
  101. #if defined(PLATFORM_DESKTOP)
  102. mat.shader = LoadShader("resources/shaders/glsl330/pbr.vs", "resources/shaders/glsl330/pbr.fs");
  103. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  104. mat.shader = LoadShader("resources/shaders/glsl100/pbr.vs", "resources/shaders/glsl100/pbr.fs");
  105. #endif
  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[LOC_MAP_ALBEDO] = GetShaderLocation(mat.shader, "albedo.sampler");
  109. mat.shader.locs[LOC_MAP_METALNESS] = GetShaderLocation(mat.shader, "metalness.sampler");
  110. mat.shader.locs[LOC_MAP_NORMAL] = GetShaderLocation(mat.shader, "normals.sampler");
  111. mat.shader.locs[LOC_MAP_ROUGHNESS] = GetShaderLocation(mat.shader, "roughness.sampler");
  112. mat.shader.locs[LOC_MAP_OCCLUSION] = GetShaderLocation(mat.shader, "occlusion.sampler");
  113. //mat.shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(mat.shader, "emission.sampler");
  114. //mat.shader.locs[LOC_MAP_HEIGHT] = GetShaderLocation(mat.shader, "height.sampler");
  115. mat.shader.locs[LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap");
  116. mat.shader.locs[LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
  117. mat.shader.locs[LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
  118. // Set view matrix location
  119. mat.shader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "matModel");
  120. //mat.shader.locs[LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "view");
  121. mat.shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
  122. // Set PBR standard maps
  123. mat.maps[MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
  124. mat.maps[MAP_NORMAL].texture = LoadTexture("resources/pbr/trooper_normals.png");
  125. mat.maps[MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
  126. mat.maps[MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
  127. mat.maps[MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");
  128. // Load equirectangular to cubemap shader
  129. #if defined(PLATFORM_DESKTOP)
  130. Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs");
  131. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  132. Shader shdrCubemap = LoadShader("resources/shaders/glsl100/cubemap.vs", "resources/shaders/glsl100/cubemap.fs");
  133. #endif
  134. // Load irradiance (GI) calculation shader
  135. #if defined(PLATFORM_DESKTOP)
  136. Shader shdrIrradiance = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/irradiance.fs");
  137. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  138. Shader shdrIrradiance = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/irradiance.fs");
  139. #endif
  140. // Load reflection prefilter calculation shader
  141. #if defined(PLATFORM_DESKTOP)
  142. Shader shdrPrefilter = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/prefilter.fs");
  143. #else
  144. Shader shdrPrefilter = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/prefilter.fs");
  145. #endif
  146. // Load bidirectional reflectance distribution function shader
  147. #if defined(PLATFORM_DESKTOP)
  148. Shader shdrBRDF = LoadShader("resources/shaders/glsl330/brdf.vs", "resources/shaders/glsl330/brdf.fs");
  149. #else
  150. Shader shdrBRDF = LoadShader("resources/shaders/glsl100/brdf.vs", "resources/shaders/glsl100/brdf.fs");
  151. #endif
  152. // Setup required shader locations
  153. SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT);
  154. SetShaderValue(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT);
  155. SetShaderValue(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT);
  156. Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
  157. Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE);
  158. mat.maps[MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE);
  159. mat.maps[MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE);
  160. mat.maps[MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, BRDF_SIZE);
  161. UnloadTexture(cubemap);
  162. UnloadTexture(texHDR);
  163. // Unload already used shaders (to create specific textures)
  164. UnloadShader(shdrCubemap);
  165. UnloadShader(shdrIrradiance);
  166. UnloadShader(shdrPrefilter);
  167. UnloadShader(shdrBRDF);
  168. // Set textures filtering for better quality
  169. SetTextureFilter(mat.maps[MAP_ALBEDO].texture, FILTER_BILINEAR);
  170. SetTextureFilter(mat.maps[MAP_NORMAL].texture, FILTER_BILINEAR);
  171. SetTextureFilter(mat.maps[MAP_METALNESS].texture, FILTER_BILINEAR);
  172. SetTextureFilter(mat.maps[MAP_ROUGHNESS].texture, FILTER_BILINEAR);
  173. SetTextureFilter(mat.maps[MAP_OCCLUSION].texture, FILTER_BILINEAR);
  174. // Enable sample usage in shader for assigned textures
  175. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  176. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  177. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  178. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  179. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  180. int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");
  181. SetShaderValue(mat.shader, renderModeLoc, (int[1]){ 0 }, UNIFORM_INT);
  182. // Set up material properties color
  183. mat.maps[MAP_ALBEDO].color = albedo;
  184. mat.maps[MAP_NORMAL].color = (Color){ 128, 128, 255, 255 };
  185. mat.maps[MAP_METALNESS].value = metalness;
  186. mat.maps[MAP_ROUGHNESS].value = roughness;
  187. mat.maps[MAP_OCCLUSION].value = 1.0f;
  188. mat.maps[MAP_EMISSION].value = 0.5f;
  189. mat.maps[MAP_HEIGHT].value = 0.5f;
  190. return mat;
  191. }