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

Revision 2981, 11.3 KB checked in by mattausch, 16 years ago (diff)

debug version

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