1 | #include "SkyPreetham.h"
|
---|
2 | #include "common.h"
|
---|
3 | #include "Vector3.h"
|
---|
4 |
|
---|
5 |
|
---|
6 | //#include <rendering/effect/FEffect.h>
|
---|
7 | //#include <rendering/FVertexbuffer.h>
|
---|
8 |
|
---|
9 |
|
---|
10 | inline float CBQ(float x)
|
---|
11 | {
|
---|
12 | return x * x * x;
|
---|
13 | }
|
---|
14 |
|
---|
15 |
|
---|
16 | inline float SQR(float x)
|
---|
17 | {
|
---|
18 | return x * x ;
|
---|
19 | }
|
---|
20 |
|
---|
21 |
|
---|
22 | using namespace CHCDemoEngine;
|
---|
23 |
|
---|
24 |
|
---|
25 | SkyPreetham::SkyPreetham(float turbitity):
|
---|
26 | // mEffect(NULL),
|
---|
27 | mTurbidity(turbitity)
|
---|
28 | // mSunEffect(NULL),
|
---|
29 | // mSunTexture(NULL),
|
---|
30 | // mSunQuad(NULL)
|
---|
31 | {
|
---|
32 | /* mEffect = D().getScene().getEffect("../effects/sky_preetham.fx");
|
---|
33 |
|
---|
34 | mVertexBuffer = D().getScene().getVertexBuffer("../models/sky.vbo");
|
---|
35 |
|
---|
36 | mSunEffect = D().getScene().getEffect("../effects/sky_sun.fx");
|
---|
37 | mSunTexture = D().getScene().getTexture2D("../textures/sky_sun.png");
|
---|
38 | */
|
---|
39 | CreateSunQuad();
|
---|
40 | }
|
---|
41 |
|
---|
42 |
|
---|
43 | SkyPreetham::~SkyPreetham()
|
---|
44 | {
|
---|
45 | //DEL_PTR(mSunQuad);
|
---|
46 | }
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | void SkyPreetham::Compute(const Vector3 &sunDir)
|
---|
51 | {
|
---|
52 | float sun_theta[2];
|
---|
53 |
|
---|
54 | sun_theta[0] = acos(sunDir.y);
|
---|
55 |
|
---|
56 | const float cos_theta = cos(sun_theta[0]);
|
---|
57 | sun_theta[1] = cos_theta * cos_theta;
|
---|
58 |
|
---|
59 | Vector3 zenithColor;
|
---|
60 |
|
---|
61 | zenithColor.x = ( 0.00165f * CBQ(sun_theta[0]) - 0.00374f * SQR(sun_theta[0]) + 0.00208f * sun_theta[0] + 0.0f) * SQR(mTurbidity) +
|
---|
62 | (-0.02902f * CBQ(sun_theta[0]) + 0.06377f * SQR(sun_theta[0]) - 0.03202f * sun_theta[0] + 0.00394f) * mTurbidity +
|
---|
63 | ( 0.11693f * CBQ(sun_theta[0]) - 0.21196f * SQR(sun_theta[0]) + 0.06052f * sun_theta[0] + 0.25885f);
|
---|
64 |
|
---|
65 | zenithColor.y = (0.00275f * CBQ(sun_theta[0]) - 0.00610f * SQR(sun_theta[0]) + 0.00316f * sun_theta[0] + 0.0f) * SQR(mTurbidity) +
|
---|
66 | (-0.04214f * CBQ(sun_theta[0]) + 0.08970f * SQR(sun_theta[0]) - 0.04153f * sun_theta[0] + 0.00515f) * mTurbidity +
|
---|
67 | ( 0.15346f * CBQ(sun_theta[0]) - 0.26756f * SQR(sun_theta[0]) + 0.06669f * sun_theta[0] + 0.26688f);
|
---|
68 |
|
---|
69 | zenithColor.z = (float)((4.0453f * mTurbidity - 4.9710f) * tan((4.0f / 9.0f - mTurbidity / 120.0f) *
|
---|
70 | (M_PI - 2.0f * sun_theta[0])) - 0.2155f * mTurbidity + 2.4192f);
|
---|
71 |
|
---|
72 | // convert kcd/m² to cd/m²
|
---|
73 | zenithColor.z *= 1000.0f;
|
---|
74 |
|
---|
75 | double ABCDE_x[5], ABCDE_y[5], ABCDE_Y[5];
|
---|
76 |
|
---|
77 | ABCDE_x[0] = -0.01925 * mTurbidity - 0.25922;
|
---|
78 | ABCDE_x[1] = -0.06651 * mTurbidity + 0.00081;
|
---|
79 | ABCDE_x[2] = -0.00041 * mTurbidity + 0.21247;
|
---|
80 | ABCDE_x[3] = -0.06409 * mTurbidity - 0.89887;
|
---|
81 | ABCDE_x[4] = -0.00325 * mTurbidity + 0.04517;
|
---|
82 |
|
---|
83 | ABCDE_y[0] = -0.01669 * mTurbidity - 0.26078;
|
---|
84 | ABCDE_y[1] = -0.09495 * mTurbidity + 0.00921;
|
---|
85 | ABCDE_y[2] = -0.00792 * mTurbidity + 0.21023;
|
---|
86 | ABCDE_y[3] = -0.04405 * mTurbidity - 1.65369;
|
---|
87 | ABCDE_y[4] = -0.01092 * mTurbidity + 0.05291;
|
---|
88 |
|
---|
89 | ABCDE_Y[0] = 0.17872 * mTurbidity - 1.46303;
|
---|
90 | ABCDE_Y[1] = -0.35540 * mTurbidity + 0.42749;
|
---|
91 | ABCDE_Y[2] = -0.02266 * mTurbidity + 5.32505;
|
---|
92 | ABCDE_Y[3] = 0.12064 * mTurbidity - 2.57705;
|
---|
93 | ABCDE_Y[4] = -0.06696 * mTurbidity + 0.37027;
|
---|
94 |
|
---|
95 | /*
|
---|
96 | F().getRenderDevice().setDefaultDepthStencilMode(FRenderDevice::DEPTHSTENCIL_TEST_NO_OVERWRITE);
|
---|
97 |
|
---|
98 | // Move skybox with camera.
|
---|
99 | mPosition = D().getScene().getCamera().getPosition();
|
---|
100 |
|
---|
101 | mPosition.y -= 2.0;
|
---|
102 |
|
---|
103 | setTransform();
|
---|
104 |
|
---|
105 | mEffect->setVariableVector3("LightDirection", D().getScene().getLight().getDirection());
|
---|
106 | mEffect->setVariableVector2("ThetaSun", sun_theta);
|
---|
107 | mEffect->setVariableVector3("ZenithColor", zenithColor);
|
---|
108 | mEffect->setVariableVector3("AColor",FVector3((FFloat)ABCDE_x[0], (FFloat)ABCDE_y[0], (FFloat)ABCDE_Y[0]));
|
---|
109 | mEffect->setVariableVector3("BColor",FVector3((FFloat)ABCDE_x[1], (FFloat)ABCDE_y[1], (FFloat)ABCDE_Y[1]));
|
---|
110 | mEffect->setVariableVector3("CColor",FVector3((FFloat)ABCDE_x[2], (FFloat)ABCDE_y[2], (FFloat)ABCDE_Y[2]));
|
---|
111 | mEffect->setVariableVector3("DColor",FVector3((FFloat)ABCDE_x[3], (FFloat)ABCDE_y[3], (FFloat)ABCDE_Y[3]));
|
---|
112 | mEffect->setVariableVector3("EColor",FVector3((FFloat)ABCDE_x[4], (FFloat)ABCDE_y[4], (FFloat)ABCDE_Y[4]));
|
---|
113 |
|
---|
114 |
|
---|
115 | mEffect->activate();
|
---|
116 |
|
---|
117 | F().getRenderDevice().setDefaultPolygonMode(true, FRenderDevice::CULL_NONE);
|
---|
118 |
|
---|
119 | // Render sky dome.
|
---|
120 | mVertexBuffer->render();
|
---|
121 |
|
---|
122 | // Render additively blended sun disc.
|
---|
123 | renderSunDisk();
|
---|
124 |
|
---|
125 | F().getRenderDevice().setDefaultPolygonMode(true, FRenderDevice::CULL_BACKFACES);
|
---|
126 |
|
---|
127 |
|
---|
128 | F().getRenderDevice().setDefaultDepthStencilMode(FRenderDevice::DEPTHSTENCIL_TEST_AND_OVERWRITE);
|
---|
129 | */
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | void SkyPreetham::RenderSunDisk()
|
---|
134 | {
|
---|
135 | /*
|
---|
136 | F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_ADDITIVE);
|
---|
137 |
|
---|
138 | // Set world matrix to sun disc position.
|
---|
139 | FMatrix4 sunposition;
|
---|
140 | sunposition.translate(D().getScene().getCamera().getPosition());
|
---|
141 | sunposition.rotateAroundY(-D().getScene().getLight().getHorizontalOrientation() + 90.0f);
|
---|
142 | sunposition.rotateAroundX(-D().getScene().getLight().getVerticalOrientation());
|
---|
143 | F().getRenderDevice().getTransform().setWorldMatrix(sunposition);
|
---|
144 |
|
---|
145 | FVector3 lightdirection = D().getScene().getLight().getDirection();
|
---|
146 | FFloat size = 0.5f + (1.0f - lightdirection.y) * 0.5f;
|
---|
147 |
|
---|
148 | mSunEffect->setVariableVector3("LightDiffuseColor", D().getScene().getLight().getDiffuseColor());
|
---|
149 | mSunEffect->setVariableFloat("SunSize", size);
|
---|
150 | mSunEffect->setVariableTexture2D("SunTexture", mSunTexture);
|
---|
151 |
|
---|
152 | mSunEffect->activate();
|
---|
153 |
|
---|
154 | mSunQuad->render();
|
---|
155 |
|
---|
156 | mSunEffect->deactivate();
|
---|
157 |
|
---|
158 | F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);
|
---|
159 | */
|
---|
160 | }
|
---|
161 |
|
---|
162 |
|
---|
163 | void SkyPreetham::CreateSunQuad()
|
---|
164 | {
|
---|
165 | /*
|
---|
166 | mSunQuad = new FVertexBuffer();
|
---|
167 | mSunQuad->setupPrimitiveType(FVertexBuffer::PRIMITIVES_TRIANGLES);
|
---|
168 | mSunQuad->setupVertexFormat(3, 0, 0, true);
|
---|
169 | mSunQuad->setupTexCoordSet(0, 2);
|
---|
170 | mSunQuad->setVertexBufferSize(4, 6);
|
---|
171 |
|
---|
172 | mSunQuad->setVertexPosition(0, FVector3(-0.1f, 0.1f, 1.0f));
|
---|
173 | mSunQuad->setVertexPosition(1, FVector3( 0.1f, 0.1f, 1.0f));
|
---|
174 | mSunQuad->setVertexPosition(2, FVector3(-0.1f, -0.1f, 1.0f));
|
---|
175 | mSunQuad->setVertexPosition(3, FVector3( 0.1f, -0.1f, 1.0f));
|
---|
176 | mSunQuad->setVertexTexCoord(0, 0, FVector2(0.0f, 0.0f));
|
---|
177 | mSunQuad->setVertexTexCoord(1, 0, FVector2(1.0f, 0.0f));
|
---|
178 | mSunQuad->setVertexTexCoord(2, 0, FVector2(0.0f, 1.0f));
|
---|
179 | mSunQuad->setVertexTexCoord(3, 0, FVector2(1.0f, 1.0f));
|
---|
180 |
|
---|
181 | mSunQuad->setIndex(0, 0);
|
---|
182 | mSunQuad->setIndex(1, 1);
|
---|
183 | mSunQuad->setIndex(2, 2);
|
---|
184 | mSunQuad->setIndex(3, 2);
|
---|
185 | mSunQuad->setIndex(4, 1);
|
---|
186 | mSunQuad->setIndex(5, 3);
|
---|
187 | */
|
---|
188 | } |
---|