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