dof.odin 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. package dof
  2. import rl "vendor:raylib"
  3. import "core:math/rand"
  4. import "core:fmt"
  5. import r3d "../r3d"
  6. X_INSTANCES :: 10
  7. Y_INSTANCES :: 10
  8. INSTANCE_COUNT :: X_INSTANCES * Y_INSTANCES
  9. main :: proc() {
  10. // Initialize window
  11. rl.InitWindow(800, 450, "[r3d] - DoF example")
  12. defer rl.CloseWindow()
  13. rl.SetTargetFPS(60)
  14. // Initialize R3D with FXAA
  15. r3d.Init(rl.GetScreenWidth(), rl.GetScreenHeight())
  16. defer r3d.Close()
  17. r3d.SetAntiAliasing(.FXAA)
  18. // Configure depth of field and background
  19. env := r3d.GetEnvironment()
  20. env.background.color = rl.BLACK
  21. env.dof.mode = .ENABLED
  22. env.dof.focusPoint = 2.0
  23. env.dof.focusScale = 3.0
  24. env.dof.maxBlurSize = 20.0
  25. // Create directional light
  26. light := r3d.CreateLight(.DIR)
  27. r3d.SetLightDirection(light, {0, -1, 0})
  28. r3d.SetLightActive(light, true)
  29. // Create sphere mesh and default material
  30. meshSphere := r3d.GenMeshSphere(0.2, 64, 64)
  31. defer r3d.UnloadMesh(meshSphere)
  32. matDefault := r3d.GetDefaultMaterial()
  33. // Generate instance matrices and colors
  34. spacing: f32 = 0.5
  35. offsetX := (X_INSTANCES * spacing) / 2.0
  36. offsetZ := (Y_INSTANCES * spacing) / 2.0
  37. idx := 0
  38. instances := r3d.LoadInstanceBuffer(INSTANCE_COUNT, {.POSITION, .COLOR})
  39. defer r3d.UnloadInstanceBuffer(instances)
  40. positions := cast([^]rl.Vector3)r3d.MapInstances(instances, {.POSITION})
  41. colors := cast([^]rl.Color)r3d.MapInstances(instances, {.COLOR})
  42. for x in 0..<X_INSTANCES {
  43. for y in 0..<Y_INSTANCES {
  44. positions[idx] = {f32(x) * spacing - offsetX, 0, f32(y) * spacing - offsetZ}
  45. colors[idx] = {u8(rand.uint32() % 256), u8(rand.uint32() % 256), u8(rand.uint32() % 256), 255}
  46. idx += 1
  47. }
  48. }
  49. r3d.UnmapInstances(instances, {.POSITION, .COLOR})
  50. // Setup camera
  51. camDefault: rl.Camera3D = {
  52. position = {0, 2, 2},
  53. target = {0, 0, 0},
  54. up = {0, 1, 0},
  55. fovy = 60,
  56. }
  57. // Main loop
  58. for !rl.WindowShouldClose()
  59. {
  60. delta := rl.GetFrameTime()
  61. // Rotate camera
  62. rotation := rl.MatrixRotate(camDefault.up, 0.1 * delta)
  63. view := camDefault.position - camDefault.target
  64. view = rl.Vector3Transform(view, rotation)
  65. camDefault.position = camDefault.target + view
  66. // Adjust DoF based on mouse
  67. mousePos := rl.GetMousePosition()
  68. focusPoint := 0.5 + (5.0 - (mousePos.y / f32(rl.GetScreenHeight())) * 5.0)
  69. focusScale := 0.5 + (5.0 - (mousePos.x / f32(rl.GetScreenWidth())) * 5.0)
  70. env := r3d.GetEnvironment()
  71. env.dof.focusPoint = focusPoint
  72. env.dof.focusScale = focusScale
  73. mouseWheel := rl.GetMouseWheelMove()
  74. if mouseWheel != 0.0 {
  75. env.dof.maxBlurSize = env.dof.maxBlurSize + mouseWheel * 0.1
  76. }
  77. if rl.IsKeyPressed(.F1) {
  78. if r3d.GetOutputMode() == .DOF {
  79. r3d.SetOutputMode(.SCENE)
  80. } else {
  81. r3d.SetOutputMode(.DOF)
  82. }
  83. }
  84. rl.BeginDrawing()
  85. rl.ClearBackground(rl.BLACK)
  86. // Render scene
  87. r3d.Begin(camDefault)
  88. r3d.DrawMeshInstanced(meshSphere, matDefault, instances, INSTANCE_COUNT)
  89. r3d.End()
  90. // Display DoF values
  91. dofText := fmt.ctprintf(
  92. "Focus Point: %.2f\nFocus Scale: %.2f\nMax Blur Size: %.2f\nDebug Mode: %v",
  93. env.dof.focusPoint, env.dof.focusScale,
  94. env.dof.maxBlurSize, r3d.GetOutputMode() == .DOF,
  95. )
  96. rl.DrawText(dofText, 10, 30, 20, {255, 255, 255, 127})
  97. // Display instructions
  98. rl.DrawText(
  99. "F1: Toggle Debug Mode\nScroll: Adjust Max Blur Size\nMouse Left/Right: Shallow/Deep DoF\nMouse Up/Down: Adjust Focus Point Depth",
  100. 300, 10, 20, {255, 255, 255, 127},
  101. )
  102. // Display FPS
  103. fpsText := fmt.ctprintf("FPS: %d", rl.GetFPS())
  104. rl.DrawText(fpsText, 10, 10, 20, {255, 255, 255, 127})
  105. rl.EndDrawing()
  106. }
  107. }