#include "SkyPreetham.h" #include "common.h" #include "Vector3.h" #include "SceneEntity.h" #include "Transform3.h" #include "Camera.h" #include "RenderState.h" using namespace CHCDemoEngine; using namespace std; static CGprogram sCgSkyProgram = NULL; static CGprogram sCgMrtFragmentSkyDomeProgram = NULL; static CGparameter sLightDirParam; static CGparameter sThetaSunParam; static CGparameter sZenithColorParam; static CGparameter sAColorParam; static CGparameter sBColorParam; static CGparameter sCColorParam; static CGparameter sDColorParam; static CGparameter sEColorParam; inline float CBQ(float x) { return x * x * x; } inline float SQR(float x) { return x * x ; } void SkyPreetham::Init(CGcontext context) { sCgSkyProgram = cgCreateProgramFromFile(context, CG_SOURCE, "src/shaders/sky_preetham.cg", RenderState::sCgVertexProfile, "default_vs", NULL); if (sCgSkyProgram != NULL) { cgGLLoadProgram(sCgSkyProgram); sLightDirParam = cgGetNamedParameter(sCgSkyProgram, "lightDir"); sThetaSunParam = cgGetNamedParameter(sCgSkyProgram, "thetaSun"); sZenithColorParam = cgGetNamedParameter(sCgSkyProgram, "zenithColor"); sAColorParam = cgGetNamedParameter(sCgSkyProgram, "aColor"); sBColorParam = cgGetNamedParameter(sCgSkyProgram, "bColor"); sCColorParam = cgGetNamedParameter(sCgSkyProgram, "cColor"); sDColorParam = cgGetNamedParameter(sCgSkyProgram, "dColor"); sEColorParam = cgGetNamedParameter(sCgSkyProgram, "eColor"); } else cerr << "sky program failed to load" << endl; sCgMrtFragmentSkyDomeProgram = cgCreateProgramFromFile(context, CG_SOURCE, "src/shaders/sky_preetham.cg", RenderState::sCgFragmentProfile, "frag_skydome", NULL); if (sCgMrtFragmentSkyDomeProgram != NULL) { cgGLLoadProgram(sCgMrtFragmentSkyDomeProgram); /* cgGLSetParameter1f(sMaxDepthParam, MAX_DEPTH_CONST / farDist); */ } else cerr << "fragment skyprogram failed to load" << endl; } SkyPreetham::SkyPreetham(float turbitity, SceneEntity *skyDome): mSkyDome(skyDome), mTurbidity(turbitity) //, mSunQuad(NULL) { CreateSunQuad(); } SkyPreetham::~SkyPreetham() { //DEL_PTR(mSunQuad); } void SkyPreetham::RenderSkyDome(const Vector3 &sunDir, Camera *camera, RenderState *state) { pair sun_theta; Vector3 zenithColor; vector ABCDE; ComputeFactors(sunDir, zenithColor, ABCDE, sun_theta); // Move skybox with camera. Vector3 position = camera->GetPosition(); const float scaleFactor = 80.0f; //const float scaleFactor = 5.0f; position.z -= 3 * scaleFactor; Matrix4x4 m = TranslationMatrix(position); Matrix4x4 s = ScaleMatrix(scaleFactor, scaleFactor, scaleFactor); mSkyDome->GetTransform()->SetMatrix(s * m); cgGLSetParameter3f(sLightDirParam, sunDir.x, sunDir.y, sunDir.z); cgGLSetParameter2f(sThetaSunParam, sun_theta.first, sun_theta.second); cgGLSetParameter3f(sZenithColorParam, zenithColor.x, zenithColor.y, zenithColor.z); cgGLSetParameter3f(sAColorParam, ABCDE[0].x, ABCDE[0].y, ABCDE[0].z); cgGLSetParameter3f(sBColorParam, ABCDE[1].x, ABCDE[1].y, ABCDE[1].z); cgGLSetParameter3f(sCColorParam, ABCDE[2].x, ABCDE[2].y, ABCDE[2].z); cgGLSetParameter3f(sDColorParam, ABCDE[3].x, ABCDE[3].y, ABCDE[3].z); cgGLSetParameter3f(sEColorParam, ABCDE[4].x, ABCDE[4].y, ABCDE[4].z); cgGLEnableProfile(RenderState::sCgVertexProfile); cgGLBindProgram(sCgSkyProgram); if (state->GetRenderPassType() == RenderState::DEFERRED) { cgGLEnableProfile(RenderState::sCgFragmentProfile); cgGLBindProgram(sCgMrtFragmentSkyDomeProgram); } // Render sky dome. mSkyDome->Render(state); // Render additively blended sun disc. //RenderSunDisk(sunDir, camera); cgGLDisableProfile(RenderState::sCgFragmentProfile); } void SkyPreetham::RenderSunDisk(const Vector3 &sunDir, Camera *camera) { // Move skybox with camera. Vector3 position = camera->GetPosition(); const float scaleFactor = 100.0f; position.z -= 10 * scaleFactor; // Set world matrix to sun disc position. Matrix4x4 sunPos = TranslationMatrix(position); Vector3 ndir = -Normalize(sunDir); const float pitch = -atan2(ndir.x, ndir.y); const float yaw = atan2(ndir.z, sqrt((ndir.x * ndir.x) + (ndir.y * ndir.y))); Matrix4x4 roty = RotationYMatrix(pitch); Matrix4x4 rotx = RotationXMatrix(yaw); sunPos *= roty; sunPos *= rotx;; //sunposition.rotateAroundY(-D().getScene().getLight().getHorizontalOrientation() + 90.0f); //sunposition.rotateAroundX(-D().getScene().getLight().getVerticalOrientation()); glMatrixMode(GL_MODELVIEW); glMultMatrixf((float *)sunPos.x); float ambient[] = {1.0f, 1.0f, 1.0f, 1.0f}; glMaterialfv(GL_FRONT, GL_AMBIENT, ambient); //Float 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);*/ glPopMatrix(); } 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); */ } void SkyPreetham::ComputeFactors(const Vector3 &sunDir, Vector3 &zenithColor, vector &ABCDE, std::pair &sunThetha) const { sunThetha.first = acos(sunDir.z); const float cos_theta = cos(sunThetha.first); sunThetha.second = cos_theta * cos_theta; zenithColor.x = ( 0.00165f * CBQ(sunThetha.first) - 0.00374f * SQR(sunThetha.first) + 0.00208f * sunThetha.first + 0.0f) * SQR(mTurbidity) + (-0.02902f * CBQ(sunThetha.first) + 0.06377f * SQR(sunThetha.first) - 0.03202f * sunThetha.first + 0.00394f) * mTurbidity + ( 0.11693f * CBQ(sunThetha.first) - 0.21196f * SQR(sunThetha.first) + 0.06052f * sunThetha.first + 0.25885f); zenithColor.y = ( 0.00275f * CBQ(sunThetha.first) - 0.00610f * SQR(sunThetha.first) + 0.00316f * sunThetha.first + 0.0f) * SQR(mTurbidity) + (-0.04214f * CBQ(sunThetha.first) + 0.08970f * SQR(sunThetha.first) - 0.04153f * sunThetha.first + 0.00515f) * mTurbidity + ( 0.15346f * CBQ(sunThetha.first) - 0.26756f * SQR(sunThetha.first) + 0.06669f * sunThetha.first + 0.26688f); zenithColor.z = (float)((4.0453f * mTurbidity - 4.9710f) * tan((4.0f / 9.0f - mTurbidity / 120.0f) * (M_PI - 2.0f * sunThetha.first)) - 0.2155f * mTurbidity + 2.4192f); // convert kcd/mē to cd/mē zenithColor.z *= 1000.0f; ABCDE.push_back(Vector3(-0.01925 * mTurbidity - 0.25922, -0.01669 * mTurbidity - 0.26078, 0.17872 * mTurbidity - 1.46303)); ABCDE.push_back(Vector3(-0.06651 * mTurbidity + 0.00081, -0.09495 * mTurbidity + 0.00921, -0.35540 * mTurbidity + 0.42749)); ABCDE.push_back(Vector3(-0.00041 * mTurbidity + 0.21247, -0.00792 * mTurbidity + 0.21023, -0.02266 * mTurbidity + 5.32505)); ABCDE.push_back(Vector3(-0.06409 * mTurbidity - 0.89887, -0.04405 * mTurbidity - 1.65369, 0.12064 * mTurbidity - 2.57705)); ABCDE.push_back(Vector3(-0.00325 * mTurbidity + 0.04517, -0.01092 * mTurbidity + 0.05291, -0.06696 * mTurbidity + 0.37027)); } void SkyPreetham::ComputeSunColor(const Vector3 &sunDir, Vector3 &ambient, Vector3 &diffuse) const { // sunDir is sun direction // ambient color: shadow color // diffuse color: sun color pair sun_theta; Vector3 zenithColor; vector ABCDE; ComputeFactors(sunDir, zenithColor, ABCDE, sun_theta); Vector3 zenith_XYZ; zenith_XYZ.x = (zenithColor.x / zenithColor.y) * zenithColor.z; zenith_XYZ.y = zenithColor.z; zenith_XYZ.z = ((1.0f - zenithColor.x - zenithColor.y) / zenithColor.y) * zenithColor.z; ambient.x = 3.240479f * zenith_XYZ.x - 1.537150f * zenith_XYZ.y - 0.498535f * zenith_XYZ.z; ambient.y = -0.969256f * zenith_XYZ.x + 1.875992f * zenith_XYZ.y + 0.041556f * zenith_XYZ.z; ambient.z = 0.055648f * zenith_XYZ.x - 0.204043f * zenith_XYZ.y + 1.057311f * zenith_XYZ.z; // downscale ambient color if (1) ambient *= 5e-5f; else ambient *= 1e-1f; // simulate the sun intensity by modulating the ambient term. ambient *= (10.0f - 9.0f * DotProd(sunDir, Vector3::UNIT_Z())); //ambient += Vector3(0.2f); Vector3 num; num.x = (1.0f + ABCDE[0].x * exp(ABCDE[1].x / sunDir.z)) * (1.0f + ABCDE[2].x)+ ABCDE[4].x; num.y = (1.0f + ABCDE[0].y * exp(ABCDE[1].y / sunDir.z)) * (1.0f + ABCDE[2].y)+ ABCDE[4].y; num.z = (1.0f + ABCDE[0].z * exp(ABCDE[1].z / sunDir.z)) * (1.0f + ABCDE[2].z)+ ABCDE[4].z; Vector3 den; den.x = (1.0f + ABCDE[0].x * exp(ABCDE[1].x)) * (1.0f + ABCDE[2].x * exp(ABCDE[3].x * sun_theta.first) + ABCDE[4].x * sun_theta.second); den.y = (1.0f + ABCDE[0].y * exp(ABCDE[1].y)) * (1.0f + ABCDE[2].y * exp(ABCDE[3].y * sun_theta.first) + ABCDE[4].y * sun_theta.second); den.z = (1.0f + ABCDE[0].z * exp(ABCDE[1].z)) * (1.0f + ABCDE[2].z * exp(ABCDE[3].z * sun_theta.first) + ABCDE[4].z * sun_theta.second); Vector3 xyY = zenithColor * num / den; Vector3 XYZ; XYZ.x = (xyY.x / xyY.y) * xyY.z; XYZ.y = xyY.z; XYZ.z = ((1.0f - xyY.x - xyY.y) / xyY.y) * xyY.z; ///////////// //-- transform to rgb Vector3 color; color.x = 3.240479f * XYZ.x - 1.537150f * XYZ.y - 0.498535f * XYZ.z; color.y = -0.969256f * XYZ.x + 1.875992f * XYZ.y + 0.041556f *XYZ.z; color.z = 0.055648f * XYZ.x - 0.204043f * XYZ.y + 1.057311f * XYZ.z; // Calculate final sun diffuse color. if (1) diffuse = color * 1.7e-4f; else diffuse = color * 3e-1f; diffuse *= (2.0f - 1.0f * DotProd(sunDir, Vector3::UNIT_Z())); //cout << "diffuse: " << Magnitude(diffuse) << " ambient: " << Magnitude(ambient) << endl; }