1 | #include "SkyPreetham.h"
|
---|
2 | #include "common.h"
|
---|
3 | #include "Vector3.h"
|
---|
4 | #include "SceneEntity.h"
|
---|
5 | #include "Transform3.h"
|
---|
6 | #include "Camera.h"
|
---|
7 | #include "RenderState.h"
|
---|
8 |
|
---|
9 |
|
---|
10 | #ifdef _CRT_SET
|
---|
11 | #define _CRTDBG_MAP_ALLOC
|
---|
12 | #include <stdlib.h>
|
---|
13 | #include <crtdbg.h>
|
---|
14 |
|
---|
15 | // redefine new operator
|
---|
16 | #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
|
---|
17 | #define new DEBUG_NEW
|
---|
18 | #endif
|
---|
19 |
|
---|
20 |
|
---|
21 | using namespace CHCDemoEngine;
|
---|
22 | using namespace std;
|
---|
23 |
|
---|
24 |
|
---|
25 | static CGprogram sCgSkyProgram = NULL;
|
---|
26 | static CGprogram sCgMrtFragmentSkyDomeProgram = NULL;
|
---|
27 |
|
---|
28 |
|
---|
29 | static CGparameter sLightDirParam;
|
---|
30 | static CGparameter sThetaSunParam;
|
---|
31 | static CGparameter sZenithColorParam;
|
---|
32 | static CGparameter sAColorParam;
|
---|
33 | static CGparameter sBColorParam;
|
---|
34 | static CGparameter sCColorParam;
|
---|
35 | static CGparameter sDColorParam;
|
---|
36 | static CGparameter sEColorParam;
|
---|
37 | static CGparameter sMultiplierParam;
|
---|
38 |
|
---|
39 | ShaderContainer SkyPreetham::sShaders;
|
---|
40 |
|
---|
41 |
|
---|
42 | inline float CBQ(float x)
|
---|
43 | {
|
---|
44 | return x * x * x;
|
---|
45 | }
|
---|
46 |
|
---|
47 |
|
---|
48 | inline float SQR(float x)
|
---|
49 | {
|
---|
50 | return x * x ;
|
---|
51 | }
|
---|
52 |
|
---|
53 |
|
---|
54 | void SkyPreetham::InitCG(CGcontext context)
|
---|
55 | {
|
---|
56 | ShaderProgram *pr;
|
---|
57 |
|
---|
58 | sCgSkyProgram =
|
---|
59 | cgCreateProgramFromFile(context,
|
---|
60 | CG_SOURCE,
|
---|
61 | "src/shaders/sky_preetham.cg",
|
---|
62 | RenderState::sCgVertexProfile,
|
---|
63 | "default_vs",
|
---|
64 | NULL);
|
---|
65 |
|
---|
66 | if (sCgSkyProgram != NULL)
|
---|
67 | {
|
---|
68 | cgGLLoadProgram(sCgSkyProgram);
|
---|
69 |
|
---|
70 | sLightDirParam = cgGetNamedParameter(sCgSkyProgram, "lightDir");
|
---|
71 | sThetaSunParam = cgGetNamedParameter(sCgSkyProgram, "thetaSun");
|
---|
72 | sZenithColorParam = cgGetNamedParameter(sCgSkyProgram, "zenithColor");
|
---|
73 | sAColorParam = cgGetNamedParameter(sCgSkyProgram, "aColor");
|
---|
74 | sBColorParam = cgGetNamedParameter(sCgSkyProgram, "bColor");
|
---|
75 | sCColorParam = cgGetNamedParameter(sCgSkyProgram, "cColor");
|
---|
76 | sDColorParam = cgGetNamedParameter(sCgSkyProgram, "dColor");
|
---|
77 | sEColorParam = cgGetNamedParameter(sCgSkyProgram, "eColor");
|
---|
78 |
|
---|
79 | sMultiplierParam = cgGetNamedParameter(sCgSkyProgram, "multiplier");
|
---|
80 |
|
---|
81 | pr = new ShaderProgram(sCgSkyProgram);
|
---|
82 | sShaders.push_back(pr);
|
---|
83 | }
|
---|
84 | else
|
---|
85 | cerr << "sky program failed to load" << endl;
|
---|
86 |
|
---|
87 | sCgMrtFragmentSkyDomeProgram =
|
---|
88 | cgCreateProgramFromFile(context,
|
---|
89 | CG_SOURCE,
|
---|
90 | "src/shaders/sky_preetham.cg",
|
---|
91 | RenderState::sCgFragmentProfile,
|
---|
92 | "frag_skydome",
|
---|
93 | NULL);
|
---|
94 |
|
---|
95 | if (sCgMrtFragmentSkyDomeProgram != NULL)
|
---|
96 | {
|
---|
97 | cgGLLoadProgram(sCgMrtFragmentSkyDomeProgram);
|
---|
98 | //cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
|
---|
99 |
|
---|
100 | pr = new ShaderProgram(sCgMrtFragmentSkyDomeProgram);
|
---|
101 | sShaders.push_back(pr);
|
---|
102 | }
|
---|
103 | else
|
---|
104 | cerr << "fragment skyprogram failed to load" << endl;
|
---|
105 | }
|
---|
106 |
|
---|
107 |
|
---|
108 | void SkyPreetham::ReleaseCG()
|
---|
109 | {
|
---|
110 | CLEAR_CONTAINER(sShaders);
|
---|
111 | }
|
---|
112 |
|
---|
113 |
|
---|
114 | SkyPreetham::SkyPreetham(float turbitity, SceneEntity *skyDome):
|
---|
115 | mSkyDome(skyDome),
|
---|
116 | mTurbidity(turbitity)
|
---|
117 | //, mSunQuad(NULL)
|
---|
118 | {
|
---|
119 | CreateSunQuad();
|
---|
120 | }
|
---|
121 |
|
---|
122 |
|
---|
123 | SkyPreetham::~SkyPreetham()
|
---|
124 | {
|
---|
125 | //DEL_PTR(mSunQuad);
|
---|
126 | }
|
---|
127 |
|
---|
128 |
|
---|
129 | void SkyPreetham::RenderSkyDome(const Vector3 &sunDir,
|
---|
130 | Camera *camera,
|
---|
131 | RenderState *state,
|
---|
132 | bool scaleToRange)
|
---|
133 | {
|
---|
134 | pair<float, float> sun_theta;
|
---|
135 | Vector3 zenithColor;
|
---|
136 | vector<Vector3> ABCDE;
|
---|
137 | ComputeFactors(sunDir, zenithColor, ABCDE, sun_theta);
|
---|
138 |
|
---|
139 | // Move skybox with camera.
|
---|
140 | Vector3 position = camera->GetPosition();
|
---|
141 |
|
---|
142 | const float scaleFactor = 80.0f;
|
---|
143 | //const float scaleFactor = 5.0f;
|
---|
144 |
|
---|
145 | position.z -= 3 * scaleFactor;
|
---|
146 | Matrix4x4 m = TranslationMatrix(position);
|
---|
147 |
|
---|
148 | Matrix4x4 s = ScaleMatrix(scaleFactor, scaleFactor, scaleFactor);
|
---|
149 | mSkyDome->GetTransform()->SetMatrix(s * m);
|
---|
150 |
|
---|
151 | cgGLSetParameter3f(sLightDirParam, sunDir.x, sunDir.y, sunDir.z);
|
---|
152 | cgGLSetParameter2f(sThetaSunParam, sun_theta.first, sun_theta.second);
|
---|
153 | cgGLSetParameter3f(sZenithColorParam, zenithColor.x, zenithColor.y, zenithColor.z);
|
---|
154 |
|
---|
155 | cgGLSetParameter3f(sAColorParam, ABCDE[0].x, ABCDE[0].y, ABCDE[0].z);
|
---|
156 | cgGLSetParameter3f(sBColorParam, ABCDE[1].x, ABCDE[1].y, ABCDE[1].z);
|
---|
157 | cgGLSetParameter3f(sCColorParam, ABCDE[2].x, ABCDE[2].y, ABCDE[2].z);
|
---|
158 | cgGLSetParameter3f(sDColorParam, ABCDE[3].x, ABCDE[3].y, ABCDE[3].z);
|
---|
159 | cgGLSetParameter3f(sEColorParam, ABCDE[4].x, ABCDE[4].y, ABCDE[4].z);
|
---|
160 |
|
---|
161 | if (state->GetRenderPassType() == RenderState::DEFERRED)
|
---|
162 | {
|
---|
163 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
164 | cgGLBindProgram(sCgMrtFragmentSkyDomeProgram);
|
---|
165 |
|
---|
166 | }
|
---|
167 |
|
---|
168 | if (!scaleToRange)
|
---|
169 | {
|
---|
170 | // use tone mapping
|
---|
171 | cgGLSetParameter1f(sMultiplierParam, 1.0f);
|
---|
172 | }
|
---|
173 | else
|
---|
174 | {
|
---|
175 | // no tone mapping => scale
|
---|
176 | cgGLSetParameter1f(sMultiplierParam, 8e-5f);
|
---|
177 | }
|
---|
178 |
|
---|
179 | cgGLEnableProfile(RenderState::sCgVertexProfile);
|
---|
180 | cgGLBindProgram(sCgSkyProgram);
|
---|
181 |
|
---|
182 | // Render sky dome.
|
---|
183 | mSkyDome->Render(state);
|
---|
184 |
|
---|
185 | // Render additively blended sun disc.
|
---|
186 | //RenderSunDisk(sunDir, camera);
|
---|
187 |
|
---|
188 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
189 | }
|
---|
190 |
|
---|
191 |
|
---|
192 | void SkyPreetham::RenderSunDisk(const Vector3 &sunDir, Camera *camera)
|
---|
193 | {
|
---|
194 | // Move skybox with camera.
|
---|
195 | Vector3 position = camera->GetPosition();
|
---|
196 |
|
---|
197 | const float scaleFactor = 100.0f;
|
---|
198 | position.z -= 10 * scaleFactor;
|
---|
199 |
|
---|
200 | // Set world matrix to sun disc position.
|
---|
201 | Matrix4x4 sunPos = TranslationMatrix(position);
|
---|
202 |
|
---|
203 | Vector3 ndir = -Normalize(sunDir);
|
---|
204 |
|
---|
205 | const float pitch = -atan2(ndir.x, ndir.y);
|
---|
206 | const float yaw = atan2(ndir.z, sqrt((ndir.x * ndir.x) + (ndir.y * ndir.y)));
|
---|
207 |
|
---|
208 | Matrix4x4 roty = RotationYMatrix(pitch);
|
---|
209 | Matrix4x4 rotx = RotationXMatrix(yaw);
|
---|
210 |
|
---|
211 | sunPos *= roty;
|
---|
212 | sunPos *= rotx;;
|
---|
213 |
|
---|
214 | //sunposition.rotateAroundY(-D().getScene().getLight().getHorizontalOrientation() + 90.0f);
|
---|
215 | //sunposition.rotateAroundX(-D().getScene().getLight().getVerticalOrientation());
|
---|
216 |
|
---|
217 | glMatrixMode(GL_MODELVIEW);
|
---|
218 | glMultMatrixf((float *)sunPos.x);
|
---|
219 |
|
---|
220 | float ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
|
---|
221 |
|
---|
222 | glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
|
---|
223 |
|
---|
224 | //Float size = 0.5f + (1.0f - lightdirection.y) * 0.5f;
|
---|
225 |
|
---|
226 | //mSunEffect->setVariableVector3("LightDiffuseColor", D().getScene().getLight().getDiffuseColor());
|
---|
227 | //mSunEffect->setVariableFloat("SunSize", size);
|
---|
228 | //mSunEffect->setVariableTexture2D("SunTexture", mSunTexture);
|
---|
229 |
|
---|
230 | /*mSunEffect->activate();
|
---|
231 |
|
---|
232 | mSunQuad->render();
|
---|
233 |
|
---|
234 | mSunEffect->deactivate();
|
---|
235 | F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);*/
|
---|
236 |
|
---|
237 | glPopMatrix();
|
---|
238 | }
|
---|
239 |
|
---|
240 |
|
---|
241 | void SkyPreetham::CreateSunQuad()
|
---|
242 | {
|
---|
243 | /*
|
---|
244 | mSunQuad = new FVertexBuffer();
|
---|
245 | mSunQuad->setupPrimitiveType(FVertexBuffer::PRIMITIVES_TRIANGLES);
|
---|
246 | mSunQuad->setupVertexFormat(3, 0, 0, true);
|
---|
247 | mSunQuad->setupTexCoordSet(0, 2);
|
---|
248 | mSunQuad->setVertexBufferSize(4, 6);
|
---|
249 |
|
---|
250 | mSunQuad->setVertexPosition(0, FVector3(-0.1f, 0.1f, 1.0f));
|
---|
251 | mSunQuad->setVertexPosition(1, FVector3( 0.1f, 0.1f, 1.0f));
|
---|
252 | mSunQuad->setVertexPosition(2, FVector3(-0.1f, -0.1f, 1.0f));
|
---|
253 | mSunQuad->setVertexPosition(3, FVector3( 0.1f, -0.1f, 1.0f));
|
---|
254 | mSunQuad->setVertexTexCoord(0, 0, FVector2(0.0f, 0.0f));
|
---|
255 | mSunQuad->setVertexTexCoord(1, 0, FVector2(1.0f, 0.0f));
|
---|
256 | mSunQuad->setVertexTexCoord(2, 0, FVector2(0.0f, 1.0f));
|
---|
257 | mSunQuad->setVertexTexCoord(3, 0, FVector2(1.0f, 1.0f));
|
---|
258 |
|
---|
259 | mSunQuad->setIndex(0, 0);
|
---|
260 | mSunQuad->setIndex(1, 1);
|
---|
261 | mSunQuad->setIndex(2, 2);
|
---|
262 | mSunQuad->setIndex(3, 2);
|
---|
263 | mSunQuad->setIndex(4, 1);
|
---|
264 | mSunQuad->setIndex(5, 3);
|
---|
265 | */
|
---|
266 | }
|
---|
267 |
|
---|
268 |
|
---|
269 | void SkyPreetham::ComputeFactors(const Vector3 &sunDir,
|
---|
270 | Vector3 &zenithColor,
|
---|
271 | vector<Vector3> &ABCDE,
|
---|
272 | std::pair<float, float> &sunThetha) const
|
---|
273 | {
|
---|
274 | sunThetha.first = acos(sunDir.z);
|
---|
275 |
|
---|
276 | const float cos_theta = cos(sunThetha.first);
|
---|
277 | sunThetha.second = cos_theta * cos_theta;
|
---|
278 |
|
---|
279 | zenithColor.x = ( 0.00165f * CBQ(sunThetha.first) - 0.00374f * SQR(sunThetha.first) + 0.00208f * sunThetha.first + 0.0f) * SQR(mTurbidity) +
|
---|
280 | (-0.02902f * CBQ(sunThetha.first) + 0.06377f * SQR(sunThetha.first) - 0.03202f * sunThetha.first + 0.00394f) * mTurbidity +
|
---|
281 | ( 0.11693f * CBQ(sunThetha.first) - 0.21196f * SQR(sunThetha.first) + 0.06052f * sunThetha.first + 0.25885f);
|
---|
282 |
|
---|
283 | zenithColor.y = ( 0.00275f * CBQ(sunThetha.first) - 0.00610f * SQR(sunThetha.first) + 0.00316f * sunThetha.first + 0.0f) * SQR(mTurbidity) +
|
---|
284 | (-0.04214f * CBQ(sunThetha.first) + 0.08970f * SQR(sunThetha.first) - 0.04153f * sunThetha.first + 0.00515f) * mTurbidity +
|
---|
285 | ( 0.15346f * CBQ(sunThetha.first) - 0.26756f * SQR(sunThetha.first) + 0.06669f * sunThetha.first + 0.26688f);
|
---|
286 |
|
---|
287 | zenithColor.z = (float)((4.0453f * mTurbidity - 4.9710f) * tan((4.0f / 9.0f - mTurbidity / 120.0f) *
|
---|
288 | (M_PI - 2.0f * sunThetha.first)) - 0.2155f * mTurbidity + 2.4192f);
|
---|
289 |
|
---|
290 | // convert kcd/m² to cd/m²
|
---|
291 | zenithColor.z *= 1000.0f;
|
---|
292 |
|
---|
293 | ABCDE.push_back(Vector3(-0.01925 * mTurbidity - 0.25922, -0.01669 * mTurbidity - 0.26078, 0.17872 * mTurbidity - 1.46303));
|
---|
294 | ABCDE.push_back(Vector3(-0.06651 * mTurbidity + 0.00081, -0.09495 * mTurbidity + 0.00921, -0.35540 * mTurbidity + 0.42749));
|
---|
295 | ABCDE.push_back(Vector3(-0.00041 * mTurbidity + 0.21247, -0.00792 * mTurbidity + 0.21023, -0.02266 * mTurbidity + 5.32505));
|
---|
296 | ABCDE.push_back(Vector3(-0.06409 * mTurbidity - 0.89887, -0.04405 * mTurbidity - 1.65369, 0.12064 * mTurbidity - 2.57705));
|
---|
297 | ABCDE.push_back(Vector3(-0.00325 * mTurbidity + 0.04517, -0.01092 * mTurbidity + 0.05291, -0.06696 * mTurbidity + 0.37027));
|
---|
298 | }
|
---|
299 |
|
---|
300 |
|
---|
301 | void SkyPreetham::ComputeSunColor(const Vector3 &sunDir,
|
---|
302 | Vector3 &ambient,
|
---|
303 | Vector3 &diffuse,
|
---|
304 | bool scaleToRange) const
|
---|
305 | {
|
---|
306 | // sunDir is sun direction
|
---|
307 | // ambient color: shadow color
|
---|
308 | // diffuse color: sun color
|
---|
309 | pair<float, float> sun_theta;
|
---|
310 | Vector3 zenithColor;
|
---|
311 | vector<Vector3> ABCDE;
|
---|
312 |
|
---|
313 | ComputeFactors(sunDir, zenithColor, ABCDE, sun_theta);
|
---|
314 |
|
---|
315 | Vector3 zenith_XYZ;
|
---|
316 |
|
---|
317 | zenith_XYZ.x = (zenithColor.x / zenithColor.y) * zenithColor.z;
|
---|
318 | zenith_XYZ.y = zenithColor.z;
|
---|
319 | zenith_XYZ.z = ((1.0f - zenithColor.x - zenithColor.y) / zenithColor.y) * zenithColor.z;
|
---|
320 |
|
---|
321 | ambient.x = 3.240479f * zenith_XYZ.x - 1.537150f * zenith_XYZ.y - 0.498535f * zenith_XYZ.z;
|
---|
322 | ambient.y = -0.969256f * zenith_XYZ.x + 1.875992f * zenith_XYZ.y + 0.041556f * zenith_XYZ.z;
|
---|
323 | ambient.z = 0.055648f * zenith_XYZ.x - 0.204043f * zenith_XYZ.y + 1.057311f * zenith_XYZ.z;
|
---|
324 |
|
---|
325 | // downscale ambient color
|
---|
326 | if (scaleToRange)
|
---|
327 | ambient *= 2e-5f;
|
---|
328 | else
|
---|
329 | ambient *= 1e-1f;
|
---|
330 |
|
---|
331 | // simulate the sun intensity by modulating the ambient term.
|
---|
332 | ambient *= (10.0f - 9.0f * DotProd(sunDir, Vector3::UNIT_Z()));
|
---|
333 | //ambient += Vector3(0.2f);
|
---|
334 |
|
---|
335 | Vector3 num;
|
---|
336 |
|
---|
337 | num.x = (1.0f + ABCDE[0].x * exp(ABCDE[1].x / sunDir.z)) * (1.0f + ABCDE[2].x)+ ABCDE[4].x;
|
---|
338 | num.y = (1.0f + ABCDE[0].y * exp(ABCDE[1].y / sunDir.z)) * (1.0f + ABCDE[2].y)+ ABCDE[4].y;
|
---|
339 | num.z = (1.0f + ABCDE[0].z * exp(ABCDE[1].z / sunDir.z)) * (1.0f + ABCDE[2].z)+ ABCDE[4].z;
|
---|
340 |
|
---|
341 | Vector3 den;
|
---|
342 |
|
---|
343 | den.x = (1.0f + ABCDE[0].x * exp(ABCDE[1].x)) * (1.0f + ABCDE[2].x * exp(ABCDE[3].x * sun_theta.first) + ABCDE[4].x * sun_theta.second);
|
---|
344 | den.y = (1.0f + ABCDE[0].y * exp(ABCDE[1].y)) * (1.0f + ABCDE[2].y * exp(ABCDE[3].y * sun_theta.first) + ABCDE[4].y * sun_theta.second);
|
---|
345 | den.z = (1.0f + ABCDE[0].z * exp(ABCDE[1].z)) * (1.0f + ABCDE[2].z * exp(ABCDE[3].z * sun_theta.first) + ABCDE[4].z * sun_theta.second);
|
---|
346 |
|
---|
347 |
|
---|
348 | Vector3 xyY = zenithColor * num / den;
|
---|
349 |
|
---|
350 | Vector3 XYZ;
|
---|
351 |
|
---|
352 | XYZ.x = (xyY.x / xyY.y) * xyY.z;
|
---|
353 | XYZ.y = xyY.z;
|
---|
354 | XYZ.z = ((1.0f - xyY.x - xyY.y) / xyY.y) * xyY.z;
|
---|
355 |
|
---|
356 |
|
---|
357 | /////////////
|
---|
358 | //-- transform to rgb
|
---|
359 |
|
---|
360 | Vector3 color;
|
---|
361 | color.x = 3.240479f * XYZ.x - 1.537150f * XYZ.y - 0.498535f * XYZ.z;
|
---|
362 | color.y = -0.969256f * XYZ.x + 1.875992f * XYZ.y + 0.041556f *XYZ.z;
|
---|
363 | color.z = 0.055648f * XYZ.x - 0.204043f * XYZ.y + 1.057311f * XYZ.z;
|
---|
364 |
|
---|
365 | // Calculate final sun diffuse color.
|
---|
366 | if (scaleToRange)
|
---|
367 | diffuse = color * 5e-2f;
|
---|
368 | else
|
---|
369 | diffuse = color * 3e-1f;
|
---|
370 |
|
---|
371 | // diffuse component should be more saturated (and less blueish) for high sun positions
|
---|
372 | diffuse.x *= 1.3f;
|
---|
373 | diffuse.z *= 0.7f;
|
---|
374 |
|
---|
375 | // scale diffuse component in order to make sky look less bright in relation to
|
---|
376 | // the geometry in the evening
|
---|
377 | diffuse *= (2.0f - 1.0f * DotProd(sunDir, Vector3::UNIT_Z()));
|
---|
378 |
|
---|
379 | //cout << "diffuse: " << Magnitude(diffuse) << " ambient: " << Magnitude(ambient) << endl;
|
---|
380 | } |
---|