sponza.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. /* --- Initialize R3D with its internal resolution --- */
  14. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  15. SetTargetFPS(60);
  16. /* --- Configure default post process settings --- */
  17. R3D_SetSSAO(true);
  18. R3D_SetSSAORadius(4.0f);
  19. R3D_SetBloomMode(R3D_BLOOM_MIX);
  20. /* --- Set default ambient color (when no skybox is activated) --- */
  21. R3D_SetAmbientColor(GRAY);
  22. /* --- Load Sponza scene --- */
  23. sponza = R3D_LoadModel(RESOURCES_PATH "sponza.glb");
  24. /* --- Load skybox (disabled by default) --- */
  25. skybox = R3D_LoadSkybox(RESOURCES_PATH "sky/skybox3.png", CUBEMAP_LAYOUT_AUTO_DETECT);
  26. //R3D_EnableSkybox(skybox);
  27. /* --- Set scene bounds, useful if you use directional lights --- */
  28. R3D_SetSceneBounds(sponza.aabb);
  29. /* --- Configure lights --- */
  30. for (int i = 0; i < 2; i++)
  31. {
  32. lights[i] = R3D_CreateLight(R3D_LIGHT_OMNI);
  33. R3D_SetLightPosition(lights[i], (Vector3) { i ? -10 : 10, 20, 0 });
  34. R3D_SetLightActive(lights[i], true);
  35. R3D_SetLightEnergy(lights[i], 1.0f);
  36. R3D_SetShadowUpdateMode(lights[i], R3D_SHADOW_UPDATE_MANUAL);
  37. R3D_EnableShadow(lights[i], 4096);
  38. }
  39. /* --- Configure camera --- */
  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. /* --- Ready to go! --- */
  47. DisableCursor();
  48. return "[r3d] - Sponza example";
  49. }
  50. void Update(float delta)
  51. {
  52. /* --- Update the camera via raylib's functions --- */
  53. UpdateCamera(&camera, CAMERA_FREE);
  54. /* --- Skybox toggling --- */
  55. if (IsKeyPressed(KEY_ZERO)) {
  56. if (sky) R3D_DisableSkybox();
  57. else R3D_EnableSkybox(skybox);
  58. sky = !sky;
  59. }
  60. /* --- SSAO toggling --- */
  61. if (IsKeyPressed(KEY_ONE)) {
  62. R3D_SetSSAO(!R3D_GetSSAO());
  63. }
  64. /* --- Fog toggling --- */
  65. if (IsKeyPressed(KEY_TWO)) {
  66. R3D_SetFogMode(R3D_GetFogMode() == R3D_FOG_DISABLED ? R3D_FOG_EXP : R3D_FOG_DISABLED);
  67. }
  68. /* --- FXAA toggling --- */
  69. if (IsKeyPressed(KEY_THREE)) {
  70. bool fxaa = R3D_HasState(R3D_FLAG_FXAA);
  71. if (fxaa) R3D_ClearState(R3D_FLAG_FXAA);
  72. else R3D_SetState(R3D_FLAG_FXAA);
  73. }
  74. /* --- Tonemapping setter --- */
  75. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT)) {
  76. R3D_Tonemap tonemap = R3D_GetTonemapMode();
  77. R3D_SetTonemapMode((tonemap + R3D_TONEMAP_COUNT - 1) % R3D_TONEMAP_COUNT);
  78. }
  79. if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT)) {
  80. R3D_Tonemap tonemap = R3D_GetTonemapMode();
  81. R3D_SetTonemapMode((tonemap + 1) % R3D_TONEMAP_COUNT);
  82. }
  83. }
  84. void Draw(void)
  85. {
  86. /* --- Render R3D scene --- */
  87. R3D_Begin(camera);
  88. R3D_DrawModel(&sponza, (Vector3) { 0 }, 1.0f);
  89. R3D_End();
  90. /* --- 'Standard' raylib rendering to show where are the lights --- */
  91. BeginMode3D(camera);
  92. DrawSphere(R3D_GetLightPosition(lights[0]), 0.5f, WHITE);
  93. DrawSphere(R3D_GetLightPosition(lights[1]), 0.5f, WHITE);
  94. EndMode3D();
  95. /* --- Indicates which tonemapping is used --- */
  96. R3D_Tonemap tonemap = R3D_GetTonemapMode();
  97. switch (tonemap) {
  98. case R3D_TONEMAP_LINEAR: {
  99. const char* txt = "< TONEMAP LINEAR >";
  100. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  101. }
  102. break;
  103. case R3D_TONEMAP_REINHARD: {
  104. const char* txt = "< TONEMAP REINHARD >";
  105. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  106. }
  107. break;
  108. case R3D_TONEMAP_FILMIC: {
  109. const char* txt = "< TONEMAP FILMIC >";
  110. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  111. }
  112. break;
  113. case R3D_TONEMAP_ACES: {
  114. const char* txt = "< TONEMAP ACES >";
  115. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  116. } break;
  117. case R3D_TONEMAP_AGX: {
  118. const char* txt = "< TONEMAP AGX >";
  119. DrawText(txt, GetScreenWidth() - MeasureText(txt, 20) - 10, 10, 20, LIME);
  120. } break;
  121. default:
  122. break;
  123. }
  124. /* --- I think we understand what's going on here --- */
  125. DrawFPS(10, 10);
  126. }
  127. void Close(void)
  128. {
  129. R3D_UnloadModel(&sponza, true);
  130. R3D_UnloadSkybox(skybox);
  131. R3D_Close();
  132. }