2021SC@SDUSC
目录
OvRendering函数库主要包括了几乎全部实现渲染管线的函数。以下是正文部分。
1.Geometry
Geometry中包含了两个函数BoundingSphere.h与Vertex.h,其中分别定义了一个数据结构。
1.1BoundingSphere.h
namespace OvRendering::Geometry
{
/**
* Data structure that defines a bounding sphere (Position + radius)
*/
struct BoundingSphere
{
OvMaths::FVector3 position;
float radius;
};
}
这个结构体很简单,我们可以看出他只有两个变量,一个3维向量position和一个单精度浮点数radius。
在实际渲染应用中,BoundingSphere(下文称包围球)用于进行两个对象的碰撞检测,position表示包围球的中心,radius表示包围球的半径。
在3D碰撞检测中,为了加快碰撞检测的效率,减少不必要的碰撞检测,会使用基本几何体作为物体的包围盒(Bounding Volume, BV)进行测试。
当网格对象被赋予了包围球后,当两个包围球发生不碰撞时,则没必要进行更复杂的碰撞检测,于是判定其归属的网格对象未发生碰撞。
在实际应用中,除了球体包围盒,还有AABB, OBB, 8-DOP, Convex Hull这几种常见的包围盒,详细的信息不在这里赘述。
1.2Vertex.h
namespace OvRendering::Geometry
{
/**
* Data structure that defines the geometry of a vertex
*/
struct Vertex
{
float position[3];
float texCoords[2];
float normals[3];
float tangent[3];
float bitangent[3];
};
}
Vertex表示一个3维顶点结构,在3D空间中,所有的物体都是一系列顶点经过拓扑连接后产生的网格,所以在渲染过程中,顶点是进行一切计算的基础。
在结构体中,定义了5个数组,其中position表示顶点的空间坐标,texCoords表示顶点的纹理坐标(纹理坐标的作用将在Texture.h中讲解),normals表示顶点所在平面的法向量,tangent表示顶点坐在平面的切向量,bitangent则是前两个向量的叉乘(t x n)。
normals,tangent,bitangent这3个向量用于构建物体表面某个顶点自身的局部坐标系。
这个坐标系将会在以后发挥作用。
2.Data/Frustum.h
Frustum是截面锥体的意思,正如前文讲过的Perspective矩阵,Frustum.h中主要进行了一个视锥体的创建以及碰撞检测的工作。
namespace OvRendering::Data
{
/**
* Mathematic representation of a 3D frustum
*/
class Frustum
{
public:
void CalculateFrustum(const OvMaths::FMatrix4& p_viewProjection);
bool PointInFrustum(float p_x, float p_y, float p_z) const;
bool SphereInFrustum(float p_x, float p_y, float p_z, float p_radius) const;
bool CubeInFrustum(float p_x, float p_y, float p_z, float p_size) const;
bool BoundingSphereInFrustum(const OvRendering::Geometry::BoundingSphere& p_boundingSphere, const OvMaths::FTransform& p_transform) const;
std::array<float, 4> GetNearPlane