*/}}

la_tns_als.c 25 KB

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