dof.c 4.4 KB

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