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

Revision 2970, 11.5 KB checked in by mattausch, 16 years ago (diff)

tone mapper working better

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