sponza.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. #include "./common.h"
  2. #include <rlgl.h>
  3. /* === Resources === */
  4. static Model sponza = { 0 };
  5. static R3D_Skybox skybox = { 0 };
  6. static Camera3D camera = { 0 };
  7. static R3D_Light lights[2] = { 0 };
  8. static bool sky = false;
  9. /* === Examples === */
  10. const char* Init(void)
  11. {
  12. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  13. SetTargetFPS(60);
  14. R3D_SetSSAO(true);
  15. R3D_SetSSAORadius(4.0f);
  16. R3D_SetBloomMode(R3D_BLOOM_SOFT_LIGHT);
  17. sponza = RES_LoadModel("sponza.glb");
  18. for (int i = 0; i < sponza.materialCount; i++) {
  19. sponza.materials[i].maps[MATERIAL_MAP_ALBEDO].color = WHITE;
  20. sponza.materials[i].maps[MATERIAL_MAP_OCCLUSION].value = 1.0f;
  21. sponza.materials[i].maps[MATERIAL_MAP_ROUGHNESS].value = 1.0f;
  22. sponza.materials[i].maps[MATERIAL_MAP_METALNESS].value = 1.0f;
  23. GenTextureMipmaps(&sponza.materials[i].maps[MATERIAL_MAP_ALBEDO].texture);
  24. SetTextureFilter(sponza.materials[i].maps[MATERIAL_MAP_ALBEDO].texture, TEXTURE_FILTER_TRILINEAR);
  25. GenTextureMipmaps(&sponza.materials[i].maps[MATERIAL_MAP_NORMAL].texture);
  26. SetTextureFilter(sponza.materials[i].maps[MATERIAL_MAP_NORMAL].texture, TEXTURE_FILTER_TRILINEAR);
  27. // REVIEW: Issue with the model textures
  28. sponza.materials[i].maps[MATERIAL_MAP_ROUGHNESS].texture = (Texture2D){ .id = rlGetTextureIdDefault() };
  29. }
  30. // NOTE: Toggle sky with 'T' key
  31. skybox = R3D_LoadSkybox(RESOURCES_PATH "sky/skybox3.png", CUBEMAP_LAYOUT_AUTO_DETECT);
  32. for (int i = 0; i < 2; i++) {
  33. lights[i] = R3D_CreateLight(R3D_LIGHT_SPOT);
  34. R3D_SetLightPosition(lights[i], (Vector3) { i ? -10 : 10, 20, 0 });
  35. R3D_SetLightTarget(lights[i], (Vector3) { 0, 0, 0 });
  36. R3D_SetLightActive(lights[i], true);
  37. R3D_SetShadowUpdateMode(lights[i], R3D_SHADOW_UPDATE_MANUAL);
  38. R3D_EnableShadow(lights[i], 4096);
  39. }
  40. camera = (Camera3D){
  41. .position = (Vector3) { 0, 0, 0 },
  42. .target = (Vector3) { 0, 0, -1 },
  43. .up = (Vector3) { 0, 1, 0 },
  44. .fovy = 60,
  45. };
  46. DisableCursor();
  47. return "[r3d] - sponza example";
  48. }
  49. void Update(float delta)
  50. {
  51. UpdateCamera(&camera, CAMERA_FREE);
  52. if (IsKeyPressed(KEY_T)) {
  53. if (sky) R3D_DisableSkybox();
  54. else R3D_EnableSkybox(skybox);
  55. sky = !sky;
  56. }
  57. if (IsKeyPressed(KEY_F)) {
  58. bool fxaa = R3D_HasState(R3D_FLAG_FXAA);
  59. if (fxaa) R3D_ClearState(R3D_FLAG_FXAA);
  60. else R3D_SetState(R3D_FLAG_FXAA);
  61. }
  62. if (IsKeyPressed(KEY_O)) {
  63. R3D_SetSSAO(!R3D_GetSSAO());
  64. }
  65. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
  66. R3D_Tonemap tonemap = R3D_GetTonemapMode();
  67. R3D_SetTonemapMode((tonemap + 5 - 1) % 5);
  68. }
  69. if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
  70. R3D_Tonemap tonemap = R3D_GetTonemapMode();
  71. R3D_SetTonemapMode((tonemap + 1) % 5);
  72. }
  73. }
  74. void Draw(void)
  75. {
  76. R3D_Begin(camera);
  77. R3D_DrawModel(sponza, (Vector3) { 0 }, 1.0f);
  78. R3D_End();
  79. BeginMode3D(camera);
  80. DrawSphere(R3D_GetLightPosition(lights[0]), 0.5f, WHITE);
  81. DrawSphere(R3D_GetLightPosition(lights[1]), 0.5f, WHITE);
  82. EndMode3D();
  83. R3D_Tonemap tonemap = R3D_GetTonemapMode();
  84. switch (tonemap) {
  85. case R3D_TONEMAP_LINEAR: {
  86. const char* txt = "< TONEMAP LINEAR >";
  87. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  88. }
  89. break;
  90. case R3D_TONEMAP_REINHARD: {
  91. const char* txt = "< TONEMAP REINHARD >";
  92. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  93. }
  94. break;
  95. case R3D_TONEMAP_FILMIC: {
  96. const char* txt = "< TONEMAP FILMIC >";
  97. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  98. }
  99. break;
  100. case R3D_TONEMAP_ACES: {
  101. const char* txt = "< TONEMAP ACES >";
  102. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  103. } break;
  104. case R3D_TONEMAP_AGX: {
  105. const char* txt = "< TONEMAP AGX >";
  106. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  107. } break;
  108. }
  109. DrawFPS(10, 10);
  110. }
  111. void Close(void)
  112. {
  113. UnloadModel(sponza);
  114. R3D_UnloadSkybox(skybox);
  115. R3D_Close();
  116. }