4.1 光源类型
Ogre::Light 用 LightTypes 枚举把光源分成四类:点光、方向光、聚光、矩形面光。四者差异在于「位置与方向谁有意义」——点光只需位置、方向光只需方向、聚光两者都要。类型决定渲染路径怎么解析光源,也决定 5 章阴影相机怎么构造;而 getAs4DVector 把位置型与方向型归一成同一种 4D 表示,让着色器不必按类型写死。
本章与站内既有内容的分工
cg 三卷讲过 Phong 光照模型的数学与向量;卷一 ogre-core 5.3 讲 MovableObject 挂接,但没有展开 Light 这个特殊对象。本节是第四章「光源与光照」的第一块:先把类型、默认值、方向推导讲清,4.2 接着讲衰减与范围,4.3 讲颜色与视觉参数。阴影相关(第 5 章)会复用这里的 getDerivedDirection 与 getAs4DVector。
LightTypes 枚举与构造默认值
OgreLight.h 里的枚举把类型定成连续小整数,每种类型注释点明它需要的数据:
// OgreMain/include/OgreLight.h:enum 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
};
Light 的构造初始化列表给出了全套默认值:漫反射白、镜面黑、聚光外角 40°、内角 30°、衰减指数 2.0、衰减四元组 (100000, 1, 0, 0)——范围 100000 个单位、常数项 1 表示不衰减。类型默认点光:
// OgreMain/src/OgreLight.cpp:Light 构造初始化列表(v14.6.3 源码摘录,已删减)
Light::Light(const String& name) : MovableObject(name),
mDiffuse(ColourValue::White),
mSpecular(ColourValue::Black),
mSpotOuter(Degree(40.0f)),
mSpotInner(Degree(30.0f)),
mSpotFalloff(2.0f),
mAttenuation(100000.f, 1.f, 0.f, 0.f),
...
mLightType(LT_POINT),
...
注意 mSpotOuter/mSpotInner 是 Radian,初始化时用 Degree(...) 隐式转换;mAttenuation 是一个 Vector4f,即 (range, constant, linear, quadratic) 四元组,4.2 将用它算距离衰减。setType/getType 只读写 mLightType,没有任何联动行为。
方向推导:-节点 z 轴
点光/聚光/矩形面光从 getDerivedPosition 取位置,方向光没有位置,方向来自挂接节点:OGRE 约定光源沿局部 -Z 照亮,所以世界空间方向就是节点派生朝向的 zAxis() 取负:
// OgreMain/include/OgreLight.h:getDerivedDirection(v14.6.3 源码摘录,已删减)
/** Retrieves the direction of the light including any transform from nodes it is attached to. */
Vector3 getDerivedDirection(void) const
{
assert(mParentNode && "Light must be attached to a SceneNode");
return -mParentNode->_getDerivedOrientation().zAxis();
}
这条约定统一了场景对象建模:把聚光/矩形面光看作「朝 -Z 看的实体」,方向光只是没有位置的那一种。旋转节点即旋转光照方向,无需单独维护方向数据。
聚光范围参数
setSpotlightRange 一次设置内角、外角与衰减指数。内角以内强度 100%,外角以外强度为 0,两者之间按衰减指数做幂渐变(4.2 的聚光因子):
// OgreMain/src/OgreLight.cpp:setSpotlightRange(v14.6.3 源码摘录,已删减)
void Light::setSpotlightRange(const Radian& innerAngle, const Radian& outerAngle, Real falloff)
{
mSpotInner = innerAngle;
mSpotOuter = outerAngle;
mSpotFalloff = falloff;
}
默认 40°/30°/2.0 意味着「内锥 30° 全亮、外锥 40° 为零、中间按平方衰减」。当渲染把内/外角与像素方向夹角的余弦比较时,这套参数直接进公式。
getAs4DVector:位置型与方向型的归一
getAs4DVector 把四种光源统一成 Vector4:方向光返回 (-dir, 0.0),其余返回 (pos, 1.0)。w 分量区分「无限远」与「有限距离」,让同一个向量既能当位置用、也能当方向用:
// OgreMain/src/OgreLight.cpp:Light::getAs4DVector(v14.6.3 源码摘录,已删减)
Vector4 Light::getAs4DVector(bool cameraRelativeIfSet) const
{
if (mLightType == Light::LT_DIRECTIONAL)
{
return Vector4(-getDerivedDirection(), // negate direction as 'position'
0.0); // infinite distance
}
else
{
return Vector4(getDerivedPosition(cameraRelativeIfSet), 1.0);
}
}
注释点明 -getDerivedDirection 的意图:方向光方向是 -Z,取负后变成「朝光源看去」的向量,当作一个无穷远的位置。5 章的阴影相机、渲染器里排序与近剪裁体积都依赖这套归一。
场景光源怎样成为绘制输入
Light 挂在场景节点上,世界位置与方向随派生变换更新。渲染时按光源类型和对象范围建立可用光源列表,Pass 与自动参数选择怎样消费它。
用同一材质分别选择方向光、点光与聚光,记录空间坐标、距离和衰减字段,避免把灯光颜色变化与光源进入列表混在一起。
源码定位
OgreMain/include/OgreLight.h:enum LightTypes、getDerivedDirection、setAttenuation、getAs4DVectorOgreMain/src/OgreLight.cpp:Light构造初始化列表、setType/getType、setSpotlightRange
上游引用固定到 OGRE v14.6.3 源码 。
可运行产物:C++ 最小验证
下面复现 Light 的类型体系:四类枚举值、构造默认值(颜色/聚光/衰减)、setType 切换、getDerivedDirection 的 -z 轴推导、getAs4DVector 的方向/位置归一。它与 src/04-1-light-types.cpp 逐字节一致:
// file: src/04-1-light-types.cpp
// tier: L1
// verify: clang++ -std=c++20 -I. src/04-1-light-types.cpp -o bin/04-1-light-types && ./bin/04-1-light-types
//
// 复现 OgreLight.h / OgreLight.cpp 的光源类型与默认值:
// LightTypes 枚举(LT_POINT=0/LT_DIRECTIONAL=1/LT_SPOTLIGHT=2/LT_RECTLIGHT=3);
// 构造默认:mDiffuse=White、mSpecular=Black、mSpotOuter=40°、mSpotInner=30°、
// mSpotFalloff=2.0、mAttenuation=(100000,1,0,0)、mLightType=LT_POINT;
// getDerivedDirection = -mParentNode 的 z 轴;
// getAs4DVector:方向光 (-dir, 0.0),点光/聚光 (pos, 1.0)。
// 独立最小验证,非 OGRE 源码。
#include "verify.h"
#include <cstdint>
namespace ogre_repro {
struct Vec3 {
float x, y, z;
};
struct Vec4 {
float x, y, z, w;
};
struct Colour {
float r, g, b;
};
inline bool vecEq3(const Vec3& a, const Vec3& b) {
return a.x == b.x && a.y == b.y && a.z == b.z;
}
inline bool colEq(const Colour& a, const Colour& b) {
return a.r == b.r && a.g == b.g && a.b == b.b;
}
// 对应 OgreLight.h:Light 的最小实现
class Light {
public:
// 对应 OgreLight.h:enum LightTypes
enum LightTypes : uint8_t {
LT_POINT = 0,
LT_DIRECTIONAL = 1,
LT_SPOTLIGHT = 2,
LT_RECTLIGHT = 3
};
Light() {
// 对应 OgreLight.cpp 构造初始化列表
mDiffuse = Colour{1.0f, 1.0f, 1.0f}; // ColourValue::White
mSpecular = Colour{0.0f, 0.0f, 0.0f}; // ColourValue::Black
mSpotOuter = 40.0f; // Degree(40)
mSpotInner = 30.0f; // Degree(30)
mSpotFalloff = 2.0f;
mAttenuation[0] = 100000.0f;
mAttenuation[1] = 1.0f;
mAttenuation[2] = 0.0f;
mAttenuation[3] = 0.0f;
mLightType = LT_POINT;
}
void setType(LightTypes type) { mLightType = type; }
LightTypes getType() const { return mLightType; }
void setDiffuseColour(float r, float g, float b) { mDiffuse = Colour{r, g, b}; }
const Colour& getDiffuseColour() const { return mDiffuse; }
void setSpecularColour(float r, float g, float b) { mSpecular = Colour{r, g, b}; }
const Colour& getSpecularColour() const { return mSpecular; }
void setSpotlightRange(float innerDeg, float outerDeg, float falloff) {
mSpotInner = innerDeg;
mSpotOuter = outerDeg;
mSpotFalloff = falloff;
}
float getSpotlightInnerAngle() const { return mSpotInner; }
float getSpotlightOuterAngle() const { return mSpotOuter; }
float getSpotlightFalloff() const { return mSpotFalloff; }
void setAttenuation(float range, float constant, float linear, float quadratic) {
mAttenuation[0] = range;
mAttenuation[1] = constant;
mAttenuation[2] = linear;
mAttenuation[3] = quadratic;
}
// 对应 getAttenuationRange/Constant/Linear/Quadric
void getAttenuation(float& range, float& constant, float& linear, float& quad) const {
range = mAttenuation[0];
constant = mAttenuation[1];
linear = mAttenuation[2];
quad = mAttenuation[3];
}
// 简化:节点世界空间 z 轴(默认 (0,0,1)),
// 对应 getDerivedDirection = -mParentNode->_getDerivedOrientation().zAxis()
Vec3 zAxis = Vec3{0, 0, 1};
Vec3 getDerivedDirection() const { return Vec3{-zAxis.x, -zAxis.y, -zAxis.z}; }
Vec3 position = Vec3{0, 0, 0};
// 对应 Light::getAs4DVector:方向光 w=0,点光/聚光 w=1
Vec4 getAs4DVector() const {
if (mLightType == LT_DIRECTIONAL) {
Vec3 d = getDerivedDirection();
return Vec4{-d.x, -d.y, -d.z, 0.0f};
}
return Vec4{position.x, position.y, position.z, 1.0f};
}
private:
Colour mDiffuse;
Colour mSpecular;
float mSpotInner;
float mSpotOuter;
float mSpotFalloff;
float mAttenuation[4];
LightTypes mLightType;
};
} // namespace ogre_repro
int main() {
using namespace ogre_repro;
// 默认构造:点光、白漫反射、黑镜面、默认聚光范围与衰减
Light def;
CHECK_EQ(static_cast<int>(def.getType()), static_cast<int>(Light::LT_POINT));
CHECK(colEq(def.getDiffuseColour(), Colour{1.0f, 1.0f, 1.0f}));
CHECK(colEq(def.getSpecularColour(), Colour{0.0f, 0.0f, 0.0f}));
CHECK_EQ(def.getSpotlightOuterAngle(), 40.0f);
CHECK_EQ(def.getSpotlightInnerAngle(), 30.0f);
CHECK_EQ(def.getSpotlightFalloff(), 2.0f);
float r, c, l, q;
def.getAttenuation(r, c, l, q);
CHECK_EQ(r, 100000.0f);
CHECK_EQ(c, 1.0f);
CHECK_EQ(l, 0.0f);
CHECK_EQ(q, 0.0f);
// setType/getType:四种类型互相切换
def.setType(Light::LT_SPOTLIGHT);
CHECK_EQ(static_cast<int>(def.getType()), static_cast<int>(Light::LT_SPOTLIGHT));
def.setType(Light::LT_DIRECTIONAL);
CHECK_EQ(static_cast<int>(def.getType()), static_cast<int>(Light::LT_DIRECTIONAL));
def.setType(Light::LT_RECTLIGHT);
CHECK_EQ(static_cast<int>(def.getType()), static_cast<int>(Light::LT_RECTLIGHT));
// 漫反射/镜面颜色
def.setType(Light::LT_POINT);
def.setDiffuseColour(1.0f, 0.5f, 0.0f);
CHECK(colEq(def.getDiffuseColour(), Colour{1.0f, 0.5f, 0.0f}));
def.setSpecularColour(1.0f, 1.0f, 1.0f);
CHECK(colEq(def.getSpecularColour(), Colour{1.0f, 1.0f, 1.0f}));
// 聚光范围:内角/外角/衰减指数
def.setSpotlightRange(20.0f, 60.0f, 1.5f);
CHECK_EQ(def.getSpotlightInnerAngle(), 20.0f);
CHECK_EQ(def.getSpotlightOuterAngle(), 60.0f);
CHECK_EQ(def.getSpotlightFalloff(), 1.5f);
// 衰减四元组
def.setAttenuation(50.0f, 0.0f, 1.0f, 0.0f);
def.getAttenuation(r, c, l, q);
CHECK_EQ(r, 50.0f);
CHECK_EQ(c, 0.0f);
CHECK_EQ(l, 1.0f);
CHECK_EQ(q, 0.0f);
// getDerivedDirection = -节点 z 轴
Light dir;
dir.setType(Light::LT_DIRECTIONAL);
dir.zAxis = Vec3{0, 0, 1};
CHECK(vecEq3(dir.getDerivedDirection(), Vec3{0, 0, -1}));
dir.zAxis = Vec3{0, 0, -1};
CHECK(vecEq3(dir.getDerivedDirection(), Vec3{0, 0, 1}));
dir.zAxis = Vec3{1, 0, 0};
CHECK(vecEq3(dir.getDerivedDirection(), Vec3{-1, 0, 0}));
// getAs4DVector:方向光 w=0,且 -dir 回补成 z 轴
dir.position = Vec3{10, 20, 30};
dir.zAxis = Vec3{0, 0, 1};
Vec4 dv = dir.getAs4DVector();
CHECK_EQ(dv.x, 0.0f);
CHECK_EQ(dv.y, 0.0f);
CHECK_EQ(dv.z, 1.0f);
CHECK_EQ(dv.w, 0.0f);
// 点光:位置 + w=1
Light pt;
pt.setType(Light::LT_POINT);
pt.position = Vec3{10, 20, 30};
Vec4 pv = pt.getAs4DVector();
CHECK_EQ(pv.x, 10.0f);
CHECK_EQ(pv.y, 20.0f);
CHECK_EQ(pv.z, 30.0f);
CHECK_EQ(pv.w, 1.0f);
// 聚光:位置 + w=1
Light sp;
sp.setType(Light::LT_SPOTLIGHT);
sp.position = Vec3{1, 2, 3};
Vec4 sv = sp.getAs4DVector();
CHECK_EQ(sv.x, 1.0f);
CHECK_EQ(sv.y, 2.0f);
CHECK_EQ(sv.z, 3.0f);
CHECK_EQ(sv.w, 1.0f);
return lab::report("04-1 光源类型:枚举/默认值/方向/4D 向量");
}
本机实测(Apple clang 16,clang++ -std=c++20 -Wall):编译零警告,断言 37/37 通过。
小结
LightTypes四类:点光(仅位置)、方向光(仅方向)、聚光(位置+方向+内外角)、矩形面光(位置+方向+尺寸)。- 默认值:漫反射白、镜面黑、外角 40°、内角 30°、衰减指数 2.0、衰减 (100000, 1, 0, 0)、类型点光。
- 方向统一从挂接节点的 -z 轴推导,旋转节点即旋转光照方向。
getAs4DVector用 w 分量归一位置型(w=1)与方向型(w=0),着色器与阴影系统统一消费。
阅读导航:上一节:3.3 Octree 场景管理 · 下一节:4.2 衰减与范围 。
继续阅读
探索更多技术文章
浏览归档,发现更多关于系统设计、工具链和工程实践的内容。