在渲染一个具体的物体之前,Ogre会查找与该物体相关的光源,根据光源的范围,内角和外角(如果是聚光灯)等参数进行计算,如果物体受到光源的影响,则将此光源的参数传入着色器进行光照计算。如果没有找到一个光源,那么传入一个空白的光源参数给着色器。
查找物体相关的光源代码如下:
bool Light::isInLightRange(const Ogre::Sphere& container) const
{
bool isIntersect = true;
//directional light always intersects (check only spotlight and point)
if (mLightType != LT_DIRECTIONAL)
{
//Check that the sphere is within the sphere of the light
isIntersect = container.intersects(Sphere(mDerivedPosition, mRange));
//If this is a spotlight, check that the sphere is within the cone of the spot light
if ((isIntersect) && (mLightType == LT_SPOTLIGHT))
{
//check first check of the sphere surrounds the position of the light
//(this covers the case where the center of the sphere is behind the position of the light
// something which is not covered in the next test).
isIntersect = container.intersects(mDerivedPosition);
//if not test cones
if (!isIntersect)
{
//Calculate the cone that exists between the sphere and the center position of the light
Ogre::Vector3 lightSphereConeDirection = container.getCenter() - mDerivedPosition;
Ogre::Radian halfLightSphereConeAngle = Math::ASin(container.getRadius() / lightSphereConeDirection.length());
//Check that the light cone and the light-position-to-sphere cone intersect)
Radian angleBetweenConeDirections = lightSphereConeDirection.angleBetween(mDerivedDirection);
isIntersect = angleBetweenConeDirections <= halfLightSphereConeAngle + mSpotOuter * 0.5;
}
}
}
return isIntersect;
}
空白光源的定义如下:
Light mBlankLight;
Light::Light()
: mLightType(LT_POINT),
mPosition(Vector3::ZERO),
mDiffuse(ColourValue::White),
mSpecular(ColourValue::Black),
mDirection(Vector3::UNIT_Z),
mSpotOuter(Degree(40.0f)),
mSpotInner(Degree(30.0f)),
mSpotFalloff(1.0f),
mSpotNearClip(0.0f),
mRange(100000),
mAttenuationConst(1.0f),
mAttenuationLinear(0.0f),
mAttenuationQuad(0.0f),
mPowerScale(1.0f),
mIndexInFrame(0),
mOwnShadowFarDist(false),
mShadowFarDist(0),
mShadowFarDistSquared(0),
mShadowNearClipDist(-1),
mShadowFarClipDist(-1),
mDerivedPosition(Vector3::ZERO),
mDerivedDirection(Vector3::UNIT_Z),
mDerivedCamRelativePosition(Vector3::ZERO),
mDerivedCamRelativeDirty(false),
mCameraToBeRelativeTo(0),
mDerivedTransformDirty(false),
mCustomShadowCameraSetup()
{
//mMinPixelSize should always be zero for lights otherwise lights will disapear
mMinPixelSize = 0;
}
mBlankLight.setDiffuseColour(ColourValue::Black);
mBlankLight.setSpecularColour(ColourValue::Black);
mBlankLight.setAttenuation(0,1,0,0);
以下因为光源参数设置不当,导致使用了空板光源参数,而产生错误的渲染效果
注意:其他参数不正确是因为hsls中没有使用这些变量,所以ogre没有传入这些参数的值,导致这些对应的变量没有得到初始化!