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.

334 lines
16 KiB

Update shaders_basic_pbr example to work on web (#4516) * basic pbr example pbr implementation includes rpbr.h and few shader files header only file, which self contain everything needed for pbr rendering. Few textures and one model of the car which is under free licence which is included inside basic_pbr.c example file currently supported shader versions are 120 and 330 , version 100 has small issue which I have to resolve * Unloading PBRMAterial I forgot unloading PBRMaterial * fix small issue with texOffset assigment. value was Vector4 at first but I found out it would be unclear for and users, so I change to have two Vector2 instead, but forgot to assign offset . * Changed size of textures and file name changed Changed size of textures from 2048x2048 to 1024x1024 and file name changed to shaders_basic_pbr.c , Added the function PBRModel PBRModelLoadFromMesh(Mesh mesh); but GenMeshPlane(2, 2.0, 3, 3) culdn't be used because it crash once GenMeshTangents() is used with that plane mesh * Update to 5.5 version of eexample and fix to work in web set GLSL_VERSION 100 set precision highp float; removed in keyword fix for loop has to use only constant * Update shader_basic_pbr example to work on web changed to GLSL_VERSION 100 update glsl100 shader set float precision to highp removed keyword in change for loop tu use constant value gives an error * Update shader_basic_pbr example to work on web changed to GLSL_VERSION 100 update glsl100 shader set float precision to highp removed keyword in change for loop tu use constant value gives an error * removed rpbr.h removed rpbr.h
1 month ago
  1. /*******************************************************************************************
  2. *
  3. * raylib [shaders] example - Basic PBR
  4. *
  5. * Example originally created with raylib 5.0, last time updated with raylib 5.1-dev
  6. *
  7. * Example contributed by Afan OLOVCIC (@_DevDad) and reviewed by Ramon Santamaria (@raysan5)
  8. *
  9. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  10. * BSD-like license that allows static linking with closed source software
  11. *
  12. * Copyright (c) 2023-2024 Afan OLOVCIC (@_DevDad)
  13. *
  14. * Model: "Old Rusty Car" (https://skfb.ly/LxRy) by Renafox,
  15. * licensed under Creative Commons Attribution-NonCommercial
  16. * (http://creativecommons.org/licenses/by-nc/4.0/)
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. #if defined(PLATFORM_DESKTOP)
  21. #define GLSL_VERSION 330
  22. #else // PLATFORM_ANDROID, PLATFORM_WEB
  23. #define GLSL_VERSION 100
  24. #endif
  25. #include <stdlib.h> // Required for: NULL
  26. #define MAX_LIGHTS 4 // Max dynamic lights supported by shader
  27. //----------------------------------------------------------------------------------
  28. // Types and Structures Definition
  29. //----------------------------------------------------------------------------------
  30. // Light type
  31. typedef enum {
  32. LIGHT_DIRECTIONAL = 0,
  33. LIGHT_POINT,
  34. LIGHT_SPOT
  35. } LightType;
  36. // Light data
  37. typedef struct {
  38. int type;
  39. int enabled;
  40. Vector3 position;
  41. Vector3 target;
  42. float color[4];
  43. float intensity;
  44. // Shader light parameters locations
  45. int typeLoc;
  46. int enabledLoc;
  47. int positionLoc;
  48. int targetLoc;
  49. int colorLoc;
  50. int intensityLoc;
  51. } Light;
  52. //----------------------------------------------------------------------------------
  53. // Global Variables Definition
  54. //----------------------------------------------------------------------------------
  55. static int lightCount = 0; // Current number of dynamic lights that have been created
  56. //----------------------------------------------------------------------------------
  57. // Module specific Functions Declaration
  58. //----------------------------------------------------------------------------------
  59. // Create a light and get shader locations
  60. static Light CreateLight(int type, Vector3 position, Vector3 target, Color color, float intensity, Shader shader);
  61. // Update light properties on shader
  62. // NOTE: Light shader locations should be available
  63. static void UpdateLight(Shader shader, Light light);
  64. //----------------------------------------------------------------------------------
  65. // Main Entry Point
  66. //----------------------------------------------------------------------------------
  67. int main()
  68. {
  69. // Initialization
  70. //--------------------------------------------------------------------------------------
  71. const int screenWidth = 800;
  72. const int screenHeight = 450;
  73. SetConfigFlags(FLAG_MSAA_4X_HINT);
  74. InitWindow(screenWidth, screenHeight, "raylib [shaders] example - basic pbr");
  75. // Define the camera to look into our 3d world
  76. Camera camera = { 0 };
  77. camera.position = (Vector3){ 2.0f, 2.0f, 6.0f }; // Camera position
  78. camera.target = (Vector3){ 0.0f, 0.5f, 0.0f }; // Camera looking at point
  79. camera.up = (Vector3){ 0.0f, 1.0f, 0.0f }; // Camera up vector (rotation towards target)
  80. camera.fovy = 45.0f; // Camera field-of-view Y
  81. camera.projection = CAMERA_PERSPECTIVE; // Camera projection type
  82. // Load PBR shader and setup all required locations
  83. Shader shader = LoadShader(TextFormat("resources/shaders/glsl%i/pbr.vs", GLSL_VERSION),
  84. TextFormat("resources/shaders/glsl%i/pbr.fs", GLSL_VERSION));
  85. shader.locs[SHADER_LOC_MAP_ALBEDO] = GetShaderLocation(shader, "albedoMap");
  86. // WARNING: Metalness, roughness, and ambient occlusion are all packed into a MRA texture
  87. // They are passed as to the SHADER_LOC_MAP_METALNESS location for convenience,
  88. // shader already takes care of it accordingly
  89. shader.locs[SHADER_LOC_MAP_METALNESS] = GetShaderLocation(shader, "mraMap");
  90. shader.locs[SHADER_LOC_MAP_NORMAL] = GetShaderLocation(shader, "normalMap");
  91. // WARNING: Similar to the MRA map, the emissive map packs different information
  92. // into a single texture: it stores height and emission data
  93. // It is binded to SHADER_LOC_MAP_EMISSION location an properly processed on shader
  94. shader.locs[SHADER_LOC_MAP_EMISSION] = GetShaderLocation(shader, "emissiveMap");
  95. shader.locs[SHADER_LOC_COLOR_DIFFUSE] = GetShaderLocation(shader, "albedoColor");
  96. // Setup additional required shader locations, including lights data
  97. shader.locs[SHADER_LOC_VECTOR_VIEW] = GetShaderLocation(shader, "viewPos");
  98. int lightCountLoc = GetShaderLocation(shader, "numOfLights");
  99. int maxLightCount = MAX_LIGHTS;
  100. SetShaderValue(shader, lightCountLoc, &maxLightCount, SHADER_UNIFORM_INT);
  101. // Setup ambient color and intensity parameters
  102. float ambientIntensity = 0.02f;
  103. Color ambientColor = (Color){ 26, 32, 135, 255 };
  104. Vector3 ambientColorNormalized = (Vector3){ ambientColor.r/255.0f, ambientColor.g/255.0f, ambientColor.b/255.0f };
  105. SetShaderValue(shader, GetShaderLocation(shader, "ambientColor"), &ambientColorNormalized, SHADER_UNIFORM_VEC3);
  106. SetShaderValue(shader, GetShaderLocation(shader, "ambient"), &ambientIntensity, SHADER_UNIFORM_FLOAT);
  107. // Get location for shader parameters that can be modified in real time
  108. int emissiveIntensityLoc = GetShaderLocation(shader, "emissivePower");
  109. int emissiveColorLoc = GetShaderLocation(shader, "emissiveColor");
  110. int textureTilingLoc = GetShaderLocation(shader, "tiling");
  111. // Load old car model using PBR maps and shader
  112. // WARNING: We know this model consists of a single model.meshes[0] and
  113. // that model.materials[0] is by default assigned to that mesh
  114. // There could be more complex models consisting of multiple meshes and
  115. // multiple materials defined for those meshes... but always 1 mesh = 1 material
  116. Model car = LoadModel("resources/models/old_car_new.glb");
  117. // Assign already setup PBR shader to model.materials[0], used by models.meshes[0]
  118. car.materials[0].shader = shader;
  119. // Setup materials[0].maps default parameters
  120. car.materials[0].maps[MATERIAL_MAP_ALBEDO].color = WHITE;
  121. car.materials[0].maps[MATERIAL_MAP_METALNESS].value = 0.0f;
  122. car.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value = 0.0f;
  123. car.materials[0].maps[MATERIAL_MAP_OCCLUSION].value = 1.0f;
  124. car.materials[0].maps[MATERIAL_MAP_EMISSION].color = (Color){ 255, 162, 0, 255 };
  125. // Setup materials[0].maps default textures
  126. car.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = LoadTexture("resources/old_car_d.png");
  127. car.materials[0].maps[MATERIAL_MAP_METALNESS].texture = LoadTexture("resources/old_car_mra.png");
  128. car.materials[0].maps[MATERIAL_MAP_NORMAL].texture = LoadTexture("resources/old_car_n.png");
  129. car.materials[0].maps[MATERIAL_MAP_EMISSION].texture = LoadTexture("resources/old_car_e.png");
  130. // Load floor model mesh and assign material parameters
  131. // NOTE: A basic plane shape can be generated instead of being loaded from a model file
  132. Model floor = LoadModel("resources/models/plane.glb");
  133. //Mesh floorMesh = GenMeshPlane(10, 10, 10, 10);
  134. //GenMeshTangents(&floorMesh); // TODO: Review tangents generation
  135. //Model floor = LoadModelFromMesh(floorMesh);
  136. // Assign material shader for our floor model, same PBR shader
  137. floor.materials[0].shader = shader;
  138. floor.materials[0].maps[MATERIAL_MAP_ALBEDO].color = WHITE;
  139. floor.materials[0].maps[MATERIAL_MAP_METALNESS].value = 0.0f;
  140. floor.materials[0].maps[MATERIAL_MAP_ROUGHNESS].value = 0.0f;
  141. floor.materials[0].maps[MATERIAL_MAP_OCCLUSION].value = 1.0f;
  142. floor.materials[0].maps[MATERIAL_MAP_EMISSION].color = BLACK;
  143. floor.materials[0].maps[MATERIAL_MAP_ALBEDO].texture = LoadTexture("resources/road_a.png");
  144. floor.materials[0].maps[MATERIAL_MAP_METALNESS].texture = LoadTexture("resources/road_mra.png");
  145. floor.materials[0].maps[MATERIAL_MAP_NORMAL].texture = LoadTexture("resources/road_n.png");
  146. // Models texture tiling parameter can be stored in the Material struct if required (CURRENTLY NOT USED)
  147. // NOTE: Material.params[4] are available for generic parameters storage (float)
  148. Vector2 carTextureTiling = (Vector2){ 0.5f, 0.5f };
  149. Vector2 floorTextureTiling = (Vector2){ 0.5f, 0.5f };
  150. // Create some lights
  151. Light lights[MAX_LIGHTS] = { 0 };
  152. lights[0] = CreateLight(LIGHT_POINT, (Vector3){ -1.0f, 1.0f, -2.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, YELLOW, 4.0f, shader);
  153. lights[1] = CreateLight(LIGHT_POINT, (Vector3){ 2.0f, 1.0f, 1.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, GREEN, 3.3f, shader);
  154. lights[2] = CreateLight(LIGHT_POINT, (Vector3){ -2.0f, 1.0f, 1.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, RED, 8.3f, shader);
  155. lights[3] = CreateLight(LIGHT_POINT, (Vector3){ 1.0f, 1.0f, -2.0f }, (Vector3){ 0.0f, 0.0f, 0.0f }, BLUE, 2.0f, shader);
  156. // Setup material texture maps usage in shader
  157. // NOTE: By default, the texture maps are always used
  158. int usage = 1;
  159. SetShaderValue(shader, GetShaderLocation(shader, "useTexAlbedo"), &usage, SHADER_UNIFORM_INT);
  160. SetShaderValue(shader, GetShaderLocation(shader, "useTexNormal"), &usage, SHADER_UNIFORM_INT);
  161. SetShaderValue(shader, GetShaderLocation(shader, "useTexMRA"), &usage, SHADER_UNIFORM_INT);
  162. SetShaderValue(shader, GetShaderLocation(shader, "useTexEmissive"), &usage, SHADER_UNIFORM_INT);
  163. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  164. //---------------------------------------------------------------------------------------
  165. // Main game loop
  166. while (!WindowShouldClose()) // Detect window close button or ESC key
  167. {
  168. // Update
  169. //----------------------------------------------------------------------------------
  170. UpdateCamera(&camera, CAMERA_ORBITAL);
  171. // Update the shader with the camera view vector (points towards { 0.0f, 0.0f, 0.0f })
  172. float cameraPos[3] = {camera.position.x, camera.position.y, camera.position.z};
  173. SetShaderValue(shader, shader.locs[SHADER_LOC_VECTOR_VIEW], cameraPos, SHADER_UNIFORM_VEC3);
  174. // Check key inputs to enable/disable lights
  175. if (IsKeyPressed(KEY_ONE)) { lights[2].enabled = !lights[2].enabled; }
  176. if (IsKeyPressed(KEY_TWO)) { lights[1].enabled = !lights[1].enabled; }
  177. if (IsKeyPressed(KEY_THREE)) { lights[3].enabled = !lights[3].enabled; }
  178. if (IsKeyPressed(KEY_FOUR)) { lights[0].enabled = !lights[0].enabled; }
  179. // Update light values on shader (actually, only enable/disable them)
  180. for (int i = 0; i < MAX_LIGHTS; i++) UpdateLight(shader, lights[i]);
  181. //----------------------------------------------------------------------------------
  182. // Draw
  183. //----------------------------------------------------------------------------------
  184. BeginDrawing();
  185. ClearBackground(BLACK);
  186. BeginMode3D(camera);
  187. // Set floor model texture tiling and emissive color parameters on shader
  188. SetShaderValue(shader, textureTilingLoc, &floorTextureTiling, SHADER_UNIFORM_VEC2);
  189. Vector4 floorEmissiveColor = ColorNormalize(floor.materials[0].maps[MATERIAL_MAP_EMISSION].color);
  190. SetShaderValue(shader, emissiveColorLoc, &floorEmissiveColor, SHADER_UNIFORM_VEC4);
  191. DrawModel(floor, (Vector3){ 0.0f, 0.0f, 0.0f }, 5.0f, WHITE); // Draw floor model
  192. // Set old car model texture tiling, emissive color and emissive intensity parameters on shader
  193. SetShaderValue(shader, textureTilingLoc, &carTextureTiling, SHADER_UNIFORM_VEC2);
  194. Vector4 carEmissiveColor = ColorNormalize(car.materials[0].maps[MATERIAL_MAP_EMISSION].color);
  195. SetShaderValue(shader, emissiveColorLoc, &carEmissiveColor, SHADER_UNIFORM_VEC4);
  196. float emissiveIntensity = 0.01f;
  197. SetShaderValue(shader, emissiveIntensityLoc, &emissiveIntensity, SHADER_UNIFORM_FLOAT);
  198. DrawModel(car, (Vector3){ 0.0f, 0.0f, 0.0f }, 0.25f, WHITE); // Draw car model
  199. // Draw spheres to show the lights positions
  200. for (int i = 0; i < MAX_LIGHTS; i++)
  201. {
  202. Color lightColor = (Color){ lights[i].color[0]*255, lights[i].color[1]*255, lights[i].color[2]*255, lights[i].color[3]*255 };
  203. if (lights[i].enabled) DrawSphereEx(lights[i].position, 0.2f, 8, 8, lightColor);
  204. else DrawSphereWires(lights[i].position, 0.2f, 8, 8, ColorAlpha(lightColor, 0.3f));
  205. }
  206. EndMode3D();
  207. DrawText("Toggle lights: [1][2][3][4]", 10, 40, 20, LIGHTGRAY);
  208. DrawText("(c) Old Rusty Car model by Renafox (https://skfb.ly/LxRy)", screenWidth - 320, screenHeight - 20, 10, LIGHTGRAY);
  209. DrawFPS(10, 10);
  210. EndDrawing();
  211. //----------------------------------------------------------------------------------
  212. }
  213. // De-Initialization
  214. //--------------------------------------------------------------------------------------
  215. // Unbind (disconnect) shader from car.material[0]
  216. // to avoid UnloadMaterial() trying to unload it automatically
  217. car.materials[0].shader = (Shader){ 0 };
  218. UnloadMaterial(car.materials[0]);
  219. car.materials[0].maps = NULL;
  220. UnloadModel(car);
  221. floor.materials[0].shader = (Shader){ 0 };
  222. UnloadMaterial(floor.materials[0]);
  223. floor.materials[0].maps = NULL;
  224. UnloadModel(floor);
  225. UnloadShader(shader); // Unload Shader
  226. CloseWindow(); // Close window and OpenGL context
  227. //--------------------------------------------------------------------------------------
  228. return 0;
  229. }
  230. // Create light with provided data
  231. // NOTE: It updated the global lightCount and it's limited to MAX_LIGHTS
  232. static Light CreateLight(int type, Vector3 position, Vector3 target, Color color, float intensity, Shader shader)
  233. {
  234. Light light = { 0 };
  235. if (lightCount < MAX_LIGHTS)
  236. {
  237. light.enabled = 1;
  238. light.type = type;
  239. light.position = position;
  240. light.target = target;
  241. light.color[0] = (float)color.r/255.0f;
  242. light.color[1] = (float)color.g/255.0f;
  243. light.color[2] = (float)color.b/255.0f;
  244. light.color[3] = (float)color.a/255.0f;
  245. light.intensity = intensity;
  246. // NOTE: Shader parameters names for lights must match the requested ones
  247. light.enabledLoc = GetShaderLocation(shader, TextFormat("lights[%i].enabled", lightCount));
  248. light.typeLoc = GetShaderLocation(shader, TextFormat("lights[%i].type", lightCount));
  249. light.positionLoc = GetShaderLocation(shader, TextFormat("lights[%i].position", lightCount));
  250. light.targetLoc = GetShaderLocation(shader, TextFormat("lights[%i].target", lightCount));
  251. light.colorLoc = GetShaderLocation(shader, TextFormat("lights[%i].color", lightCount));
  252. light.intensityLoc = GetShaderLocation(shader, TextFormat("lights[%i].intensity", lightCount));
  253. UpdateLight(shader, light);
  254. lightCount++;
  255. }
  256. return light;
  257. }
  258. // Send light properties to shader
  259. // NOTE: Light shader locations should be available
  260. static void UpdateLight(Shader shader, Light light)
  261. {
  262. SetShaderValue(shader, light.enabledLoc, &light.enabled, SHADER_UNIFORM_INT);
  263. SetShaderValue(shader, light.typeLoc, &light.type, SHADER_UNIFORM_INT);
  264. // Send to shader light position values
  265. float position[3] = { light.position.x, light.position.y, light.position.z };
  266. SetShaderValue(shader, light.positionLoc, position, SHADER_UNIFORM_VEC3);
  267. // Send to shader light target position values
  268. float target[3] = { light.target.x, light.target.y, light.target.z };
  269. SetShaderValue(shader, light.targetLoc, target, SHADER_UNIFORM_VEC3);
  270. SetShaderValue(shader, light.colorLoc, light.color, SHADER_UNIFORM_VEC4);
  271. SetShaderValue(shader, light.intensityLoc, &light.intensity, SHADER_UNIFORM_FLOAT);
  272. }