dof.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. #include "./common.h"
  2. #include "r3d.h"
  3. #include "rcamera.h"
  4. #include <stdlib.h>
  5. #include <stdio.h>
  6. /* === Resources === */
  7. #define X_INSTANCES 10
  8. #define Y_INSTANCES 10
  9. #define INSTANCE_COUNT (X_INSTANCES * Y_INSTANCES)
  10. static R3D_Mesh meshSphere = { 0 };
  11. static R3D_Material matDefault = { 0 };
  12. static Camera3D camDefault = { 0 };
  13. static Matrix instances[INSTANCE_COUNT];
  14. static Color instanceColors[INSTANCE_COUNT];
  15. /* === Example === */
  16. const char* Init(void)
  17. {
  18. R3D_Init(GetScreenWidth(), GetScreenHeight(), R3D_FLAG_FXAA);
  19. SetTargetFPS(60);
  20. /* --- Enable and configure DOF --- */
  21. R3D_SetDofMode(R3D_DOF_ENABLED);
  22. R3D_SetDofFocusPoint(2.0f);
  23. R3D_SetDofFocusScale(3.0f);
  24. R3D_SetDofMaxBlurSize(20.0f);
  25. R3D_SetDofDebugMode(0);
  26. /* --- Setup scene lighting --- */
  27. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  28. R3D_SetLightDirection(light, (Vector3) { 0, -1, 0 });
  29. R3D_SetLightActive(light, true);
  30. /* --- Load sphere mesh and material --- */
  31. meshSphere = R3D_GenMeshSphere(0.2f, 64, 64, true);
  32. matDefault = R3D_GetDefaultMaterial();
  33. /* --- Generate instances --- */
  34. const float spacing = 0.5f;
  35. const float offsetX = (X_INSTANCES * spacing) / 2;
  36. const float offsetZ = (Y_INSTANCES * spacing) / 2;
  37. int idx = 0;
  38. for (int x = 0; x < X_INSTANCES; x++) {
  39. for (int y = 0; y < Y_INSTANCES; y++) {
  40. instances[idx] = MatrixTranslate(x * spacing - offsetX, 0, y * spacing - offsetZ);
  41. instanceColors[idx] = (Color) {
  42. (unsigned char)(rand() % 255),
  43. (unsigned char)(rand() % 255),
  44. (unsigned char)(rand() % 255),
  45. 255
  46. };
  47. idx++;
  48. }
  49. }
  50. /* --- Configure the camera and ready to go! */
  51. camDefault = (Camera3D) {
  52. .position = (Vector3) { 0, 2, 2 },
  53. .target = (Vector3) { 0, 0, 0 },
  54. .up = (Vector3) { 0, 1, 0 },
  55. .fovy = 60,
  56. };
  57. return "[r3d] - DoF example";
  58. }
  59. void Update(float delta)
  60. {
  61. /* --- Rotate camera --- */
  62. Matrix rotation = MatrixRotate(GetCameraUp(&camDefault), 0.1f * delta);
  63. Vector3 view = Vector3Subtract(camDefault.position, camDefault.target);
  64. view = Vector3Transform(view, rotation);
  65. camDefault.position = Vector3Add(camDefault.target, view);
  66. /* --- Adjust DoF based on mouse position --- */
  67. Vector2 mousePosition = GetMousePosition();
  68. float mouseWheel = GetMouseWheelMove();
  69. float focusPoint = 0.5f + (5.0f - (mousePosition.y / GetScreenHeight()) * 5.0f);
  70. R3D_SetDofFocusPoint(focusPoint);
  71. float focusScale = 0.5f + (5.0f - (mousePosition.x / GetScreenWidth()) * 5.0f);
  72. R3D_SetDofFocusScale(focusScale);
  73. if (mouseWheel != 0.0f) {
  74. float maxBlurSize = R3D_GetDofMaxBlurSize();
  75. maxBlurSize += mouseWheel * 0.1f;
  76. R3D_SetDofMaxBlurSize(maxBlurSize);
  77. }
  78. if (IsKeyPressed(KEY_F1)) {
  79. int debugMode = R3D_GetDofDebugMode();
  80. R3D_SetDofDebugMode((debugMode + 1) % 3);
  81. }
  82. }
  83. void Draw(void)
  84. {
  85. /* --- Ensure Clear Background --- */
  86. ClearBackground(BLACK);
  87. /* --- Render R3D scene --- */
  88. R3D_Begin(camDefault);
  89. R3D_SetBackgroundColor((Color){0, 0, 0, 255});
  90. R3D_DrawMeshInstancedEx(&meshSphere, &matDefault, instances, instanceColors, INSTANCE_COUNT);
  91. R3D_End();
  92. /* --- Draw DoF values --- */
  93. char dofText[128];
  94. snprintf(dofText, sizeof(dofText), "Focus Point: %.2f\nFocus Scale: %.2f\nMax Blur Size: %.2f\nDebug Mode: %d",
  95. R3D_GetDofFocusPoint(), R3D_GetDofFocusScale(), R3D_GetDofMaxBlurSize(), R3D_GetDofDebugMode());
  96. DrawText(dofText, 10, 30, 20, WHITE);
  97. /* --- Print instructions --- */
  98. DrawText("F1: Toggle Debug Mode\nScroll: Adjust Max Blur Size\nMouse Left/Right: Shallow/Deep DoF\nMouse Up/Down: Adjust Focus Point Depth", 300, 10, 20, WHITE);
  99. /* --- Draw FPS --- */
  100. char fpsText[32];
  101. snprintf(fpsText, sizeof(fpsText), "FPS: %d", GetFPS());
  102. DrawText(fpsText, 10, 10, 20, WHITE);
  103. }
  104. void Close(void)
  105. {
  106. R3D_UnloadMesh(&meshSphere);
  107. R3D_Close();
  108. }