r3d_core.odin 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /* r3d_core.odin -- R3D Core 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 Anti-aliasing modes for rendering.
  31. */
  32. AntiAliasing :: enum u32 {
  33. DISABLED = 0, ///< Anti-aliasing is disabled. Edges may appear jagged.
  34. FXAA = 1, ///< FXAA is applied. Smooths edges efficiently but may appear blurry.
  35. }
  36. /**
  37. * @brief Aspect ratio handling modes for rendering.
  38. */
  39. AspectMode :: enum u32 {
  40. EXPAND = 0, ///< Expands the rendered output to fully fill the target (render texture or window).
  41. KEEP = 1, ///< Preserves the target's aspect ratio without distortion, adding empty gaps if necessary.
  42. }
  43. /**
  44. * @brief Upscaling/filtering methods for rendering output.
  45. *
  46. * Upscale mode to apply when the output window is larger than the internal render resolution.
  47. */
  48. UpscaleMode :: enum u32 {
  49. NEAREST = 0, ///< Nearest-neighbor upscaling: very fast, but produces blocky pixels.
  50. LINEAR = 1, ///< Bilinear upscaling: very fast, smoother than nearest, but can appear blurry.
  51. BICUBIC = 2, ///< Bicubic (Catmull-Rom) upscaling: slower, smoother, and less blurry than linear.
  52. LANCZOS = 3, ///< Lanczos-2 upscaling: preserves more fine details, but is the most expensive.
  53. }
  54. /**
  55. * @brief Downscaling/filtering methods for rendering output.
  56. *
  57. * Downscale mode to apply when the output window is smaller than the internal render resolution.
  58. */
  59. DownscaleMode :: enum u32 {
  60. NEAREST = 0, ///< Nearest-neighbor downscaling: very fast, but produces aliasing.
  61. LINEAR = 1, ///< Bilinear downscaling: very fast, can serve as a basic form of anti-aliasing (SSAA).
  62. BOX = 2, ///< Box-blur downscaling: uses a simple but effective box blur, slightly more expensive than linear, smooths moiré better.
  63. }
  64. /**
  65. * @brief Defines the buffer to output (render texture or window).
  66. * @note Nothing will be output if the requested target has not been created / used.
  67. */
  68. OutputMode :: enum u32 {
  69. SCENE = 0,
  70. ALBEDO = 1,
  71. NORMAL = 2,
  72. ORM = 3,
  73. DIFFUSE = 4,
  74. SPECULAR = 5,
  75. SSAO = 6,
  76. SSIL = 7,
  77. SSR = 8,
  78. BLOOM = 9,
  79. DOF = 10,
  80. }
  81. /**
  82. * @brief Specifies the color space for user-provided colors and color textures.
  83. *
  84. * This enum defines how colors are interpreted for material inputs:
  85. * - Surface colors (e.g., albedo or emission tint)
  86. * - rl.Color textures (albedo, emission maps)
  87. *
  88. * Lighting values (direct or indirect light) are always linear and
  89. * are not affected by this setting.
  90. *
  91. * Used with `R3D_SetColorSpace()` to control whether input colors
  92. * should be treated as linear or sRGB.
  93. */
  94. ColorSpace :: enum u32 {
  95. LINEAR = 0, ///< Linear color space: values are used as-is.
  96. SRGB = 1, ///< sRGB color space: values are converted to linear on load.
  97. }
  98. @(default_calling_convention="c", link_prefix="R3D_")
  99. foreign lib {
  100. /**
  101. * @brief Initializes the rendering engine.
  102. *
  103. * This function sets up the internal rendering system with the provided resolution.
  104. *
  105. * @param resWidth Width of the internal resolution.
  106. * @param resHeight Height of the internal resolution.
  107. *
  108. * @return True if the initialization is successful.
  109. */
  110. Init :: proc(resWidth: i32, resHeight: i32) -> bool ---
  111. /**
  112. * @brief Closes the rendering engine and deallocates all resources.
  113. *
  114. * This function shuts down the rendering system and frees all allocated memory,
  115. * including the resources associated with the created lights.
  116. */
  117. Close :: proc() ---
  118. /**
  119. * @brief Gets the current internal resolution.
  120. *
  121. * This function retrieves the current internal resolution being used by the
  122. * rendering engine.
  123. *
  124. * @param width Pointer to store the width of the internal resolution.
  125. * @param height Pointer to store the height of the internal resolution.
  126. */
  127. GetResolution :: proc(width: ^i32, height: ^i32) ---
  128. /**
  129. * @brief Updates the internal resolution.
  130. *
  131. * This function changes the internal resolution of the rendering engine. Note that
  132. * this process destroys and recreates all framebuffers, which may be a slow operation.
  133. *
  134. * @param width The new width for the internal resolution.
  135. * @param height The new height for the internal resolution.
  136. *
  137. * @warning This function may be slow due to the destruction and recreation of framebuffers.
  138. */
  139. UpdateResolution :: proc(width: i32, height: i32) ---
  140. /**
  141. * @brief Retrieves the current anti-aliasing mode used for rendering.
  142. * @return The currently active R3D_AntiAliasing mode.
  143. */
  144. GetAntiAliasing :: proc() -> AntiAliasing ---
  145. /**
  146. * @brief Sets the anti-aliasing mode for rendering.
  147. * @param mode The desired R3D_AntiAliasing mode.
  148. */
  149. SetAntiAliasing :: proc(mode: AntiAliasing) ---
  150. /**
  151. * @brief Retrieves the current aspect ratio handling mode.
  152. * @return The currently active R3D_AspectMode.
  153. */
  154. GetAspectMode :: proc() -> AspectMode ---
  155. /**
  156. * @brief Sets the aspect ratio handling mode for rendering.
  157. * @param mode The desired R3D_AspectMode.
  158. */
  159. SetAspectMode :: proc(mode: AspectMode) ---
  160. /**
  161. * @brief Retrieves the current upscaling/filtering method.
  162. * @return The currently active R3D_UpscaleMode.
  163. */
  164. GetUpscaleMode :: proc() -> UpscaleMode ---
  165. /**
  166. * @brief Sets the upscaling/filtering method for rendering output.
  167. * @param mode The desired R3D_UpscaleMode.
  168. */
  169. SetUpscaleMode :: proc(mode: UpscaleMode) ---
  170. /**
  171. * @brief Retrieves the current downscaling mode used for rendering.
  172. * @return The currently active R3D_DownscaleMode.
  173. */
  174. GetDownscaleMode :: proc() -> DownscaleMode ---
  175. /**
  176. * @brief Sets the downscaling mode for rendering output.
  177. * @param mode The desired R3D_DownscaleMode.
  178. */
  179. SetDownscaleMode :: proc(mode: DownscaleMode) ---
  180. /**
  181. * @brief Gets the current output mode.
  182. * @return The currently active R3D_OutputMode.
  183. */
  184. GetOutputMode :: proc() -> OutputMode ---
  185. /**
  186. * @brief Sets the output mode for rendering.
  187. * @param mode The R3D_OutputMode to use.
  188. * @note Nothing will be output if the requested target has not been created / used.
  189. */
  190. SetOutputMode :: proc(mode: OutputMode) ---
  191. /**
  192. * @brief Sets the default texture filtering mode.
  193. *
  194. * This function defines the default texture filter that will be applied to all subsequently
  195. * loaded textures, including those used in materials, sprites, and other resources.
  196. *
  197. * If a trilinear or anisotropic filter is selected, mipmaps will be automatically generated
  198. * for the textures, but they will not be generated when using nearest or bilinear filtering.
  199. *
  200. * The default texture filter mode is `TEXTURE_FILTER_TRILINEAR`.
  201. *
  202. * @param filter The texture filtering mode to be applied by default.
  203. */
  204. SetTextureFilter :: proc(filter: rl.TextureFilter) ---
  205. /**
  206. * @brief Set the working color space for user-provided surface colors and color textures.
  207. *
  208. * Defines how all *color inputs* should be interpreted:
  209. * - surface colors provided in materials (e.g. albedo/emission tints)
  210. * - color textures such as albedo and emission maps
  211. *
  212. * When set to sRGB, these values are converted to linear before shading.
  213. * When set to linear, values are used as-is.
  214. *
  215. * This does NOT affect lighting inputs (direct or indirect light),
  216. * which are always expected to be provided in linear space.
  217. *
  218. * The default color space is `R3D_COLORSPACE_SRGB`.
  219. *
  220. * @param space rl.Color space to use for color inputs (linear or sRGB).
  221. */
  222. SetColorSpace :: proc(space: ColorSpace) ---
  223. /**
  224. * @brief Get the currently active global rendering layers.
  225. *
  226. * Returns the bitfield representing the currently active layers in the renderer.
  227. * By default, the internal active layers are set to 0, which means that any
  228. * non-zero layer assigned to an object will NOT be rendered unless explicitly
  229. * activated.
  230. *
  231. * @return R3D_Layer Bitfield of active layers.
  232. */
  233. GetActiveLayers :: proc() -> Layer ---
  234. /**
  235. * @brief Set the active global rendering layers.
  236. *
  237. * Replaces the current set of active layers with the given bitfield.
  238. *
  239. * @param bitfield Bitfield representing the layers to activate.
  240. */
  241. SetActiveLayers :: proc(bitfield: Layer) ---
  242. /**
  243. * @brief Enable one or more layers without affecting other active layers.
  244. *
  245. * This function sets the bits in the global active layers corresponding to
  246. * the bits in the provided bitfield. Layers already active remain active.
  247. *
  248. * @param bitfield Bitfield representing one or more layers to enable.
  249. */
  250. EnableLayers :: proc(bitfield: Layer) ---
  251. /**
  252. * @brief Disable one or more layers without affecting other active layers.
  253. *
  254. * This function clears the bits in the global active layers corresponding to
  255. * the bits in the provided bitfield. Layers not included in the bitfield
  256. * remain unchanged.
  257. *
  258. * @param bitfield Bitfield representing one or more layers to disable.
  259. */
  260. DisableLayers :: proc(bitfield: Layer) ---
  261. }
  262. /**
  263. * @brief Bitfield type used to specify rendering layers for 3D objects.
  264. *
  265. * This type is used by `R3D_Mesh` and `R3D_Sprite` objects to indicate
  266. * which rendering layer(s) they belong to. Active layers are controlled
  267. * globally via the functions:
  268. *
  269. * - void R3D_EnableLayers(R3D_Layer bitfield);
  270. * - void R3D_DisableLayers(R3D_Layer bitfield);
  271. *
  272. * A mesh or sprite will be rendered if at least one of its assigned layers is active.
  273. *
  274. * For simplicity, 16 layers are defined in this header, but the maximum number
  275. * of layers is 32 for an uint32_t.
  276. */
  277. Layer :: enum u32 {
  278. LAYER_01 = 1 << 0,
  279. LAYER_02 = 1 << 1,
  280. LAYER_03 = 1 << 2,
  281. LAYER_04 = 1 << 3,
  282. LAYER_05 = 1 << 4,
  283. LAYER_06 = 1 << 5,
  284. LAYER_07 = 1 << 6,
  285. LAYER_08 = 1 << 7,
  286. LAYER_09 = 1 << 8,
  287. LAYER_10 = 1 << 9,
  288. LAYER_11 = 1 << 10,
  289. LAYER_12 = 1 << 11,
  290. LAYER_13 = 1 << 12,
  291. LAYER_14 = 1 << 13,
  292. LAYER_15 = 1 << 14,
  293. LAYER_16 = 1 << 15,
  294. LAYER_ALL = 0xFFFFFFFF,
  295. }