-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathConstants.cpp
339 lines (277 loc) · 11 KB
/
Constants.cpp
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
/* Version: MPL 1.1/LGPL 3.0
*
* "The contents of this file are subject to the Mozilla Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
* License for the specific language governing rights and limitations
* under the License.
*
* The Original Code is the Oblivion Graphics Extender, short OBGE.
*
* The Initial Developer of the Original Code is
* Ethatron <[email protected]>. Portions created by The Initial
* Developer are Copyright (C) 2011 The Initial Developer.
* All Rights Reserved.
*
* Contributor(s):
* Timeslip (Version 1)
* scanti (Version 2)
* IlmrynAkios (Version 3)
*
* Alternatively, the contents of this file may be used under the terms
* of the GNU Library General Public License Version 3 license (the
* "LGPL License"), in which case the provisions of LGPL License are
* applicable instead of those above. If you wish to allow use of your
* version of this file only under the terms of the LGPL License and not
* to allow others to use your version of this file under the MPL,
* indicate your decision by deleting the provisions above and replace
* them with the notice and other provisions required by the LGPL License.
* If you do not delete the provisions above, a recipient may use your
* version of this file under either the MPL or the LGPL License."
*/
#include "Constants.h"
#include "GlobalSettings.h"
static global<float> SunlightDimmer(1.0, "Oblivion.ini", "BlurShaderHDR", "fSunlightDimmer");
/* -----------------------------------------------------------------------------
*/
struct sConstants Constants;
void sConstants::Update() {
Update(v1_2_416::GetRenderer());
UpdateSun();
//ShaderManager *sm = ShaderManager::GetSingleton();
//sm->UpdateFrameConstants();
}
void sConstants::Update(v1_2_416::NiDX9Renderer *Renderer) {
float (_cdecl * GetTimer)(bool, bool) = (float( *)(bool, bool))0x0043F490; // (TimePassed,GameTime)
int gtime = GetTimer(0, 1); LARGE_INTEGER tick;
QueryPerformanceCounter(&tick);
iTikTiming.z = (__int64)((tick.QuadPart * 1000 * 1000) / iTikTiming.w);
iTikTiming.y = (__int64)((tick.QuadPart * 1000 * 1 ) / iTikTiming.w);
iTikTiming.x = (__int64)((tick.QuadPart * 1 * 1 ) / iTikTiming.w);
fTikTiming.z = (float)(tick.QuadPart) * 1000 * 1000 / fTikTiming.w;
fTikTiming.y = (float)(tick.QuadPart) * 1000 * 1 / fTikTiming.w;
fTikTiming.x = (float)(tick.QuadPart) * 1 * 1 / fTikTiming.w;
iGameTime.x = gtime;
iGameTime.w = (gtime ) % 60;
iGameTime.z = (gtime / 60) % 60;
iGameTime.y = (gtime / 60) / 60;
fGameTime.x = gtime;
fGameTime.w = ((int)gtime ) % 60;
fGameTime.z = ((int)gtime / 60) % 60;
fGameTime.y = ((int)gtime / 60) / 60;
// Noon is at (20:00 + 06:00) / 2 == 13:00 // [-PI,0,+PI]
fGameTime.x = M_PI * (gtime - (13 * 60 * 60)) / (14 * 60 * 60);
/* deprecated */
#ifndef NO_DEPRECATED
time.x = gtime;
time.w = (int)(gtime ) % 60;
time.z = (int)(gtime / 60) % 60;
time.y = (int)(gtime / 60) / 60;
#endif
TES *tes = TES::GetSingleton();
// nearF, farF, farF - nearF, farF + nearF
FogRange.x = tes->fogProperty->fogStart;
FogRange.y = tes->fogProperty->fogEnd;
FogRange.z = FogRange.y - FogRange.x;
FogRange.w = FogRange.y + FogRange.x;
// colorF
FogColor.x = tes->fogProperty->color.r;
FogColor.y = tes->fogProperty->color.g;
FogColor.z = tes->fogProperty->color.b;
if (tes->niDirectionalLight) {
float dim = SunlightDimmer.Get();
LightDir.x = -tes->niDirectionalLight->m_direction.x;
LightDir.y = -tes->niDirectionalLight->m_direction.y;
LightDir.z = -tes->niDirectionalLight->m_direction.z;
LightDir.Normalize3();
LightColor.x = tes->niDirectionalLight->m_kDiff.r * dim;
LightColor.y = tes->niDirectionalLight->m_kDiff.g * dim;
LightColor.z = tes->niDirectionalLight->m_kDiff.b * dim;
AmbientColor.x = tes->niDirectionalLight->m_kAmb.r;
AmbientColor.y = tes->niDirectionalLight->m_kAmb.g;
AmbientColor.z = tes->niDirectionalLight->m_kAmb.b;
}
// worldspace
WorldSpace = 0;
Oblivion = false;
if (tes->currentWorldSpace) {
WorldSpace = tes->currentWorldSpace->refID;
Oblivion = tes->currentWorldSpace->IsOblivionWorld();
}
// location
Coordinates.x = 0;
Coordinates.y = 0;
if (tes->currentExteriorCell && tes->currentExteriorCell->coords) {
Coordinates.x = tes->currentExteriorCell->coords->x;
Coordinates.y = tes->currentExteriorCell->coords->y;
}
Coordinates.z = 0;
Coordinates.w = 0;
if (tes->currentInteriorCell && tes->currentInteriorCell->coords) {
Coordinates.z = tes->currentInteriorCell->coords->x;
Coordinates.w = tes->currentInteriorCell->coords->y;
}
Exteriour = !tes->currentInteriorCell;
}
void sConstants::UpdateSun() {
OBGEfork::Sky *pSky = OBGEfork::Sky::GetSingleton();
OBGEfork::Sun *pSun = pSky->sun;
TESClimate *climate = pSky->firstClimate;
TESWeather *weather = pSky->firstWeather;
SunTiming.x = climate->sunriseBegin * 10 * 60;
SunTiming.y = climate->sunriseEnd * 10 * 60;
SunTiming.z = climate->sunsetBegin * 10 * 60;
SunTiming.w = climate->sunsetEnd * 10 * 60;
v1_2_416::NiNode *SunContainer = pSun->SunBillboard.Get()->ParentNode;
float deltaz = SunDir.z;
bool SunHasBenCulled = SunContainer->m_flags.individual.AppCulled;
SunDir.x = SunContainer->m_localTranslate.x;
SunDir.y = SunContainer->m_localTranslate.y;
SunDir.z = SunContainer->m_localTranslate.z;
SunDir.Normalize3();
#if 1
/* calculation of the real position of the sun */
/* length of day in "seconds" */
float daylength = SunTiming.w - SunTiming.x;
float highnoon = SunTiming.x + (daylength / 2);
float midnight = highnoon - (12 * 60 * 60);
/* length of day in "degree / 2" */
float dayangleh = daylength * (M_PI / (24 * 60 * 60));
float midangle = midnight * (M_PI * 2 / (24 * 60 * 60));
float curangle = iGameTime.x * (M_PI * 2 / (24 * 60 * 60));
/* dis-position of tangent-line from the center
* of the circle in [0,1] == [center,radius], d
* lies on the rotated z-axis midnight->highnoon
*/
float deltapos = 1.0 * cos(dayangleh);
/* calculate unit-positions on the circle [0,1] */
SunPos.x = sin(curangle - midangle);
SunPos.y = 0;
SunPos.z = -cos(curangle - midangle);
/* apply dis-poition and renormalize */
SunPos.y = SunDir.y;
SunPos.z = SunPos.z - deltapos;
SunPos.Normalize3();
/* tilt sun-circle
SunPos.y = SunPos.z * sin(M_PI / 2 - dayangleh);
SunPos.z = SunPos.z * cos(M_PI / 2 - dayangleh);
SunPos.Normalize3(); */
SunDir = SunPos;
#endif
#if 0
// Sunrise is at 06:00, Sunset at 20:00
if ((iGameTime.x > SunTiming.w + (10 * 60)) ||
(iGameTime.x < SunTiming.x - (10 * 60)))
SunDir.z = -SunDir.z;
else if ((iGameTime.x > SunTiming.z - (10 * 60))) {
/* needs to go down aways */
if ((fabs(deltaz) - SunDir.z) <= 0.0)
SunDir.z = -SunDir.z;
}
else if ((iGameTime.x < SunTiming.y + (10 * 60))) {
/* needs to go up aways */
if ((fabs(deltaz) - SunDir.z) >= 0.0)
SunDir.z = -SunDir.z;
}
//if ((GameTime.y < 6) || (GameTime.y >= 21))
// SunDir.z = -fabs(SunDir.z);
#endif
SunDir.w = SunHasBenCulled;
}
/* -----------------------------------------------------------------------------
*/
#if 0
typedef v1_2_416::NiVector3 float3;
typedef v1_2_416::NiVector4 float4;
#define lerp(ths, tht, morph) (ths + ((tht - ths) * morph))
#define PLAYER_HEIGHT PlayerPosition.z
/* Light properties */
static const float3 cust_SunIntensity(1.0, 0.960784, 0.949019);
/* A&P dimensions */
static const float fCoverDimension = 4000.0;
static const float fAtmosphereRadius = 6432797.0;
static const float fCloudRadius = 6380797.0;
static const float fPlanetRadius = 6372797.0;
static const float fThicknessInv = 1.0 / (6432797.0 - 6372797.0);
static const float fDistanceInv = 6380797.0 / (6380797.0 - 6372797.0);
//efine SUN_SIZE (1024 - (256 * pow(1.0 - -SunDir.z, 0.125)))
#define SUN_SIZE (1024)
#define SUN_BRIGHTNESS (400)
#define HORIZONT_BIAS 11.0
inline float get_height_coefficient(float3 Eye, float fPlanetRadius, float fAtmosphereRadius)
{
float r = Eye.GetLength();
return
sqrt( r * r - fPlanetRadius * fPlanetRadius) /
sqrt(fAtmosphereRadius * fAtmosphereRadius - fPlanetRadius * fPlanetRadius);
}
#define ONEmEPINV 1.0280912f
inline float get_suna_coefficient(float x)
{
// 1 / (1.0f - exp(-3.6f))
return (1.0f - exp(-2.8f * x - 0.8f)) * ONEmEPINV;
}
void sConstants::UpdateSunCoEffs() {
/* scattering shader ------------------------------------- */
float cust_fIndRay = 1.0;
float cust_fIndMie = 1.0;
float cust_fMieCoefSB = 0.0025;
#define VARYING_EFFICIENTS
#ifdef VARYING_EFFICIENTS
/* morning: 1.5, 4.0, 0.0085
* noon: 1.0, 1.0, 0.0025
* afternoon: 2.0, 0.5, 0.0015
*/
float3 T; T.Set(-SunDir.x, 0, -SunDir.z); T.Normalize();
float noon = T.z;//-T.z;//saturate(dot(float3(0, 0, 1), T));
if (T.x < 0) {
cust_fIndRay = lerp(2.0000, 1.0000, noon);
cust_fIndMie = lerp(0.5000, 1.0000, noon);
cust_fMieCoefSB = lerp(0.0015, 0.0025, noon);
}
else {
cust_fIndRay = lerp(1.5000, 1.0000, noon);
cust_fIndMie = lerp(4.0000, 1.0000, noon);
cust_fMieCoefSB = lerp(0.0085, 0.0025, noon);
}
#endif
SunCoEffs.x = cust_fIndRay;
SunCoEffs.y = cust_fIndMie;
SunCoEffs.z = cust_fMieCoefSB;
}
void sConstants::UpdateSunColor() {
/* sun color ----------------------------------------------------------- */
/* we normally should put the real player-position here (from where?)
*
* additionally, the reflections' pass camera is below surface, we could
* intersect the view-vector with the planet's surface to get an individual
* view-vector per pixel, though I don't know if the few hundret meters we
* can go around really affect the sky's appearance
*
* for the moment we neglect any variable position of the camera
*/
float3 Eye; Eye.Set(0.0, 0.0, fPlanetRadius + max(PLAYER_HEIGHT, 0)); // Eye is 0km up in global
// float3 L = -SunDir.xyz;
// new parameterization
// if we are directly on the planet-surface this is 0 and 1 for above atmosphere
// range [0.0, 1.0] or more
float height = get_height_coefficient(Eye, fPlanetRadius, fAtmosphereRadius);
/* eye-normal */
float3 nEye; nEye = Eye; nEye.Normalize();
/* zenith: dot([0,0,1], [0,0,-1]) = -1 = 0.0
* horizont: dot([0,0,1], [0,-1,0]) = 0 = 0.5
*/
float4 tl = tex2Dlod(cust_samplerOpticalDepth, float4(height, 0.5f * (dot(nEye, -SunDir)) + 0.5f, 0, 0));
float4 cust_SunColor = tex2Dlod(cust_samplerSun, float4(0.5, (dot(-SunDir, -SunDir) - 1.0) * SUN_SIZE + 0.5, 0, 0));
float3 sunIntensityAtt = float3(1,1,1) * exp(
-cust_fIndRay * tl.xyz
-cust_fIndMie * tl.www
);
float4 sunColor = float4(SUN_BRIGHTNESS * cust_SunColor.rgb * sunIntensityAtt, cust_SunColor.g);
return sunColor;
}
#endif