📄 fxsunlight.cc
字号:
PosFrom = (S32)(mFloor(ScaledTime));
// Are we Lerping?
if (mLerpElevation)
{
// Yes, so calculate Position To.
PosTo = (S32)(mCeil(ScaledTime));
// Calculate Lerp Factor.
LerpFactor = ScaledTime - PosFrom;
}
else
{
// No, so clamp Position.
PosTo = PosFrom;
// Clamp lerp factor.
LerpFactor = 0.0f;
}
// ********************************************
// Elevation Keys.
// ********************************************
if (mElevationKeysLength)
{
// No, so calculate Size.
mAnimationElevation = GetLerpKey(mElevationKeys, PosFrom, PosTo, mMinElevation, mMaxElevation, LerpFactor);
}
else
{
// Set to static Elevation if we failed to set Size correctly.
mAnimationElevation = mMinElevation;
}
}
else
{
// No, so set to static Elevation.
mAnimationElevation = 0;
}
// ********************************************
// Calculate Sun Position.
// ********************************************
// *************************************************************
// Calculate sun position in the sky relative from the
// controlling object eye transform.
// *************************************************************
// Calculate Sun Vector.
Point3F SunlightOffset;
// Fetch Sun Azimuth/Elevation (animated if using it).
F32 mTempAzimuth = mUseAzimuth ? mAnimationAzimuth : mSunAzimuth;
F32 mTempElevation = mUseElevation ? mAnimationElevation : mSunElevation;
// Adjust orbit calculation for elevations exceeding -90 or +90 so
// that we can effectively have elevations from -360/+360 handled
// correctly. We can then happily animate full cyclic sun orbits.
if (mTempElevation < -90)
{
// Adjust Azimuth/Elevations.
mTempElevation = -90.0f + (mTempElevation + 90.0f);
mTempAzimuth += (mTempAzimuth >= 180.0f) ? -180.0f : 180.0f;
} else if (mTempElevation > +90)
{
// Adjust Azimuth/Elevations.
mTempElevation = 90.0f - (mTempElevation - 90.0f);
mTempAzimuth += (mTempAzimuth >= 180.0f) ? -180.0f : 180.0f;
}
//celestial
// Consult celesial management <sami>
F32 Yaw, Pitch;
if(gCelestials != NULL)
{
gCelestials->UpdateSunPosition();
mSunAzimuth = gCelestials->getAzimuthDegrees();
mSunElevation = gCelestials->getElevationDegrees();
Yaw = gCelestials->getAzimuthRads();
Pitch = gCelestials->getElevationRads();
}
else
{
Yaw = mDegToRad(mClampF(mTempAzimuth,0,359));
Pitch = mDegToRad(mClampF(mTempElevation,-360,+360));
}
//celestial end <sami>
// Create clamped Yaw/Patch and calculate sun direction.
//F32 Yaw = mDegToRad(mClampF(mTempAzimuth,0,359));
//F32 Pitch = mDegToRad(mClampF(mTempElevation,-360,+360));
MathUtils::getVectorFromAngles(SunlightOffset, Yaw, Pitch);
// Get Control Camera Eye Position.
Point3F eyePos;
MatrixF eye;
GameConnection* conn = GameConnection::getConnectionToServer();
conn->getControlCameraTransform(0, &eye);
eye.getColumn(3, &eyePos);
// Calculate new Sun Position.
F32 Radius = gClientSceneGraph->getVisibleDistance() * 0.999f;
mSunlightPosition = eyePos + (Radius * SunlightOffset);
}
//------------------------------------------------------------------------------
void fxSunLight::InitialiseAnimation(void)
{
// Debugging.
mEnable = true;
// Media.
//
// Texture Handles.
mLocalFlareTextureHandle = NULL;
mRemoteFlareTextureHandle = NULL;
// Flare Texture Names.
mLocalFlareTextureName = StringTable->insert("common/lighting/corona");
mRemoteFlareTextureName = StringTable->insert("common/lighting/corona");
// Sun Orbit.
mSunAzimuth = 0.0f;
mSunElevation = 30.0f;
mLockToRealSun = true;
// Flare.
mFlareTP = true;
mFlareColour .set(1,1,1);
mFlareBrightness = 1.0f;
mFlareSize = 1.0f;
mFadeTime = 0.1f;
mBlendMode = 0;
// Animation Options.
mUseColour = false;
mUseBrightness = false;
mUseRotation = false;
mUseSize = false;
mUseAzimuth = false;
mUseElevation = false;
mLerpColour = true;
mLerpBrightness = true;
mLerpRotation = true;
mLerpSize = true;
mLerpAzimuth = true;
mLerpElevation = true;
mLinkFlareSize = false;
mSingleColourKeys = true;
// Animation Extents.
mMinColour .set(0,0,0);
mMaxColour .set(1,1,1);
mMinBrightness = 0.0f;
mMaxBrightness = 1.0f;
mMinRotation = 0;
mMaxRotation = 359;
mMinSize = 0.5f;
mMaxSize = 1.0f;
mMinAzimuth = 0.0f;
mMaxAzimuth = 359.0f;
mMinElevation = -30.0f;
mMaxElevation = 180.0f + 30.0f;
// Animation Keys.
mRedKeys = StringTable->insert("AZA");
mGreenKeys = StringTable->insert("AZA");
mBlueKeys = StringTable->insert("AZA");
mBrightnessKeys = StringTable->insert("AZA");
mRotationKeys = StringTable->insert("AZA");
mSizeKeys = StringTable->insert("AZA");
mAzimuthKeys = StringTable->insert("AZ");
mElevationKeys = StringTable->insert("AZ");
// Animation Times.
mColourTime = 5.0f;
mBrightnessTime = 5.0f;
mRotationTime = 5.0f;
mSizeTime = 5.0f;
mAzimuthTime = 5.0f;
mElevationTime = 5.0f;
// Reset Local Flare Scale.
mLocalFlareScale = 0.0f;
// Reset Animation.
ResetAnimation();
}
//------------------------------------------------------------------------------
void fxSunLight::ResetAnimation(void)
{
// Check Animation Keys.
CheckAnimationKeys();
// Reset Times.
mColourElapsedTime =
mBrightnessElapsedTime =
mRotationElapsedTime =
mSizeElapsedTime =
mAzimuthElapsedTime =
mElevationElapsedTime = 0.0f;
// Reset Last Animation Time.
mLastRenderTime = Platform::getVirtualMilliseconds();
// Check Flare Details.
if (mBlendMode > 2) mBlendMode = 0;
}
//------------------------------------------------------------------------------
F32 fxSunLight::GetLerpKey(StringTableEntry Key, U32 PosFrom, U32 PosTo, F32 ValueFrom, F32 ValueTo, F32 Lerp)
{
// Get Key at Selected Positions.
char KeyFrameFrom = dToupper(*(Key + PosFrom)) - 65;
char KeyFrameTo = dToupper(*(Key + PosTo)) - 65;
// Calculate Range.
F32 ValueRange = (ValueTo-ValueFrom)/25.0f;
// Calculate Key Lerp.
F32 KeyFrameLerp = (KeyFrameTo - KeyFrameFrom) * Lerp;
// Return Lerped Value.
return ValueFrom + ((KeyFrameFrom + KeyFrameLerp) * ValueRange);
}
//------------------------------------------------------------------------------
U32 fxSunLight::CheckKeySyntax(StringTableEntry Key)
{
// Return problem.
if (!Key) return 0;
// Copy KeyCheck.
StringTableEntry KeyCheck = Key;
// Give benefit of doubt!
bool KeyValid = true;
// Check Key-frame validity.
do
{
if (dToupper(*KeyCheck) < 'A' && dToupper(*KeyCheck) > 'Z')
KeyValid = false;
} while (*(KeyCheck++));
// Return result.
if (KeyValid)
return dStrlen(Key);
else
return 0;
}
//------------------------------------------------------------------------------
void fxSunLight::CheckAnimationKeys(void)
{
// Check Key Validities.
mRedKeysLength = CheckKeySyntax(mRedKeys);
mGreenKeysLength = CheckKeySyntax(mGreenKeys);
mBlueKeysLength = CheckKeySyntax(mBlueKeys);
mBrightnessKeysLength = CheckKeySyntax(mBrightnessKeys);
mRotationKeysLength = CheckKeySyntax(mRotationKeys);
mSizeKeysLength = CheckKeySyntax(mSizeKeys);
mAzimuthKeysLength = CheckKeySyntax(mAzimuthKeys);
mElevationKeysLength = CheckKeySyntax(mElevationKeys);
// Calculate Time Scales.
if (mColourTime) mColourTimeScale = (mRedKeysLength-1) / mColourTime;
if (mBrightnessTime) mBrightnessTimeScale = (mBrightnessKeysLength-1) / mBrightnessTime;
if (mRotationTime) mRotationTimeScale = (mRotationKeysLength-1) / mRotationTime;
if (mSizeTime) mSizeTimeScale = (mSizeKeysLength-1) / mSizeTime;
if (mAzimuthTime) mAzimuthTimeScale = (mAzimuthKeysLength-1) / mAzimuthTime;
if (mElevationTime) mElevationTimeScale = (mElevationKeysLength-1) / mElevationTime;
}
//------------------------------------------------------------------------------
bool fxSunLight::onAdd()
{
// Add Parent.
if(!Parent::onAdd()) return(false);
// Set Default Object Box.
mObjBox.min.set(-1e5, -1e5, -1e5);
mObjBox.max.set( 1e5, 1e5, 1e5);
// Reset the World Box.
resetWorldBox();
// Set the Render Transform.
setRenderTransform(mObjToWorld);
// Add to Scene.
addToScene();
mAddedToScene = true;
// Only on client.
if (isClientObject())
{
// Fetch Textures.
setFlareBitmaps(mLocalFlareTextureName, mRemoteFlareTextureName);
}
// Return OK.
return(true);
}
//------------------------------------------------------------------------------
void fxSunLight::onRemove()
{
// Remove from Scene.
removeFromScene();
mAddedToScene = false;
// Only on client.
if (isClientObject())
{
// Remove Texture References.
mLocalFlareTextureHandle = NULL;
mRemoteFlareTextureHandle = NULL;
}
// Do Parent.
Parent::onRemove();
}
//------------------------------------------------------------------------------
void fxSunLight::inspectPostApply()
{
// Reset Animation.
ResetAnimation();
// Set Parent.
Parent::inspectPostApply();
// Set Config Change Mask.
setMaskBits(fxSunLightConfigChangeMask);
}
//------------------------------------------------------------------------------
bool fxSunLight::prepRenderImage(SceneState* state, const U32 stateKey, const U32 startZone,
const bool modifyBaseZoneState)
{
// No need to render if disabled.
if (!mEnable) return false;
// Return if last state.
if (isLastState(state, stateKey)) return false;
// Set Last State.
setLastState(state, stateKey);
// Is Object Rendered?
if (state->isObjectRendered(this))
{
// Yes, so get a SceneRenderImage.
SceneRenderImage* image = new SceneRenderImage;
// Populate it.
image->obj = this;
image->isTranslucent = true;
image->sortType = SceneRenderImage::BeginSort;
// Insert it into the scene images.
state->insertRenderImage(image);
// Yes, so get a SceneRenderImage.
image = new SceneRenderImage;
// Populate it.
image->obj = this;
image->isTranslucent = true;
image->sortType = SceneRenderImage::EndSort;
// Insert it into the scene images.
state->insertRenderImage(image);
}
// Are we locking to real sun and have not done it yet?
if ( mLockToRealSun /*&& !mDoneSunLock*/ )
{
// Yes, so calculate real sun elevation/azimuth.
LightInfo *sunLight;
if((sunLight = gClientSceneGraph->getLightManager()->getSunLight()) != NULL)
{
VectorF sunVector = -sunLight->mDirection; // Do we have any lights?
// Calculate Pitch/Yaw.
F32 Yaw, Pitch;
MathUtils::getAnglesFromVector(sunVector, Yaw, Pitch);
// Set Elevation/Azimiuth.
setSunElevation(mRadToDeg(Pitch));
setSunAzimuth(mRadToDeg(Yaw));
// Flag sun lock occurred.
mDoneSunLock = true;
}
}
return false;
}
//------------------------------------------------------------------------------
void fxSunLight::renderObject(SceneState* state, SceneRenderImage* ri)
{
// Check we are in Canonical State.
AssertFatal(dglIsInCanonicalState(), "Error, GL not in canonical state on entry");
MatrixF ModelView;
MatrixF RXF;
Point4F Position;
F32 BBRadius;
// Find if it's the Local or Remote Sun.
bool LocalSun = (ri->sortType == SceneRenderImage::EndSort);
// Remote Sun?
//
// NOTE:- We are doing this on the remote sun because then both suns will
// have the same animation positions.
if (!LocalSun)
{
// Yes, so calculate Elapsed Time.
mElapsedTime = (F32)((Platform::getVirtualMilliseconds() - mLastRenderTime) / 1000.0f);
// Reset Last Render Time.
mLastRenderTime = Platform::getVirtualMilliseconds();
// Animate Sun.
AnimateSun(mElapsedTime);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -