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

Revision 2960, 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
9using namespace CHCDemoEngine;
10using namespace std;
11
12
13
14static CGprogram sCgSkyProgram = NULL;
15static CGprogram sCgMrtFragmentSkyDomeProgram = NULL;
16
17
18static CGparameter sLightDirParam;
19static CGparameter sThetaSunParam;
20static CGparameter sZenithColorParam;
21static CGparameter sAColorParam;
22static CGparameter sBColorParam;
23static CGparameter sCColorParam;
24static CGparameter sDColorParam;
25static CGparameter sEColorParam;
26
27
28inline float CBQ(float x)
29{
30        return x * x * x;
31}
32
33
34inline float SQR(float x)
35{
36        return x * x ;
37}
38
39
40void SkyPreetham::Init(CGcontext context)
41{       
42        sCgSkyProgram =
43                cgCreateProgramFromFile(context,
44                                                                CG_SOURCE,
45                                                                "src/shaders/sky_preetham.cg",
46                                                                RenderState::sCgVertexProfile,
47                                                                "default_vs",
48                                                                NULL);
49
50        if (sCgSkyProgram != NULL)
51        {
52                cgGLLoadProgram(sCgSkyProgram);
53
54                sLightDirParam = cgGetNamedParameter(sCgSkyProgram, "lightDir");
55                sThetaSunParam = cgGetNamedParameter(sCgSkyProgram, "thetaSun");
56                sZenithColorParam = cgGetNamedParameter(sCgSkyProgram, "zenithColor");
57                sAColorParam = cgGetNamedParameter(sCgSkyProgram, "aColor");
58                sBColorParam = cgGetNamedParameter(sCgSkyProgram, "bColor");
59                sCColorParam = cgGetNamedParameter(sCgSkyProgram, "cColor");
60                sDColorParam = cgGetNamedParameter(sCgSkyProgram, "dColor");
61                sEColorParam = cgGetNamedParameter(sCgSkyProgram, "eColor");
62        }
63        else
64                cerr << "sky program failed to load" << endl;
65
66        sCgMrtFragmentSkyDomeProgram =
67                cgCreateProgramFromFile(context,
68                                                                CG_SOURCE,
69                                                                "src/shaders/sky_preetham.cg",
70                                                                RenderState::sCgFragmentProfile,
71                                                                "frag_skydome",
72                                                                NULL);
73
74        if (sCgMrtFragmentSkyDomeProgram != NULL)
75        {
76                cgGLLoadProgram(sCgMrtFragmentSkyDomeProgram);
77
78                /*sMaxDepthParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "maxDepth");
79                Material::sDiffuseParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "diffuse");
80                Material::sAmbientParam = cgGetNamedParameter(RenderState::sCgMrtFragmentProgram, "ambient");
81
82                cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist);
83                */
84        }
85        else
86                cerr << "fragment skyprogram failed to load" << endl;
87}
88
89
90SkyPreetham::SkyPreetham(float turbitity, SceneEntity *skyDome):
91mSkyDome(skyDome),
92mTurbidity(turbitity)
93//      mSunQuad(NULL)
94{
95        CreateSunQuad();
96}
97
98
99SkyPreetham::~SkyPreetham()
100{
101        //DEL_PTR(mSunQuad);
102}
103
104
105void SkyPreetham::RenderSkyDome(const Vector3 &sunDir, Camera *camera, RenderState *state)
106{
107        pair<float, float> sun_theta;
108        Vector3 zenithColor;
109        vector<Vector3> ABCDE;
110        ComputeFactors(sunDir, zenithColor, ABCDE, sun_theta);
111
112        // Move skybox with camera.
113        Vector3 position = camera->GetPosition();
114
115        const float scaleFactor = 80.0f;
116
117        position.z -= 3 * scaleFactor;
118        Matrix4x4 m = TranslationMatrix(position);
119
120        Matrix4x4 s = ScaleMatrix(scaleFactor, scaleFactor, scaleFactor);
121        mSkyDome->GetTransform()->SetMatrix(s * m);
122       
123        cgGLSetParameter3f(sLightDirParam, sunDir.x, sunDir.y, sunDir.z);
124        cgGLSetParameter2f(sThetaSunParam, sun_theta.first, sun_theta.second);
125        cgGLSetParameter3f(sZenithColorParam, zenithColor.x, zenithColor.y, zenithColor.z);
126
127        cgGLSetParameter3f(sAColorParam, ABCDE[0].x, ABCDE[0].y, ABCDE[0].z);
128        cgGLSetParameter3f(sBColorParam, ABCDE[1].x, ABCDE[1].y, ABCDE[1].z);
129        cgGLSetParameter3f(sCColorParam, ABCDE[2].x, ABCDE[2].y, ABCDE[2].z);
130        cgGLSetParameter3f(sDColorParam, ABCDE[3].x, ABCDE[3].y, ABCDE[3].z);
131        cgGLSetParameter3f(sEColorParam, ABCDE[4].x, ABCDE[4].y, ABCDE[4].z);
132
133        cgGLEnableProfile(RenderState::sCgVertexProfile);
134        cgGLBindProgram(sCgSkyProgram);
135
136       
137        if (state->GetRenderPassType() == RenderState::DEFERRED)
138        {
139                cgGLEnableProfile(RenderState::sCgFragmentProfile);
140                cgGLBindProgram(sCgMrtFragmentSkyDomeProgram);
141        }       
142
143        // Render sky dome.
144        mSkyDome->Render(state);
145
146        // Render additively blended sun disc.
147        //RenderSunDisk(sunDir, camera);
148
149        cgGLDisableProfile(RenderState::sCgFragmentProfile);
150}
151
152
153void SkyPreetham::RenderSunDisk(const Vector3 &sunDir, Camera *camera)
154{
155        // Move skybox with camera.
156        Vector3 position = camera->GetPosition();
157
158        const float scaleFactor = 100.0f;
159        position.z -= 10 * scaleFactor;
160
161        // Set world matrix to sun disc position.
162        Matrix4x4 sunPos = TranslationMatrix(position);
163
164        Vector3 ndir = -Normalize(sunDir);
165
166        const float pitch = -atan2(ndir.x, ndir.y);
167        const float yaw = atan2(ndir.z, sqrt((ndir.x * ndir.x) + (ndir.y * ndir.y)));
168
169        Matrix4x4 roty = RotationYMatrix(pitch);
170        Matrix4x4 rotx = RotationXMatrix(yaw);
171
172        sunPos *= roty;
173        sunPos *= rotx;;
174
175        //sunposition.rotateAroundY(-D().getScene().getLight().getHorizontalOrientation() + 90.0f);
176        //sunposition.rotateAroundX(-D().getScene().getLight().getVerticalOrientation());
177       
178        glMatrixMode(GL_MODELVIEW);
179        glMultMatrixf((float *)sunPos.x);
180
181        float ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
182
183        glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
184
185        //Float size = 0.5f + (1.0f - lightdirection.y) * 0.5f;
186
187        //mSunEffect->setVariableVector3("LightDiffuseColor", D().getScene().getLight().getDiffuseColor());
188        //mSunEffect->setVariableFloat("SunSize", size);
189        //mSunEffect->setVariableTexture2D("SunTexture", mSunTexture);
190
191        /*mSunEffect->activate();
192       
193        mSunQuad->render();
194
195        mSunEffect->deactivate();
196        F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);*/
197
198        glPopMatrix();
199}
200
201
202void SkyPreetham::CreateSunQuad()
203{
204        /*
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        */
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, Vector3 &ambient, Vector3 &diffuse) const
263{
264        // sunDir is sun direction
265        // ambient color: shadow color
266        // diffuse color: sun color
267        pair<float, float> sun_theta;
268        Vector3 zenithColor;
269        vector<Vector3> ABCDE;
270
271        ComputeFactors(sunDir, zenithColor, ABCDE, sun_theta);
272
273        ambient = zenithColor;
274
275        Vector3 zenith_XYZ;                                                                                             
276
277        zenith_XYZ.x = (ambient.x / ambient.y) * ambient.z;                                                                       
278        zenith_XYZ.y = ambient.z;                                                                                         
279        zenith_XYZ.z = ((1.0f - ambient.x - ambient.y) / ambient.y) * ambient.z;                                                               
280
281        ambient.x =  3.240479f * zenith_XYZ.x - 1.537150f * zenith_XYZ.y - 0.498535f * zenith_XYZ.z;
282        ambient.y = -0.969256f * zenith_XYZ.x + 1.875992f * zenith_XYZ.y + 0.041556f * zenith_XYZ.z;   
283        ambient.z =  0.055648f * zenith_XYZ.x - 0.204043f * zenith_XYZ.y + 1.057311f * zenith_XYZ.z;
284
285        // downscale ambient color
286        ambient *= 5e-5f;
287
288        // simulate the sun intensity by modulating the ambient term.
289        ambient *= (1.0f - 0.9f * DotProd(sunDir, Vector3::UNIT_Z()));
290        ambient += Vector3(0.2f);
291
292        Vector3 num;
293         
294        num.x = (1.0f + ABCDE[0].x * exp(ABCDE[1].x / sunDir.z)) * (1.0f + ABCDE[2].x)+ ABCDE[4].x;   
295        num.y = (1.0f + ABCDE[0].y * exp(ABCDE[1].y / sunDir.z)) * (1.0f + ABCDE[2].y)+ ABCDE[4].y;   
296        num.z = (1.0f + ABCDE[0].z * exp(ABCDE[1].z / sunDir.z)) * (1.0f + ABCDE[2].z)+ ABCDE[4].z;   
297
298        Vector3 den;
299
300        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); 
301        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);   
302        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);   
303
304
305        Vector3 xyY = (num / den) * zenithColor;   
306
307        Vector3 XYZ;                                                                                             
308
309        XYZ.x = (xyY.x / xyY.y) * xyY.z;                                                                       
310        XYZ.y = xyY.z;                                                                                         
311        XYZ.z = ((1.0f - xyY.x - xyY.y) / xyY.y) * xyY.z;                                                               
312
313
314        Vector3 color; 
315        color.x =  3.240479f * XYZ.x - 1.537150f * XYZ.y - 0.498535f * XYZ.z;
316        color.y = -0.969256f * XYZ.x + 1.875992f * XYZ.y + 0.041556f *XYZ.z;   
317        color.z =  0.055648f * XYZ.x - 0.204043f * XYZ.y + 1.057311f * XYZ.z;
318
319        // Calculate final sun diffuse color.
320        diffuse = color * 0.00017f;
321}
Note: See TracBrowser for help on using the repository browser.