dof.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. #include <r3d/r3d.h>
  2. #include <raymath.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #define X_INSTANCES 10
  6. #define Y_INSTANCES 10
  7. #define INSTANCE_COUNT (X_INSTANCES * Y_INSTANCES)
  8. int main(void)
  9. {
  10. // Initialize window
  11. InitWindow(800, 450, "[r3d] - DoF example");
  12. SetTargetFPS(60);
  13. // Initialize R3D with FXAA
  14. R3D_Init(GetScreenWidth(), GetScreenHeight(), R3D_FLAG_FXAA);
  15. // Configure depth of field and background
  16. R3D_ENVIRONMENT_SET(background.color, BLACK);
  17. R3D_ENVIRONMENT_SET(dof.mode, R3D_DOF_ENABLED);
  18. R3D_ENVIRONMENT_SET(dof.focusPoint, 2.0f);
  19. R3D_ENVIRONMENT_SET(dof.focusScale, 3.0f);
  20. R3D_ENVIRONMENT_SET(dof.maxBlurSize, 20.0f);
  21. R3D_ENVIRONMENT_SET(dof.debugMode, false);
  22. // Create directional light
  23. R3D_Light light = R3D_CreateLight(R3D_LIGHT_DIR);
  24. R3D_SetLightDirection(light, (Vector3){0, -1, 0});
  25. R3D_SetLightActive(light, true);
  26. // Create sphere mesh and default material
  27. R3D_Mesh meshSphere = R3D_GenMeshSphere(0.2f, 64, 64);
  28. R3D_Material matDefault = R3D_GetDefaultMaterial();
  29. // Generate instance matrices and colors
  30. Matrix instances[INSTANCE_COUNT];
  31. Color instanceColors[INSTANCE_COUNT];
  32. float spacing = 0.5f;
  33. float offsetX = (X_INSTANCES * spacing) / 2.0f;
  34. float offsetZ = (Y_INSTANCES * spacing) / 2.0f;
  35. int idx = 0;
  36. for (int x = 0; x < X_INSTANCES; x++) {
  37. for (int y = 0; y < Y_INSTANCES; y++) {
  38. instances[idx] = MatrixTranslate(x * spacing - offsetX, 0, y * spacing - offsetZ);
  39. instanceColors[idx] = (Color){rand()%256, rand()%256, rand()%256, 255};
  40. idx++;
  41. }
  42. }
  43. // Setup camera
  44. Camera3D camDefault = {
  45. .position = {0, 2, 2},
  46. .target = {0, 0, 0},
  47. .up = {0, 1, 0},
  48. .fovy = 60
  49. };
  50. // Main loop
  51. while (!WindowShouldClose())
  52. {
  53. float delta = GetFrameTime();
  54. // Rotate camera
  55. Matrix rotation = MatrixRotate(camDefault.up, 0.1f * delta);
  56. Vector3 view = Vector3Subtract(camDefault.position, camDefault.target);
  57. view = Vector3Transform(view, rotation);
  58. camDefault.position = Vector3Add(camDefault.target, view);
  59. // Adjust DoF based on mouse
  60. Vector2 mousePos = GetMousePosition();
  61. float focusPoint = 0.5f + (5.0f - (mousePos.y / GetScreenHeight()) * 5.0f);
  62. float focusScale = 0.5f + (5.0f - (mousePos.x / GetScreenWidth()) * 5.0f);
  63. R3D_ENVIRONMENT_SET(dof.focusPoint, focusPoint);
  64. R3D_ENVIRONMENT_SET(dof.focusScale, focusScale);
  65. float mouseWheel = GetMouseWheelMove();
  66. if (mouseWheel != 0.0f) {
  67. float maxBlur = R3D_ENVIRONMENT_GET(dof.maxBlurSize);
  68. R3D_ENVIRONMENT_SET(dof.maxBlurSize, maxBlur + mouseWheel * 0.1f);
  69. }
  70. if (IsKeyPressed(KEY_F1)) {
  71. R3D_ENVIRONMENT_SET(dof.debugMode, !R3D_ENVIRONMENT_GET(dof.debugMode));
  72. }
  73. BeginDrawing();
  74. ClearBackground(BLACK);
  75. // Render scene
  76. R3D_Begin(camDefault);
  77. R3D_DrawMeshInstancedEx(&meshSphere, &matDefault, instances, instanceColors, INSTANCE_COUNT);
  78. R3D_End();
  79. // Display DoF values
  80. char dofText[128];
  81. snprintf(dofText, sizeof(dofText), "Focus Point: %.2f\nFocus Scale: %.2f\nMax Blur Size: %.2f\nDebug Mode: %d",
  82. R3D_ENVIRONMENT_GET(dof.focusPoint), R3D_ENVIRONMENT_GET(dof.focusScale),
  83. R3D_ENVIRONMENT_GET(dof.maxBlurSize), R3D_ENVIRONMENT_GET(dof.debugMode));
  84. DrawText(dofText, 10, 30, 20, (Color) {255, 255, 255, 127});
  85. // Display instructions
  86. 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, (Color) {255, 255, 255, 127});
  87. // Display FPS
  88. char fpsText[32];
  89. snprintf(fpsText, sizeof(fpsText), "FPS: %d", GetFPS());
  90. DrawText(fpsText, 10, 10, 20, (Color) {255, 255, 255, 127});
  91. EndDrawing();
  92. }
  93. // Cleanup
  94. R3D_UnloadMesh(&meshSphere);
  95. R3D_Close();
  96. CloseWindow();
  97. return 0;
  98. }