Skip to content

Commit c23a2e3

Browse files
authored
update hw6
1 parent 3756c73 commit c23a2e3

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

Assignment5/codes/Sphere.hpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ class Sphere : public Object
5858
if (!solveQuadratic(a, b, c, t0, t1))
5959
return false;
6060
if (t0 < 0)
61-
t0 = t1;
61+
t0 = t2;
6262
if (t0 < 0)
6363
return false;
6464
tnear = t0;

Assignment6/codes/BVH.cpp

+11
Original file line numberDiff line numberDiff line change
@@ -105,5 +105,16 @@ Intersection BVHAccel::Intersect(const Ray& ray) const
105105
Intersection BVHAccel::getIntersection(BVHBuildNode* node, const Ray& ray) const
106106
{
107107
// TODO Traverse the BVH to find intersection
108+
Intersection isect;
109+
std::arrray<int, 3> dirIsNeg= {ray.direction.x > 0, ray.direction.y > 0, ray,direction.z > 0};
110+
if (!node->bounds.IntersectP(ray, ray.direction_inv, dirIsNeg)) {
111+
return isect;
112+
}
113+
if (node->left == nullptr && node->right == nullptr) {
114+
return node->object->getIntersection(ray);
115+
}
116+
Intersection lsec = getIntersection(node->left, ray);
117+
Intersection rsec = getIntersection(node->right, ray);
118+
return lsec.distance < rsec.distance ? lsec : rsec;
108119

109120
}

Assignment6/codes/Bounds3.hpp

+19
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,26 @@ inline bool Bounds3::IntersectP(const Ray& ray, const Vector3f& invDir,
9696
// invDir: ray direction(x,y,z), invDir=(1.0/x,1.0/y,1.0/z), use this because Multiply is faster that Division
9797
// dirIsNeg: ray direction(x,y,z), dirIsNeg=[int(x>0),int(y>0),int(z>0)], use this to simplify your logic
9898
// TODO test if ray bound intersects
99+
Vector3f tMin = (pMin - ray.direction) * invDir;
100+
Vector3f tMax = (pMax - ray.direction) * invDir;
101+
102+
if (dirIsNeg[0]) {
103+
std::swap(tMin.x, tMax.x);
104+
}
105+
if (dirIsNeg[1]) {
106+
std::swap(tMin.y, tMax.y);
107+
}
108+
if (dirIsNeg[2]) {
109+
std::swap(tMin.z, tMax.z);
110+
}
111+
float tEnter, tExit;
112+
tEnter = fmin(tMin,x, fmin(tMin.y, tMin.z));
113+
tExit = fmax(tMax,x, fmax(tMax.y, tMax.z));
99114

115+
if (tEnter < tExit && tExit > 0) {
116+
return true;
117+
}
118+
return false;
100119
}
101120

102121
inline Bounds3 Union(const Bounds3& b1, const Bounds3& b2)

Assignment6/codes/Triangle.hpp

+5-9
Original file line numberDiff line numberDiff line change
@@ -207,10 +207,6 @@ inline bool Triangle::intersect(const Ray& ray, float& tnear,
207207
}
208208

209209
inline Bounds3 Triangle::getBounds() { return Union(Bounds3(v0, v1), v2); }
210-
/*
211-
212-
*/
213-
214210

215211
inline Intersection Triangle::getIntersection(Ray ray)
216212
{
@@ -237,11 +233,11 @@ inline Intersection Triangle::getIntersection(Ray ray)
237233

238234
// TODO find ray triangle intersection
239235
inter.happened = true;
240-
inter.coords = Vector3f(ray.origin + ray.direction * t_tmp);
241-
inter.normal = normal;
242-
inter.m = this->m;
243-
inter.obj = this;
244-
inter.distance = t_tmp;
236+
inter.coords = Vector3f(ray.origin + ray.direction * t_tmp);
237+
inter.normal = normal;
238+
inter.m = this->m;
239+
inter.obj = this;
240+
inter.distance = t_tmp;
245241

246242

247243
return inter;

0 commit comments

Comments
 (0)