Skip to content

Commit c84c53d

Browse files
committed
add more to notes13
1 parent d3b7487 commit c84c53d

File tree

6 files changed

+55
-8
lines changed

6 files changed

+55
-8
lines changed

task5/task5/task5.xcodeproj/xcuserdata/lilin.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,16 @@
77
<BreakpointProxy
88
BreakpointExtensionID = "Xcode.Breakpoint.FileBreakpoint">
99
<BreakpointContent
10-
uuid = "B81C8E48-F2F4-4CC9-96B5-8C9E67FDA84E"
10+
uuid = "A6B7847D-653A-4307-8168-E26F3DD812B4"
1111
shouldBeEnabled = "No"
1212
ignoreCount = "0"
1313
continueAfterRunningActions = "No"
1414
filePath = "task5/Renderer.cpp"
1515
startingColumnNumber = "9223372036854775807"
1616
endingColumnNumber = "9223372036854775807"
17-
startingLineNumber = "96"
18-
endingLineNumber = "96"
19-
landmarkName = "trace(orig, dir, objects)"
17+
startingLineNumber = "154"
18+
endingLineNumber = "154"
19+
landmarkName = "castRay(orig, dir, scene, depth)"
2020
landmarkType = "9">
2121
</BreakpointContent>
2222
</BreakpointProxy>

task5/task5/task5/Renderer.cpp

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ Vector3f refract(const Vector3f &I, const Vector3f &N, const float &ior)
4646
//
4747
// \param ior is the material refractive index
4848
// [/comment]
49+
50+
51+
// 菲涅耳方程(Fresnel equation)描述了光线经过两个介质的界面时,反射和透射的光强比重。
52+
// 参考 https://www.zhihu.com/question/53022233
53+
// https : //zhuanlan.zhihu.com/p/31534769
54+
// snell law n1*sinθ1=n2*sinθ2;
4955
float fresnel(const Vector3f &I, const Vector3f &N, const float &ior)
5056
{
5157
float cosi = clamp(-1, 1, dotProduct(I, N));
@@ -87,16 +93,20 @@ std::optional<hit_payload> trace(
8793
float tNear = kInfinity;
8894
std::optional<hit_payload> payload;
8995

90-
//
9196
for (const auto & object : objects)
9297
{
9398
float tNearK = kInfinity;
9499
uint32_t indexK;
95100
Vector2f uvK;
96101

97102
// 调用的三角形 OR 球体 intersect 方法
103+
// intersect 中每个参数都是引用传递
104+
// 注意tNearK < tNear 经过循环后得到的是最近的相交物体
98105
if (object->intersect(orig, dir, tNearK, indexK, uvK) && tNearK < tNear)
99106
{
107+
// 这个 emplace 是 std::optional 下的函数
108+
// https://en.cppreference.com/w/cpp/utility/optional/emplace
109+
// 看起来很像构造函数
100110
payload.emplace();
101111
payload->hit_obj = object.get();
102112
payload->tNear = tNearK;
@@ -124,6 +134,8 @@ std::optional<hit_payload> trace(
124134
// If the surface is diffuse/glossy we use the Phong illumation model to compute the color
125135
// at the intersection point.
126136
// [/comment]
137+
138+
// castRay 就是递归的过程
127139
Vector3f castRay(
128140
const Vector3f &orig, const Vector3f &dir, const Scene& scene,
129141
int depth)
@@ -133,17 +145,31 @@ Vector3f castRay(
133145
}
134146

135147
Vector3f hitColor = scene.backgroundColor;
148+
149+
// payload 类型是 hit_payload trace 是获取碰撞点的详情
136150
if (auto payload = trace(orig, dir, scene.get_objects()); payload)
137151
{
138152
Vector3f hitPoint = orig + dir * payload->tNear;
139153
Vector3f N; // normal
140154
Vector2f st; // st coordinates
155+
156+
// getSurfaceProperties
157+
// getSurfaceProperties 所有参数都是引用传递 但实际上只有 st N 被写入了值
158+
// st 是纹理坐标 (重心坐标系统) N是三角形面的法线
141159
payload->hit_obj->getSurfaceProperties(hitPoint, dir, payload->index, payload->uv, N, st);
142160
switch (payload->hit_obj->materialType) {
161+
162+
// 如果ray 射出到了玻璃球体
143163
case REFLECTION_AND_REFRACTION:
144-
{
164+
{
165+
// 反射方向
145166
Vector3f reflectionDirection = normalize(reflect(dir, N));
167+
168+
// 折射方向
146169
Vector3f refractionDirection = normalize(refract(dir, N, payload->hit_obj->ior));
170+
171+
// 注意在castRay 一开始定义ray dir 的时候 z 是 -1
172+
// 点乘 > 0 hitPoint 会变大 看起来是略微向摄像机的位置移动了一点
147173
Vector3f reflectionRayOrig = (dotProduct(reflectionDirection, N) < 0) ?
148174
hitPoint - N * scene.epsilon :
149175
hitPoint + N * scene.epsilon;

task5/task5/task5/Triangle.hpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,12 @@ class MeshTriangle : public Object
6464
// 此刻 maxIndex = 4 numTris = 2 verts 是包含4个顶点的数组
6565
vertices = std::unique_ptr<Vector3f[]>(new Vector3f[maxIndex]);
6666
memcpy(vertices.get(), verts, sizeof(Vector3f) * maxIndex); // 相当于数组复制 verts 中的数据复制到 vertices 中
67+
6768
vertexIndex = std::unique_ptr<uint32_t[]>(new uint32_t[numTris * 3]);
6869
memcpy(vertexIndex.get(), vertsIndex, sizeof(uint32_t) * numTris * 3);
70+
6971
numTriangles = numTris;
72+
7073
stCoordinates = std::unique_ptr<Vector2f[]>(new Vector2f[maxIndex]);
7174
memcpy(stCoordinates.get(), st, sizeof(Vector2f) * maxIndex);
7275
}
@@ -100,15 +103,24 @@ class MeshTriangle : public Object
100103
return intersect;
101104
}
102105

106+
107+
// PS : C++ 中如果参数不使用可以省略名字
108+
109+
// index 是当前三角形在全部三角形list 中的下标
110+
// N 是三角形面的法线
103111
void getSurfaceProperties(const Vector3f&, const Vector3f&, const uint32_t& index, const Vector2f& uv, Vector3f& N,
104112
Vector2f& st) const override
105113
{
114+
// vertices {{-5,-3,-6}, {5,-3,-6}, {5,-3,-16}, {-5,-3,-16}};
115+
// vertexIndex {0, 1, 3, 1, 2, 3};
116+
// stCoordinates {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
106117
const Vector3f& v0 = vertices[vertexIndex[index * 3]];
107118
const Vector3f& v1 = vertices[vertexIndex[index * 3 + 1]];
108119
const Vector3f& v2 = vertices[vertexIndex[index * 3 + 2]];
109120
Vector3f e0 = normalize(v1 - v0);
110121
Vector3f e1 = normalize(v2 - v1);
111122
N = normalize(crossProduct(e0, e1));
123+
112124
const Vector2f& st0 = stCoordinates[vertexIndex[index * 3]];
113125
const Vector2f& st1 = stCoordinates[vertexIndex[index * 3 + 1]];
114126
const Vector2f& st2 = stCoordinates[vertexIndex[index * 3 + 2]];

task5/task5/task5/main.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,25 @@ int main()
1515
sph1->materialType = DIFFUSE_AND_GLOSSY;
1616
sph1->diffuseColor = Vector3f(0.6, 0.7, 0.8);
1717

18+
// 右手坐标系
19+
// 玻璃球离自己更近
1820
auto sph2 = std::make_unique<Sphere>(Vector3f(0.5, -0.5, -8), 1.5);
19-
sph2->ior = 1.5;
21+
sph2->ior = 1.5; // 折射率
2022
sph2->materialType = REFLECTION_AND_REFRACTION;
2123

2224
scene.Add(std::move(sph1));
2325
scene.Add(std::move(sph2));
2426

27+
// 顶点信息
2528
Vector3f verts[4] = {{-5,-3,-6}, {5,-3,-6}, {5,-3,-16}, {-5,-3,-16}};
26-
uint32_t vertIndex[6] = {0, 1, 3, 1, 2, 3};
29+
30+
// 这里描述了texture 信息 也就是texture的四个顶点
31+
// 作用和 verts 相同
2732
Vector2f st[4] = {{0, 0}, {1, 0}, {1, 1}, {0, 1}};
33+
34+
// 所有点的下标 构成的数组
35+
// 这个数组三个一组 构成一个三角形
36+
uint32_t vertIndex[6] = {0, 1, 3, 1, 2, 3};
2837

2938
// 后面这些参数就是 MeshTriangle 构造函数所需参数
3039
auto mesh = std::make_unique<MeshTriangle>(verts, vertIndex, 2, st);

0 commit comments

Comments
 (0)