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 |
|
---|
9 | using namespace CHCDemoEngine;
|
---|
10 | using namespace std;
|
---|
11 |
|
---|
12 |
|
---|
13 |
|
---|
14 | static CGprogram sCgSkyProgram = NULL;
|
---|
15 | static CGparameter sLightDirParam;
|
---|
16 | static CGparameter sThetaSunParam;
|
---|
17 | static CGparameter sZenithColorParam;
|
---|
18 | static CGparameter sAColorParam;
|
---|
19 | static CGparameter sBColorParam;
|
---|
20 | static CGparameter sCColorParam;
|
---|
21 | static CGparameter sDColorParam;
|
---|
22 | static CGparameter sEColorParam;
|
---|
23 |
|
---|
24 |
|
---|
25 | inline float CBQ(float x)
|
---|
26 | {
|
---|
27 | return x * x * x;
|
---|
28 | }
|
---|
29 |
|
---|
30 |
|
---|
31 | inline float SQR(float x)
|
---|
32 | {
|
---|
33 | return x * x ;
|
---|
34 | }
|
---|
35 |
|
---|
36 |
|
---|
37 | void 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 |
|
---|
65 | SkyPreetham::SkyPreetham(float turbitity, SceneEntity *skyDome):
|
---|
66 | mSkyDome(skyDome),
|
---|
67 | mTurbidity(turbitity)
|
---|
68 | // mSunQuad(NULL)
|
---|
69 | {
|
---|
70 | CreateSunQuad();
|
---|
71 | }
|
---|
72 |
|
---|
73 |
|
---|
74 | SkyPreetham::~SkyPreetham()
|
---|
75 | {
|
---|
76 | //DEL_PTR(mSunQuad);
|
---|
77 | }
|
---|
78 |
|
---|
79 |
|
---|
80 | void SkyPreetham::Compute(const Vector3 &sunDir, Camera *camera, RenderState *state)
|
---|
81 | {
|
---|
82 | float sun_theta[2];
|
---|
83 |
|
---|
84 | sun_theta[0] = acos(sunDir.z);
|
---|
85 |
|
---|
86 | const float cos_theta = cos(sun_theta[0]);
|
---|
87 | sun_theta[1] = cos_theta * cos_theta;
|
---|
88 |
|
---|
89 | Vector3 zenithColor;
|
---|
90 |
|
---|
91 | zenithColor.x = ( 0.00165f * CBQ(sun_theta[0]) - 0.00374f * SQR(sun_theta[0]) + 0.00208f * sun_theta[0] + 0.0f) * SQR(mTurbidity) +
|
---|
92 | (-0.02902f * CBQ(sun_theta[0]) + 0.06377f * SQR(sun_theta[0]) - 0.03202f * sun_theta[0] + 0.00394f) * mTurbidity +
|
---|
93 | ( 0.11693f * CBQ(sun_theta[0]) - 0.21196f * SQR(sun_theta[0]) + 0.06052f * sun_theta[0] + 0.25885f);
|
---|
94 |
|
---|
95 | zenithColor.y = (0.00275f * CBQ(sun_theta[0]) - 0.00610f * SQR(sun_theta[0]) + 0.00316f * sun_theta[0] + 0.0f) * SQR(mTurbidity) +
|
---|
96 | (-0.04214f * CBQ(sun_theta[0]) + 0.08970f * SQR(sun_theta[0]) - 0.04153f * sun_theta[0] + 0.00515f) * mTurbidity +
|
---|
97 | ( 0.15346f * CBQ(sun_theta[0]) - 0.26756f * SQR(sun_theta[0]) + 0.06669f * sun_theta[0] + 0.26688f);
|
---|
98 |
|
---|
99 | zenithColor.z = (float)((4.0453f * mTurbidity - 4.9710f) * tan((4.0f / 9.0f - mTurbidity / 120.0f) *
|
---|
100 | (M_PI - 2.0f * sun_theta[0])) - 0.2155f * mTurbidity + 2.4192f);
|
---|
101 |
|
---|
102 | // convert kcd/m² to cd/m²
|
---|
103 | zenithColor.z *= 1000.0f;
|
---|
104 |
|
---|
105 | double ABCDE_x[5], ABCDE_y[5], ABCDE_Y[5];
|
---|
106 |
|
---|
107 | ABCDE_x[0] = -0.01925 * mTurbidity - 0.25922;
|
---|
108 | ABCDE_x[1] = -0.06651 * mTurbidity + 0.00081;
|
---|
109 | ABCDE_x[2] = -0.00041 * mTurbidity + 0.21247;
|
---|
110 | ABCDE_x[3] = -0.06409 * mTurbidity - 0.89887;
|
---|
111 | ABCDE_x[4] = -0.00325 * mTurbidity + 0.04517;
|
---|
112 |
|
---|
113 | ABCDE_y[0] = -0.01669 * mTurbidity - 0.26078;
|
---|
114 | ABCDE_y[1] = -0.09495 * mTurbidity + 0.00921;
|
---|
115 | ABCDE_y[2] = -0.00792 * mTurbidity + 0.21023;
|
---|
116 | ABCDE_y[3] = -0.04405 * mTurbidity - 1.65369;
|
---|
117 | ABCDE_y[4] = -0.01092 * mTurbidity + 0.05291;
|
---|
118 |
|
---|
119 | ABCDE_Y[0] = 0.17872 * mTurbidity - 1.46303;
|
---|
120 | ABCDE_Y[1] = -0.35540 * mTurbidity + 0.42749;
|
---|
121 | ABCDE_Y[2] = -0.02266 * mTurbidity + 5.32505;
|
---|
122 | ABCDE_Y[3] = 0.12064 * mTurbidity - 2.57705;
|
---|
123 | ABCDE_Y[4] = -0.06696 * mTurbidity + 0.37027;
|
---|
124 |
|
---|
125 |
|
---|
126 | // Move skybox with camera.
|
---|
127 | Vector3 position = camera->GetPosition();
|
---|
128 |
|
---|
129 | const float scaleFactor = 100.0f;
|
---|
130 |
|
---|
131 | position.z -= 10 * scaleFactor;
|
---|
132 | Matrix4x4 m = TranslationMatrix(position);
|
---|
133 |
|
---|
134 | Matrix4x4 s = ScaleMatrix(scaleFactor, scaleFactor, scaleFactor);
|
---|
135 | mSkyDome->GetTransform()->SetMatrix(s * m);
|
---|
136 |
|
---|
137 | cgGLSetParameter3f(sLightDirParam, sunDir.x, sunDir.y, sunDir.z);
|
---|
138 | cgGLSetParameter2f(sThetaSunParam, sun_theta[0], sun_theta[1]);
|
---|
139 | cgGLSetParameter3f(sZenithColorParam, zenithColor.x, zenithColor.y, zenithColor.z);
|
---|
140 |
|
---|
141 | cgGLSetParameter3f(sAColorParam, ABCDE_x[0], ABCDE_y[0], ABCDE_Y[0]);
|
---|
142 | cgGLSetParameter3f(sBColorParam, ABCDE_x[1], ABCDE_y[1], ABCDE_Y[1]);
|
---|
143 | cgGLSetParameter3f(sCColorParam, ABCDE_x[2], ABCDE_y[2], ABCDE_Y[2]);
|
---|
144 | cgGLSetParameter3f(sDColorParam, ABCDE_x[3], ABCDE_y[3], ABCDE_Y[3]);
|
---|
145 | cgGLSetParameter3f(sEColorParam, ABCDE_x[4], ABCDE_y[4], ABCDE_Y[4]);
|
---|
146 |
|
---|
147 | cgGLEnableProfile(RenderState::sCgVertexProfile);
|
---|
148 | cgGLBindProgram(sCgSkyProgram);
|
---|
149 |
|
---|
150 |
|
---|
151 |
|
---|
152 | // Render sky dome.
|
---|
153 | mSkyDome->Render(state);
|
---|
154 |
|
---|
155 | // Render additively blended sun disc.
|
---|
156 | //RenderSunDisk(sunDir, camera);
|
---|
157 | }
|
---|
158 |
|
---|
159 |
|
---|
160 | void SkyPreetham::RenderSunDisk(const Vector3 &sunDir, Camera *camera)
|
---|
161 | {
|
---|
162 | // Move skybox with camera.
|
---|
163 | Vector3 position = camera->GetPosition();
|
---|
164 |
|
---|
165 | const float scaleFactor = 100.0f;
|
---|
166 | position.z -= 10 * scaleFactor;
|
---|
167 |
|
---|
168 | // Set world matrix to sun disc position.
|
---|
169 | Matrix4x4 sunPos = TranslationMatrix(position);
|
---|
170 |
|
---|
171 | Vector3 ndir = -Normalize(sunDir);
|
---|
172 |
|
---|
173 | const float pitch = -atan2(ndir.x, ndir.y);
|
---|
174 | const float yaw = atan2(ndir.z, sqrt((ndir.x * ndir.x) + (ndir.y * ndir.y)));
|
---|
175 |
|
---|
176 | Matrix4x4 roty = RotationYMatrix(pitch);
|
---|
177 | Matrix4x4 rotx = RotationXMatrix(yaw);
|
---|
178 |
|
---|
179 | sunPos *= roty;
|
---|
180 | sunPos *= rotx;;
|
---|
181 |
|
---|
182 | //sunposition.rotateAroundY(-D().getScene().getLight().getHorizontalOrientation() + 90.0f);
|
---|
183 | //sunposition.rotateAroundX(-D().getScene().getLight().getVerticalOrientation());
|
---|
184 |
|
---|
185 | glMatrixMode(GL_MODELVIEW);
|
---|
186 | glMultMatrixf((float *)sunPos.x);
|
---|
187 |
|
---|
188 | float ambient[] = {1.0f, 1.0f, 1.0f, 1.0f};
|
---|
189 |
|
---|
190 | glMaterialfv(GL_FRONT, GL_AMBIENT, ambient);
|
---|
191 |
|
---|
192 | //Float size = 0.5f + (1.0f - lightdirection.y) * 0.5f;
|
---|
193 |
|
---|
194 | //mSunEffect->setVariableVector3("LightDiffuseColor", D().getScene().getLight().getDiffuseColor());
|
---|
195 | //mSunEffect->setVariableFloat("SunSize", size);
|
---|
196 | //mSunEffect->setVariableTexture2D("SunTexture", mSunTexture);
|
---|
197 |
|
---|
198 | /*mSunEffect->activate();
|
---|
199 |
|
---|
200 | mSunQuad->render();
|
---|
201 |
|
---|
202 | mSunEffect->deactivate();
|
---|
203 | F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);*/
|
---|
204 |
|
---|
205 | glPopMatrix();
|
---|
206 | }
|
---|
207 |
|
---|
208 |
|
---|
209 | void SkyPreetham::CreateSunQuad()
|
---|
210 | {
|
---|
211 | /*
|
---|
212 | mSunQuad = new FVertexBuffer();
|
---|
213 | mSunQuad->setupPrimitiveType(FVertexBuffer::PRIMITIVES_TRIANGLES);
|
---|
214 | mSunQuad->setupVertexFormat(3, 0, 0, true);
|
---|
215 | mSunQuad->setupTexCoordSet(0, 2);
|
---|
216 | mSunQuad->setVertexBufferSize(4, 6);
|
---|
217 |
|
---|
218 | mSunQuad->setVertexPosition(0, FVector3(-0.1f, 0.1f, 1.0f));
|
---|
219 | mSunQuad->setVertexPosition(1, FVector3( 0.1f, 0.1f, 1.0f));
|
---|
220 | mSunQuad->setVertexPosition(2, FVector3(-0.1f, -0.1f, 1.0f));
|
---|
221 | mSunQuad->setVertexPosition(3, FVector3( 0.1f, -0.1f, 1.0f));
|
---|
222 | mSunQuad->setVertexTexCoord(0, 0, FVector2(0.0f, 0.0f));
|
---|
223 | mSunQuad->setVertexTexCoord(1, 0, FVector2(1.0f, 0.0f));
|
---|
224 | mSunQuad->setVertexTexCoord(2, 0, FVector2(0.0f, 1.0f));
|
---|
225 | mSunQuad->setVertexTexCoord(3, 0, FVector2(1.0f, 1.0f));
|
---|
226 |
|
---|
227 | mSunQuad->setIndex(0, 0);
|
---|
228 | mSunQuad->setIndex(1, 1);
|
---|
229 | mSunQuad->setIndex(2, 2);
|
---|
230 | mSunQuad->setIndex(3, 2);
|
---|
231 | mSunQuad->setIndex(4, 1);
|
---|
232 | mSunQuad->setIndex(5, 3);
|
---|
233 | */
|
---|
234 | } |
---|