#include "SkyPreetham.h" #include "common.h" #include "Vector3.h" #include "SceneEntity.h" #include "Transform3.h" #include "Camera.h" #include "RenderState.h" #include "ShaderProgram.h" #include "Shape.h" #include "Material.h" #include "ShaderManager.h" #ifdef _CRT_SET #define _CRTDBG_MAP_ALLOC #include #include // redefine new operator #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__) #define new DEBUG_NEW #endif using namespace CHCDemoEngine; using namespace std; inline float CBQ(float x) { return x * x * x; } inline float SQR(float x) { return x * x ; } void SkyPreetham::InitCG() { mSkyVtxProgram = ShaderManager::GetSingleton()->CreateVertexProgram("sky_preetham", "default_vs", "skyVtx"); if (mSkyVtxProgram->IsValid()) { mSkyVtxProgram->AddParameter("lightDir", 0); mSkyVtxProgram->AddParameter("thetaSun", 1); mSkyVtxProgram->AddParameter("zenithColor", 2); mSkyVtxProgram->AddParameter("aColor", 3); mSkyVtxProgram->AddParameter("bColor", 4); mSkyVtxProgram->AddParameter("cColor", 5); mSkyVtxProgram->AddParameter("dColor", 6); mSkyVtxProgram->AddParameter("eColor", 7); mSkyVtxProgram->AddParameter("multiplier", 8); } else cerr << "sky program failed to load" << endl; mSkyFragProgram = ShaderManager::GetSingleton()->CreateFragmentProgram("sky_preetham", "frag_skydome", "skyFragMrt"); } SkyPreetham::SkyPreetham(float turbitity, SceneEntity *skyDome): mSkyDome(skyDome), mTurbidity(turbitity) //, mSunQuad(NULL) { CreateSunQuad(); Shape *shape = mSkyDome->GetShape(0); InitCG(); Material *mat = shape->GetMaterial(); mat->GetTechnique(0)->SetFragmentProgram(mSkyFragProgram); mat->GetTechnique(0)->SetVertexProgram(mSkyVtxProgram); mat->GetTechnique(1)->SetFragmentProgram(mSkyFragProgram); mat->GetTechnique(1)->SetVertexProgram(mSkyVtxProgram); } SkyPreetham::~SkyPreetham() { //DEL_PTR(mSunQuad); } void SkyPreetham::RenderSkyDome(const Vector3 &sunDir, Camera *camera, RenderState *state, bool scaleToRange) { 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.0f * scaleFactor; Matrix4x4 m = TranslationMatrix(position); Matrix4x4 s = ScaleMatrix(scaleFactor, scaleFactor, scaleFactor); mSkyDome->GetTransform()->SetMatrix(s * m); Material *mat = mSkyDome->GetShape(0)->GetMaterial(); Technique *tech; tech = mat->GetTechnique(state->GetRenderTechnique()); GPUProgramParameters *vtxParams = tech->GetVertexProgramParameters(); vtxParams->SetValue3f(0, sunDir.x, sunDir.y, sunDir.z); vtxParams->SetValue2f(1, sun_theta.first, sun_theta.second); vtxParams->SetValue3f(2, zenithColor.x, zenithColor.y, zenithColor.z); vtxParams->SetValue3f(3, ABCDE[0].x, ABCDE[0].y, ABCDE[0].z); vtxParams->SetValue3f(4, ABCDE[1].x, ABCDE[1].y, ABCDE[1].z); vtxParams->SetValue3f(5, ABCDE[2].x, ABCDE[2].y, ABCDE[2].z); vtxParams->SetValue3f(6, ABCDE[3].x, ABCDE[3].y, ABCDE[3].z); vtxParams->SetValue3f(7, ABCDE[4].x, ABCDE[4].y, ABCDE[4].z); if (!scaleToRange) { // use tone mapping vtxParams->SetValue1f(8, 1.0f); } else { // no tone mapping => linearily scale to displayable dynamic range vtxParams->SetValue1f(8, 8e-5f); } // render sky dome mSkyDome->Render(state); // render additively blended sun disc. //RenderSunDisk(sunDir, camera); } void SkyPreetham::RenderSunDisk(const Vector3 &sunDir, Camera *camera) { #if TODO // 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(); #endif } void SkyPreetham::CreateSunQuad() { #if TODO 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); #endif } 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, bool scaleToRange) 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 (scaleToRange) ambient *= 8e-6f; else ambient *= 1e-1f; // simulate the sun intensity by modulating the ambient term. ambient *= (10.0f - 9.0f * DotProd(sunDir, Vector3::UNIT_Z())); 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 (scaleToRange) diffuse = color * 3e-5f; else diffuse = color * 3e-1f; // diffuse component should be more saturated (and less blueish) for high sun positions diffuse.x *= 1.3f; diffuse.z *= 0.7f; // scale diffuse component in order to make sky look less bright in relation to // the geometry in the evening diffuse *= (2.0f - 1.0f * DotProd(sunDir, Vector3::UNIT_Z())); //cout << "diffuse: " << Magnitude(diffuse) << " ambient: " << Magnitude(ambient) << endl; }