1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
|
/*
This source is published under the following 3-clause BSD license.
Copyright (c) 2012 - 2013, Lukas Hosek and Alexander Wilkie
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
* None of the names of the contributors may be used to endorse or promote
products derived from this software without specific prior written
permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
// with slight modifications
// Copyright 2020 Intel Corporation
/* ============================================================================
This file is part of a sample implementation of the analytical skylight and
solar radiance models presented in the SIGGRAPH 2012 paper
"An Analytic Model for Full Spectral Sky-Dome Radiance"
and the 2013 IEEE CG&A paper
"Adding a Solar Radiance Function to the Hosek Skylight Model"
both by
Lukas Hosek and Alexander Wilkie
Charles University in Prague, Czech Republic
Version: 1.4a, February 22nd, 2013
Version history:
1.4a February 22nd, 2013
Removed unnecessary and counter-intuitive solar radius parameters
from the interface of the colourspace sky dome initialisation functions.
1.4 February 11th, 2013
Fixed a bug which caused the relative brightness of the solar disc
and the sky dome to be off by a factor of about 6. The sun was too
bright: this affected both normal and alien sun scenarios. The
coefficients of the solar radiance function were changed to fix this.
1.3 January 21st, 2013 (not released to the public)
Added support for solar discs that are not exactly the same size as
the terrestrial sun. Also added support for suns with a different
emission spectrum ("Alien World" functionality).
1.2a December 18th, 2012
Fixed a mistake and some inaccuracies in the solar radiance function
explanations found in ArHosekSkyModel.h. The actual source code is
unchanged compared to version 1.2.
1.2 December 17th, 2012
Native RGB data and a solar radiance function that matches the turbidity
conditions were added.
1.1 September 2012
The coefficients of the spectral model are now scaled so that the output
is given in physical units: W / (m^-2 * sr * nm). Also, the output of the
XYZ model is now no longer scaled to the range [0...1]. Instead, it is
the result of a simple conversion from spectral data via the CIE 2 degree
standard observer matching functions. Therefore, after multiplication
with 683 lm / W, the Y channel now corresponds to luminance in lm.
1.0 May 11th, 2012
Initial release.
Please visit http://cgg.mff.cuni.cz/projects/SkylightModelling/ to check if
an updated version of this code has been published!
============================================================================ */
/*
All instructions on how to use this code are in the accompanying header file.
*/
#include "sky_model.h"
#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include "sky_model_data_ciexyz.h"
#include "sky_model_data_rgb.h"
#include "sky_model_data_spectral.h"
// Some macro definitions that occur elsewhere in ART, and that have to be
// replicated to make this a stand-alone module.
#ifndef MATH_PI
#define MATH_PI 3.141592653589793
#endif
#ifndef MATH_DEG_TO_RAD
#define MATH_DEG_TO_RAD (MATH_PI / 180.0)
#endif
#ifndef DEGREES
#define DEGREES *MATH_DEG_TO_RAD
#endif
#ifndef TERRESTRIAL_SOLAR_RADIUS
#define TERRESTRIAL_SOLAR_RADIUS ((0.51 DEGREES) / 2.0)
#endif
#ifndef ALLOC
#define ALLOC(_struct) (new (_struct))
#endif
// internal definitions
typedef float *ArHosekSkyModel_Dataset;
typedef float *ArHosekSkyModel_Radiance_Dataset;
// internal functions
void ArHosekSkyModel_CookConfiguration(ArHosekSkyModel_Dataset dataset,
ArHosekSkyModelConfiguration config,
float turbidity,
float albedo,
float solar_elevation)
{
float *elev_matrix;
int int_turbidity = (int)turbidity;
float turbidity_rem = turbidity - (float)int_turbidity;
solar_elevation = pow(solar_elevation / (MATH_PI / 2.0), (1.0 / 3.0));
// alb 0 low turb
elev_matrix = dataset + (9 * 6 * (int_turbidity - 1));
for (unsigned int i = 0; i < 9; ++i) {
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
config[i] = (1.0 - albedo) * (1.0 - turbidity_rem)
* (pow(1.0 - solar_elevation, 5.0) * elev_matrix[i]
+ 5.0 * pow(1.0 - solar_elevation, 4.0) * solar_elevation
* elev_matrix[i + 9]
+ 10.0 * pow(1.0 - solar_elevation, 3.0) * pow(solar_elevation, 2.0)
* elev_matrix[i + 18]
+ 10.0 * pow(1.0 - solar_elevation, 2.0) * pow(solar_elevation, 3.0)
* elev_matrix[i + 27]
+ 5.0 * (1.0 - solar_elevation) * pow(solar_elevation, 4.0)
* elev_matrix[i + 36]
+ pow(solar_elevation, 5.0) * elev_matrix[i + 45]);
}
// alb 1 low turb
elev_matrix = dataset + (9 * 6 * 10 + 9 * 6 * (int_turbidity - 1));
for (unsigned int i = 0; i < 9; ++i) {
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
config[i] += (albedo) * (1.0 - turbidity_rem)
* (pow(1.0 - solar_elevation, 5.0) * elev_matrix[i]
+ 5.0 * pow(1.0 - solar_elevation, 4.0) * solar_elevation
* elev_matrix[i + 9]
+ 10.0 * pow(1.0 - solar_elevation, 3.0) * pow(solar_elevation, 2.0)
* elev_matrix[i + 18]
+ 10.0 * pow(1.0 - solar_elevation, 2.0) * pow(solar_elevation, 3.0)
* elev_matrix[i + 27]
+ 5.0 * (1.0 - solar_elevation) * pow(solar_elevation, 4.0)
* elev_matrix[i + 36]
+ pow(solar_elevation, 5.0) * elev_matrix[i + 45]);
}
if (int_turbidity == 10)
return;
// alb 0 high turb
elev_matrix = dataset + (9 * 6 * (int_turbidity));
for (unsigned int i = 0; i < 9; ++i) {
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
config[i] += (1.0 - albedo) * (turbidity_rem)
* (pow(1.0 - solar_elevation, 5.0) * elev_matrix[i]
+ 5.0 * pow(1.0 - solar_elevation, 4.0) * solar_elevation
* elev_matrix[i + 9]
+ 10.0 * pow(1.0 - solar_elevation, 3.0) * pow(solar_elevation, 2.0)
* elev_matrix[i + 18]
+ 10.0 * pow(1.0 - solar_elevation, 2.0) * pow(solar_elevation, 3.0)
* elev_matrix[i + 27]
+ 5.0 * (1.0 - solar_elevation) * pow(solar_elevation, 4.0)
* elev_matrix[i + 36]
+ pow(solar_elevation, 5.0) * elev_matrix[i + 45]);
}
// alb 1 high turb
elev_matrix = dataset + (9 * 6 * 10 + 9 * 6 * (int_turbidity));
for (unsigned int i = 0; i < 9; ++i) {
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
config[i] += (albedo) * (turbidity_rem)
* (pow(1.0 - solar_elevation, 5.0) * elev_matrix[i]
+ 5.0 * pow(1.0 - solar_elevation, 4.0) * solar_elevation
* elev_matrix[i + 9]
+ 10.0 * pow(1.0 - solar_elevation, 3.0) * pow(solar_elevation, 2.0)
* elev_matrix[i + 18]
+ 10.0 * pow(1.0 - solar_elevation, 2.0) * pow(solar_elevation, 3.0)
* elev_matrix[i + 27]
+ 5.0 * (1.0 - solar_elevation) * pow(solar_elevation, 4.0)
* elev_matrix[i + 36]
+ pow(solar_elevation, 5.0) * elev_matrix[i + 45]);
}
}
float ArHosekSkyModel_CookRadianceConfiguration(
ArHosekSkyModel_Radiance_Dataset dataset,
float turbidity,
float albedo,
float solar_elevation)
{
float *elev_matrix;
int int_turbidity = (int)turbidity;
float turbidity_rem = turbidity - (float)int_turbidity;
float res;
solar_elevation = pow(solar_elevation / (MATH_PI / 2.0), (1.0 / 3.0));
// alb 0 low turb
elev_matrix = dataset + (6 * (int_turbidity - 1));
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
res = (1.0 - albedo) * (1.0 - turbidity_rem)
* (pow(1.0 - solar_elevation, 5.0) * elev_matrix[0]
+ 5.0 * pow(1.0 - solar_elevation, 4.0) * solar_elevation
* elev_matrix[1]
+ 10.0 * pow(1.0 - solar_elevation, 3.0) * pow(solar_elevation, 2.0)
* elev_matrix[2]
+ 10.0 * pow(1.0 - solar_elevation, 2.0) * pow(solar_elevation, 3.0)
* elev_matrix[3]
+ 5.0 * (1.0 - solar_elevation) * pow(solar_elevation, 4.0)
* elev_matrix[4]
+ pow(solar_elevation, 5.0) * elev_matrix[5]);
// alb 1 low turb
elev_matrix = dataset + (6 * 10 + 6 * (int_turbidity - 1));
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
res += (albedo) * (1.0 - turbidity_rem)
* (pow(1.0 - solar_elevation, 5.0) * elev_matrix[0]
+ 5.0 * pow(1.0 - solar_elevation, 4.0) * solar_elevation
* elev_matrix[1]
+ 10.0 * pow(1.0 - solar_elevation, 3.0) * pow(solar_elevation, 2.0)
* elev_matrix[2]
+ 10.0 * pow(1.0 - solar_elevation, 2.0) * pow(solar_elevation, 3.0)
* elev_matrix[3]
+ 5.0 * (1.0 - solar_elevation) * pow(solar_elevation, 4.0)
* elev_matrix[4]
+ pow(solar_elevation, 5.0) * elev_matrix[5]);
if (int_turbidity == 10)
return res;
// alb 0 high turb
elev_matrix = dataset + (6 * (int_turbidity));
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
res += (1.0 - albedo) * (turbidity_rem)
* (pow(1.0 - solar_elevation, 5.0) * elev_matrix[0]
+ 5.0 * pow(1.0 - solar_elevation, 4.0) * solar_elevation
* elev_matrix[1]
+ 10.0 * pow(1.0 - solar_elevation, 3.0) * pow(solar_elevation, 2.0)
* elev_matrix[2]
+ 10.0 * pow(1.0 - solar_elevation, 2.0) * pow(solar_elevation, 3.0)
* elev_matrix[3]
+ 5.0 * (1.0 - solar_elevation) * pow(solar_elevation, 4.0)
* elev_matrix[4]
+ pow(solar_elevation, 5.0) * elev_matrix[5]);
// alb 1 high turb
elev_matrix = dataset + (6 * 10 + 6 * (int_turbidity));
//(1-t).^3* A1 + 3*(1-t).^2.*t * A2 + 3*(1-t) .* t .^ 2 * A3 + t.^3 * A4;
res += (albedo) * (turbidity_rem)
* (pow(1.0 - solar_elevation, 5.0) * elev_matrix[0]
+ 5.0 * pow(1.0 - solar_elevation, 4.0) * solar_elevation
* elev_matrix[1]
+ 10.0 * pow(1.0 - solar_elevation, 3.0) * pow(solar_elevation, 2.0)
* elev_matrix[2]
+ 10.0 * pow(1.0 - solar_elevation, 2.0) * pow(solar_elevation, 3.0)
* elev_matrix[3]
+ 5.0 * (1.0 - solar_elevation) * pow(solar_elevation, 4.0)
* elev_matrix[4]
+ pow(solar_elevation, 5.0) * elev_matrix[5]);
return res;
}
float ArHosekSkyModel_GetRadianceInternal(
ArHosekSkyModelConfiguration configuration, float theta, float gamma)
{
const float expM = exp(configuration[4] * gamma);
const float rayM = cos(gamma) * cos(gamma);
const float mieM = (1.0 + cos(gamma) * cos(gamma))
/ pow((1.0 + configuration[8] * configuration[8]
- 2.0 * configuration[8] * cos(gamma)),
1.5);
const float zenith = sqrt(cos(theta));
return (1.0 + configuration[0] * exp(configuration[1] / (cos(theta) + 0.01)))
* (configuration[2] + configuration[3] * expM + configuration[5] * rayM
+ configuration[6] * mieM + configuration[7] * zenith);
}
// spectral version
ArHosekSkyModelState *arhosekskymodelstate_alloc_init(
const float solar_elevation,
const float atmospheric_turbidity,
const float ground_albedo)
{
ArHosekSkyModelState *state = ALLOC(ArHosekSkyModelState);
state->solar_radius = (0.51 DEGREES) / 2.0;
state->turbidity = atmospheric_turbidity;
state->albedo = ground_albedo;
state->elevation = solar_elevation;
for (unsigned int wl = 0; wl < 11; ++wl) {
ArHosekSkyModel_CookConfiguration(datasets[wl],
state->configs[wl],
atmospheric_turbidity,
ground_albedo,
solar_elevation);
state->radiances[wl] = ArHosekSkyModel_CookRadianceConfiguration(
datasetsRad[wl], atmospheric_turbidity, ground_albedo, solar_elevation);
state->emission_correction_factor_sun[wl] = 1.0;
state->emission_correction_factor_sky[wl] = 1.0;
}
return state;
}
// 'blackbody_scaling_factor'
//
// Fudge factor, computed in Mathematica, to scale the results of the
// following function to match the solar radiance spectrum used in the
// original simulation. The scaling is done so their integrals over the
// range from 380.0 to 720.0 nanometers match for a blackbody temperature
// of 5800 K.
// Which leaves the original spectrum being less bright overall than the 5.8k
// blackbody radiation curve if the ultra-violet part of the spectrum is
// also considered. But the visible brightness should be very similar.
const float blackbody_scaling_factor = 3.19992 * 10E-11;
// 'art_blackbody_dd_value()' function
//
// Blackbody radiance, Planck's formula
float art_blackbody_dd_value(const float temperature, const float lambda)
{
float c1 = 3.74177 * 10E-17;
float c2 = 0.0143878;
float value;
value = (c1 / (pow(lambda, 5.0)))
* (1.0 / (exp(c2 / (lambda * temperature)) - 1.0));
return value;
}
// 'originalSolarRadianceTable[]'
//
// The solar spectrum incident at the top of the atmosphere, as it was used
// in the brute force path tracer that generated the reference results the
// model was fitted to. We need this as the yardstick to compare any altered
// Blackbody emission spectra for alien world stars to.
// This is just the data from the Preetham paper, extended into the UV range.
const float originalSolarRadianceTable[] = {7500.0,
12500.0,
21127.5,
26760.5,
30663.7,
27825.0,
25503.8,
25134.2,
23212.1,
21526.7,
19870.8};
ArHosekSkyModelState *arhosekskymodelstate_alienworld_alloc_init(
const float solar_elevation,
const float solar_intensity,
const float solar_surface_temperature_kelvin,
const float atmospheric_turbidity,
const float ground_albedo)
{
ArHosekSkyModelState *state = ALLOC(ArHosekSkyModelState);
state->turbidity = atmospheric_turbidity;
state->albedo = ground_albedo;
state->elevation = solar_elevation;
for (unsigned int wl = 0; wl < 11; ++wl) {
// Basic init as for the normal scenario
ArHosekSkyModel_CookConfiguration(datasets[wl],
state->configs[wl],
atmospheric_turbidity,
ground_albedo,
solar_elevation);
state->radiances[wl] = ArHosekSkyModel_CookRadianceConfiguration(
datasetsRad[wl], atmospheric_turbidity, ground_albedo, solar_elevation);
// The wavelength of this band in nanometers
float owl = (320.0 + 40.0 * wl) * 10E-10;
// The original intensity we just computed
float osr = originalSolarRadianceTable[wl];
// The intensity of a blackbody with the desired temperature
// The fudge factor described above is used to make sure the BB
// function matches the used radiance data reasonably well
// in magnitude.
float nsr = art_blackbody_dd_value(solar_surface_temperature_kelvin, owl)
* blackbody_scaling_factor;
// Correction factor for this waveband is simply the ratio of
// the two.
state->emission_correction_factor_sun[wl] = nsr / osr;
}
// We then compute the average correction factor of all wavebands.
// Theoretically, some weighting to favour wavelengths human vision is
// more sensitive to could be introduced here - think V(lambda). But
// given that the whole effort is not *that* accurate to begin with (we
// are talking about the appearance of alien worlds, after all), simple
// averaging over the visible wavelengths (! - this is why we start at
// WL #2, and only use 2-11) seems like a sane first approximation.
float correctionFactor = 0.0;
for (unsigned int i = 2; i < 11; i++) {
correctionFactor += state->emission_correction_factor_sun[i];
}
// This is the average ratio in emitted energy between our sun, and an
// equally large sun with the blackbody spectrum we requested.
// Division by 9 because we only used 9 of the 11 wavelengths for this
// (see above).
float ratio = correctionFactor / 9.0;
// This ratio is then used to determine the radius of the alien sun
// on the sky dome. The additional factor 'solar_intensity' can be used
// to make the alien sun brighter or dimmer compared to our sun.
state->solar_radius =
(sqrt(solar_intensity) * TERRESTRIAL_SOLAR_RADIUS) / sqrt(ratio);
// Finally, we have to reduce the scaling factor of the sky by the
// ratio used to scale the solar disc size. The rationale behind this is
// that the scaling factors apply to the new blackbody spectrum, which
// can be more or less bright than the one our sun emits. However, we
// just scaled the size of the alien solar disc so it is roughly as
// bright (in terms of energy emitted) as the terrestrial sun. So the sky
// dome has to be reduced in brightness appropriately - but not in an
// uniform fashion across wavebands. If we did that, the sky colour would
// be wrong.
for (unsigned int i = 0; i < 11; i++) {
state->emission_correction_factor_sky[i] =
solar_intensity * state->emission_correction_factor_sun[i] / ratio;
}
return state;
}
void arhosekskymodelstate_free(ArHosekSkyModelState *state)
{
delete state;
}
float arhosekskymodel_radiance(
ArHosekSkyModelState *state, float theta, float gamma, float wavelength)
{
int low_wl = (wavelength - 320.0) / 40.0;
if (low_wl < 0 || low_wl >= 11)
return 0.0f;
float interp = fmod((wavelength - 320.0) / 40.0, 1.0);
float val_low =
ArHosekSkyModel_GetRadianceInternal(state->configs[low_wl], theta, gamma)
* state->radiances[low_wl]
* state->emission_correction_factor_sky[low_wl];
if (interp < 1e-6)
return val_low;
float result = (1.0 - interp) * val_low;
if (low_wl + 1 < 11) {
result += interp
* ArHosekSkyModel_GetRadianceInternal(
state->configs[low_wl + 1], theta, gamma)
* state->radiances[low_wl + 1]
* state->emission_correction_factor_sky[low_wl + 1];
}
return result;
}
// xyz and rgb versions
ArHosekSkyModelState *arhosek_xyz_skymodelstate_alloc_init(
const float turbidity, const float albedo, const float elevation)
{
ArHosekSkyModelState *state = ALLOC(ArHosekSkyModelState);
state->solar_radius = TERRESTRIAL_SOLAR_RADIUS;
state->turbidity = turbidity;
state->albedo = albedo;
state->elevation = elevation;
for (unsigned int channel = 0; channel < 3; ++channel) {
ArHosekSkyModel_CookConfiguration(datasetsXYZ[channel],
state->configs[channel],
turbidity,
albedo,
elevation);
state->radiances[channel] = ArHosekSkyModel_CookRadianceConfiguration(
datasetsXYZRad[channel], turbidity, albedo, elevation);
}
return state;
}
ArHosekSkyModelState *arhosek_rgb_skymodelstate_alloc_init(
const float turbidity, const float albedo, const float elevation)
{
ArHosekSkyModelState *state = ALLOC(ArHosekSkyModelState);
state->solar_radius = TERRESTRIAL_SOLAR_RADIUS;
state->turbidity = turbidity;
state->albedo = albedo;
state->elevation = elevation;
for (unsigned int channel = 0; channel < 3; ++channel) {
ArHosekSkyModel_CookConfiguration(datasetsRGB[channel],
state->configs[channel],
turbidity,
albedo,
elevation);
state->radiances[channel] = ArHosekSkyModel_CookRadianceConfiguration(
datasetsRGBRad[channel], turbidity, albedo, elevation);
}
return state;
}
float arhosek_tristim_skymodel_radiance(
ArHosekSkyModelState *state, float theta, float gamma, int channel)
{
return ArHosekSkyModel_GetRadianceInternal(
state->configs[channel], theta, gamma)
* state->radiances[channel];
}
const int pieces = 45;
const int order = 4;
float arhosekskymodel_sr_internal(
ArHosekSkyModelState *state, int turbidity, int wl, float elevation)
{
int pos = (int)(pow(2.0 * elevation / MATH_PI, 1.0 / 3.0) * pieces); // floor
if (pos > 44)
pos = 44;
const float break_x =
pow(((float)pos / (float)pieces), 3.0) * (MATH_PI * 0.5);
const float *coefs =
solarDatasets[wl] + (order * pieces * turbidity + order * (pos + 1) - 1);
float res = 0.0;
const float x = elevation - break_x;
float x_exp = 1.0;
for (int i = 0; i < order; ++i) {
res += x_exp * *coefs--;
x_exp *= x;
}
return res * state->emission_correction_factor_sun[wl];
}
float arhosekskymodel_solar_radiance_internal2(ArHosekSkyModelState *state,
float wavelength,
float elevation,
float gamma)
{
assert(wavelength >= 320.0 && wavelength <= 720.0 && state->turbidity >= 1.0
&& state->turbidity <= 10.0);
int turb_low = (int)state->turbidity - 1;
float turb_frac = state->turbidity - (float)(turb_low + 1);
if (turb_low == 9) {
turb_low = 8;
turb_frac = 1.0;
}
int wl_low = (int)((wavelength - 320.0) / 40.0);
float wl_frac = fmod(wavelength, 40.0) / 40.0;
if (wl_low == 10) {
wl_low = 9;
wl_frac = 1.0;
}
float direct_radiance = (1.0 - turb_frac)
* ((1.0 - wl_frac)
* arhosekskymodel_sr_internal(
state, turb_low, wl_low, elevation)
+ wl_frac
* arhosekskymodel_sr_internal(
state, turb_low, wl_low + 1, elevation))
+ turb_frac
* ((1.0 - wl_frac)
* arhosekskymodel_sr_internal(
state, turb_low + 1, wl_low, elevation)
+ wl_frac
* arhosekskymodel_sr_internal(
state, turb_low + 1, wl_low + 1, elevation));
float ldCoefficient[6];
for (int i = 0; i < 6; i++)
ldCoefficient[i] = (1.0 - wl_frac) * limbDarkeningDatasets[wl_low][i]
+ wl_frac * limbDarkeningDatasets[wl_low + 1][i];
// sun distance to diameter ratio, squared
const float sol_rad_sin = sin(state->solar_radius);
const float ar2 = 1 / (sol_rad_sin * sol_rad_sin);
const float singamma = sin(gamma);
float sc2 = 1.0 - ar2 * singamma * singamma;
if (sc2 < 0.0)
sc2 = 0.0;
float sampleCosine = sqrt(sc2);
// The following will be improved in future versions of the model:
// here, we directly use fitted 5th order polynomials provided by the
// astronomical community for the limb darkening effect. Astronomers need
// such accurate fittings for their predictions. However, this sort of
// accuracy is not really needed for CG purposes, so an approximated
// dataset based on quadratic polynomials will be provided in a future
// release.
float darkeningFactor = ldCoefficient[0] + ldCoefficient[1] * sampleCosine
+ ldCoefficient[2] * pow(sampleCosine, 2.0)
+ ldCoefficient[3] * pow(sampleCosine, 3.0)
+ ldCoefficient[4] * pow(sampleCosine, 4.0)
+ ldCoefficient[5] * pow(sampleCosine, 5.0);
direct_radiance *= darkeningFactor;
return direct_radiance;
}
float arhosekskymodel_solar_radiance(
ArHosekSkyModelState *state, float theta, float gamma, float wavelength)
{
float direct_radiance = arhosekskymodel_solar_radiance_internal2(
state, wavelength, ((MATH_PI / 2.0) - theta), gamma);
float inscattered_radiance =
arhosekskymodel_radiance(state, theta, gamma, wavelength);
return direct_radiance + inscattered_radiance;
}
|