dof.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. // 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. float spacing = 0.5f;
  31. float offsetX = (X_INSTANCES * spacing) / 2.0f;
  32. float offsetZ = (Y_INSTANCES * spacing) / 2.0f;
  33. int idx = 0;
  34. R3D_InstanceBuffer instances = R3D_LoadInstanceBuffer(INSTANCE_COUNT, R3D_INSTANCE_POSITION | R3D_INSTANCE_COLOR);
  35. Vector3* positions = R3D_MapInstances(instances, R3D_INSTANCE_POSITION);
  36. Color* colors = R3D_MapInstances(instances, R3D_INSTANCE_COLOR);
  37. for (int x = 0; x < X_INSTANCES; x++) {
  38. for (int y = 0; y < Y_INSTANCES; y++) {
  39. positions[idx] = (Vector3) {x * spacing - offsetX, 0, y * spacing - offsetZ};
  40. colors[idx] = (Color){rand()%256, rand()%256, rand()%256, 255};
  41. idx++;
  42. }
  43. }
  44. R3D_UnmapInstances(instances, R3D_INSTANCE_POSITION | R3D_INSTANCE_COLOR);
  45. // Setup camera
  46. Camera3D camDefault = {
  47. .position = {0, 2, 2},
  48. .target = {0, 0, 0},
  49. .up = {0, 1, 0},
  50. .fovy = 60
  51. };
  52. // Main loop
  53. while (!WindowShouldClose())
  54. {
  55. float delta = GetFrameTime();
  56. // Rotate camera
  57. Matrix rotation = MatrixRotate(camDefault.up, 0.1f * delta);
  58. Vector3 view = Vector3Subtract(camDefault.position, camDefault.target);
  59. view = Vector3Transform(view, rotation);
  60. camDefault.position = Vector3Add(camDefault.target, view);
  61. // Adjust DoF based on mouse
  62. Vector2 mousePos = GetMousePosition();
  63. float focusPoint = 0.5f + (5.0f - (mousePos.y / GetScreenHeight()) * 5.0f);
  64. float focusScale = 0.5f + (5.0f - (mousePos.x / GetScreenWidth()) * 5.0f);
  65. R3D_ENVIRONMENT_SET(dof.focusPoint, focusPoint);
  66. R3D_ENVIRONMENT_SET(dof.focusScale, focusScale);
  67. float mouseWheel = GetMouseWheelMove();
  68. if (mouseWheel != 0.0f) {
  69. float maxBlur = R3D_ENVIRONMENT_GET(dof.maxBlurSize);
  70. R3D_ENVIRONMENT_SET(dof.maxBlurSize, maxBlur + mouseWheel * 0.1f);
  71. }
  72. if (IsKeyPressed(KEY_F1)) {
  73. R3D_SetOutputMode(R3D_GetOutputMode() == R3D_OUTPUT_SCENE ? R3D_OUTPUT_DOF : R3D_OUTPUT_SCENE);
  74. }
  75. BeginDrawing();
  76. ClearBackground(BLACK);
  77. // Render scene
  78. R3D_Begin(camDefault);
  79. R3D_DrawMeshInstanced(meshSphere, matDefault, instances, INSTANCE_COUNT);
  80. R3D_End();
  81. // Display DoF values
  82. char dofText[128];
  83. snprintf(dofText, sizeof(dofText), "Focus Point: %.2f\nFocus Scale: %.2f\nMax Blur Size: %.2f\nDebug Mode: %d",
  84. R3D_ENVIRONMENT_GET(dof.focusPoint), R3D_ENVIRONMENT_GET(dof.focusScale),
  85. R3D_ENVIRONMENT_GET(dof.maxBlurSize), (R3D_GetOutputMode() == R3D_OUTPUT_SCENE));
  86. DrawText(dofText, 10, 30, 20, (Color) {255, 255, 255, 127});
  87. // Display instructions
  88. 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});
  89. // Display FPS
  90. char fpsText[32];
  91. snprintf(fpsText, sizeof(fpsText), "FPS: %d", GetFPS());
  92. DrawText(fpsText, 10, 10, 20, (Color) {255, 255, 255, 127});
  93. EndDrawing();
  94. }
  95. // Cleanup
  96. R3D_UnloadInstanceBuffer(instances);
  97. R3D_UnloadMesh(meshSphere);
  98. R3D_Close();
  99. CloseWindow();
  100. return 0;
  101. }