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

Revision 3036, 11.7 KB checked in by mattausch, 16 years ago (diff)

shader system starting to work

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