source: GTP/trunk/App/Demos/Vis/FriendlyCulling/src/SkyPreetham.cpp @ 3039

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