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

Revision 2983, 11.7 KB checked in by mattausch, 16 years ago (diff)
RevLine 
[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
[2957]9using namespace CHCDemoEngine;
10using namespace std;
[2955]11
[2954]12
13
[2957]14static CGprogram sCgSkyProgram = NULL;
[2959]15static CGprogram sCgMrtFragmentSkyDomeProgram = NULL;
16
17
[2957]18static CGparameter sLightDirParam;
19static CGparameter sThetaSunParam;
20static CGparameter sZenithColorParam;
21static CGparameter sAColorParam;
22static CGparameter sBColorParam;
23static CGparameter sCColorParam;
24static CGparameter sDColorParam;
25static CGparameter sEColorParam;
[2982]26static CGparameter sMultiplierParam;
[2957]27
28
[2982]29
[2955]30inline float CBQ(float x)
31{
32        return x * x * x;
33}
[2954]34
35
[2955]36inline float SQR(float x)
37{
38        return x * x ;
39}
40
41
[2957]42void 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);
[2954]51
[2957]52        if (sCgSkyProgram != NULL)
53        {
54                cgGLLoadProgram(sCgSkyProgram);
[2954]55
[2957]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");
[2982]64
65                sMultiplierParam = cgGetNamedParameter(sCgSkyProgram, "multiplier");
[2957]66        }
67        else
68                cerr << "sky program failed to load" << endl;
[2959]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);
[2982]81                //cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
[2959]82        }
83        else
84                cerr << "fragment skyprogram failed to load" << endl;
[2957]85}
86
87
88SkyPreetham::SkyPreetham(float turbitity, SceneEntity *skyDome):
89mSkyDome(skyDome),
90mTurbidity(turbitity)
[2981]91//, mSunQuad(NULL)
[2954]92{
[2955]93        CreateSunQuad();
[2954]94}
95
96
97SkyPreetham::~SkyPreetham()
98{
[2955]99        //DEL_PTR(mSunQuad);
[2954]100}
101
102
[2983]103void SkyPreetham::RenderSkyDome(const Vector3 &sunDir,
104                                                                Camera *camera,
105                                                                RenderState *state,
106                                                                bool scaleToRange)
[2954]107{
[2960]108        pair<float, float> sun_theta;
[2955]109        Vector3 zenithColor;
[2960]110        vector<Vector3> ABCDE;
111        ComputeFactors(sunDir, zenithColor, ABCDE, sun_theta);
[2954]112
113        // Move skybox with camera.
[2957]114        Vector3 position = camera->GetPosition();
[2954]115
[2959]116        const float scaleFactor = 80.0f;
[2961]117        //const float scaleFactor = 5.0f;
[2954]118
[2959]119        position.z -= 3 * scaleFactor;
[2957]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);
[2960]126        cgGLSetParameter2f(sThetaSunParam, sun_theta.first, sun_theta.second);
[2957]127        cgGLSetParameter3f(sZenithColorParam, zenithColor.x, zenithColor.y, zenithColor.z);
128
[2960]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);
[2957]134
[2959]135        if (state->GetRenderPassType() == RenderState::DEFERRED)
136        {
137                cgGLEnableProfile(RenderState::sCgFragmentProfile);
138                cgGLBindProgram(sCgMrtFragmentSkyDomeProgram);
[2983]139
140        }
141
142        if (!scaleToRange)
143        {       
144                // use tone mapping
[2982]145                cgGLSetParameter1f(sMultiplierParam, 1.0f);
[2983]146        }
147        else
[2982]148        {
149                // no tone mapping => scale
150                cgGLSetParameter1f(sMultiplierParam, 8e-5f);
151        }
[2954]152
[2982]153        cgGLEnableProfile(RenderState::sCgVertexProfile);
154        cgGLBindProgram(sCgSkyProgram);
155
[2954]156        // Render sky dome.
[2957]157        mSkyDome->Render(state);
[2954]158
159        // Render additively blended sun disc.
[2958]160        //RenderSunDisk(sunDir, camera);
[2959]161
162        cgGLDisableProfile(RenderState::sCgFragmentProfile);
[2954]163}
164
165
[2958]166void SkyPreetham::RenderSunDisk(const Vector3 &sunDir, Camera *camera)
[2954]167{
[2958]168        // Move skybox with camera.
169        Vector3 position = camera->GetPosition();
[2954]170
[2958]171        const float scaleFactor = 100.0f;
172        position.z -= 10 * scaleFactor;
173
[2954]174        // Set world matrix to sun disc position.
[2958]175        Matrix4x4 sunPos = TranslationMatrix(position);
[2954]176
[2958]177        Vector3 ndir = -Normalize(sunDir);
[2954]178
[2958]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)));
[2954]181
[2958]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());
[2954]190       
[2958]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       
[2954]206        mSunQuad->render();
207
208        mSunEffect->deactivate();
[2958]209        F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);*/
[2954]210
[2958]211        glPopMatrix();
[2954]212}
213
214
[2955]215void SkyPreetham::CreateSunQuad()
[2954]216{
[2955]217        /*
[2954]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);
[2955]239        */
[2960]240}
241
242
243void 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
[2983]275void SkyPreetham::ComputeSunColor(const Vector3 &sunDir,
276                                                                  Vector3 &ambient,
277                                                                  Vector3 &diffuse,
278                                                                  bool scaleToRange) const
[2960]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
[2967]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;                                                               
[2960]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
[2982]300        if (scaleToRange)
301                ambient *= 2e-5f;
[2981]302        else
303                ambient *= 1e-1f;
[2960]304
305        // simulate the sun intensity by modulating the ambient term.
[2970]306        ambient *= (10.0f - 9.0f * DotProd(sunDir, Vector3::UNIT_Z()));
[2968]307        //ambient += Vector3(0.2f);
[2960]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
[2967]322        Vector3 xyY = zenithColor * num / den;   
[2960]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
[2967]331        /////////////
332        //-- transform to rgb
333
[2960]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.
[2982]340        if (scaleToRange)
341                diffuse = color * 5e-2f;
[2981]342        else
343                diffuse = color * 3e-1f;
344
[2971]345        diffuse *= (2.0f - 1.0f * DotProd(sunDir, Vector3::UNIT_Z()));
[2967]346
[2968]347        //cout << "diffuse: " << Magnitude(diffuse) << " ambient: " << Magnitude(ambient) << endl;
[2954]348}
Note: See TracBrowser for help on using the repository browser.