billboards.odin 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package billboards
  2. import rl "vendor:raylib"
  3. import r3d "../r3d"
  4. main :: proc() {
  5. // Initialize window
  6. rl.InitWindow(800, 450, "[r3d] - Billboards example")
  7. defer rl.CloseWindow()
  8. rl.SetTargetFPS(60)
  9. // Initialize R3D
  10. r3d.Init(rl.GetScreenWidth(), rl.GetScreenHeight())
  11. defer r3d.Close()
  12. r3d.SetTextureFilter(.POINT)
  13. // Set background/ambient color
  14. env := r3d.GetEnvironment()
  15. env.background.color = {102, 191, 255, 255}
  16. env.ambient.color = {10, 19, 25, 255}
  17. env.tonemap.mode = .FILMIC
  18. // Create ground mesh and material
  19. meshGround := r3d.GenMeshPlane(200, 200, 1, 1)
  20. defer r3d.UnloadMesh(meshGround)
  21. matGround := r3d.GetDefaultMaterial()
  22. matGround.albedo.color = rl.GREEN
  23. // Create billboard mesh and material
  24. meshBillboard := r3d.GenMeshQuad(1.0, 1.0, 1, 1, {0.0, 0.0, 1.0})
  25. defer r3d.UnloadMesh(meshBillboard)
  26. meshBillboard.shadowCastMode = .ON_DOUBLE_SIDED
  27. matBillboard := r3d.GetDefaultMaterial()
  28. defer r3d.UnloadMaterial(matBillboard)
  29. matBillboard.albedo = r3d.LoadAlbedoMap("./resources/images/tree.png", rl.WHITE)
  30. matBillboard.billboardMode = .Y_AXIS
  31. // Create transforms for instanced billboards
  32. instances := r3d.LoadInstanceBuffer(64, {.POSITION, .SCALE})
  33. positions := cast([^]rl.Vector3)r3d.MapInstances(instances, {.POSITION})
  34. scales := cast([^]rl.Vector3)r3d.MapInstances(instances, {.SCALE})
  35. for i in 0..<64 {
  36. scaleFactor := f32(rl.GetRandomValue(25, 50)) / 10.0
  37. scales[i] = {scaleFactor, scaleFactor, 1.0}
  38. positions[i] = {
  39. f32(rl.GetRandomValue(-100, 100)),
  40. scaleFactor * 0.5,
  41. f32(rl.GetRandomValue(-100, 100)),
  42. }
  43. }
  44. r3d.UnmapInstances(instances, {.POSITION, .SCALE})
  45. // Setup directional light with shadows
  46. light := r3d.CreateLight(.DIR)
  47. r3d.SetLightDirection(light, {-1, -1, -1})
  48. r3d.SetShadowDepthBias(light, 0.01)
  49. r3d.EnableShadow(light)
  50. r3d.SetLightActive(light, true)
  51. r3d.SetLightRange(light, 32.0)
  52. // Setup camera
  53. camera: rl.Camera3D = {
  54. position = {0, 5, 0},
  55. target = {0, 5, -1},
  56. up = {0, 1, 0},
  57. fovy = 60,
  58. }
  59. // Capture mouse
  60. rl.DisableCursor()
  61. // Main loop
  62. for !rl.WindowShouldClose()
  63. {
  64. rl.UpdateCamera(&camera, rl.CameraMode.FREE)
  65. rl.BeginDrawing()
  66. rl.ClearBackground(rl.RAYWHITE)
  67. r3d.Begin(camera)
  68. r3d.DrawMesh(meshGround, matGround, {0, 0, 0}, 1.0)
  69. r3d.DrawMeshInstanced(meshBillboard, matBillboard, instances, 64)
  70. r3d.End()
  71. rl.EndDrawing()
  72. }
  73. }