emission.c 2.7 KB

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