Skip to content

Commit

Permalink
assignment6
Browse files Browse the repository at this point in the history
  • Loading branch information
roeas committed Nov 23, 2023
1 parent c4d8278 commit 7e7aa08
Show file tree
Hide file tree
Showing 5 changed files with 4,824 additions and 23 deletions.
4,762 changes: 4,762 additions & 0 deletions Frame/Asset/output/assigment6.ppm

Large diffs are not rendered by default.

24 changes: 21 additions & 3 deletions Frame/Source/Assignment/Assignment6/BVH.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,24 @@ Intersection BVHAccel::Intersect(const Ray& ray) const

Intersection BVHAccel::getIntersection(BVHBuildNode* node, const Ray& ray) const
{
// TODO Traverse the BVH to find intersection
return Intersection{};
}
std::array<int, 3> dirIsNeg{
static_cast<int>(ray.direction.x < 0),
static_cast<int>(ray.direction.y < 0),
static_cast<int>(ray.direction.z < 0)
};
if (!node->bounds.IntersectP(ray, ray.direction_inv, std::move(dirIsNeg)))
{
return Intersection{};
}

// Ò¶½Úµã
if (node->left == nullptr && node->right == nullptr)
{
return node->object->getIntersection(ray);
}

Intersection leaf1 = getIntersection(node->left, ray);
Intersection leaf2 = getIntersection(node->right, ray);

return leaf1.distance < leaf2.distance ? leaf1 : leaf2;
}
36 changes: 30 additions & 6 deletions Frame/Source/Assignment/Assignment6/Bounds3.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,37 @@ class Bounds3



inline bool Bounds3::IntersectP(const Ray& ray, const Vector3f& invDir,
const std::array<int, 3>& dirIsNeg) const
inline bool Bounds3::IntersectP(const Ray& ray, const Vector3f& invDir, const std::array<int, 3>& dirIsNeg) const
{
// invDir: ray direction(x,y,z), invDir=(1.0/x,1.0/y,1.0/z), use this because Multiply is faster that Division
// dirIsNeg: ray direction(x,y,z), dirIsNeg=[int(x>0),int(y>0),int(z>0)], use this to simplify your logic
// TODO test if ray bound intersects

float min_x = (pMin.x - ray.origin.x) * invDir[0];
float max_x = (pMax.x - ray.origin.x) * invDir[0];
float min_y = (pMin.y - ray.origin.y) * invDir[1];
float max_y = (pMax.y - ray.origin.y) * invDir[1];
float min_z = (pMin.z - ray.origin.z) * invDir[2];
float max_z = (pMax.z - ray.origin.z) * invDir[2];

if (dirIsNeg[0])
{
std::swap(min_x, max_x);
}
if (dirIsNeg[1])
{
std::swap(min_y, max_y);
}
if (dirIsNeg[2])
{
std::swap(min_z, max_z);
}

float enter = std::max(min_x, std::max(min_y, min_z));
float exit = std::min(max_x, std::min(max_y, max_z));

if (enter < exit && exit >= 0)
{
return true;
}

return false;
}

inline Bounds3 Union(const Bounds3& b1, const Bounds3& b2)
Expand Down
12 changes: 3 additions & 9 deletions Frame/Source/Assignment/Assignment6/Renderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,11 @@ void Renderer::Render(const Scene& scene)
for (uint32_t j = 0; j < scene.height; ++j) {
for (uint32_t i = 0; i < scene.width; ++i) {
// generate primary ray direction
float x = (2 * (i + 0.5) / (float)scene.width - 1) *
imageAspectRatio * scale;
float x = (2 * (i + 0.5) / (float)scene.width - 1) * imageAspectRatio * scale;
float y = (1 - 2 * (j + 0.5) / (float)scene.height) * scale;
// TODO: Find the x and y positions of the current pixel to get the
// direction
// vector that passes through it.
// Also, don't forget to multiply both of them with the variable
// *scale*, and x (horizontal) variable with the *imageAspectRatio*

// Don't forget to normalize this direction!

Ray ray{ eye_pos, normalize(Vector3f(x, y, -1)) };
framebuffer[m++] = scene.castRay(std::move(ray), 0);
}
UpdateProgress(j / (float)scene.height);
}
Expand Down
13 changes: 8 additions & 5 deletions Frame/Source/Assignment/Assignment6/Triangle.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,6 @@ inline Intersection Triangle::getIntersection(Ray ray)
double det = dotProduct(e1, pvec);
if (fabs(det) < EPSILON)
return inter;

double det_inv = 1. / det;
Vector3f tvec = ray.origin - v0;
u = dotProduct(tvec, pvec) * det_inv;
Expand All @@ -230,11 +229,15 @@ inline Intersection Triangle::getIntersection(Ray ray)
if (v < 0 || u + v > 1)
return inter;
t_tmp = dotProduct(e2, qvec) * det_inv;
if (t_tmp < 0)
return inter;

// TODO find ray triangle intersection



inter.distance = t_tmp;
inter.happened = true;
inter.m = m;
inter.obj = this;
inter.normal = normal;
inter.coords = ray(t_tmp);

return inter;
}
Expand Down

0 comments on commit 7e7aa08

Please sign in to comment.