sponza.c 4.6 KB

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