[toc]

# Chapter 4 图元和相交加速

# 综述

直接渲染的类和直接操作信息的类统称为 Primitive 类,基于 Primitive 类之上的是 Aggregate
这样有一个好处,就是在进行相交检测加速时,可以进行整组的剔除。
本章会介绍两个加速器
BVH 和 K-d

# Primitive 基类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// Primitive Declarations
class Primitive {
public:
// Primitive Interface
virtual ~Primitive();
virtual Bounds3f WorldBound() const = 0;
virtual bool Intersect(const Ray &r, SurfaceInteraction *) const = 0;
virtual bool IntersectP(const Ray &r) const = 0;
///描述该几何图元是否是光源
virtual const AreaLight *GetAreaLight() const = 0;
virtual const Material *GetMaterial() const = 0;
///初始化曲面上交点处的光散射材质的性质表示,如BSDF BSSRDF
///SurfaceInteraction中存放该点的BSDF 和 BSSRDF的指针
virtual void ComputeScatteringFunctions(SurfaceInteraction *isect,
MemoryArena &arena,
TransportMode mode,
bool allowMultipleLobes) const = 0;
};

Primitive 是图圆的最小基类,如上述接口可以看出,可以获得当前图元集合的 Bounds, 同时可以对该图元进行相交检测

基于 Primitive 派生出:

  1. 几何图元、Tranform 图元
  2. 图元祖 (Aggregate)

# 相交函数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool GeometricPrimitive::Intersect(const Ray &r,
SurfaceInteraction *isect) const {
Float tHit;
if (!shape->Intersect(r, &tHit, isect)) return false;
r.tMax = tHit;
isect->primitive = this;
CHECK_GE(Dot(isect->n, isect->shading.n), 0.);
// Initialize _SurfaceInteraction::mediumInterface_ after _Shape_
// intersection
if (mediumInterface.IsMediumTransition())
isect->mediumInterface = mediumInterface;
else
isect->mediumInterface = MediumInterface(r.medium);
return true;
}

即先通过 shape 与 Ray 进行相交检测,然后在最后判断介质类型

# TransformPrimitive (实例化图元)

物体实例化技术,即将同样信息的图元集合到一起,避免重复将物体加载到内存中