[2954] | 1 | #include "SkyPreetham.h"
|
---|
[2955] | 2 | #include "common.h"
|
---|
| 3 | #include "Vector3.h"
|
---|
[2957] | 4 | #include "SceneEntity.h"
|
---|
| 5 | #include "Transform3.h"
|
---|
| 6 | #include "Camera.h"
|
---|
| 7 | #include "RenderState.h"
|
---|
[2955] | 8 |
|
---|
[2957] | 9 | using namespace CHCDemoEngine;
|
---|
| 10 | using namespace std;
|
---|
[2955] | 11 |
|
---|
[2954] | 12 |
|
---|
| 13 |
|
---|
[2957] | 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 |
|
---|
[2955] | 25 | inline float CBQ(float x)
|
---|
| 26 | {
|
---|
| 27 | return x * x * x;
|
---|
| 28 | }
|
---|
[2954] | 29 |
|
---|
| 30 |
|
---|
[2955] | 31 | inline float SQR(float x)
|
---|
| 32 | {
|
---|
| 33 | return x * x ;
|
---|
| 34 | }
|
---|
| 35 |
|
---|
| 36 |
|
---|
[2957] | 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);
|
---|
[2954] | 46 |
|
---|
[2957] | 47 | if (sCgSkyProgram != NULL)
|
---|
| 48 | {
|
---|
| 49 | cgGLLoadProgram(sCgSkyProgram);
|
---|
[2954] | 50 |
|
---|
[2957] | 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)
|
---|
[2955] | 68 | // mSunQuad(NULL)
|
---|
[2954] | 69 | {
|
---|
[2955] | 70 | CreateSunQuad();
|
---|
[2954] | 71 | }
|
---|
| 72 |
|
---|
| 73 |
|
---|
| 74 | SkyPreetham::~SkyPreetham()
|
---|
| 75 | {
|
---|
[2955] | 76 | //DEL_PTR(mSunQuad);
|
---|
[2954] | 77 | }
|
---|
| 78 |
|
---|
| 79 |
|
---|
[2957] | 80 | void SkyPreetham::Compute(const Vector3 &sunDir, Camera *camera, RenderState *state)
|
---|
[2954] | 81 | {
|
---|
[2955] | 82 | float sun_theta[2];
|
---|
[2954] | 83 |
|
---|
[2957] | 84 | sun_theta[0] = acos(sunDir.z);
|
---|
[2954] | 85 |
|
---|
[2957] | 86 | const float cos_theta = cos(sun_theta[0]);
|
---|
[2955] | 87 | sun_theta[1] = cos_theta * cos_theta;
|
---|
[2954] | 88 |
|
---|
[2955] | 89 | Vector3 zenithColor;
|
---|
[2954] | 90 |
|
---|
[2955] | 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);
|
---|
[2954] | 94 |
|
---|
[2955] | 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);
|
---|
[2954] | 101 |
|
---|
| 102 | // convert kcd/m² to cd/m²
|
---|
[2956] | 103 | zenithColor.z *= 1000.0f;
|
---|
[2954] | 104 |
|
---|
| 105 | double ABCDE_x[5], ABCDE_y[5], ABCDE_Y[5];
|
---|
| 106 |
|
---|
[2955] | 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;
|
---|
[2954] | 112 |
|
---|
[2955] | 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;
|
---|
[2954] | 118 |
|
---|
[2955] | 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;
|
---|
[2954] | 124 |
|
---|
| 125 |
|
---|
| 126 | // Move skybox with camera.
|
---|
[2957] | 127 | Vector3 position = camera->GetPosition();
|
---|
[2954] | 128 |
|
---|
[2957] | 129 | const float scaleFactor = 100.0f;
|
---|
[2954] | 130 |
|
---|
[2957] | 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);
|
---|
[2958] | 149 |
|
---|
| 150 |
|
---|
[2954] | 151 |
|
---|
| 152 | // Render sky dome.
|
---|
[2957] | 153 | mSkyDome->Render(state);
|
---|
[2954] | 154 |
|
---|
| 155 | // Render additively blended sun disc.
|
---|
[2958] | 156 | //RenderSunDisk(sunDir, camera);
|
---|
[2954] | 157 | }
|
---|
| 158 |
|
---|
| 159 |
|
---|
[2958] | 160 | void SkyPreetham::RenderSunDisk(const Vector3 &sunDir, Camera *camera)
|
---|
[2954] | 161 | {
|
---|
[2958] | 162 | // Move skybox with camera.
|
---|
| 163 | Vector3 position = camera->GetPosition();
|
---|
[2954] | 164 |
|
---|
[2958] | 165 | const float scaleFactor = 100.0f;
|
---|
| 166 | position.z -= 10 * scaleFactor;
|
---|
| 167 |
|
---|
[2954] | 168 | // Set world matrix to sun disc position.
|
---|
[2958] | 169 | Matrix4x4 sunPos = TranslationMatrix(position);
|
---|
[2954] | 170 |
|
---|
[2958] | 171 | Vector3 ndir = -Normalize(sunDir);
|
---|
[2954] | 172 |
|
---|
[2958] | 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)));
|
---|
[2954] | 175 |
|
---|
[2958] | 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());
|
---|
[2954] | 184 |
|
---|
[2958] | 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 |
|
---|
[2954] | 200 | mSunQuad->render();
|
---|
| 201 |
|
---|
| 202 | mSunEffect->deactivate();
|
---|
[2958] | 203 | F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE);*/
|
---|
[2954] | 204 |
|
---|
[2958] | 205 | glPopMatrix();
|
---|
[2954] | 206 | }
|
---|
| 207 |
|
---|
| 208 |
|
---|
[2955] | 209 | void SkyPreetham::CreateSunQuad()
|
---|
[2954] | 210 | {
|
---|
[2955] | 211 | /*
|
---|
[2954] | 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);
|
---|
[2955] | 233 | */
|
---|
[2954] | 234 | } |
---|