[2954] | 1 | #include "SkyPreetham.h"
|
---|
[2955] | 2 | #include "common.h"
|
---|
| 3 | #include "Vector3.h"
|
---|
[2957] | 4 | #include "SceneEntity.h"
|
---|
| 5 | #include "Transform3.h"
|
---|
| 6 | #include "Camera.h"
|
---|
| 7 | #include "RenderState.h"
|
---|
[2955] | 8 |
|
---|
[3021] | 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 |
|
---|
[2957] | 21 | using namespace CHCDemoEngine;
|
---|
| 22 | using namespace std;
|
---|
[2955] | 23 |
|
---|
[2954] | 24 |
|
---|
[2957] | 25 | static CGprogram sCgSkyProgram = NULL;
|
---|
[2959] | 26 | static CGprogram sCgMrtFragmentSkyDomeProgram = NULL;
|
---|
| 27 |
|
---|
| 28 |
|
---|
[2957] | 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;
|
---|
[2982] | 37 | static CGparameter sMultiplierParam;
|
---|
[2957] | 38 |
|
---|
[3021] | 39 | ShaderContainer SkyPreetham::sShaders;
|
---|
[2957] | 40 |
|
---|
[2982] | 41 |
|
---|
[2955] | 42 | inline float CBQ(float x)
|
---|
| 43 | {
|
---|
| 44 | return x * x * x;
|
---|
| 45 | }
|
---|
[2954] | 46 |
|
---|
| 47 |
|
---|
[2955] | 48 | inline float SQR(float x)
|
---|
| 49 | {
|
---|
| 50 | return x * x ;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 |
|
---|
[3021] | 54 | void SkyPreetham::InitCG(CGcontext context)
|
---|
[2957] | 55 | {
|
---|
[3021] | 56 | ShaderProgram *pr;
|
---|
| 57 |
|
---|
[2957] | 58 | sCgSkyProgram =
|
---|
| 59 | cgCreateProgramFromFile(context,
|
---|
| 60 | CG_SOURCE,
|
---|
| 61 | "src/shaders/sky_preetham.cg",
|
---|
| 62 | RenderState::sCgVertexProfile,
|
---|
| 63 | "default_vs",
|
---|
| 64 | NULL);
|
---|
[2954] | 65 |
|
---|
[2957] | 66 | if (sCgSkyProgram != NULL)
|
---|
| 67 | {
|
---|
| 68 | cgGLLoadProgram(sCgSkyProgram);
|
---|
[2954] | 69 |
|
---|
[2957] | 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");
|
---|
[2982] | 78 |
|
---|
| 79 | sMultiplierParam = cgGetNamedParameter(sCgSkyProgram, "multiplier");
|
---|
[3021] | 80 |
|
---|
| 81 | pr = new ShaderProgram(sCgSkyProgram);
|
---|
| 82 | sShaders.push_back(pr);
|
---|
[2957] | 83 | }
|
---|
| 84 | else
|
---|
| 85 | cerr << "sky program failed to load" << endl;
|
---|
[2959] | 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);
|
---|
[2982] | 98 | //cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
|
---|
[3021] | 99 |
|
---|
| 100 | pr = new ShaderProgram(sCgMrtFragmentSkyDomeProgram);
|
---|
| 101 | sShaders.push_back(pr);
|
---|
[2959] | 102 | }
|
---|
| 103 | else
|
---|
| 104 | cerr << "fragment skyprogram failed to load" << endl;
|
---|
[2957] | 105 | }
|
---|
| 106 |
|
---|
| 107 |
|
---|
[3021] | 108 | void SkyPreetham::ReleaseCG()
|
---|
| 109 | {
|
---|
| 110 | CLEAR_CONTAINER(sShaders);
|
---|
| 111 | }
|
---|
| 112 |
|
---|
| 113 |
|
---|
[2957] | 114 | SkyPreetham::SkyPreetham(float turbitity, SceneEntity *skyDome):
|
---|
| 115 | mSkyDome(skyDome),
|
---|
| 116 | mTurbidity(turbitity)
|
---|
[2981] | 117 | //, mSunQuad(NULL)
|
---|
[2954] | 118 | {
|
---|
[2955] | 119 | CreateSunQuad();
|
---|
[2954] | 120 | }
|
---|
| 121 |
|
---|
| 122 |
|
---|
| 123 | SkyPreetham::~SkyPreetham()
|
---|
| 124 | {
|
---|
[2955] | 125 | //DEL_PTR(mSunQuad);
|
---|
[2954] | 126 | }
|
---|
| 127 |
|
---|
| 128 |
|
---|
[2983] | 129 | void SkyPreetham::RenderSkyDome(const Vector3 &sunDir,
|
---|
| 130 | Camera *camera,
|
---|
| 131 | RenderState *state,
|
---|
| 132 | bool scaleToRange)
|
---|
[2954] | 133 | {
|
---|
[2960] | 134 | pair<float, float> sun_theta;
|
---|
[2955] | 135 | Vector3 zenithColor;
|
---|
[2960] | 136 | vector<Vector3> ABCDE;
|
---|
| 137 | ComputeFactors(sunDir, zenithColor, ABCDE, sun_theta);
|
---|
[2954] | 138 |
|
---|
| 139 | // Move skybox with camera.
|
---|
[2957] | 140 | Vector3 position = camera->GetPosition();
|
---|
[2954] | 141 |
|
---|
[2959] | 142 | const float scaleFactor = 80.0f;
|
---|
[2961] | 143 | //const float scaleFactor = 5.0f;
|
---|
[2954] | 144 |
|
---|
[2959] | 145 | position.z -= 3 * scaleFactor;
|
---|
[2957] | 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);
|
---|
[2960] | 152 | cgGLSetParameter2f(sThetaSunParam, sun_theta.first, sun_theta.second);
|
---|
[2957] | 153 | cgGLSetParameter3f(sZenithColorParam, zenithColor.x, zenithColor.y, zenithColor.z);
|
---|
| 154 |
|
---|
[2960] | 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);
|
---|
[2957] | 160 |
|
---|
[2959] | 161 | if (state->GetRenderPassType() == RenderState::DEFERRED)
|
---|
| 162 | {
|
---|
| 163 | cgGLEnableProfile(RenderState::sCgFragmentProfile);
|
---|
| 164 | cgGLBindProgram(sCgMrtFragmentSkyDomeProgram);
|
---|
[2983] | 165 |
|
---|
| 166 | }
|
---|
| 167 |
|
---|
| 168 | if (!scaleToRange)
|
---|
| 169 | {
|
---|
| 170 | // use tone mapping
|
---|
[2982] | 171 | cgGLSetParameter1f(sMultiplierParam, 1.0f);
|
---|
[2983] | 172 | }
|
---|
| 173 | else
|
---|
[2982] | 174 | {
|
---|
| 175 | // no tone mapping => scale
|
---|
| 176 | cgGLSetParameter1f(sMultiplierParam, 8e-5f);
|
---|
| 177 | }
|
---|
[2954] | 178 |
|
---|
[2982] | 179 | cgGLEnableProfile(RenderState::sCgVertexProfile);
|
---|
| 180 | cgGLBindProgram(sCgSkyProgram);
|
---|
| 181 |
|
---|
[2954] | 182 | // Render sky dome.
|
---|
[2957] | 183 | mSkyDome->Render(state);
|
---|
[2954] | 184 |
|
---|
| 185 | // Render additively blended sun disc.
|
---|
[2958] | 186 | //RenderSunDisk(sunDir, camera);
|
---|
[2959] | 187 |
|
---|
| 188 | cgGLDisableProfile(RenderState::sCgFragmentProfile);
|
---|
[2954] | 189 | }
|
---|
| 190 |
|
---|
| 191 |
|
---|
[2958] | 192 | void SkyPreetham::RenderSunDisk(const Vector3 &sunDir, Camera *camera)
|
---|
[2954] | 193 | {
|
---|
[2958] | 194 | // Move skybox with camera.
|
---|
| 195 | Vector3 position = camera->GetPosition();
|
---|
[2954] | 196 |
|
---|
[2958] | 197 | const float scaleFactor = 100.0f;
|
---|
| 198 | position.z -= 10 * scaleFactor;
|
---|
| 199 |
|
---|
[2954] | 200 | // Set world matrix to sun disc position.
|
---|
[2958] | 201 | Matrix4x4 sunPos = TranslationMatrix(position);
|
---|
[2954] | 202 |
|
---|
[2958] | 203 | Vector3 ndir = -Normalize(sunDir);
|
---|
[2954] | 204 |
|
---|
[2958] | 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)));
|
---|
[2954] | 207 |
|
---|
[2958] | 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());
|
---|
[2954] | 216 |
|
---|
[2958] | 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 |
|
---|
[2954] | 232 | mSunQuad->render();
|
---|
| 233 |
|
---|
| 234 | mSunEffect->deactivate();
|
---|
[2958] | 235 | F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);*/
|
---|
[2954] | 236 |
|
---|
[2958] | 237 | glPopMatrix();
|
---|
[2954] | 238 | }
|
---|
| 239 |
|
---|
| 240 |
|
---|
[2955] | 241 | void SkyPreetham::CreateSunQuad()
|
---|
[2954] | 242 | {
|
---|
[2955] | 243 | /*
|
---|
[2954] | 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);
|
---|
[2955] | 265 | */
|
---|
[2960] | 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 |
|
---|
[2983] | 301 | void SkyPreetham::ComputeSunColor(const Vector3 &sunDir,
|
---|
| 302 | Vector3 &ambient,
|
---|
| 303 | Vector3 &diffuse,
|
---|
| 304 | bool scaleToRange) const
|
---|
[2960] | 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 |
|
---|
[2967] | 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;
|
---|
[2960] | 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
|
---|
[2982] | 326 | if (scaleToRange)
|
---|
| 327 | ambient *= 2e-5f;
|
---|
[2981] | 328 | else
|
---|
| 329 | ambient *= 1e-1f;
|
---|
[2960] | 330 |
|
---|
| 331 | // simulate the sun intensity by modulating the ambient term.
|
---|
[2970] | 332 | ambient *= (10.0f - 9.0f * DotProd(sunDir, Vector3::UNIT_Z()));
|
---|
[2968] | 333 | //ambient += Vector3(0.2f);
|
---|
[2960] | 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 |
|
---|
[2967] | 348 | Vector3 xyY = zenithColor * num / den;
|
---|
[2960] | 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 |
|
---|
[2967] | 357 | /////////////
|
---|
| 358 | //-- transform to rgb
|
---|
| 359 |
|
---|
[2960] | 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.
|
---|
[2982] | 366 | if (scaleToRange)
|
---|
| 367 | diffuse = color * 5e-2f;
|
---|
[2981] | 368 | else
|
---|
| 369 | diffuse = color * 3e-1f;
|
---|
| 370 |
|
---|
[3017] | 371 | // diffuse component should be more saturated (and less blueish) for high sun positions
|
---|
[3020] | 372 | diffuse.x *= 1.3f;
|
---|
| 373 | diffuse.z *= 0.7f;
|
---|
[3017] | 374 |
|
---|
| 375 | // scale diffuse component in order to make sky look less bright in relation to
|
---|
| 376 | // the geometry in the evening
|
---|
[2971] | 377 | diffuse *= (2.0f - 1.0f * DotProd(sunDir, Vector3::UNIT_Z()));
|
---|
[2967] | 378 |
|
---|
[2968] | 379 | //cout << "diffuse: " << Magnitude(diffuse) << " ambient: " << Magnitude(ambient) << endl;
|
---|
[2954] | 380 | } |
---|