//---------------------------------------------------------------------------- void AmbientRegionActor::_UpdateDirLightCamera() { AVector dir = AVector::AnglesToDirection(Mathf::DEG_TO_RAD*mHorAngle, Mathf::DEG_TO_RAD*mVerAngle); dir.Normalize(); Scene *scene = DynamicCast<Scene>(GetTopestParent()); if (scene) { EnvirParam *envirParam = scene->GetEnvirParam(); Light *lightDir = envirParam->GetLight_Dir(); Projector *projector = envirParam->GetLight_Dir_Projector(); lightDir->Ambient = Float4(mAmbientColor[0], mAmbientColor[1], mAmbientColor[2], mIntensity); lightDir->Intensity = mIntensity; lightDir->Diffuse = Float4(mDiffuseColor[0], mDiffuseColor[1], mDiffuseColor[2], 1.0f); lightDir->Specular = Float4(mSpecularColor[0], mSpecularColor[1], mSpecularColor[2], mSpecularPow); float upDot = dir.Dot(-AVector::UNIT_Z); if (upDot >= 0.99f) { } else { AVector upTemp = AVector::UNIT_Z; AVector right = dir.UnitCross(upTemp); AVector up = right.UnitCross(dir); lightDir->DVector = dir; lightDir->UVector = up; lightDir->RVector = right; APoint camPos = mLightCameraLookPosition - dir*mLightCameraLookDistance; projector->SetFrame(camPos, lightDir->DVector, lightDir->UVector, lightDir->RVector); } if (!projector->IsPerspective()) { projector->SetFrustum(0.1f, 100.0f, -mLightCameraExtent, mLightCameraExtent, -mLightCameraExtent, mLightCameraExtent); } else { projector->SetFrustum(mLightCameraExtent, 1.0f, 1.0f, 100.0f); } } }
//---------------------------------------------------------------------------- void ProjectedTextures::CreateScene () { mScene = new0 Node(); mTrnNode = new0 Node(); mScene->AttachChild(mTrnNode); // Load the face model. #ifdef WM5_LITTLE_ENDIAN std::string path = Environment::GetPathR("FacePN.wmof"); #else std::string path = Environment::GetPathR("FacePN.be.wmof"); #endif InStream inStream; inStream.Load(path); TriMesh* mesh = StaticCast<TriMesh>(inStream.GetObjectAt(0)); // Create a camera to project the texture. Projector* projector = new0 Projector(Camera::PM_DEPTH_ZERO_TO_ONE); projector->SetFrustum(1.0f, 10.0f, -0.4125f, 0.4125f, -0.55f, 0.55f); AVector proDVector(0.0f, 1.0f, 0.0f); AVector proUVector(0.0f, 0.0f, 1.0f); AVector proRVector = proDVector.Cross(proUVector); APoint proPosition = APoint::ORIGIN - 303.0f*proDVector; projector->SetFrame(proPosition, proDVector, proUVector, proRVector); // Create a directional light for the face. Light* light = new0 Light(Light::LT_DIRECTIONAL); light->Ambient = Float4(0.25f, 0.25f, 0.25f, 1.0f); light->Diffuse = Float4(1.0f, 1.0f, 1.0f, 1.0f); light->Specular = Float4(0.0f, 0.0f, 0.0f, 1.0f); light->DVector = AVector::UNIT_Y; // scene-camera direction // Create a material for the face. Material* material = new0 Material(); material->Emissive = Float4(0.0f, 0.0f, 0.0f, 1.0f); material->Ambient = Float4(0.5f, 0.5f, 0.5f, 1.0f); material->Diffuse = Float4(0.99607f, 0.83920f, 0.67059f, 1.0f); material->Specular = Float4(0.8f, 0.8f, 0.8f, 0.0f); // Create the effect. std::string effectFile = Environment::GetPathR("ProjectedTexture.wmfx"); ProjectedTextureEffect* effect = new0 ProjectedTextureEffect(effectFile); std::string projectedName = Environment::GetPathR("Magician.wmtf"); Texture2D* projectedTexture = Texture2D::LoadWMTF(projectedName); mesh->SetEffectInstance(effect->CreateInstance(projector, light, material, projectedTexture)); mTrnNode->AttachChild(mesh); }
//---------------------------------------------------------------------------- void ShadowMaps::CreateShaders () { // Create the shader constants. Some of these are shared. // Create the light projector. Projector* projector = new0 Projector(Camera::PM_DEPTH_ZERO_TO_ONE); projector->SetFrustum(60.0f, 1.0f, 0.1f, 100.0f); APoint projPosition(4.0f, 4.0f, 4.0f); AVector projDVector(-1.0f, -1.0f, -1.0f); projDVector.Normalize(); AVector projUVector(-1.0f, -1.0f, 2.0f); projUVector.Normalize(); AVector projRVector = projDVector.Cross(projUVector); projector->SetFrame(projPosition, projDVector, projUVector, projRVector); // For SMSceneEffect and SMUnlitEffect. ProjectorMatrixConstant* lightPVMatrix = new0 ProjectorMatrixConstant(projector, false, 0); ShaderFloat* lightBSMatrixUnlit = new0 ShaderFloat(4); ShaderFloat* lightBSMatrixScene = new0 ShaderFloat(4); ShaderFloat* screenBSMatrix = new0 ShaderFloat(4); const float* src; if (VertexShader::GetProfile() == VertexShader::VP_ARBVP1) { src = (const float*)Projector::BiasScaleMatrix[1]; memcpy(lightBSMatrixUnlit->GetData(), src, 16*sizeof(float)); memcpy(lightBSMatrixScene->GetData(), src, 16*sizeof(float)); memcpy(screenBSMatrix->GetData(), src, 16*sizeof(float)); } else { src = (const float*)Projector::BiasScaleMatrix[0]; memcpy(lightBSMatrixUnlit->GetData(), src, 16*sizeof(float)); memcpy(screenBSMatrix->GetData(), src, 16*sizeof(float)); src = (const float*)Projector::BiasScaleMatrix[1]; memcpy(lightBSMatrixScene->GetData(), src, 16*sizeof(float)); } // For SMSceneEffect. ProjectorWorldPositionConstant* lightWorldPosition = new0 ProjectorWorldPositionConstant(projector); ShaderFloat* lightColor = new0 ShaderFloat(1); (*lightColor)[0] = 1.0f; (*lightColor)[1] = 1.0f; (*lightColor)[2] = 1.0f; (*lightColor)[3] = 1.0f; // For SMUnlitEffect. ShaderFloat* depthBiasConstant = new0 ShaderFloat(1); (*depthBiasConstant)[0] = 0.1f; ShaderFloat* texelSizeConstant = new0 ShaderFloat(1); (*texelSizeConstant)[0] = 1.0f/(float)mScreenTargetSize; (*texelSizeConstant)[1] = 1.0f/(float)mScreenTargetSize; // For SMBlurEffect. const int numRegisters = 11; ShaderFloat* weights = new0 ShaderFloat(numRegisters); ShaderFloat* hOffsets = new0 ShaderFloat(numRegisters); ShaderFloat* vOffsets = new0 ShaderFloat(numRegisters); // Compute the weights. They must sum to 1. Float4* weightsData = (Float4*)weights->GetData(); const float stdDev = 1.0f; const float invTwoVariance = 1.0f/(2.0f*stdDev*stdDev); float totalWeight = 0.0f; int i, j; for (i = 0, j = -numRegisters/2; i < numRegisters; ++i, ++j) { float weight = Mathf::Exp(-j*j*invTwoVariance); weightsData[i] = Float4(weight, weight, weight, 0.0f); totalWeight += weight; } float invTotalWeight = 1.0f/totalWeight; for (i = 0; i < numRegisters; ++i) { weightsData[i][0] *= invTotalWeight; weightsData[i][1] *= invTotalWeight; weightsData[i][2] *= invTotalWeight; } // Compute the horizontal and vertical offsets. Float4* hOffsetsData = (Float4*)hOffsets->GetData(); Float4* vOffsetsData = (Float4*)vOffsets->GetData(); float uDelta = 1.0f/(float)GetWidth(); float vDelta = 1.0f/(float)GetHeight(); for (i = 0, j = -numRegisters/2; i < numRegisters; ++i, ++j) { hOffsetsData[i] = Float4(j*uDelta, 0.0f, 0.0f, 0.0f); vOffsetsData[i] = Float4(0.0f, j*vDelta, 0.0f, 0.0f); } // Create the scene effect. std::string effectFile = Environment::GetPathR("SMScene.wmfx"); SMSceneEffect* sceneEffect = new0 SMSceneEffect(effectFile); std::string stoneName = Environment::GetPathR("Stone.wmtf"); Texture2D* stoneTexture = Texture2D::LoadWMTF(stoneName); std::string ballName = Environment::GetPathR("BallTexture.wmtf"); Texture2D* ballTexture = Texture2D::LoadWMTF(ballName); std::string projectedName = Environment::GetPathR("Magician.wmtf"); Texture2D* projectedTexture = Texture2D::LoadWMTF(projectedName); mPlaneSceneInstance = sceneEffect->CreateInstance(lightWorldPosition, lightPVMatrix, lightBSMatrixScene, screenBSMatrix, lightColor, stoneTexture, mVBlurTarget->GetColorTexture(0), projectedTexture); mSphereSceneInstance = sceneEffect->CreateInstance(lightWorldPosition, lightPVMatrix, lightBSMatrixScene, screenBSMatrix, lightColor, ballTexture, mVBlurTarget->GetColorTexture(0), projectedTexture); // Create the shadow effect. effectFile = Environment::GetPathR("SMShadow.wmfx"); mShadowEffect = new0 SMShadowEffect(effectFile, lightPVMatrix); // Create the unlit effect. effectFile = Environment::GetPathR("SMUnlit.wmfx"); mUnlitEffect = new0 SMUnlitEffect(effectFile, lightPVMatrix, lightBSMatrixUnlit, depthBiasConstant, texelSizeConstant, mShadowTarget->GetColorTexture(0)); // Create the blur effect and instantiate for horizontal and vertical // blurring. effectFile = Environment::GetPathR("SMBlur.wmfx"); SMBlurEffect* blurEffect = new0 SMBlurEffect(effectFile); mHBlurInstance = blurEffect->CreateInstance(weights, hOffsets, mUnlitTarget->GetColorTexture(0)); mVBlurInstance = blurEffect->CreateInstance(weights, vOffsets, mHBlurTarget->GetColorTexture(0)); }