r3d_ambient_map.odin 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* r3d_ambient_map.odin -- R3D Ambient Map Module.
  2. *
  3. * Copyright (c) 2025-2026 Le Juez Victor
  4. *
  5. * This software is provided 'as-is', without any express or implied warranty.
  6. * For conditions of distribution and use, see accompanying LICENSE file.
  7. */
  8. package r3d
  9. import rl "vendor:raylib"
  10. when ODIN_OS == .Windows {
  11. foreign import lib {
  12. "windows/libr3d.a",
  13. "system:raylib",
  14. "system:assimp",
  15. }
  16. } else when ODIN_OS == .Linux {
  17. foreign import lib {
  18. "linux/libr3d.a",
  19. "system:raylib",
  20. "system:assimp",
  21. }
  22. } else when ODIN_OS == .Darwin {
  23. foreign import lib {
  24. "darwin/libr3d.a",
  25. "system:raylib",
  26. "system:assimp",
  27. }
  28. }
  29. /**
  30. * @brief Global environment lighting data.
  31. *
  32. * An ambient map is built from a cubemap (like a skybox)
  33. * and preprocessed into two specialized textures:
  34. *
  35. * - irradiance:
  36. * Low-frequency lighting used for diffuse IBL.
  37. * Captures soft ambient light from all directions.
  38. *
  39. * - prefilter:
  40. * Mipmapped environment used for specular reflections.
  41. * Higher mip levels simulate rougher surfaces.
  42. *
  43. * Both textures are derived from the same source cubemap,
  44. * but serve different shading purposes.
  45. */
  46. AmbientMap :: struct {
  47. flags: AmbientFlags, ///< Components generated for this map
  48. irradiance: u32, ///< Diffuse IBL cubemap (may be 0 if not generated)
  49. prefilter: u32, ///< Specular prefiltered cubemap (may be 0 if not generated)
  50. }
  51. @(default_calling_convention="c", link_prefix="R3D_")
  52. foreign lib {
  53. /**
  54. * @brief Loads a ambient map from an image file.
  55. *
  56. * The layout parameter tells how faces are arranged inside the source image.
  57. */
  58. LoadAmbientMap :: proc(fileName: cstring, layout: CubemapLayout, flags: AmbientFlags) -> AmbientMap ---
  59. /**
  60. * @brief Builds a ambient map from an existing rl.Image.
  61. *
  62. * Same behavior as R3D_LoadAmbientMap(), but without loading from disk.
  63. */
  64. LoadAmbientMapFromImage :: proc(image: rl.Image, layout: CubemapLayout, flags: AmbientFlags) -> AmbientMap ---
  65. /**
  66. * @brief Generates an ambient map from a cubemap.
  67. *
  68. * The source cubemap should usually be an HDR sky/environment.
  69. *
  70. * Depending on the provided flags, this function:
  71. * - convolves the cubemap into diffuse irradiance
  72. * - builds a mipmapped prefiltered cubemap for reflections
  73. *
  74. * @param cubemap Source cubemap (environment / sky).
  75. * @param flags Which components to generate (irradiance, reflection, or both).
  76. * @return A fully initialized ambient map.
  77. */
  78. GenAmbientMap :: proc(cubemap: Cubemap, flags: AmbientFlags) -> AmbientMap ---
  79. /**
  80. * @brief Frees the textures used by an ambient map.
  81. *
  82. * After this call, the ambient map is no longer valid.
  83. */
  84. UnloadAmbientMap :: proc(ambientMap: AmbientMap) ---
  85. /**
  86. * @brief Rebuilds an existing ambient map from a new cubemap.
  87. *
  88. * Use this when the environment changes dynamically (time of day,
  89. * weather, interior/exterior transitions, etc).
  90. *
  91. * Only the components enabled in `ambientMap.flags` are regenerated.
  92. *
  93. * @param ambientMap Existing ambient map to update.
  94. * @param cubemap New cubemap source.
  95. */
  96. UpdateAmbientMap :: proc(ambientMap: AmbientMap, cubemap: Cubemap) ---
  97. }
  98. /**
  99. * @brief Bit-flags controlling what components are generated.
  100. *
  101. * - R3D_AMBIENT_ILLUMINATION -> generate diffuse irradiance
  102. * - R3D_AMBIENT_REFLECTION -> generate specular prefiltered map
  103. */
  104. AmbientFlag :: enum u32 {
  105. ILLUMINATION = 0,
  106. REFLECTION = 1,
  107. }
  108. AmbientFlags :: bit_set[AmbientFlag; u32]