*/}}

la_tns_als.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554
  1. /*
  2. * LaGUI: A graphical application framework.
  3. * Copyright (C) 2022 Wu Yiming
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation, either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include "la_5.h"
  19. const int TRANSMITTANCE_TEXTURE_WIDTH = 256;
  20. const int TRANSMITTANCE_TEXTURE_HEIGHT = 64;
  21. const int SCATTERING_TEXTURE_R_SIZE = 32;
  22. const int SCATTERING_TEXTURE_MU_SIZE = 128;
  23. const int SCATTERING_TEXTURE_MU_S_SIZE = 32;
  24. const int SCATTERING_TEXTURE_NU_SIZE = 8;
  25. const int SCATTERING_TEXTURE_WIDTH =
  26. SCATTERING_TEXTURE_NU_SIZE * SCATTERING_TEXTURE_MU_S_SIZE;
  27. const int SCATTERING_TEXTURE_HEIGHT = SCATTERING_TEXTURE_MU_SIZE;
  28. const int SCATTERING_TEXTURE_DEPTH = SCATTERING_TEXTURE_R_SIZE;
  29. const int IRRADIANCE_TEXTURE_WIDTH = 64;
  30. const int IRRADIANCE_TEXTURE_HEIGHT = 16;
  31. // The conversion factor between watts and lumens.
  32. const double MAX_LUMINOUS_EFFICACY = 683.0;
  33. // Values from "CIE (1931) 2-deg color matching functions", see
  34. // "http://web.archive.org/web/20081228084047/
  35. // http://www.cvrl.org/database/data/cmfs/ciexyz31.txt".
  36. const double CIE_2_DEG_COLOR_MATCHING_FUNCTIONS[380] = {
  37. 360, 0.000129900000, 0.000003917000, 0.000606100000,
  38. 365, 0.000232100000, 0.000006965000, 0.001086000000,
  39. 370, 0.000414900000, 0.000012390000, 0.001946000000,
  40. 375, 0.000741600000, 0.000022020000, 0.003486000000,
  41. 380, 0.001368000000, 0.000039000000, 0.006450001000,
  42. 385, 0.002236000000, 0.000064000000, 0.010549990000,
  43. 390, 0.004243000000, 0.000120000000, 0.020050010000,
  44. 395, 0.007650000000, 0.000217000000, 0.036210000000,
  45. 400, 0.014310000000, 0.000396000000, 0.067850010000,
  46. 405, 0.023190000000, 0.000640000000, 0.110200000000,
  47. 410, 0.043510000000, 0.001210000000, 0.207400000000,
  48. 415, 0.077630000000, 0.002180000000, 0.371300000000,
  49. 420, 0.134380000000, 0.004000000000, 0.645600000000,
  50. 425, 0.214770000000, 0.007300000000, 1.039050100000,
  51. 430, 0.283900000000, 0.011600000000, 1.385600000000,
  52. 435, 0.328500000000, 0.016840000000, 1.622960000000,
  53. 440, 0.348280000000, 0.023000000000, 1.747060000000,
  54. 445, 0.348060000000, 0.029800000000, 1.782600000000,
  55. 450, 0.336200000000, 0.038000000000, 1.772110000000,
  56. 455, 0.318700000000, 0.048000000000, 1.744100000000,
  57. 460, 0.290800000000, 0.060000000000, 1.669200000000,
  58. 465, 0.251100000000, 0.073900000000, 1.528100000000,
  59. 470, 0.195360000000, 0.090980000000, 1.287640000000,
  60. 475, 0.142100000000, 0.112600000000, 1.041900000000,
  61. 480, 0.095640000000, 0.139020000000, 0.812950100000,
  62. 485, 0.057950010000, 0.169300000000, 0.616200000000,
  63. 490, 0.032010000000, 0.208020000000, 0.465180000000,
  64. 495, 0.014700000000, 0.258600000000, 0.353300000000,
  65. 500, 0.004900000000, 0.323000000000, 0.272000000000,
  66. 505, 0.002400000000, 0.407300000000, 0.212300000000,
  67. 510, 0.009300000000, 0.503000000000, 0.158200000000,
  68. 515, 0.029100000000, 0.608200000000, 0.111700000000,
  69. 520, 0.063270000000, 0.710000000000, 0.078249990000,
  70. 525, 0.109600000000, 0.793200000000, 0.057250010000,
  71. 530, 0.165500000000, 0.862000000000, 0.042160000000,
  72. 535, 0.225749900000, 0.914850100000, 0.029840000000,
  73. 540, 0.290400000000, 0.954000000000, 0.020300000000,
  74. 545, 0.359700000000, 0.980300000000, 0.013400000000,
  75. 550, 0.433449900000, 0.994950100000, 0.008749999000,
  76. 555, 0.512050100000, 1.000000000000, 0.005749999000,
  77. 560, 0.594500000000, 0.995000000000, 0.003900000000,
  78. 565, 0.678400000000, 0.978600000000, 0.002749999000,
  79. 570, 0.762100000000, 0.952000000000, 0.002100000000,
  80. 575, 0.842500000000, 0.915400000000, 0.001800000000,
  81. 580, 0.916300000000, 0.870000000000, 0.001650001000,
  82. 585, 0.978600000000, 0.816300000000, 0.001400000000,
  83. 590, 1.026300000000, 0.757000000000, 0.001100000000,
  84. 595, 1.056700000000, 0.694900000000, 0.001000000000,
  85. 600, 1.062200000000, 0.631000000000, 0.000800000000,
  86. 605, 1.045600000000, 0.566800000000, 0.000600000000,
  87. 610, 1.002600000000, 0.503000000000, 0.000340000000,
  88. 615, 0.938400000000, 0.441200000000, 0.000240000000,
  89. 620, 0.854449900000, 0.381000000000, 0.000190000000,
  90. 625, 0.751400000000, 0.321000000000, 0.000100000000,
  91. 630, 0.642400000000, 0.265000000000, 0.000049999990,
  92. 635, 0.541900000000, 0.217000000000, 0.000030000000,
  93. 640, 0.447900000000, 0.175000000000, 0.000020000000,
  94. 645, 0.360800000000, 0.138200000000, 0.000010000000,
  95. 650, 0.283500000000, 0.107000000000, 0.000000000000,
  96. 655, 0.218700000000, 0.081600000000, 0.000000000000,
  97. 660, 0.164900000000, 0.061000000000, 0.000000000000,
  98. 665, 0.121200000000, 0.044580000000, 0.000000000000,
  99. 670, 0.087400000000, 0.032000000000, 0.000000000000,
  100. 675, 0.063600000000, 0.023200000000, 0.000000000000,
  101. 680, 0.046770000000, 0.017000000000, 0.000000000000,
  102. 685, 0.032900000000, 0.011920000000, 0.000000000000,
  103. 690, 0.022700000000, 0.008210000000, 0.000000000000,
  104. 695, 0.015840000000, 0.005723000000, 0.000000000000,
  105. 700, 0.011359160000, 0.004102000000, 0.000000000000,
  106. 705, 0.008110916000, 0.002929000000, 0.000000000000,
  107. 710, 0.005790346000, 0.002091000000, 0.000000000000,
  108. 715, 0.004109457000, 0.001484000000, 0.000000000000,
  109. 720, 0.002899327000, 0.001047000000, 0.000000000000,
  110. 725, 0.002049190000, 0.000740000000, 0.000000000000,
  111. 730, 0.001439971000, 0.000520000000, 0.000000000000,
  112. 735, 0.000999949300, 0.000361100000, 0.000000000000,
  113. 740, 0.000690078600, 0.000249200000, 0.000000000000,
  114. 745, 0.000476021300, 0.000171900000, 0.000000000000,
  115. 750, 0.000332301100, 0.000120000000, 0.000000000000,
  116. 755, 0.000234826100, 0.000084800000, 0.000000000000,
  117. 760, 0.000166150500, 0.000060000000, 0.000000000000,
  118. 765, 0.000117413000, 0.000042400000, 0.000000000000,
  119. 770, 0.000083075270, 0.000030000000, 0.000000000000,
  120. 775, 0.000058706520, 0.000021200000, 0.000000000000,
  121. 780, 0.000041509940, 0.000014990000, 0.000000000000,
  122. 785, 0.000029353260, 0.000010600000, 0.000000000000,
  123. 790, 0.000020673830, 0.000007465700, 0.000000000000,
  124. 795, 0.000014559770, 0.000005257800, 0.000000000000,
  125. 800, 0.000010253980, 0.000003702900, 0.000000000000,
  126. 805, 0.000007221456, 0.000002607800, 0.000000000000,
  127. 810, 0.000005085868, 0.000001836600, 0.000000000000,
  128. 815, 0.000003581652, 0.000001293400, 0.000000000000,
  129. 820, 0.000002522525, 0.000000910930, 0.000000000000,
  130. 825, 0.000001776509, 0.000000641530, 0.000000000000,
  131. 830, 0.000001251141, 0.000000451810, 0.000000000000,
  132. };
  133. // The conversion matrix from XYZ to linear sRGB color spaces.
  134. // Values from https://en.wikipedia.org/wiki/SRGB.
  135. const double XYZ_TO_SRGB[9] = {
  136. +3.2406, -1.5372, -0.4986,
  137. -0.9689, +1.8758, +0.0415,
  138. +0.0557, -0.2040, +1.0570
  139. };
  140. const char kVertexShader[] = "\n\
  141. #version 330\n\
  142. layout(location = 0) in vec2 vertex;\n\
  143. void main() {\n\
  144. gl_Position = vec4(vertex, 0.0, 1.0);\n\
  145. }";
  146. const char kGeometryShader[] = "\n\
  147. #version 330\n\
  148. layout(triangles) in;\n\
  149. layout(triangle_strip, max_vertices = 3) out;\n\
  150. uniform int layer;\n\
  151. void main() {\n\
  152. gl_Position = gl_in[0].gl_Position;\n\
  153. gl_Layer = layer;\n\
  154. EmitVertex();\n\
  155. gl_Position = gl_in[1].gl_Position;\n\
  156. gl_Layer = layer;\n\
  157. EmitVertex();\n\
  158. gl_Position = gl_in[2].gl_Position;\n\
  159. gl_Layer = layer;\n\
  160. EmitVertex();\n\
  161. EndPrimitive();\n\
  162. }";
  163. const char kComputeTransmittanceShader[] = "\n\
  164. layout(location = 0) out vec3 transmittance;\n\
  165. void main() {\n\
  166. transmittance = ComputeTransmittanceToTopAtmosphereBoundaryTexture(\n\
  167. ATMOSPHERE, gl_FragCoord.xy);\n\
  168. }";
  169. const char kComputeDirectIrradianceShader[] = "\n\
  170. layout(location = 0) out vec3 delta_irradiance;\n\
  171. layout(location = 1) out vec3 irradiance;\n\
  172. uniform sampler2D transmittance_texture;\n\
  173. void main() {\n\
  174. delta_irradiance = ComputeDirectIrradianceTexture(\n\
  175. ATMOSPHERE, transmittance_texture, gl_FragCoord.xy);\n\
  176. irradiance = vec3(0.0);\n\
  177. }";
  178. const char kComputeSingleScatteringShader[] = "\n\
  179. layout(location = 0) out vec3 delta_rayleigh;\n\
  180. layout(location = 1) out vec3 delta_mie;\n\
  181. layout(location = 2) out vec4 scattering;\n\
  182. layout(location = 3) out vec3 single_mie_scattering;\n\
  183. uniform mat3 luminance_from_radiance;\n\
  184. uniform sampler2D transmittance_texture;\n\
  185. uniform int layer;\n\
  186. void main() {\n\
  187. ComputeSingleScatteringTexture(\n\
  188. ATMOSPHERE, transmittance_texture, vec3(gl_FragCoord.xy, layer + 0.5),\n\
  189. delta_rayleigh, delta_mie);\n\
  190. scattering = vec4(luminance_from_radiance * delta_rayleigh.rgb,\n\
  191. (luminance_from_radiance * delta_mie).r);\n\
  192. single_mie_scattering = luminance_from_radiance * delta_mie;\n\
  193. }";
  194. const char kComputeScatteringDensityShader[] = "\n\
  195. layout(location = 0) out vec3 scattering_density;\n\
  196. uniform sampler2D transmittance_texture;\n\
  197. uniform sampler3D single_rayleigh_scattering_texture;\n\
  198. uniform sampler3D single_mie_scattering_texture;\n\
  199. uniform sampler3D multiple_scattering_texture;\n\
  200. uniform sampler2D irradiance_texture;\n\
  201. uniform int scattering_order;\n\
  202. uniform int layer;\n\
  203. void main() {\n\
  204. scattering_density = ComputeScatteringDensityTexture(\n\
  205. ATMOSPHERE, transmittance_texture, single_rayleigh_scattering_texture,\n\
  206. single_mie_scattering_texture, multiple_scattering_texture,\n\
  207. irradiance_texture, vec3(gl_FragCoord.xy, layer + 0.5),\n\
  208. scattering_order);\n\
  209. }";
  210. const char kComputeIndirectIrradianceShader[] = "\n\
  211. layout(location = 0) out vec3 delta_irradiance;\n\
  212. layout(location = 1) out vec3 irradiance;\n\
  213. uniform mat3 luminance_from_radiance;\n\
  214. uniform sampler3D single_rayleigh_scattering_texture;\n\
  215. uniform sampler3D single_mie_scattering_texture;\n\
  216. uniform sampler3D multiple_scattering_texture;\n\
  217. uniform int scattering_order;\n\
  218. void main() {\n\
  219. delta_irradiance = ComputeIndirectIrradianceTexture(\n\
  220. ATMOSPHERE, single_rayleigh_scattering_texture,\n\
  221. single_mie_scattering_texture, multiple_scattering_texture,\n\
  222. gl_FragCoord.xy, scattering_order);\n\
  223. irradiance = luminance_from_radiance * delta_irradiance;\n\
  224. }";
  225. const char kComputeMultipleScatteringShader[] = "\n\
  226. layout(location = 0) out vec3 delta_multiple_scattering;\n\
  227. layout(location = 1) out vec4 scattering;\n\
  228. uniform mat3 luminance_from_radiance;\n\
  229. uniform sampler2D transmittance_texture;\n\
  230. uniform sampler3D scattering_density_texture;\n\
  231. uniform int layer;\n\
  232. void main() {\n\
  233. float nu;\n\
  234. delta_multiple_scattering = ComputeMultipleScatteringTexture(\n\
  235. ATMOSPHERE, transmittance_texture, scattering_density_texture,\n\
  236. vec3(gl_FragCoord.xy, layer + 0.5), nu);\n\
  237. scattering = vec4(\n\
  238. luminance_from_radiance *\n\
  239. delta_multiple_scattering.rgb / RayleighPhaseFunction(nu),\n\
  240. 0.0);\n\
  241. }";
  242. const char kAtmosphereShader[] = "\n\
  243. uniform sampler2D transmittance_texture;\n\
  244. uniform sampler3D scattering_texture;\n\
  245. uniform sampler3D single_mie_scattering_texture;\n\
  246. uniform sampler2D irradiance_texture;\n\
  247. #ifdef RADIANCE_API_ENABLED\n\
  248. RadianceSpectrum GetSolarRadiance() {\n\
  249. return ATMOSPHERE.solar_irradiance /\n\
  250. (PI * ATMOSPHERE.sun_angular_radius * ATMOSPHERE.sun_angular_radius);\n\
  251. }\n\
  252. RadianceSpectrum GetSkyRadiance(\n\
  253. Position camera, Direction view_ray, Length shadow_length,\n\
  254. Direction sun_direction, out DimensionlessSpectrum transmittance) {\n\
  255. return GetSkyRadiance(ATMOSPHERE, transmittance_texture,\n\
  256. scattering_texture, single_mie_scattering_texture,\n\
  257. camera, view_ray, shadow_length, sun_direction, transmittance);\n\
  258. }\n\
  259. RadianceSpectrum GetSkyRadianceToPoint(\n\
  260. Position camera, Position point, Length shadow_length,\n\
  261. Direction sun_direction, out DimensionlessSpectrum transmittance) {\n\
  262. return GetSkyRadianceToPoint(ATMOSPHERE, transmittance_texture,\n\
  263. scattering_texture, single_mie_scattering_texture,\n\
  264. camera, point, shadow_length, sun_direction, transmittance);\n\
  265. }\n\
  266. IrradianceSpectrum GetSunAndSkyIrradiance(\n\
  267. Position p, Direction normal, Direction sun_direction,\n\
  268. out IrradianceSpectrum sky_irradiance) {\n\
  269. return GetSunAndSkyIrradiance(ATMOSPHERE, transmittance_texture,\n\
  270. irradiance_texture, p, normal, sun_direction, sky_irradiance);\n\
  271. }\n\
  272. #endif\n\
  273. Luminance3 GetSolarLuminance() {\n\
  274. return ATMOSPHERE.solar_irradiance /\n\
  275. (PI * ATMOSPHERE.sun_angular_radius * ATMOSPHERE.sun_angular_radius) *\n\
  276. SUN_SPECTRAL_RADIANCE_TO_LUMINANCE;\n\
  277. }\n\
  278. Luminance3 GetSkyLuminance(\n\
  279. Position camera, Direction view_ray, Length shadow_length,\n\
  280. Direction sun_direction, out DimensionlessSpectrum transmittance) {\n\
  281. return GetSkyRadiance(ATMOSPHERE, transmittance_texture,\n\
  282. scattering_texture, single_mie_scattering_texture,\n\
  283. camera, view_ray, shadow_length, sun_direction, transmittance) *\n\
  284. SKY_SPECTRAL_RADIANCE_TO_LUMINANCE;\n\
  285. }\n\
  286. Luminance3 GetSkyLuminanceToPoint(\n\
  287. Position camera, Position point, Length shadow_length,\n\
  288. Direction sun_direction, out DimensionlessSpectrum transmittance) {\n\
  289. return GetSkyRadianceToPoint(ATMOSPHERE, transmittance_texture,\n\
  290. scattering_texture, single_mie_scattering_texture,\n\
  291. camera, point, shadow_length, sun_direction, transmittance) *\n\
  292. SKY_SPECTRAL_RADIANCE_TO_LUMINANCE;\n\
  293. }\n\
  294. Illuminance3 GetSunAndSkyIlluminance(\n\
  295. Position p, Direction normal, Direction sun_direction,\n\
  296. out IrradianceSpectrum sky_irradiance) {\n\
  297. IrradianceSpectrum sun_irradiance = GetSunAndSkyIrradiance(\n\
  298. ATMOSPHERE, transmittance_texture, irradiance_texture, p, normal,\n\
  299. sun_direction, sky_irradiance);\n\
  300. sky_irradiance *= SKY_SPECTRAL_RADIANCE_TO_LUMINANCE;\n\
  301. return sun_irradiance * SUN_SPECTRAL_RADIANCE_TO_LUMINANCE;\n\
  302. }";
  303. const char header[]="#version 330\n\
  304. #define IN(x) const in x\n\
  305. #define OUT(x) out x\n\
  306. #define TEMPLATE(x)\n\
  307. #define TEMPLATE_ARGUMENT(x)\n\
  308. #define assert(x)\n\
  309. const int TRANSMITTANCE_TEXTURE_WIDTH = 256;\n\
  310. const int TRANSMITTANCE_TEXTURE_HEIGHT = 64;\n\
  311. const int SCATTERING_TEXTURE_R_SIZE = 32;\n\
  312. const int SCATTERING_TEXTURE_MU_SIZE = 128;\n\
  313. const int SCATTERING_TEXTURE_MU_S_SIZE = 32;\n\
  314. const int SCATTERING_TEXTURE_NU_SIZE = 8;\n\
  315. const int IRRADIANCE_TEXTURE_WIDTH = 64;\n\
  316. const int IRRADIANCE_TEXTURE_HEIGHT = 16;\n\
  317. #define COMBINED_SCATTERING_TEXTURES\n\
  318. \n\
  319. #define Length float\n\
  320. #define Wavelength float\n\
  321. #define Angle float\n\
  322. #define SolidAngle float\n\
  323. #define Power float\n\
  324. #define LuminousPower float\n\
  325. #define Number float\n\
  326. #define InverseLength float\n\
  327. #define Area float\n\
  328. #define Volume float\n\
  329. #define NumberDensity float\n\
  330. #define Irradiance float\n\
  331. #define Radiance float\n\
  332. #define SpectralPower float\n\
  333. #define SpectralIrradiance float\n\
  334. #define SpectralRadiance float\n\
  335. #define SpectralRadianceDensity float\n\
  336. #define ScatteringCoefficient float\n\
  337. #define InverseSolidAngle float\n\
  338. #define LuminousIntensity float\n\
  339. #define Luminance float\n\
  340. #define Illuminance float\n\
  341. #define AbstractSpectrum vec3\n\
  342. #define DimensionlessSpectrum vec3\n\
  343. #define PowerSpectrum vec3\n\
  344. #define IrradianceSpectrum vec3\n\
  345. #define RadianceSpectrum vec3\n\
  346. #define RadianceDensitySpectrum vec3\n\
  347. #define ScatteringSpectrum vec3\n\
  348. #define Position vec3\n\
  349. #define Direction vec3\n\
  350. #define Luminance3 vec3\n\
  351. #define Illuminance3 vec3\n\
  352. #define TransmittanceTexture sampler2D\n\
  353. #define AbstractScatteringTexture sampler3D\n\
  354. #define ReducedScatteringTexture sampler3D\n\
  355. #define ScatteringTexture sampler3D\n\
  356. #define ScatteringDensityTexture sampler3D\n\
  357. #define IrradianceTexture sampler2D\n\
  358. const Length m = 1.0;\n\
  359. const Wavelength nm = 1.0;\n\
  360. const Angle rad = 1.0;\n\
  361. const SolidAngle sr = 1.0;\n\
  362. const Power watt = 1.0;\n\
  363. const LuminousPower lm = 1.0;\n\
  364. const float PI = 3.14159265358979323846;\n\
  365. const Length km = 1000.0 * m;\n\
  366. const Area m2 = m * m;\n\
  367. const Volume m3 = m * m * m;\n\
  368. const Angle pi = PI * rad;\n\
  369. const Angle deg = pi / 180.0;\n\
  370. const Irradiance watt_per_square_meter = watt / m2;\n\
  371. const Radiance watt_per_square_meter_per_sr = watt / (m2 * sr);\n\
  372. const SpectralIrradiance watt_per_square_meter_per_nm = watt / (m2 * nm);\n\
  373. const SpectralRadiance watt_per_square_meter_per_sr_per_nm =\n\
  374. watt / (m2 * sr * nm);\n\
  375. const SpectralRadianceDensity watt_per_cubic_meter_per_sr_per_nm =\n\
  376. watt / (m3 * sr * nm);\n\
  377. const LuminousIntensity cd = lm / sr;\n\
  378. const LuminousIntensity kcd = 1000.0 * cd;\n\
  379. const Luminance cd_per_square_meter = cd / m2;\n\
  380. const Luminance kcd_per_square_meter = kcd / m2;\n\
  381. struct DensityProfileLayer {\n\
  382. Length width;\n\
  383. Number exp_term;\n\
  384. InverseLength exp_scale;\n\
  385. InverseLength linear_term;\n\
  386. Number constant_term;\n\
  387. };\n\
  388. struct DensityProfile {\n\
  389. DensityProfileLayer layers[2];\n\
  390. };\n\
  391. struct AtmosphereParameters {\n\
  392. IrradianceSpectrum solar_irradiance;\n\
  393. Angle sun_angular_radius;\n\
  394. Length bottom_radius;\n\
  395. Length top_radius;\n\
  396. DensityProfile rayleigh_density;\n\
  397. ScatteringSpectrum rayleigh_scattering;\n\
  398. DensityProfile mie_density;\n\
  399. ScatteringSpectrum mie_scattering;\n\
  400. ScatteringSpectrum mie_extinction;\n\
  401. Number mie_phase_function_g;\n\
  402. DensityProfile absorption_density;\n\
  403. ScatteringSpectrum absorption_extinction;\n\
  404. DimensionlessSpectrum ground_albedo;\n\
  405. Number mu_s_min;\n\
  406. };\n\
  407. const AtmosphereParameters ATMOSPHERE = AtmosphereParameters(\n\
  408. vec3(1.474000,1.850400,1.911980),\n\
  409. 0.004675,\n\
  410. 6360.000000,\n\
  411. 6420.000000,\n\
  412. DensityProfile(DensityProfileLayer[2](DensityProfileLayer(0.000000,0.000000,0.000000,0.000000,0.000000),DensityProfileLayer(0.000000,1.000000,-0.125000,0.000000,0.000000))),\n\
  413. vec3(0.005802,0.013558,0.033100),\n\
  414. DensityProfile(DensityProfileLayer[2](DensityProfileLayer(0.000000,0.000000,0.000000,0.000000,0.000000),DensityProfileLayer(0.000000,1.000000,-0.833333,0.000000,0.000000))),\n\
  415. vec3(0.003996,0.003996,0.003996),\n\
  416. vec3(0.004440,0.004440,0.004440),\n\
  417. 0.800000,\n\
  418. DensityProfile(DensityProfileLayer[2](DensityProfileLayer(25.000000,0.000000,0.000000,0.066667,-0.666667),DensityProfileLayer(0.000000,0.000000,0.000000,-0.066667,2.666667))),\n\
  419. vec3(0.000650,0.001881,0.000085),\n\
  420. vec3(0.100000,0.100000,0.100000),\n\
  421. -0.207912);\n\
  422. const vec3 SKY_SPECTRAL_RADIANCE_TO_LUMINANCE = vec3(683.000000,683.000000,683.000000);\n\
  423. const vec3 SUN_SPECTRAL_RADIANCE_TO_LUMINANCE = vec3(98242.786222,69954.398112,66475.012354);\n\
  424. ";
  425. void tns_InitAtmosphere(){
  426. laSafeString* ss=0;
  427. tnsTexture* transmittance_texture_ = tnsCreate2DTexture(GL_RGBA32F, TRANSMITTANCE_TEXTURE_WIDTH, TRANSMITTANCE_TEXTURE_HEIGHT, 0);
  428. tnsTexture* scattering_texture_ = tnsCreate3DTexture(GL_RGBA16F, SCATTERING_TEXTURE_WIDTH, SCATTERING_TEXTURE_HEIGHT,SCATTERING_TEXTURE_DEPTH);
  429. tnsTexture* irradiance_texture_ = tnsCreate2DTexture(GL_RGBA32F, IRRADIANCE_TEXTURE_WIDTH, IRRADIANCE_TEXTURE_HEIGHT, 0);
  430. tnsTexture* delta_irradiance_texture = tnsCreate2DTexture(GL_RGBA32F, IRRADIANCE_TEXTURE_WIDTH, IRRADIANCE_TEXTURE_HEIGHT, 0);
  431. tnsTexture* delta_rayleigh_scattering_texture = tnsCreate3DTexture(GL_RGBA16F, SCATTERING_TEXTURE_WIDTH, SCATTERING_TEXTURE_HEIGHT,SCATTERING_TEXTURE_DEPTH);
  432. tnsTexture* delta_mie_scattering_texture = tnsCreate3DTexture(GL_RGBA16F, SCATTERING_TEXTURE_WIDTH, SCATTERING_TEXTURE_HEIGHT,SCATTERING_TEXTURE_DEPTH);
  433. tnsTexture* delta_scattering_density_texture =tnsCreate3DTexture(GL_RGBA16F, SCATTERING_TEXTURE_WIDTH, SCATTERING_TEXTURE_HEIGHT,SCATTERING_TEXTURE_DEPTH);
  434. tnsTexture* delta_multiple_scattering_texture = delta_rayleigh_scattering_texture;
  435. GLuint fbo;
  436. glGenFramebuffers(1, &fbo);
  437. glBindFramebuffer(GL_FRAMEBUFFER, fbo);
  438. // The actual precomputations depend on whether we want to store precomputed
  439. // irradiance or illuminance values.
  440. //if (num_precomputed_wavelengths_ <= 3) {
  441. // vec3 lambdas{kLambdaR, kLambdaG, kLambdaB};
  442. // mat3 luminance_from_radiance{1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 1.0};
  443. // Precompute(fbo, delta_irradiance_texture, delta_rayleigh_scattering_texture,
  444. // delta_mie_scattering_texture, delta_scattering_density_texture,
  445. // delta_multiple_scattering_texture, lambdas, luminance_from_radiance,
  446. // false /* blend */, num_scattering_orders);
  447. //} else {
  448. // constexpr double kLambdaMin = 360.0;
  449. // constexpr double kLambdaMax = 830.0;
  450. // int num_iterations = (num_precomputed_wavelengths_ + 2) / 3;
  451. // double dlambda = (kLambdaMax - kLambdaMin) / (3 * num_iterations);
  452. // for (int i = 0; i < num_iterations; ++i) {
  453. // vec3 lambdas{
  454. // kLambdaMin + (3 * i + 0.5) * dlambda,
  455. // kLambdaMin + (3 * i + 1.5) * dlambda,
  456. // kLambdaMin + (3 * i + 2.5) * dlambda
  457. // };
  458. // auto coeff = [dlambda](double lambda, int component) {
  459. // // Note that we don't include MAX_LUMINOUS_EFFICACY here, to avoid
  460. // // artefacts due to too large values when using half precision on GPU.
  461. // // We add this term back in kAtmosphereShader, via
  462. // // SKY_SPECTRAL_RADIANCE_TO_LUMINANCE (see also the comments in the
  463. // // Model constructor).
  464. // double x = CieColorMatchingFunctionTableValue(lambda, 1);
  465. // double y = CieColorMatchingFunctionTableValue(lambda, 2);
  466. // double z = CieColorMatchingFunctionTableValue(lambda, 3);
  467. // return static_cast<float>((
  468. // XYZ_TO_SRGB[component * 3] * x +
  469. // XYZ_TO_SRGB[component * 3 + 1] * y +
  470. // XYZ_TO_SRGB[component * 3 + 2] * z) * dlambda);
  471. // };
  472. // mat3 luminance_from_radiance{
  473. // coeff(lambdas[0], 0), coeff(lambdas[1], 0), coeff(lambdas[2], 0),
  474. // coeff(lambdas[0], 1), coeff(lambdas[1], 1), coeff(lambdas[2], 1),
  475. // coeff(lambdas[0], 2), coeff(lambdas[1], 2), coeff(lambdas[2], 2)
  476. // };
  477. // Precompute(fbo, delta_irradiance_texture,
  478. // delta_rayleigh_scattering_texture, delta_mie_scattering_texture,
  479. // delta_scattering_density_texture, delta_multiple_scattering_texture,
  480. // lambdas, luminance_from_radiance, i > 0 /* blend */,
  481. // num_scattering_orders);
  482. // }
  483. //
  484. // // After the above iterations, the transmittance texture contains the
  485. // // transmittance for the 3 wavelengths used at the last iteration. But we
  486. // // want the transmittance at kLambdaR, kLambdaG, kLambdaB instead, so we
  487. // // must recompute it here for these 3 wavelengths:
  488. // std::string header = glsl_header_factory_({kLambdaR, kLambdaG, kLambdaB});
  489. // Program compute_transmittance(
  490. // kVertexShader, header + kComputeTransmittanceShader);
  491. // glFramebufferTexture(
  492. // GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, transmittance_texture_, 0);
  493. // glDrawBuffer(GL_COLOR_ATTACHMENT0);
  494. // glViewport(0, 0, TRANSMITTANCE_TEXTURE_WIDTH, TRANSMITTANCE_TEXTURE_HEIGHT);
  495. // compute_transmittance.Use();
  496. // DrawQuad({}, full_screen_quad_vao_);
  497. //}
  498. // Delete the temporary resources allocated at the begining of this method.
  499. glUseProgram(0);
  500. glBindFramebuffer(GL_FRAMEBUFFER, 0);
  501. glDeleteFramebuffers(1, &fbo);
  502. glDeleteTextures(1, &delta_scattering_density_texture);
  503. glDeleteTextures(1, &delta_mie_scattering_texture);
  504. glDeleteTextures(1, &delta_rayleigh_scattering_texture);
  505. glDeleteTextures(1, &delta_irradiance_texture);
  506. assert(glGetError() == 0);
  507. strSafeDestroy(&ss); strSafePrint(&ss,"%s%s",header,kComputeTransmittanceShader);
  508. tnsShader* compute_transmittance=tnsNewShaderProgram(tnsNewVertexShader(kVertexShader),tnsNewFragmentShader(ss->Ptr),-1);
  509. strSafeDestroy(&ss); strSafePrint(&ss,"%s%s",header,kComputeDirectIrradianceShader);
  510. tnsShader* compute_direct_irradiance=tnsNewShaderProgram(tnsNewVertexShader(kVertexShader),tnsNewFragmentShader(ss->Ptr),-1);
  511. strSafeDestroy(&ss); strSafePrint(&ss,"%s%s",header,kComputeSingleScatteringShader);
  512. tnsShader* compute_single_scattering=tnsNewShaderProgram(tnsNewVertexShader(kVertexShader),tnsNewFragmentShader(ss->Ptr),tnsNewGeometryShader(kGeometryShader));
  513. strSafeDestroy(&ss); strSafePrint(&ss,"%s%s",header,kComputeScatteringDensityShader);
  514. tnsShader* compute_scattering_density=tnsNewShaderProgram(tnsNewVertexShader(kVertexShader),tnsNewFragmentShader(ss->Ptr),tnsNewGeometryShader(kGeometryShader));
  515. strSafeDestroy(&ss); strSafePrint(&ss,"%s%s",header,kComputeIndirectIrradianceShader);
  516. tnsShader* compute_indirect_irradiance=tnsNewShaderProgram(tnsNewVertexShader(kVertexShader),tnsNewFragmentShader(ss->Ptr),-1);
  517. strSafeDestroy(&ss); strSafePrint(&ss,"%s%s",header,kComputeMultipleScatteringShader);
  518. tnsShader* compute_multiple_scattering=tnsNewShaderProgram(tnsNewVertexShader(kVertexShader),tnsNewFragmentShader(ss->Ptr),tnsNewGeometryShader(kGeometryShader));
  519. const GLuint kDrawBuffers[4] = {
  520. GL_COLOR_ATTACHMENT0,
  521. GL_COLOR_ATTACHMENT1,
  522. GL_COLOR_ATTACHMENT2,
  523. GL_COLOR_ATTACHMENT3
  524. };
  525. glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);
  526. glBlendFuncSeparate(GL_ONE, GL_ONE, GL_ONE, GL_ONE);;
  527. }