emission.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #include "./common.h"
  2. #include "r3d.h"
  3. #include <raylib.h>
  4. #include <raymath.h>
  5. /* === Resources === */
  6. static R3D_Model model = { 0 };
  7. static R3D_Mesh plane = { 0 };
  8. static R3D_Material material = { 0 };
  9. static Camera3D camera = { 0 };
  10. static R3D_Light light = { 0 };
  11. static float rotModel = 0.0f;
  12. /* === Toggle Light === */
  13. void ToggleLight(void)
  14. {
  15. if (R3D_IsLightActive(light)) {
  16. R3D_SetLightActive(light, false);
  17. R3D_SetAmbientColor(BLACK);
  18. }
  19. else {
  20. R3D_SetLightActive(light, true);
  21. R3D_SetAmbientColor(DARKGRAY);
  22. }
  23. }
  24. /* === Example === */
  25. const char* Init(void)
  26. {
  27. /* --- Initialize R3D with its internal resolution --- */
  28. R3D_Init(GetScreenWidth(), GetScreenHeight(), 0);
  29. SetTargetFPS(60);
  30. /* --- Configure the background color and ambient lighting --- */
  31. R3D_SetBackgroundColor(BLACK);
  32. R3D_SetAmbientColor(DARKGRAY);
  33. /* --- Configure the post process parameters --- */
  34. R3D_SetTonemapMode(R3D_TONEMAP_ACES);
  35. R3D_SetTonemapExposure(0.8f);
  36. R3D_SetTonemapWhite(2.5f);
  37. R3D_SetBloomMode(R3D_BLOOM_ADDITIVE);
  38. R3D_SetBloomSoftThreshold(0.2f);
  39. R3D_SetBloomIntensity(0.2f);
  40. R3D_SetBloomThreshold(0.6f);
  41. /* --- Loads the main model of the scene --- */
  42. R3D_SetModelImportScale(0.1f);
  43. model = R3D_LoadModel(RESOURCES_PATH "emission.glb");
  44. /* --- Generates a mesh for the ground and load a material for it --- */
  45. plane = R3D_GenMeshPlane(1000, 1000, 1, 1, true);
  46. material = R3D_GetDefaultMaterial();
  47. /* --- Setup the scene lighting --- */
  48. light = R3D_CreateLight(R3D_LIGHT_SPOT);
  49. {
  50. R3D_LightLookAt(light, (Vector3) { 0, 10, 5 }, (Vector3) { 0 });
  51. R3D_SetLightOuterCutOff(light, 45.0f);
  52. R3D_SetLightInnerCutOff(light, 22.5f);
  53. R3D_EnableShadow(light, 4096);
  54. R3D_SetLightActive(light, true);
  55. }
  56. /* --- Setup the camera --- */
  57. camera = (Camera3D) {
  58. .position = (Vector3) { -1.0f, 1.75f, 1.75f },
  59. .target = (Vector3) { 0, 0.5f, 0 },
  60. .up = (Vector3) { 0, 1, 0 },
  61. .fovy = 60,
  62. };
  63. return "[r3d] - Emission example";
  64. }
  65. void Update(float delta)
  66. {
  67. if (IsKeyPressed(KEY_SPACE)) {
  68. ToggleLight();
  69. }
  70. if (IsMouseButtonDown(MOUSE_LEFT_BUTTON)) {
  71. camera.position.y = Clamp(camera.position.y + 0.01f * GetMouseDelta().y, 0.25f, 2.5f);
  72. rotModel += 0.01f * GetMouseDelta().x;
  73. }
  74. }
  75. void Draw(void)
  76. {
  77. R3D_Begin(camera);
  78. R3D_DrawMesh(&plane, &material, MatrixIdentity());
  79. R3D_DrawModelEx(&model, (Vector3) { 0 }, (Vector3) { 0, 1, 0 }, rotModel, (Vector3) { 1.0f, 1.0f, 1.0f });
  80. R3D_End();
  81. DrawText("Press SPACE to toggle the light", 10, 10, 20, LIME);
  82. DrawCredits("Model by har15204405");
  83. }
  84. void Close(void)
  85. {
  86. R3D_UnloadModel(&model, true);
  87. R3D_UnloadMesh(&plane);
  88. R3D_Close();
  89. }