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.

234 rivejä
12 KiB

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