sun.odin 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. package sun
  2. import rl "vendor:raylib"
  3. import r3d "../r3d"
  4. X_INSTANCES :: 50
  5. Y_INSTANCES :: 50
  6. INSTANCE_COUNT :: X_INSTANCES * Y_INSTANCES
  7. main :: proc() {
  8. // Initialize window
  9. rl.InitWindow(800, 450, "[r3d] - Sun example")
  10. defer rl.CloseWindow()
  11. rl.SetTargetFPS(60)
  12. // Initialize R3D
  13. r3d.Init(rl.GetScreenWidth(), rl.GetScreenHeight())
  14. defer r3d.Close()
  15. r3d.SetAntiAliasing(.FXAA)
  16. // Create meshes and material
  17. plane := r3d.GenMeshPlane(1000, 1000, 1, 1)
  18. defer r3d.UnloadMesh(plane)
  19. sphere := r3d.GenMeshSphere(0.35, 16, 32)
  20. defer r3d.UnloadMesh(sphere)
  21. material := r3d.GetDefaultMaterial()
  22. defer r3d.UnloadMaterial(material)
  23. // Create transforms for instanced spheres
  24. instances := r3d.LoadInstanceBuffer(INSTANCE_COUNT, {.POSITION})
  25. defer r3d.UnloadInstanceBuffer(instances)
  26. positions := cast([^]rl.Vector3)r3d.MapInstances(instances, {.POSITION})
  27. spacing: f32 = 1.5
  28. offsetX := (X_INSTANCES * spacing) / 2.0
  29. offsetZ := (Y_INSTANCES * spacing) / 2.0
  30. idx := 0
  31. for x in 0..<X_INSTANCES {
  32. for y in 0..<Y_INSTANCES {
  33. positions[idx] = {f32(x) * spacing - offsetX, 0, f32(y) * spacing - offsetZ}
  34. idx += 1
  35. }
  36. }
  37. r3d.UnmapInstances(instances, {.POSITION})
  38. // Setup environment
  39. skybox := r3d.GenCubemapSky(1024, r3d.CUBEMAP_SKY_BASE)
  40. env := r3d.GetEnvironment()
  41. env.background.sky = skybox
  42. ambientMap := r3d.GenAmbientMap(skybox, {.ILLUMINATION, .REFLECTION})
  43. env.ambient._map = ambientMap
  44. // Create directional light with shadows
  45. light := r3d.CreateLight(.DIR)
  46. r3d.SetLightDirection(light, {-1, -1, -1})
  47. r3d.SetLightActive(light, true)
  48. r3d.SetLightRange(light, 16.0)
  49. r3d.SetShadowSoftness(light, 2.0)
  50. r3d.SetShadowDepthBias(light, 0.01)
  51. r3d.EnableShadow(light)
  52. // Setup camera
  53. camera: rl.Camera3D = {
  54. position = {0, 1, 0},
  55. target = {1, 1.25, 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(plane, material, {0, -0.5, 0}, 1.0)
  69. r3d.DrawMeshInstanced(sphere, material, instances, INSTANCE_COUNT)
  70. r3d.End()
  71. rl.EndDrawing()
  72. }
  73. }