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

Revision 2957, 7.6 KB checked in by mattausch, 16 years ago (diff)

preetham working

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 CGparameter sLightDirParam;
16static CGparameter sThetaSunParam;
17static CGparameter sZenithColorParam;
18static CGparameter sAColorParam;
19static CGparameter sBColorParam;
20static CGparameter sCColorParam;
21static CGparameter sDColorParam;
22static CGparameter sEColorParam;
23
24
25inline float CBQ(float x)
26{
27        return x * x * x;
28}
29
30
31inline float SQR(float x)
32{
33        return x * x ;
34}
35
36
37void SkyPreetham::Init(CGcontext context)
38{       
39        sCgSkyProgram =
40                cgCreateProgramFromFile(context,
41                                                                CG_SOURCE,
42                                                                "src/shaders/sky_preetham.cg",
43                                                                RenderState::sCgVertexProfile,
44                                                                "default_vs",
45                                                                NULL);
46
47        if (sCgSkyProgram != NULL)
48        {
49                cgGLLoadProgram(sCgSkyProgram);
50
51                sLightDirParam = cgGetNamedParameter(sCgSkyProgram, "lightDir");
52                sThetaSunParam = cgGetNamedParameter(sCgSkyProgram, "thetaSun");
53                sZenithColorParam = cgGetNamedParameter(sCgSkyProgram, "zenithColor");
54                sAColorParam = cgGetNamedParameter(sCgSkyProgram, "aColor");
55                sBColorParam = cgGetNamedParameter(sCgSkyProgram, "bColor");
56                sCColorParam = cgGetNamedParameter(sCgSkyProgram, "cColor");
57                sDColorParam = cgGetNamedParameter(sCgSkyProgram, "dColor");
58                sEColorParam = cgGetNamedParameter(sCgSkyProgram, "eColor");
59        }
60        else
61                cerr << "sky program failed to load" << endl;
62}
63
64
65SkyPreetham::SkyPreetham(float turbitity, SceneEntity *skyDome):
66mSkyDome(skyDome),
67mTurbidity(turbitity)
68//      mSunQuad(NULL)
69{
70        CreateSunQuad();
71}
72
73
74SkyPreetham::~SkyPreetham()
75{
76        //DEL_PTR(mSunQuad);
77}
78
79
80void SkyPreetham::Compute(const Vector3 &sunDir, Camera *camera, RenderState *state)
81{
82        float sun_theta[2];
83
84        //sun_theta[0] = acos(sunDir.y);
85        sun_theta[0] = acos(sunDir.z);
86
87        const float cos_theta = cos(sun_theta[0]);
88        sun_theta[1] = cos_theta * cos_theta;
89
90        Vector3 zenithColor;
91
92        zenithColor.x = ( 0.00165f * CBQ(sun_theta[0]) - 0.00374f * SQR(sun_theta[0]) + 0.00208f * sun_theta[0] + 0.0f)     * SQR(mTurbidity) +
93                                    (-0.02902f * CBQ(sun_theta[0]) + 0.06377f * SQR(sun_theta[0]) - 0.03202f * sun_theta[0] + 0.00394f) * mTurbidity +
94                                        ( 0.11693f * CBQ(sun_theta[0]) - 0.21196f * SQR(sun_theta[0]) + 0.06052f * sun_theta[0] + 0.25885f);
95
96        zenithColor.y = (0.00275f  * CBQ(sun_theta[0]) - 0.00610f * SQR(sun_theta[0]) + 0.00316f * sun_theta[0] + 0.0f)     * SQR(mTurbidity) +
97                        (-0.04214f * CBQ(sun_theta[0]) + 0.08970f * SQR(sun_theta[0]) - 0.04153f * sun_theta[0] + 0.00515f) * mTurbidity +
98                            ( 0.15346f * CBQ(sun_theta[0]) - 0.26756f * SQR(sun_theta[0]) + 0.06669f * sun_theta[0] + 0.26688f);
99       
100        zenithColor.z  = (float)((4.0453f * mTurbidity - 4.9710f) *     tan((4.0f / 9.0f - mTurbidity / 120.0f) *
101                                                         (M_PI - 2.0f * sun_theta[0])) - 0.2155f * mTurbidity + 2.4192f);
102
103        // convert kcd/m² to cd/m²
104        zenithColor.z *= 1000.0f;
105
106        double ABCDE_x[5], ABCDE_y[5], ABCDE_Y[5];
107
108        ABCDE_x[0] = -0.01925 * mTurbidity - 0.25922;
109        ABCDE_x[1] = -0.06651 * mTurbidity + 0.00081;
110        ABCDE_x[2] = -0.00041 * mTurbidity + 0.21247;
111        ABCDE_x[3] = -0.06409 * mTurbidity - 0.89887;
112        ABCDE_x[4] = -0.00325 * mTurbidity + 0.04517;
113
114        ABCDE_y[0] = -0.01669 * mTurbidity - 0.26078;
115        ABCDE_y[1] = -0.09495 * mTurbidity + 0.00921;
116        ABCDE_y[2] = -0.00792 * mTurbidity + 0.21023;
117        ABCDE_y[3] = -0.04405 * mTurbidity - 1.65369;
118        ABCDE_y[4] = -0.01092 * mTurbidity + 0.05291;
119
120        ABCDE_Y[0] =  0.17872 * mTurbidity - 1.46303;
121        ABCDE_Y[1] = -0.35540 * mTurbidity + 0.42749;
122        ABCDE_Y[2] = -0.02266 * mTurbidity + 5.32505;
123        ABCDE_Y[3] =  0.12064 * mTurbidity - 2.57705;
124        ABCDE_Y[4] = -0.06696 * mTurbidity + 0.37027;
125
126        //F().getRenderDevice().setDefaultDepthStencilMode(FRenderDevice::DEPTHSTENCIL_TEST_NO_OVERWRITE);
127
128        // Move skybox with camera.
129        Vector3 position = camera->GetPosition();
130
131
132        const float scaleFactor = 100.0f;
133
134        position.z -= 10 * scaleFactor;
135        Matrix4x4 m = TranslationMatrix(position);
136
137        Matrix4x4 s = ScaleMatrix(scaleFactor, scaleFactor, scaleFactor);
138        mSkyDome->GetTransform()->SetMatrix(s * m);
139
140       
141        cgGLSetParameter3f(sLightDirParam, sunDir.x, sunDir.y, sunDir.z);
142        cgGLSetParameter2f(sThetaSunParam, sun_theta[0], sun_theta[1]);
143        cgGLSetParameter3f(sZenithColorParam, zenithColor.x, zenithColor.y, zenithColor.z);
144
145        cgGLSetParameter3f(sAColorParam, ABCDE_x[0], ABCDE_y[0], ABCDE_Y[0]);
146        cgGLSetParameter3f(sBColorParam, ABCDE_x[1], ABCDE_y[1], ABCDE_Y[1]);
147        cgGLSetParameter3f(sCColorParam, ABCDE_x[2], ABCDE_y[2], ABCDE_Y[2]);
148        cgGLSetParameter3f(sDColorParam, ABCDE_x[3], ABCDE_y[3], ABCDE_Y[3]);
149        cgGLSetParameter3f(sEColorParam, ABCDE_x[4], ABCDE_y[4], ABCDE_Y[4]);
150
151        /*mEffect->setVariableVector3("LightDirection", D().getScene().getLight().getDirection());
152        mEffect->setVariableVector2("ThetaSun", sun_theta);
153        mEffect->setVariableVector3("ZenithColor", zenithColor);
154        mEffect->setVariableVector3("AColor",FVector3((FFloat)ABCDE_x[0], (FFloat)ABCDE_y[0], (FFloat)ABCDE_Y[0]));
155        mEffect->setVariableVector3("BColor",FVector3((FFloat)ABCDE_x[1], (FFloat)ABCDE_y[1], (FFloat)ABCDE_Y[1]));
156        mEffect->setVariableVector3("CColor",FVector3((FFloat)ABCDE_x[2], (FFloat)ABCDE_y[2], (FFloat)ABCDE_Y[2]));
157        mEffect->setVariableVector3("DColor",FVector3((FFloat)ABCDE_x[3], (FFloat)ABCDE_y[3], (FFloat)ABCDE_Y[3]));
158        mEffect->setVariableVector3("EColor",FVector3((FFloat)ABCDE_x[4], (FFloat)ABCDE_y[4], (FFloat)ABCDE_Y[4]));
159*/
160
161        cgGLEnableProfile(RenderState::sCgVertexProfile);
162        cgGLBindProgram(sCgSkyProgram);
163
164        // Render sky dome.
165        mSkyDome->Render(state);
166
167        // Render additively blended sun disc.
168        RenderSunDisk();
169}
170
171
172void SkyPreetham::RenderSunDisk()
173{
174        /*
175        F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_ADDITIVE);
176
177        // Set world matrix to sun disc position.
178        FMatrix4 sunposition;
179        sunposition.translate(D().getScene().getCamera().getPosition());
180        sunposition.rotateAroundY(-D().getScene().getLight().getHorizontalOrientation() + 90.0f);
181        sunposition.rotateAroundX(-D().getScene().getLight().getVerticalOrientation());
182        F().getRenderDevice().getTransform().setWorldMatrix(sunposition);
183
184        FVector3 lightdirection = D().getScene().getLight().getDirection();
185        FFloat 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
197        F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);
198        */
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}
Note: See TracBrowser for help on using the repository browser.