2
0

pbr.c 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. #include "./common.h"
  2. #include "r3d.h"
  3. /* === Resources === */
  4. static Model model = { 0 };
  5. static R3D_Skybox skybox = { 0 };
  6. static Camera3D camera = { 0 };
  7. static float modelScale = 1.0f;
  8. /* === Examples === */
  9. const char* Init(void)
  10. {
  11. R3D_Init(GetScreenWidth(), GetScreenHeight(), R3D_FLAG_FXAA);
  12. SetTargetFPS(60);
  13. R3D_SetSSAO(true);
  14. R3D_SetSSAORadius(4.0f);
  15. R3D_SetTonemapMode(R3D_TONEMAP_ACES);
  16. R3D_SetTonemapExposure(0.75f);
  17. R3D_SetTonemapWhite(1.25f);
  18. model = RES_LoadModel("pbr/musket.glb");
  19. {
  20. model.transform = MatrixMultiply(model.transform, MatrixRotateY(PI / 2));
  21. //for (int i = 0; i < model.meshCount; i++)
  22. for (int i = 0; i < model.materialCount; i++) {
  23. model.materials[i].maps[MATERIAL_MAP_ALBEDO].color = WHITE;
  24. model.materials[i].maps[MATERIAL_MAP_ROUGHNESS].value = 1.0f;
  25. model.materials[i].maps[MATERIAL_MAP_METALNESS].value = 1.0f;
  26. SetTextureFilter(model.materials[i].maps[MATERIAL_MAP_ALBEDO].texture, TEXTURE_FILTER_BILINEAR);
  27. SetTextureFilter(model.materials[i].maps[MATERIAL_MAP_ROUGHNESS].texture, TEXTURE_FILTER_BILINEAR);
  28. SetTextureFilter(model.materials[i].maps[MATERIAL_MAP_METALNESS].texture, TEXTURE_FILTER_BILINEAR);
  29. SetTextureFilter(model.materials[i].maps[MATERIAL_MAP_NORMAL].texture, TEXTURE_FILTER_BILINEAR);
  30. }
  31. }
  32. skybox = R3D_LoadSkybox(RESOURCES_PATH "sky/skybox2.png", CUBEMAP_LAYOUT_AUTO_DETECT);
  33. R3D_EnableSkybox(skybox);
  34. camera = (Camera3D){
  35. .position = (Vector3) { 0, 0, 50 },
  36. .target = (Vector3) { 0, 0, 0 },
  37. .up = (Vector3) { 0, 1, 0 },
  38. .fovy = 60,
  39. };
  40. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  41. {
  42. R3D_SetLightDirection(light, (Vector3) { 0, -1, -1 });
  43. R3D_SetLightActive(light, true);
  44. }
  45. return "[r3d] - PBR example";
  46. }
  47. void Update(float delta)
  48. {
  49. modelScale = Clamp(modelScale + GetMouseWheelMove() * 0.1, 0.25f, 2.5f);
  50. if (IsMouseButtonDown(MOUSE_BUTTON_LEFT))
  51. {
  52. float pitch = (GetMouseDelta().y * 0.005f) / modelScale;
  53. float yaw = (GetMouseDelta().x * 0.005f) / modelScale;
  54. model.transform = MatrixMultiply(
  55. model.transform, MatrixRotateXYZ((Vector3) { pitch, yaw, 0.0f })
  56. );
  57. }
  58. }
  59. void Draw(void)
  60. {
  61. R3D_Begin(camera);
  62. R3D_DrawModel(model, (Vector3) { 0 }, modelScale);
  63. R3D_End();
  64. DrawFPS(10, 10);
  65. DrawText("Model made by TommyLingL", 10, GetScreenHeight() - 30, 20, LIME);
  66. }
  67. void Close(void)
  68. {
  69. UnloadModel(model);
  70. R3D_UnloadSkybox(skybox);
  71. R3D_Close();
  72. }