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.

225 lines
11 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. 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. #if defined(PLATFORM_DESKTOP)
  91. mat.shader = LoadShader("resources/shaders/glsl330/pbr.vs", "resources/shaders/glsl330/pbr.fs");
  92. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  93. mat.shader = LoadShader("resources/shaders/glsl100/pbr.vs", "resources/shaders/glsl100/pbr.fs");
  94. #endif
  95. // Get required locations points for PBR material
  96. // NOTE: Those location names must be available and used in the shader code
  97. mat.shader.locs[LOC_MAP_ALBEDO] = GetShaderLocation(mat.shader, "albedo.sampler");
  98. mat.shader.locs[LOC_MAP_METALNESS] = GetShaderLocation(mat.shader, "metalness.sampler");
  99. mat.shader.locs[LOC_MAP_NORMAL] = GetShaderLocation(mat.shader, "normals.sampler");
  100. mat.shader.locs[LOC_MAP_ROUGHNESS] = GetShaderLocation(mat.shader, "roughness.sampler");
  101. mat.shader.locs[LOC_MAP_OCCLUSION] = GetShaderLocation(mat.shader, "occlusion.sampler");
  102. //mat.shader.locs[LOC_MAP_EMISSION] = GetShaderLocation(mat.shader, "emission.sampler");
  103. //mat.shader.locs[LOC_MAP_HEIGHT] = GetShaderLocation(mat.shader, "height.sampler");
  104. mat.shader.locs[LOC_MAP_IRRADIANCE] = GetShaderLocation(mat.shader, "irradianceMap");
  105. mat.shader.locs[LOC_MAP_PREFILTER] = GetShaderLocation(mat.shader, "prefilterMap");
  106. mat.shader.locs[LOC_MAP_BRDF] = GetShaderLocation(mat.shader, "brdfLUT");
  107. // Set view matrix location
  108. mat.shader.locs[LOC_MATRIX_MODEL] = GetShaderLocation(mat.shader, "matModel");
  109. //mat.shader.locs[LOC_MATRIX_VIEW] = GetShaderLocation(mat.shader, "view");
  110. mat.shader.locs[LOC_VECTOR_VIEW] = GetShaderLocation(mat.shader, "viewPos");
  111. // Set PBR standard maps
  112. mat.maps[MAP_ALBEDO].texture = LoadTexture("resources/pbr/trooper_albedo.png");
  113. mat.maps[MAP_NORMAL].texture = LoadTexture("resources/pbr/trooper_normals.png");
  114. mat.maps[MAP_METALNESS].texture = LoadTexture("resources/pbr/trooper_metalness.png");
  115. mat.maps[MAP_ROUGHNESS].texture = LoadTexture("resources/pbr/trooper_roughness.png");
  116. mat.maps[MAP_OCCLUSION].texture = LoadTexture("resources/pbr/trooper_ao.png");
  117. // Load equirectangular to cubemap shader
  118. #if defined(PLATFORM_DESKTOP)
  119. Shader shdrCubemap = LoadShader("resources/shaders/glsl330/cubemap.vs", "resources/shaders/glsl330/cubemap.fs");
  120. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  121. Shader shdrCubemap = LoadShader("resources/shaders/glsl100/cubemap.vs", "resources/shaders/glsl100/cubemap.fs");
  122. #endif
  123. // Load irradiance (GI) calculation shader
  124. #if defined(PLATFORM_DESKTOP)
  125. Shader shdrIrradiance = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/irradiance.fs");
  126. #else // PLATFORM_RPI, PLATFORM_ANDROID, PLATFORM_WEB
  127. Shader shdrIrradiance = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/irradiance.fs");
  128. #endif
  129. // Load reflection prefilter calculation shader
  130. #if defined(PLATFORM_DESKTOP)
  131. Shader shdrPrefilter = LoadShader("resources/shaders/glsl330/skybox.vs", "resources/shaders/glsl330/prefilter.fs");
  132. #else
  133. Shader shdrPrefilter = LoadShader("resources/shaders/glsl100/skybox.vs", "resources/shaders/glsl100/prefilter.fs");
  134. #endif
  135. // Load bidirectional reflectance distribution function shader
  136. #if defined(PLATFORM_DESKTOP)
  137. Shader shdrBRDF = LoadShader("resources/shaders/glsl330/brdf.vs", "resources/shaders/glsl330/brdf.fs");
  138. #else
  139. Shader shdrBRDF = LoadShader("resources/shaders/glsl100/brdf.vs", "resources/shaders/glsl100/brdf.fs");
  140. #endif
  141. // Setup required shader locations
  142. SetShaderValue(shdrCubemap, GetShaderLocation(shdrCubemap, "equirectangularMap"), (int[1]){ 0 }, UNIFORM_INT);
  143. SetShaderValue(shdrIrradiance, GetShaderLocation(shdrIrradiance, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT);
  144. SetShaderValue(shdrPrefilter, GetShaderLocation(shdrPrefilter, "environmentMap"), (int[1]){ 0 }, UNIFORM_INT);
  145. Texture2D texHDR = LoadTexture("resources/dresden_square.hdr");
  146. Texture2D cubemap = GenTextureCubemap(shdrCubemap, texHDR, CUBEMAP_SIZE);
  147. mat.maps[MAP_IRRADIANCE].texture = GenTextureIrradiance(shdrIrradiance, cubemap, IRRADIANCE_SIZE);
  148. mat.maps[MAP_PREFILTER].texture = GenTexturePrefilter(shdrPrefilter, cubemap, PREFILTERED_SIZE);
  149. mat.maps[MAP_BRDF].texture = GenTextureBRDF(shdrBRDF, BRDF_SIZE);
  150. UnloadTexture(cubemap);
  151. UnloadTexture(texHDR);
  152. // Unload already used shaders (to create specific textures)
  153. UnloadShader(shdrCubemap);
  154. UnloadShader(shdrIrradiance);
  155. UnloadShader(shdrPrefilter);
  156. UnloadShader(shdrBRDF);
  157. // Set textures filtering for better quality
  158. SetTextureFilter(mat.maps[MAP_ALBEDO].texture, FILTER_BILINEAR);
  159. SetTextureFilter(mat.maps[MAP_NORMAL].texture, FILTER_BILINEAR);
  160. SetTextureFilter(mat.maps[MAP_METALNESS].texture, FILTER_BILINEAR);
  161. SetTextureFilter(mat.maps[MAP_ROUGHNESS].texture, FILTER_BILINEAR);
  162. SetTextureFilter(mat.maps[MAP_OCCLUSION].texture, FILTER_BILINEAR);
  163. // Enable sample usage in shader for assigned textures
  164. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "albedo.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  165. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "normals.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  166. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "metalness.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  167. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "roughness.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  168. SetShaderValue(mat.shader, GetShaderLocation(mat.shader, "occlusion.useSampler"), (int[1]){ 1 }, UNIFORM_INT);
  169. int renderModeLoc = GetShaderLocation(mat.shader, "renderMode");
  170. SetShaderValue(mat.shader, renderModeLoc, (int[1]){ 0 }, UNIFORM_INT);
  171. // Set up material properties color
  172. mat.maps[MAP_ALBEDO].color = albedo;
  173. mat.maps[MAP_NORMAL].color = (Color){ 128, 128, 255, 255 };
  174. mat.maps[MAP_METALNESS].value = metalness;
  175. mat.maps[MAP_ROUGHNESS].value = roughness;
  176. mat.maps[MAP_OCCLUSION].value = 1.0f;
  177. mat.maps[MAP_EMISSION].value = 0.5f;
  178. mat.maps[MAP_HEIGHT].value = 0.5f;
  179. return mat;
  180. }