dof.c 4.0 KB

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