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