《OGRE 3D 渲染引擎原理与架构》11.2 Light 类型与光照参数

Light 是场景里的光源对象:LightTypes 枚举(LT_POINT/LT_DIRECTIONAL/LT_SPOTLIGHT/LT_RECTLIGHT)决定光照语义,setType 只是赋值;setAttenuation 存 range/constant/linear/quadratic 四元衰减,setSpotlightRange 存聚光灯内外角与衰减指数。lab 用 C++ 复现类型、颜色、衰减公式与聚光参数。

11.2 Light 类型与光照参数

Light 是场景里代表光源的 MovableObject。它本身不做任何渲染——它存的是一组参数,真正的光照计算发生在着色器里。这一节拆它的类型与参数结构。

本章与站内既有内容的分工

cg 三卷讲光照的物理与着色原理,helio 讲自研引擎的光照系统;本节只讲 OGRE 里 Light 这个对象怎么存类型和参数——源码级的字段与 setter。

LightTypes:四个光源类型

OgreLight.h 定义光源类型枚举,每个值带用途注释:

// OgreMain/include/OgreLight.h:LightTypes(v14.6.3 源码摘录,已删减)
        /// Defines the type of light
        enum LightTypes : uint8
        {
            /// Point light sources give off light equally in all directions, so require only position not direction
            LT_POINT = 0,
            /// Directional lights simulate parallel light beams from a distant source, hence have direction but no position
            LT_DIRECTIONAL = 1,
            /// Spotlights simulate a cone of light from a source so require position and direction, plus extra values for falloff
            LT_SPOTLIGHT = 2,
            /// A rectangular area light, requires position, direction, width and height
            LT_RECTLIGHT = 3
        };

注释点出三种几何语义:

  • LT_POINT:全方向,只需要位置;
  • LT_DIRECTIONAL:平行光,只需要方向;
  • LT_SPOTLIGHT:聚光灯,位置 + 方向 + 内外角;
  • LT_RECTLIGHT:矩形面光,位置 + 方向 + 宽高。

setType 的实现只是赋值:

// OgreMain/src/OgreLight.cpp:Light::setType(v14.6.3 源码摘录,已删减)
    void Light::setType(LightTypes type)
    {
        mLightType = type;
    }

衰减:range/constant/linear/quadratic

光照按距离衰减。setAttenuation 的四个参数对应公式的三个系数加一个上限范围:

// OgreMain/include/OgreLight.h:setAttenuation(v14.6.3 源码摘录,已删减)
        void setAttenuation(float range, float constant, float linear, float quadratic)
        {
            mAttenuation = {range, constant, linear, quadratic};
        }

        /** Returns the absolute upper range of the light.
        */
        float getAttenuationRange(void) const { return mAttenuation[0]; }

衰减公式(OGRE 文档口径):

att = 1 / (constant + linear * d + quadratic * d²)

超出 range 后光照为 0。三个系数的分工:

  • constant:1 表示不随距离衰减的常数量;
  • linear:线性衰减,1 表示均匀衰减;
  • quadratic:二次衰减,模拟真实光的平方反比。

mAttenuation 存成一个 float[4],getAttenuationRange 直接取 [0]。

聚光参数:内外角与 falloff

聚光灯用内外两个圆锥角界定光锥:内角以内满强度,外角以外为 0,之间按 falloff 衰减:

// OgreMain/src/OgreLight.cpp:Light::setSpotlightRange(v14.6.3 源码摘录,已删减)
    void Light::setSpotlightRange(const Radian& innerAngle, const Radian& outerAngle, Real falloff)
    {
        mSpotInner = innerAngle;
        mSpotOuter = outerAngle;
        mSpotFalloff = falloff;
    }

Radian 是 OGRE 的角度包装类型(4.1 数学库讲过),Real 默认是 float。三个字段各存一值,真正计算也在着色器端。

源码定位

上游引用固定到 OGRE v14.6.3 源码 。

可运行产物:C++ 最小验证

下面复现 Light 的类型、颜色、衰减公式与聚光参数。它与 src/11-2-light-type-params.cpp 逐字节一致:

// file: src/11-2-light-type-params.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/11-2-light-type-params.cpp -o bin/11-2-light-type-params && ./bin/11-2-light-type-params
//
// 复现 Light 的类型与光照参数:
//   LightTypes 枚举(LT_POINT/LT_DIRECTIONAL/LT_SPOTLIGHT,对应 OgreLight.h);
//   Light::setType 设置光源类型;
//   Light::setAttenuation 四元衰减参数(range/constant/linear/quadratic);
//   Light::setSpotlightRange 设置聚光灯内外角与衰减指数;
//   简化衰减公式 1/(c + l*d + q*d^2) 验证各系数作用。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"

#include <cmath>

namespace ogre_repro {

// 对应 Ogre::Light::LightTypes(uint8)
enum LightTypes : uint8_t {
  LT_POINT = 0,       // 点光源:只需位置
  LT_DIRECTIONAL = 1, // 平行光:只需方向
  LT_SPOTLIGHT = 2,   // 聚光灯:位置 + 方向 + 内外角
};

// 对应 Ogre::Light:类型 + 颜色 + 衰减 + 聚光参数
class Light {
 public:
  Light() : mLightType(LT_POINT), mAttenuation{1000, 1, 0, 0}, mSpotInner(30), mSpotOuter(40), mSpotFalloff(2.0f) {}

  // 对应 Light::setType
  void setType(LightTypes type) { mLightType = type; }
  LightTypes getType() const { return mLightType; }

  // 对应 Light::setDiffuseColour / setSpecularColour
  void setDiffuseColour(float r, float g, float b) {
    mDiffuse[0] = r; mDiffuse[1] = g; mDiffuse[2] = b;
  }
  void setSpecularColour(float r, float g, float b) {
    mSpecular[0] = r; mSpecular[1] = g; mSpecular[2] = b;
  }

  // 对应 Light::setAttenuation(range, constant, linear, quadratic)
  void setAttenuation(float range, float constant, float linear, float quadratic) {
    mAttenuation[0] = range;
    mAttenuation[1] = constant;
    mAttenuation[2] = linear;
    mAttenuation[3] = quadratic;
  }
  float getAttenuationRange() const { return mAttenuation[0]; }

  // 对应 Light::setSpotlightRange(innerAngle, outerAngle, falloff)
  void setSpotlightRange(float innerAngle, float outerAngle, float falloff) {
    mSpotInner = innerAngle;
    mSpotOuter = outerAngle;
    mSpotFalloff = falloff;
  }

  // 简化衰减公式:1/(constant + linear*d + quadratic*d^2),超出范围取 0
  float attenuationAt(float distance) const {
    if (distance > mAttenuation[0]) return 0.0f;
    float denom = mAttenuation[1] + mAttenuation[2] * distance +
                  mAttenuation[3] * distance * distance;
    return 1.0f / denom;
  }

  LightTypes mLightType;
  float mDiffuse[3] = {1, 1, 1};
  float mSpecular[3] = {1, 1, 1};
  float mAttenuation[4];
  float mSpotInner, mSpotOuter, mSpotFalloff;
};

}  // namespace ogre_repro

int main() {
  using namespace ogre_repro;

  // 类型与颜色
  Light l;
  CHECK(l.getType() == LT_POINT);
  l.setType(LT_DIRECTIONAL);
  CHECK(l.getType() == LT_DIRECTIONAL);
  l.setDiffuseColour(0.8f, 0.2f, 0.2f);
  CHECK(l.mDiffuse[0] == 0.8f && l.mDiffuse[2] == 0.2f);
  l.setType(LT_SPOTLIGHT);
  CHECK(l.getType() == LT_SPOTLIGHT);

  // 衰减:constant/linear/quadratic
  l.setType(LT_POINT);
  l.setAttenuation(50.0f, 1.0f, 0.09f, 0.032f);
  CHECK(l.getAttenuationRange() == 50.0f);
  // 距离 10:1/(1 + 0.09*10 + 0.032*100) = 1/5.1
  CHECK(std::abs(l.attenuationAt(10.0f) - 1.0f / 5.1f) < 1e-6);
  // 超出范围 → 0
  CHECK(l.attenuationAt(60.0f) == 0.0f);

  // 聚光灯:内外角与衰减指数
  l.setType(LT_SPOTLIGHT);
  l.setSpotlightRange(20.0f, 30.0f, 1.5f);
  CHECK(l.mSpotInner == 20.0f);
  CHECK(l.mSpotOuter == 30.0f);
  CHECK(l.mSpotFalloff == 1.5f);

  return lab::report("11-2 渲染对象体系:Light 类型与光照参数");
}

本机实测(Apple clang 16,clang++ -std=c++20):编译通过(-Wall 零警告),断言 10/10 通过。

小结

  • LightTypes 四值对应四种光照几何语义(点/平行/聚光/面光),注释点明各自需要的位置与方向。
  • setAttenuation(range, constant, linear, quadratic) 存衰减参数,公式 1/(c + l*d + q*d²)。
  • setSpotlightRange(inner, outer, falloff) 存聚光灯内外角与指数。
  • Light 只存参数,光照计算发生在着色器端。

阅读导航:上一节:11.1 Entity 与 SubEntity · 下一节:11.3 Camera 与投影 。

继续阅读

探索更多技术文章

浏览归档,发现更多关于系统设计、工具链和工程实践的内容。

全部文章 返回首页

「books」更多文章

  1. 《大规模分布式计算深入解析》15.3 落地:从 Neo4j 到自研图引擎
  2. 《大规模分布式计算深入解析·图计算与图数据库内核》 · 目录与学习路径(45 篇)
  3. 《大规模分布式计算深入解析》15.2 十一卷主线:从切分到图