~ubuntu-branches/ubuntu/utopic/blender/utopic-proposed

« back to all changes in this revision

Viewing changes to source/blender/render/intern/source/sunsky.c

  • Committer: Package Import Robot
  • Author(s): Matteo F. Vescovi
  • Date: 2012-04-28 12:11:12 UTC
  • mto: (14.1.6 experimental) (1.5.1)
  • mto: This revision was merged to the branch mainline in revision 34.
  • Revision ID: package-import@ubuntu.com-20120428121112-2zi0vp8b6vejda8i
Tags: upstream-2.63
ImportĀ upstreamĀ versionĀ 2.63

Show diffs side-by-side

added added

removed removed

Lines of Context:
20
20
 
21
21
/** \file blender/render/intern/source/sunsky.c
22
22
 *  \ingroup render
 
23
 *
 
24
 * This feature comes from Preetham paper on "A Practical Analytic Model for Daylight"
 
25
 * and example code from Brian Smits, another author of that paper in
 
26
 * http://www.cs.utah.edu/vissim/papers/sunsky/code/
23
27
 */
24
28
 
25
 
 
26
 
 
27
29
#include "sunsky.h"
28
 
#include "math.h"
29
30
#include "BLI_math.h"
30
31
#include "BKE_global.h"
31
32
 
37
38
 * compute v1 = v2 op v3
38
39
 * v1, v2 and v3 are vectors contains 3 float
39
40
 * */
40
 
#define vec3opv(v1, v2, op, v3) \
41
 
        v1[0] = (v2[0] op v3[0]); \
42
 
        v1[1] = (v2[1] op v3[1]);\
43
 
        v1[2] = (v2[2] op v3[2]);
 
41
#define VEC3OPV(v1, v2, op, v3)                                               \
 
42
        {                                                                         \
 
43
                v1[0] = (v2[0] op v3[0]);                                             \
 
44
                v1[1] = (v2[1] op v3[1]);                                             \
 
45
                v1[2] = (v2[2] op v3[2]);                                             \
 
46
        } (void)0
44
47
 
45
48
/**
46
49
 * compute v1 = v2 op f1
47
50
 * v1, v2 are vectors contains 3 float
48
51
 * and f1 is a float
49
52
 * */
50
 
#define vec3opf(v1, v2, op, f1)\
51
 
        v1[0] = (v2[0] op (f1));\
52
 
        v1[1] = (v2[1] op (f1));\
53
 
        v1[2] = (v2[2] op (f1));
 
53
#define VEC3OPF(v1, v2, op, f1)                                               \
 
54
        {                                                                         \
 
55
                v1[0] = (v2[0] op (f1));                                              \
 
56
                v1[1] = (v2[1] op (f1));                                              \
 
57
                v1[2] = (v2[2] op (f1));                                              \
 
58
        } (void)0
54
59
 
55
60
/**
56
61
 * compute v1 = f1 op v2
57
62
 * v1, v2 are vectors contains 3 float
58
63
 * and f1 is a float
59
64
 * */
60
 
#define fopvec3(v1, f1, op, v2)\
61
 
        v1[0] = ((f1) op v2[0]);\
62
 
        v1[1] = ((f1) op v2[1]);\
63
 
        v1[2] = ((f1) op v2[2]);
 
65
#define FOPVEC3(v1, f1, op, v2)                                               \
 
66
        {                                                                         \
 
67
                v1[0] = ((f1) op v2[0]);                                              \
 
68
                v1[1] = ((f1) op v2[1]);                                              \
 
69
                v1[2] = ((f1) op v2[2]);                                              \
 
70
        } (void)0
64
71
 
65
72
/**
66
73
 * ClipColor:
129
136
 * InitSunSky:
130
137
 * this function compute some sun,sky parameters according to input parameters and also initiate some other sun, sky parameters
131
138
 * parameters:
132
 
 * sunSky, is a structure that contains informtion about sun, sky and atmosphere, in this function, most of its values initiated
 
139
 * sunSky, is a structure that contains information about sun, sky and atmosphere, in this function, most of its values initiated
133
140
 * turb, is atmosphere turbidity
134
141
 * toSun, contains sun direction
135
142
 * horizon_brighness, controls the brightness of the horizon colors
269
276
        y = PerezFunction(sunsky, sunsky->perez_y, theta, gamma, sunsky->zenith_y);
270
277
        Y = 6.666666667e-5f * nfade * hfade * PerezFunction(sunsky, sunsky->perez_Y, theta, gamma, sunsky->zenith_Y);
271
278
 
272
 
        if(sunsky->sky_exposure!=0.0f)
 
279
        if (sunsky->sky_exposure!=0.0f)
273
280
                Y = 1.0 - exp(Y*sunsky->sky_exposure);
274
281
        
275
282
        X = (x / y) * Y;
315
322
 * */
316
323
static void ComputeAttenuatedSunlight(float theta, int turbidity, float fTau[3])
317
324
{
318
 
        float fBeta ;
 
325
        float fBeta;
319
326
        float fTauR, fTauA;
320
 
        float m ;
 
327
        float m;
321
328
        float fAlpha;
322
329
 
323
330
        int i;
331
338
        
332
339
        m =  1.0f/(cosf(theta) + 0.15f*powf(93.885f-theta/(float)M_PI*180.0f,-1.253f));
333
340
 
334
 
        for(i = 0; i < 3; i++)
 
341
        for (i = 0; i < 3; i++)
335
342
        {
336
343
                // Rayleigh Scattering
337
344
                fTauR = expf( -m * 0.008735f * powf(fLambda[i], (float)(-4.08f)));
345
352
 
346
353
/**
347
354
 * InitAtmosphere:
348
 
 * this function intiate sunSky structure with user input parameters.
 
355
 * this function initiate sunSky structure with user input parameters.
349
356
 * parameters:
350
357
 * sunSky, contains information about sun, and in this function some atmosphere parameters will initiated
351
358
 * sun_intens, shows sun intensity value
404
411
        fTemp = pi*pi*(n*n-1)*(n*n-1)*(6+3*pn)/(6-7*pn)/N;
405
412
        fBeta = 8*fTemp*pi/3;
406
413
                
407
 
        vec3opf(sunSky->atm_BetaRay, vLambda4, *, fBeta);
 
414
        VEC3OPF(sunSky->atm_BetaRay, vLambda4, *, fBeta);
408
415
        fBetaDash = fTemp/2;
409
 
        vec3opf(sunSky->atm_BetaDashRay, vLambda4,*, fBetaDash);
 
416
        VEC3OPF(sunSky->atm_BetaDashRay, vLambda4,*, fBetaDash);
410
417
        
411
418
 
412
419
        // Mie scattering constants.
413
420
        fTemp2 = 0.434f*c*(2*pi)*(2*pi)*0.5f;
414
 
        vec3opf(sunSky->atm_BetaDashMie, vLambda2, *, fTemp2);
 
421
        VEC3OPF(sunSky->atm_BetaDashMie, vLambda2, *, fTemp2);
415
422
        
416
423
        fTemp3 = 0.434f*c*pi*(2*pi)*(2*pi);
417
424
        
418
 
        vec3opv(vBetaMieTemp, K, *, fLambda);
419
 
        vec3opf(sunSky->atm_BetaMie, vBetaMieTemp,*, fTemp3);
 
425
        VEC3OPV(vBetaMieTemp, K, *, fLambda);
 
426
        VEC3OPF(sunSky->atm_BetaMie, vBetaMieTemp,*, fTemp3);
420
427
        
421
428
}
422
429
 
455
462
        costheta = dot_v3v3(view, sunDirection); // cos(theta)
456
463
        Phase_1 = 1 + (costheta * costheta); // Phase_1
457
464
        
458
 
        vec3opf(sunSky->atm_BetaRay, sunSky->atm_BetaRay, *, sunSky->atm_BetaRayMultiplier);
459
 
        vec3opf(sunSky->atm_BetaMie, sunSky->atm_BetaMie, *, sunSky->atm_BetaMieMultiplier);
460
 
        vec3opv(sunSky->atm_BetaRM, sunSky->atm_BetaRay, +, sunSky->atm_BetaMie);
 
465
        VEC3OPF(sunSky->atm_BetaRay, sunSky->atm_BetaRay, *, sunSky->atm_BetaRayMultiplier);
 
466
        VEC3OPF(sunSky->atm_BetaMie, sunSky->atm_BetaMie, *, sunSky->atm_BetaMieMultiplier);
 
467
        VEC3OPV(sunSky->atm_BetaRM, sunSky->atm_BetaRay, +, sunSky->atm_BetaMie);
461
468
        
462
469
        //e^(-(beta_1 + beta_2) * s) = E1
463
 
        vec3opf(E1, sunSky->atm_BetaRM, *, -s/(float)M_LN2);
 
470
        VEC3OPF(E1, sunSky->atm_BetaRM, *, -s/(float)M_LN2);
464
471
        E1[0] = exp(E1[0]);
465
472
        E1[1] = exp(E1[1]);
466
473
        E1[2] = exp(E1[2]);
472
479
        fTemp = fTemp * sqrtf(fTemp);
473
480
        Phase_2 = (1 - sunSky->atm_HGg * sunSky->atm_HGg)/fTemp;
474
481
        
475
 
        vec3opf(vTemp1, sunSky->atm_BetaDashRay, *, Phase_1);
476
 
        vec3opf(vTemp2, sunSky->atm_BetaDashMie, *, Phase_2);   
477
 
 
478
 
        vec3opv(vTemp1, vTemp1, +, vTemp2);
479
 
        fopvec3(vTemp2, 1.0f, -, E1);
480
 
        vec3opv(vTemp1, vTemp1, *, vTemp2);
481
 
 
482
 
        fopvec3(vTemp2, 1.0f, / , sunSky->atm_BetaRM);
483
 
 
484
 
        vec3opv(I, vTemp1, *, vTemp2);
 
482
        VEC3OPF(vTemp1, sunSky->atm_BetaDashRay, *, Phase_1);
 
483
        VEC3OPF(vTemp2, sunSky->atm_BetaDashMie, *, Phase_2);
 
484
 
 
485
        VEC3OPV(vTemp1, vTemp1, +, vTemp2);
 
486
        FOPVEC3(vTemp2, 1.0f, -, E1);
 
487
        VEC3OPV(vTemp1, vTemp1, *, vTemp2);
 
488
 
 
489
        FOPVEC3(vTemp2, 1.0f, / , sunSky->atm_BetaRM);
 
490
 
 
491
        VEC3OPV(I, vTemp1, *, vTemp2);
485
492
                
486
 
        vec3opf(I, I, *, sunSky->atm_InscatteringMultiplier);
487
 
        vec3opf(E, E, *, sunSky->atm_ExtinctionMultiplier);
 
493
        VEC3OPF(I, I, *, sunSky->atm_InscatteringMultiplier);
 
494
        VEC3OPF(E, E, *, sunSky->atm_ExtinctionMultiplier);
488
495
                
489
496
        //scale to color sun
490
497
        ComputeAttenuatedSunlight(sunSky->theta, sunSky->turbidity, sunColor);
491
 
        vec3opv(E, E, *, sunColor);
492
 
 
493
 
        vec3opf(I, I, *, sunSky->atm_SunIntensity);
494
 
 
495
 
        vec3opv(rgb, rgb, *, E);
496
 
        vec3opv(rgb, rgb, +, I);
 
498
        VEC3OPV(E, E, *, sunColor);
 
499
 
 
500
        VEC3OPF(I, I, *, sunSky->atm_SunIntensity);
 
501
 
 
502
        VEC3OPV(rgb, rgb, *, E);
 
503
        VEC3OPV(rgb, rgb, +, I);
497
504
}
498
505
 
499
 
#undef vec3opv
500
 
#undef vec3opf
501
 
#undef fopvec3
 
506
#undef VEC3OPV
 
507
#undef VEC3OPF
 
508
#undef FOPVEC3
502
509
 
503
510
/* EOF */