summaryrefslogtreecommitdiff
path: root/indra/newview/lllegacyatmospherics.h
blob: 95700227f94fac8ca49a5a413c603861c8b55c0e (plain)
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
/** 
 * @file lllegacyatmospherics.h
 * @brief LLVOSky class header file
 *
 * $LicenseInfo:firstyear=2001&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2010, Linden Research, Inc.
 * 
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License only.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#ifndef LL_LLLEGACYATMOSPHERICS_H
#define LL_LLLEGACYATMOSPHERICS_H

#include "stdtypes.h"
#include "v3color.h"
#include "v4coloru.h"
#include "llviewertexture.h"
#include "llviewerobject.h"
#include "llframetimer.h"
#include "v3colorutil.h"
#include "llsettingssky.h"

//////////////////////////////////
//
// Lots of constants
//
// Will clean these up at some point...
//

const F32 HORIZON_DIST          = 1024.0f;
const F32 ATM_EXP_FALLOFF       = 0.000126f;
const F32 ATM_SEA_LEVEL_NDENS   = 2.55e25f;
const F32 ATM_HEIGHT            = 100000.f;

// constants used in calculation of scattering coeff of clear air
const F32 sigma     = 0.035f;
const F32 fsigma    = (6.f + 3.f * sigma) / (6.f-7.f*sigma);
const F64 Ndens     = 2.55e25;
const F64 Ndens2    = Ndens*Ndens;

class LLFace;
class LLHaze;

LL_FORCE_INLINE LLColor3 refr_ind_calc(const LLColor3 &wave_length)
{
    LLColor3 refr_ind;
    for (S32 i = 0; i < 3; ++i)
    {
        const F32 wl2 = wave_length.mV[i] * wave_length.mV[i] * 1e-6f;
        refr_ind.mV[i] = 6.43e3f + ( 2.95e6f / ( 146.0f - 1.f/wl2 ) ) + ( 2.55e4f / ( 41.0f - 1.f/wl2 ) );
        refr_ind.mV[i] *= 1.0e-8f;
        refr_ind.mV[i] += 1.f;
    }
    return refr_ind;
}


class LLHaze
{
public:
    LLHaze() : mG(0), mFalloff(1), mAbsCoef(0.f) {mSigSca.setToBlack();}
    LLHaze(const F32 g, const LLColor3& sca, const F32 fo = 2.f) : 
            mG(g), mSigSca(0.25f/F_PI * sca), mFalloff(fo), mAbsCoef(0.f)
    {
        mAbsCoef = color_intens(mSigSca) / sAirScaIntense;
    }

    LLHaze(const F32 g, const F32 sca, const F32 fo = 2.f) : mG(g),
            mSigSca(0.25f/F_PI * LLColor3(sca, sca, sca)), mFalloff(fo)
    {
        mAbsCoef = 0.01f * sca / sAirScaAvg;
    }

/* Proportion of light that is scattered into 'path' from 'in' over distance dt. */
/* assumes that vectors 'path' and 'in' are normalized. Scattering coef / 2pi */

    LL_FORCE_INLINE LLColor3 calcAirSca(const F32 h)
    {
       return calcFalloff(h) * sAirScaSeaLevel;
    }

    LL_FORCE_INLINE void calcAirSca(const F32 h, LLColor3 &result)
    {
        result = sAirScaSeaLevel;
        result *= calcFalloff(h);
    }

    F32 getG() const                { return mG; }

    void setG(const F32 g)
    {
        mG = g;
    }

    const LLColor3& getSigSca() const // sea level
    {
        return mSigSca;
    } 

    void setSigSca(const LLColor3& s)
    {
        mSigSca = s;
        mAbsCoef = 0.01f * color_intens(mSigSca) / sAirScaIntense;
    }

    void setSigSca(const F32 s0, const F32 s1, const F32 s2)
    {
        mSigSca = sAirScaAvg * LLColor3 (s0, s1, s2);
        mAbsCoef = 0.01f * (s0 + s1 + s2) / 3;
    }

    F32 getFalloff() const
    {
        return mFalloff;
    }

    void setFalloff(const F32 fo)
    {
        mFalloff = fo;
    }

    F32 getAbsCoef() const
    {
        return mAbsCoef;
    }

    inline static F32 calcFalloff(const F32 h)
    {
        return (h <= 0) ? 1.0f : (F32)LL_FAST_EXP(-ATM_EXP_FALLOFF * h);
    }

    inline LLColor3 calcSigSca(const F32 h) const
    {
        return calcFalloff(h * mFalloff) * mSigSca;
    }

    inline void calcSigSca(const F32 h, LLColor3 &result) const
    {
        result = mSigSca;
        result *= calcFalloff(h * mFalloff);
    }

    LLColor3 calcSigExt(const F32 h) const
    {
        return calcFalloff(h * mFalloff) * (1 + mAbsCoef) * mSigSca;
    }

    F32 calcPhase(const F32 cos_theta) const;

private:
    static LLColor3 const sAirScaSeaLevel;
    static F32 const sAirScaIntense;
    static F32 const sAirScaAvg;

protected:
    F32         mG;
    LLColor3    mSigSca;
    F32         mFalloff;   // 1 - slow, >1 - faster
    F32         mAbsCoef;
};


class LLCubeMap;

class AtmosphericsVars
{
public:
    AtmosphericsVars()
    : hazeColor(0,0,0)
    , hazeColorBelowCloud(0,0,0)
    , cloudColorSun(0,0,0)
    , cloudColorAmbient(0,0,0)
    , cloudDensity(0.0f)
    , blue_density()
    , blue_horizon()
    , haze_density(0.0f)
    , haze_horizon(0.0f)
    , density_multiplier(0.0f)
    , max_y(0.0f)
    , gamma(1.0f)
    , sun_norm(0.0f, 1.0f, 0.0f, 1.0f)
    , sunlight()
    , ambient()
    , glow()
    , cloud_shadow(1.0f)
    , dome_radius(1.0f)
    , dome_offset(1.0f)
    , light_atten()
    , light_transmittance()
    {
    }

    LL_FORCE_INLINE friend bool operator==(const AtmosphericsVars& a, const AtmosphericsVars& b);

    LLColor3  hazeColor;
    LLColor3  hazeColorBelowCloud;
	LLColor3  cloudColorSun;
	LLColor3  cloudColorAmbient;
	F32       cloudDensity;
    LLColor3  blue_density;
    LLColor3  blue_horizon;
    F32       haze_density;
    F32       haze_horizon;
    F32       density_multiplier;
    F32       distance_multiplier;
    F32       max_y;
    F32       gamma;
    LLVector4 sun_norm;
    LLColor3  sunlight;
    LLColor3  ambient;
    LLColor3  glow;
    F32       cloud_shadow;
    F32       dome_radius;
    F32       dome_offset;
    LLColor3 light_atten;
    LLColor3 light_transmittance;
    LLColor3 total_density;
};

bool operator==(const AtmosphericsVars& a, const AtmosphericsVars& b)
{
    if (a.hazeColor != b.hazeColor)
    {
        return false;
    }

    if (a.hazeColorBelowCloud != b.hazeColorBelowCloud)
    {
        return false;
    }

    if (a.cloudColorSun != b.cloudColorSun)
    {
        return false;
    }

    if (a.cloudColorAmbient != b.cloudColorAmbient)
    {
        return false;
    }

    if (a.cloudDensity != b.cloudDensity)
    {
        return false;
    }

    if (a.density_multiplier != b.density_multiplier)
    {
        return false;
    }

    if (a.haze_horizon != b.haze_horizon)
    {
        return false;
    }

    if (a.haze_density != b.haze_density)
    {
        return false;
    }

    if (a.blue_horizon != b.blue_horizon)
    {
        return false;
    }

    if (a.blue_density != b.blue_density)
    {
        return false;
    }

    if (a.dome_offset != b.dome_offset)
    {
        return false;
    }

    if (a.dome_radius != b.dome_radius)
    {
        return false;
    }

    if (a.cloud_shadow != b.cloud_shadow)
    {
        return false;
    }

    if (a.glow != b.glow)
    {
        return false;
    }

    if (a.ambient != b.ambient)
    {
        return false;
    }

    if (a.sunlight != b.sunlight)
    {
        return false;
    }

    if (a.sun_norm != b.sun_norm)
    {
        return false;
    }

    if (a.gamma != b.gamma)
    {
        return false;
    }

    if (a.max_y != b.max_y)
    {
        return false;
    }

    if (a.distance_multiplier != b.distance_multiplier)
    {
        return false;
    }

    // light_atten, light_transmittance, total_density
    // are ignored as they always change when the values above do
    // they're just shared calc across the sky map generation to save cycles

    return true;
}

class LLAtmospherics
{
public:    
    LLAtmospherics();
    ~LLAtmospherics();

    void init();
    void updateFog(const F32 distance, const LLVector3& tosun);

    const LLHaze& getHaze() const                    { return mHaze; }
    LLHaze& getHaze()                                { return mHaze; }
    F32 getHazeConcentration() const                 { return mHazeConcentration; }
    void setHaze(const LLHaze& h)                    { mHaze = h; }
    void setFogRatio(const F32 fog_ratio)            { mFogRatio = fog_ratio; }

    F32      getFogRatio() const                     { return mFogRatio; }
    LLColor4 getFogColor() const                     { return mFogColor; }
    LLColor4 getGLFogColor() const                   { return mGLFogCol; }

    void setCloudDensity(F32 cloud_density)          { mCloudDensity = cloud_density; }
    void setWind ( const LLVector3& wind )           { mWind = wind.length(); }

    LLColor4 calcSkyColorInDir(AtmosphericsVars& vars, const LLVector3& dir, bool isShiny = false);

protected:    

    void     calcSkyColorWLVert(LLVector3 & Pn, AtmosphericsVars& vars);
    LLColor3 getHazeColor(LLSettingsSky::ptr_t psky, AtmosphericsVars& vars, F32 costheta, F32 cloud_shadow);

    LLHaze              mHaze;
    F32                 mHazeConcentration;
    F32                 mCloudDensity;
    F32                 mWind;
    BOOL                mInitialized;
    LLVector3           mLastLightingDirection;
    LLColor3            mLastTotalAmbient;
    F32                 mAmbientScale;
    LLColor3            mNightColorShift;
    F32                 mInterpVal;
    LLColor4            mFogColor;
    LLColor4            mGLFogCol;
    F32                 mFogRatio;
    F32                 mWorldScale;
};

#endif