#include "SkyPreetham.h" #include "common.h" #include "Vector3.h" //#include //#include inline float CBQ(float x) { return x * x * x; } inline float SQR(float x) { return x * x ; } using namespace CHCDemoEngine; SkyPreetham::SkyPreetham(float turbitity): // mEffect(NULL), mTurbidity(turbitity) // mSunEffect(NULL), // mSunTexture(NULL), // mSunQuad(NULL) { /* mEffect = D().getScene().getEffect("../effects/sky_preetham.fx"); mVertexBuffer = D().getScene().getVertexBuffer("../models/sky.vbo"); mSunEffect = D().getScene().getEffect("../effects/sky_sun.fx"); mSunTexture = D().getScene().getTexture2D("../textures/sky_sun.png"); */ CreateSunQuad(); } SkyPreetham::~SkyPreetham() { //DEL_PTR(mSunQuad); } void SkyPreetham::Compute(const Vector3 &sunDir) { float sun_theta[2]; sun_theta[0] = acos(sunDir.y); const float cos_theta = cos(sun_theta[0]); sun_theta[1] = cos_theta * cos_theta; Vector3 zenithColor; zenithColor.x = ( 0.00165f * CBQ(sun_theta[0]) - 0.00374f * SQR(sun_theta[0]) + 0.00208f * sun_theta[0] + 0.0f) * SQR(mTurbidity) + (-0.02902f * CBQ(sun_theta[0]) + 0.06377f * SQR(sun_theta[0]) - 0.03202f * sun_theta[0] + 0.00394f) * mTurbidity + ( 0.11693f * CBQ(sun_theta[0]) - 0.21196f * SQR(sun_theta[0]) + 0.06052f * sun_theta[0] + 0.25885f); zenithColor.y = (0.00275f * CBQ(sun_theta[0]) - 0.00610f * SQR(sun_theta[0]) + 0.00316f * sun_theta[0] + 0.0f) * SQR(mTurbidity) + (-0.04214f * CBQ(sun_theta[0]) + 0.08970f * SQR(sun_theta[0]) - 0.04153f * sun_theta[0] + 0.00515f) * mTurbidity + ( 0.15346f * CBQ(sun_theta[0]) - 0.26756f * SQR(sun_theta[0]) + 0.06669f * sun_theta[0] + 0.26688f); zenithColor.z = (float)((4.0453f * mTurbidity - 4.9710f) * tan((4.0f / 9.0f - mTurbidity / 120.0f) * (M_PI - 2.0f * sun_theta[0])) - 0.2155f * mTurbidity + 2.4192f); // convert kcd/mē to cd/mē zenithColor.z *= 1000.0f; double ABCDE_x[5], ABCDE_y[5], ABCDE_Y[5]; ABCDE_x[0] = -0.01925 * mTurbidity - 0.25922; ABCDE_x[1] = -0.06651 * mTurbidity + 0.00081; ABCDE_x[2] = -0.00041 * mTurbidity + 0.21247; ABCDE_x[3] = -0.06409 * mTurbidity - 0.89887; ABCDE_x[4] = -0.00325 * mTurbidity + 0.04517; ABCDE_y[0] = -0.01669 * mTurbidity - 0.26078; ABCDE_y[1] = -0.09495 * mTurbidity + 0.00921; ABCDE_y[2] = -0.00792 * mTurbidity + 0.21023; ABCDE_y[3] = -0.04405 * mTurbidity - 1.65369; ABCDE_y[4] = -0.01092 * mTurbidity + 0.05291; ABCDE_Y[0] = 0.17872 * mTurbidity - 1.46303; ABCDE_Y[1] = -0.35540 * mTurbidity + 0.42749; ABCDE_Y[2] = -0.02266 * mTurbidity + 5.32505; ABCDE_Y[3] = 0.12064 * mTurbidity - 2.57705; ABCDE_Y[4] = -0.06696 * mTurbidity + 0.37027; /* F().getRenderDevice().setDefaultDepthStencilMode(FRenderDevice::DEPTHSTENCIL_TEST_NO_OVERWRITE); // Move skybox with camera. mPosition = D().getScene().getCamera().getPosition(); mPosition.y -= 2.0; setTransform(); mEffect->setVariableVector3("LightDirection", D().getScene().getLight().getDirection()); mEffect->setVariableVector2("ThetaSun", sun_theta); mEffect->setVariableVector3("ZenithColor", zenithColor); mEffect->setVariableVector3("AColor",FVector3((FFloat)ABCDE_x[0], (FFloat)ABCDE_y[0], (FFloat)ABCDE_Y[0])); mEffect->setVariableVector3("BColor",FVector3((FFloat)ABCDE_x[1], (FFloat)ABCDE_y[1], (FFloat)ABCDE_Y[1])); mEffect->setVariableVector3("CColor",FVector3((FFloat)ABCDE_x[2], (FFloat)ABCDE_y[2], (FFloat)ABCDE_Y[2])); mEffect->setVariableVector3("DColor",FVector3((FFloat)ABCDE_x[3], (FFloat)ABCDE_y[3], (FFloat)ABCDE_Y[3])); mEffect->setVariableVector3("EColor",FVector3((FFloat)ABCDE_x[4], (FFloat)ABCDE_y[4], (FFloat)ABCDE_Y[4])); mEffect->activate(); F().getRenderDevice().setDefaultPolygonMode(true, FRenderDevice::CULL_NONE); // Render sky dome. mVertexBuffer->render(); // Render additively blended sun disc. renderSunDisk(); F().getRenderDevice().setDefaultPolygonMode(true, FRenderDevice::CULL_BACKFACES); F().getRenderDevice().setDefaultDepthStencilMode(FRenderDevice::DEPTHSTENCIL_TEST_AND_OVERWRITE); */ } void SkyPreetham::RenderSunDisk() { /* F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_ADDITIVE); // Set world matrix to sun disc position. FMatrix4 sunposition; sunposition.translate(D().getScene().getCamera().getPosition()); sunposition.rotateAroundY(-D().getScene().getLight().getHorizontalOrientation() + 90.0f); sunposition.rotateAroundX(-D().getScene().getLight().getVerticalOrientation()); F().getRenderDevice().getTransform().setWorldMatrix(sunposition); FVector3 lightdirection = D().getScene().getLight().getDirection(); FFloat size = 0.5f + (1.0f - lightdirection.y) * 0.5f; mSunEffect->setVariableVector3("LightDiffuseColor", D().getScene().getLight().getDiffuseColor()); mSunEffect->setVariableFloat("SunSize", size); mSunEffect->setVariableTexture2D("SunTexture", mSunTexture); mSunEffect->activate(); mSunQuad->render(); mSunEffect->deactivate(); F().getRenderDevice().setDefaultBlendingMode(FRenderDevice::BLENDING_NONE); */ } void SkyPreetham::CreateSunQuad() { /* mSunQuad = new FVertexBuffer(); mSunQuad->setupPrimitiveType(FVertexBuffer::PRIMITIVES_TRIANGLES); mSunQuad->setupVertexFormat(3, 0, 0, true); mSunQuad->setupTexCoordSet(0, 2); mSunQuad->setVertexBufferSize(4, 6); mSunQuad->setVertexPosition(0, FVector3(-0.1f, 0.1f, 1.0f)); mSunQuad->setVertexPosition(1, FVector3( 0.1f, 0.1f, 1.0f)); mSunQuad->setVertexPosition(2, FVector3(-0.1f, -0.1f, 1.0f)); mSunQuad->setVertexPosition(3, FVector3( 0.1f, -0.1f, 1.0f)); mSunQuad->setVertexTexCoord(0, 0, FVector2(0.0f, 0.0f)); mSunQuad->setVertexTexCoord(1, 0, FVector2(1.0f, 0.0f)); mSunQuad->setVertexTexCoord(2, 0, FVector2(0.0f, 1.0f)); mSunQuad->setVertexTexCoord(3, 0, FVector2(1.0f, 1.0f)); mSunQuad->setIndex(0, 0); mSunQuad->setIndex(1, 1); mSunQuad->setIndex(2, 2); mSunQuad->setIndex(3, 2); mSunQuad->setIndex(4, 1); mSunQuad->setIndex(5, 3); */ }