sponza.c 3.5 KB

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