util_sky_model.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  1. /*
  2. This source is published under the following 3-clause BSD license.
  3. Copyright (c) 2012 - 2013, Lukas Hosek and Alexander Wilkie
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without
  6. modification, are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright
  8. notice, this list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright
  10. notice, this list of conditions and the following disclaimer in the
  11. documentation and/or other materials provided with the distribution.
  12. * None of the names of the contributors may be used to endorse or promote
  13. products derived from this software without specific prior written
  14. permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  22. ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. /* ============================================================================
  27. This file is part of a sample implementation of the analytical skylight and
  28. solar radiance models presented in the SIGGRAPH 2012 paper
  29. "An Analytic Model for Full Spectral Sky-Dome Radiance"
  30. and the 2013 IEEE CG&A paper
  31. "Adding a Solar Radiance Function to the Hosek Skylight Model"
  32. both by
  33. Lukas Hosek and Alexander Wilkie
  34. Charles University in Prague, Czech Republic
  35. Version: 1.4a, February 22nd, 2013
  36. Version history:
  37. 1.4a February 22nd, 2013
  38. Removed unnecessary and counter-intuitive solar radius parameters
  39. from the interface of the colourspace sky dome initialisation functions.
  40. 1.4 February 11th, 2013
  41. Fixed a bug which caused the relative brightness of the solar disc
  42. and the sky dome to be off by a factor of about 6. The sun was too
  43. bright: this affected both normal and alien sun scenarios. The
  44. coefficients of the solar radiance function were changed to fix this.
  45. 1.3 January 21st, 2013 (not released to the public)
  46. Added support for solar discs that are not exactly the same size as
  47. the terrestrial sun. Also added support for suns with a different
  48. emission spectrum ("Alien World" functionality).
  49. 1.2a December 18th, 2012
  50. Fixed a mistake and some inaccuracies in the solar radiance function
  51. explanations found in ArHosekSkyModel.h. The actual source code is
  52. unchanged compared to version 1.2.
  53. 1.2 December 17th, 2012
  54. Native RGB data and a solar radiance function that matches the turbidity
  55. conditions were added.
  56. 1.1 September 2012
  57. The coefficients of the spectral model are now scaled so that the output
  58. is given in physical units: W / (m^-2 * sr * nm). Also, the output of the
  59. XYZ model is now no longer scaled to the range [0...1]. Instead, it is
  60. the result of a simple conversion from spectral data via the CIE 2 degree
  61. standard observer matching functions. Therefore, after multiplication
  62. with 683 lm / W, the Y channel now corresponds to luminance in lm.
  63. 1.0 May 11th, 2012
  64. Initial release.
  65. Please visit http://cgg.mff.cuni.cz/projects/SkylightModelling/ to check if
  66. an updated version of this code has been published!
  67. ============================================================================ */
  68. /*
  69. This code is taken from ART, a rendering research system written in a
  70. mix of C99 / Objective C. Since ART is not a small system and is intended to
  71. be inter-operable with other libraries, and since C does not have namespaces,
  72. the structures and functions in ART all have to have somewhat wordy
  73. canonical names that begin with Ar.../ar..., like those seen in this example.
  74. Usage information:
  75. ==================
  76. Model initialisation
  77. --------------------
  78. A separate ArHosekSkyModelState has to be maintained for each spectral
  79. band you want to use the model for. So in a renderer with 'num_channels'
  80. bands, you would need something like
  81. ArHosekSkyModelState * skymodel_state[num_channels];
  82. You then have to allocate and initialise these states. In the following code
  83. snippet, we assume that 'albedo' is defined as
  84. double albedo[num_channels];
  85. with a ground albedo value between [0,1] for each channel. The solar elevation
  86. is given in radians.
  87. for ( unsigned int i = 0; i < num_channels; i++ )
  88. skymodel_state[i] =
  89. arhosekskymodelstate_alloc_init(
  90. turbidity,
  91. albedo[i],
  92. solarElevation
  93. );
  94. Note that starting with version 1.3, there is also a second initialisation
  95. function which generates skydome states for different solar emission spectra
  96. and solar radii: 'arhosekskymodelstate_alienworld_alloc_init()'.
  97. See the notes about the "Alien World" functionality provided further down for a
  98. discussion of the usefulness and limits of that second initalisation function.
  99. Sky model states that have been initialized with either function behave in a
  100. completely identical fashion during use and cleanup.
  101. Using the model to generate skydome samples
  102. -------------------------------------------
  103. Generating a skydome radiance spectrum "skydome_result" for a given location
  104. on the skydome determined via the angles theta and gamma works as follows:
  105. double skydome_result[num_channels];
  106. for ( unsigned int i = 0; i < num_channels; i++ )
  107. skydome_result[i] =
  108. arhosekskymodel_radiance(
  109. skymodel_state[i],
  110. theta,
  111. gamma,
  112. channel_center[i]
  113. );
  114. The variable "channel_center" is assumed to hold the channel center wavelengths
  115. for each of the num_channels samples of the spectrum we are building.
  116. Cleanup after use
  117. -----------------
  118. After rendering is complete, the content of the sky model states should be
  119. disposed of via
  120. for ( unsigned int i = 0; i < num_channels; i++ )
  121. arhosekskymodelstate_free( skymodel_state[i] );
  122. CIE XYZ Version of the Model
  123. ----------------------------
  124. Usage of the CIE XYZ version of the model is exactly the same, except that
  125. num_channels is of course always 3, and that ArHosekTristimSkyModelState and
  126. arhosek_tristim_skymodel_radiance() have to be used instead of their spectral
  127. counterparts.
  128. RGB Version of the Model
  129. ------------------------
  130. The RGB version uses sRGB primaries with a linear gamma ramp. The same set of
  131. functions as with the XYZ data is used, except the model is initialized
  132. by calling arhosek_rgb_skymodelstate_alloc_init.
  133. Solar Radiance Function
  134. -----------------------
  135. For each position on the solar disc, this function returns the entire radiance
  136. one sees - direct emission, as well as in-scattered light in the area of the
  137. solar disc. The latter is important for low solar elevations - nice images of
  138. the setting sun would not be possible without this. This is also the reason why
  139. this function, just like the regular sky dome model evaluation function, needs
  140. access to the sky dome data structures, as these provide information on
  141. in-scattered radiance.
  142. CAVEAT #1: in this release, this function is only provided in spectral form!
  143. RGB/XYZ versions to follow at a later date.
  144. CAVEAT #2: (fixed from release 1.3 onwards)
  145. CAVEAT #3: limb darkening renders the brightness of the solar disc
  146. inhomogeneous even for high solar elevations - only taking a single
  147. sample at the centre of the sun will yield an incorrect power
  148. estimate for the solar disc! Always take multiple random samples
  149. across the entire solar disc to estimate its power!
  150. CAVEAT #4: in this version, the limb darkening calculations still use a fairly
  151. computationally expensive 5th order polynomial that was directly
  152. taken from astronomical literature. For the purposes of Computer
  153. Graphics, this is needlessly accurate, though, and will be replaced
  154. by a cheaper approximation in a future release.
  155. "Alien World" functionality
  156. ---------------------------
  157. The Hosek sky model can be used to roughly (!) predict the appearance of
  158. outdoor scenes on earth-like planets, i.e. planets of a similar size and
  159. atmospheric make-up. Since the spectral version of our model predicts sky dome
  160. luminance patterns and solar radiance independently for each waveband, and
  161. since the intensity of each waveband is solely dependent on the input radiance
  162. from the star that the world in question is orbiting, it is trivial to re-scale
  163. the wavebands to match a different star radiance.
  164. At least in theory, the spectral version of the model has always been capable
  165. of this sort of thing, and the actual sky dome and solar radiance models were
  166. actually not altered at all in this release. All we did was to add some support
  167. functionality for doing this more easily with the existing data and functions,
  168. and to add some explanations.
  169. Just use 'arhosekskymodelstate_alienworld_alloc_init()' to initialise the sky
  170. model states (you will have to provide values for star temperature and solar
  171. intensity compared to the terrestrial sun), and do everything else as you
  172. did before.
  173. CAVEAT #1: we assume the emission of the star that illuminates the alien world
  174. to be a perfect blackbody emission spectrum. This is never entirely
  175. realistic - real star emission spectra are considerably more complex
  176. than this, mainly due to absorption effects in the outer layers of
  177. stars. However, blackbody spectra are a reasonable first assumption
  178. in a usage scenario like this, where 100% accuracy is simply not
  179. necessary: for rendering purposes, there are likely no visible
  180. differences between a highly accurate solution based on a more
  181. involved simulation, and this approximation.
  182. CAVEAT #2: we always use limb darkening data from our own sun to provide this
  183. "appearance feature", even for suns of strongly different
  184. temperature. Which is presumably not very realistic, but (as with
  185. the unaltered blackbody spectrum from caveat #1) probably not a bad
  186. first guess, either. If you need more accuracy than we provide here,
  187. please make inquiries with a friendly astro-physicst of your choice.
  188. CAVEAT #3: you have to provide a value for the solar intensity of the star
  189. which illuminates the alien world. For this, please bear in mind
  190. that there is very likely a comparatively tight range of absolute
  191. solar irradiance values for which an earth-like planet with an
  192. atmosphere like the one we assume in our model can exist in the
  193. first place!
  194. Too much irradiance, and the atmosphere probably boils off into
  195. space, too little, it freezes. Which means that stars of
  196. considerably different emission colour than our sun will have to be
  197. fairly different in size from it, to still provide a reasonable and
  198. inhabitable amount of irradiance. Red stars will need to be much
  199. larger than our sun, while white or blue stars will have to be
  200. comparatively tiny. The initialisation function handles this and
  201. computes a plausible solar radius for a given emission spectrum. In
  202. terms of absolute radiometric values, you should probably not stray
  203. all too far from a solar intensity value of 1.0.
  204. CAVEAT #4: although we now support different solar radii for the actual solar
  205. disc, the sky dome luminance patterns are *not* parameterised by
  206. this value - i.e. the patterns stay exactly the same for different
  207. solar radii! Which is of course not correct. But in our experience,
  208. solar discs up to several degrees in diameter (! - our own sun is
  209. half a degree across) do not cause the luminance patterns on the sky
  210. to change perceptibly. The reason we know this is that we initially
  211. used unrealistically large suns in our brute force path tracer, in
  212. order to improve convergence speeds (which in the beginning were
  213. abysmal). Later, we managed to do the reference renderings much
  214. faster even with realistically small suns, and found that there was
  215. no real difference in skydome appearance anyway.
  216. Conclusion: changing the solar radius should not be over-done, so
  217. close orbits around red supergiants are a no-no. But for the
  218. purposes of getting a fairly credible first impression of what an
  219. alien world with a reasonably sized sun would look like, what we are
  220. doing here is probably still o.k.
  221. HINT #1: if you want to model the sky of an earth-like planet that orbits
  222. a binary star, just super-impose two of these models with solar
  223. intensity of ~0.5 each, and closely spaced solar positions. Light is
  224. additive, after all. Tattooine, here we come... :-)
  225. P.S. according to Star Wars canon, Tattooine orbits a binary
  226. that is made up of a G and K class star, respectively.
  227. So ~5500K and ~4200K should be good first guesses for their
  228. temperature. Just in case you were wondering, after reading the
  229. previous paragraph.
  230. */
  231. CCL_NAMESPACE_BEGIN
  232. #ifndef _SKY_MODEL_H_
  233. # define _SKY_MODEL_H_
  234. typedef double ArHosekSkyModelConfiguration[9];
  235. // Spectral version of the model
  236. /* ----------------------------------------------------------------------------
  237. ArHosekSkyModelState struct
  238. ---------------------------
  239. This struct holds the pre-computation data for one particular albedo value.
  240. Most fields are self-explanatory, but users should never directly
  241. manipulate any of them anyway. The only consistent way to manipulate such
  242. structs is via the functions 'arhosekskymodelstate_alloc_init' and
  243. 'arhosekskymodelstate_free'.
  244. 'emission_correction_factor_sky'
  245. 'emission_correction_factor_sun'
  246. The original model coefficients were fitted against the emission of
  247. our local sun. If a different solar emission is desired (i.e. if the
  248. model is being used to predict skydome appearance for an earth-like
  249. planet that orbits a different star), these correction factors, which
  250. are determined during the alloc_init step, are applied to each waveband
  251. separately (they default to 1.0 in normal usage). This is the simplest
  252. way to retrofit this sort of capability to the existing model. The
  253. different factors for sky and sun are needed since the solar disc may
  254. be of a different size compared to the terrestrial sun.
  255. ---------------------------------------------------------------------------- */
  256. typedef struct ArHosekSkyModelState {
  257. ArHosekSkyModelConfiguration configs[11];
  258. double radiances[11];
  259. double turbidity;
  260. double solar_radius;
  261. double emission_correction_factor_sky[11];
  262. double emission_correction_factor_sun[11];
  263. double albedo;
  264. double elevation;
  265. } ArHosekSkyModelState;
  266. /* ----------------------------------------------------------------------------
  267. arhosekskymodelstate_alloc_init() function
  268. ------------------------------------------
  269. Initialises an ArHosekSkyModelState struct for a terrestrial setting.
  270. ---------------------------------------------------------------------------- */
  271. ArHosekSkyModelState *arhosekskymodelstate_alloc_init(const double solar_elevation,
  272. const double atmospheric_turbidity,
  273. const double ground_albedo);
  274. /* ----------------------------------------------------------------------------
  275. arhosekskymodelstate_alienworld_alloc_init() function
  276. -----------------------------------------------------
  277. Initialises an ArHosekSkyModelState struct for an "alien world" setting
  278. with a sun of a surface temperature given in 'kelvin'. The parameter
  279. 'solar_intensity' controls the overall brightness of the sky, relative
  280. to the solar irradiance on Earth. A value of 1.0 yields a sky dome that
  281. is, on average over the wavelenghts covered in the model (!), as bright
  282. as the terrestrial sky in radiometric terms.
  283. Which means that the solar radius has to be adjusted, since the
  284. emissivity of a solar surface with a given temperature is more or less
  285. fixed. So hotter suns have to be smaller to be equally bright as the
  286. terrestrial sun, while cooler suns have to be larger. Note that there are
  287. limits to the validity of the luminance patterns of the underlying model:
  288. see the discussion above for more on this. In particular, an alien sun with
  289. a surface temperature of only 2000 Kelvin has to be very large if it is
  290. to be as bright as the terrestrial sun - so large that the luminance
  291. patterns are no longer a really good fit in that case.
  292. If you need information about the solar radius that the model computes
  293. for a given temperature (say, for light source sampling purposes), you
  294. have to query the 'solar_radius' variable of the sky model state returned
  295. *after* running this function.
  296. ---------------------------------------------------------------------------- */
  297. ArHosekSkyModelState *arhosekskymodelstate_alienworld_alloc_init(
  298. const double solar_elevation,
  299. const double solar_intensity,
  300. const double solar_surface_temperature_kelvin,
  301. const double atmospheric_turbidity,
  302. const double ground_albedo);
  303. void arhosekskymodelstate_free(ArHosekSkyModelState *state);
  304. double arhosekskymodel_radiance(ArHosekSkyModelState *state,
  305. double theta,
  306. double gamma,
  307. double wavelength);
  308. // CIE XYZ and RGB versions
  309. ArHosekSkyModelState *arhosek_xyz_skymodelstate_alloc_init(const double turbidity,
  310. const double albedo,
  311. const double elevation);
  312. ArHosekSkyModelState *arhosek_rgb_skymodelstate_alloc_init(const double turbidity,
  313. const double albedo,
  314. const double elevation);
  315. double arhosek_tristim_skymodel_radiance(ArHosekSkyModelState *state,
  316. double theta,
  317. double gamma,
  318. int channel);
  319. // Delivers the complete function: sky + sun, including limb darkening.
  320. // Please read the above description before using this - there are several
  321. // caveats!
  322. double arhosekskymodel_solar_radiance(ArHosekSkyModelState *state,
  323. double theta,
  324. double gamma,
  325. double wavelength);
  326. #endif // _SKY_MODEL_H_
  327. CCL_NAMESPACE_END