Dandelion
2.0.0
A light-weight 3D builder for educational usage
Toggle main menu visibility
载入中...
搜索中...
未找到
ray.h
浏览该文件的文档.
1
#ifndef DANDELION_UTILS_RAY_H
2
#define DANDELION_UTILS_RAY_H
3
4
/*!
5
* \ingroup utils
6
* \file utils/ray.h
7
* \~chinese
8
* \brief 提供生成射线、判定相交的工具函数。
9
*/
10
11
#include <cstddef>
12
#include <optional>
13
14
#include <Eigen/Core>
15
16
#include "
../geometry/mesh.hpp
"
17
#include "
../scene/camera.h
"
18
19
/*!
20
* \ingroup rendering
21
* \ingroup utils
22
*/
23
struct
Ray
24
{
25
Eigen::Vector3f origin;
26
Eigen::Vector3f direction;
27
};
28
29
/*!
30
* \ingroup rendering
31
* \ingroup utils
32
* \~chinese
33
* \brief 表示射线与 Mesh 相交结果的结构体。
34
*
35
* Intersection 对象总是有意义的,一旦被创建就表示相交确实发生了。
36
*/
37
struct
Intersection
38
{
39
/*! \~chinese Intersection的构造函数:将t设置为无穷,face_index为零 */
40
Intersection
();
41
/*! \~chinese 射线以 \f$\mathrm{o}+t\mathrm{d}\f$ 表示,交点处对应的 \f$t\f$ 值。 */
42
float
t
;
43
/*! \~chinese 交点所在面片的序号,可用作 `GL::Mesh::face` 方法的参数。 */
44
std::size_t
face_index
;
45
/*! \~chinese 交点处的重心坐标。 */
46
Eigen::Vector3f
barycentric_coord
;
47
/*! \~chinese 交点所在面片的法向量。 */
48
Eigen::Vector3f
normal
;
49
};
50
51
/*!
52
* \ingroup rendering
53
* \~chinese
54
* \brief 给定成像平面的宽度和高度、成像平面上的坐标、成像平面的深度和相机,生成一条射线。
55
*
56
* \param width 成像平面的宽度(以像素计)
57
* \param height 成像平面的高度(以像素计)
58
* \param x 成像平面上指定点的 x 坐标(宽度方向,以像素计)
59
* \param y 成像平面上指定点的 y 坐标(高度方向,以像素计)
60
* \param camera 指定的相机
61
* \param depth 成像平面在指定相机观察空间 (view space) 下的深度值(应当为正数)
62
*
63
* \returns 构造出的射线
64
*/
65
Ray
generate_ray
(
int
width,
int
height,
int
x,
int
y,
Camera
& camera,
float
depth);
66
67
/*!
68
* \ingroup rendering
69
* \~chinese
70
* \brief 判断光线ray是否与某个面片相交
71
*
72
* 这个函数判断是否与一个三角形面片相交
73
*
74
* \param ray 射线
75
* \param mesh 面片所在的mesh
76
* \param index 面片的索引
77
*
78
* \returns 一个 `std::optional` 对象,若相交,则此对象有值 (`has_value() == true`);不相交则返回
79
* `std::nullopt` 。
80
*/
81
std::optional<Intersection>
82
ray_triangle_intersect
(
const
Ray
& ray,
const
Mesh
& mesh,
size_t
index);
83
84
/*!
85
* \ingroup rendering
86
* \~chinese
87
* \brief 用朴素方法判断射线是否与给定的 mesh 相交。
88
*
89
* 这个函数顺序遍历 mesh 中的所有面片,逐一判断射线是否与之相交,最终取所有交点中 \f$t\f$
90
* 值最小的一个作为结果。
91
*
92
* \param ray 射线
93
* \param mesh 指定的 mesh
94
* \param model 这个 mesh 对应的模型变换矩阵 (model transform)
95
*
96
* \returns 一个 `std::optional` 对象,若相交,则此对象有值 (`has_value() == true`);不相交则返回
97
* `std::nullopt` 。
98
*/
99
std::optional<Intersection>
100
naive_intersect(
const
Ray
& ray,
const
Mesh
& mesh,
const
Eigen::Matrix4f model);
101
102
#endif
// DANDELION_UTILS_RAY_H
camera.h
包含相机的类,用来表示场景中的相机。
generate_ray
Ray generate_ray(int width, int height, int x, int y, Camera &camera, float depth)
给定成像平面的宽度和高度、成像平面上的坐标、成像平面的深度和相机,生成一条射线。
定义
ray.cpp:28
ray_triangle_intersect
optional< Intersection > ray_triangle_intersect(const Ray &ray, const Mesh &mesh, size_t index)
判断光线ray是否与某个面片相交
定义
ray.cpp:45
mesh.hpp
Camera
表示观察点的相机,既可以用于预览视角,也可以用于渲染视角。
定义
camera.h:24
Intersection::t
float t
定义
ray.h:42
Intersection::normal
Eigen::Vector3f normal
定义
ray.h:48
Intersection::Intersection
Intersection()
定义
ray.cpp:24
Intersection::face_index
std::size_t face_index
定义
ray.h:44
Intersection::barycentric_coord
Eigen::Vector3f barycentric_coord
定义
ray.h:46
Mesh
用于同步渲染数据的三角形 Mesh 。
定义
mesh.hpp:28
Ray
定义
ray.h:24
src
utils
ray.h
制作者
1.18.0