-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
83f48bf
commit 1f7bcf3
Showing
11 changed files
with
112 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ | |
.cache | ||
/target | ||
/scenes | ||
.vscode | ||
.vscode | ||
/examples |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
#include "AABB.h" | ||
|
||
Point3f minP(const Point3f &p1, const Point3f &p2) { | ||
return Point3f{std::min(p1[0], p2[0]), std::min(p1[1], p2[1]), | ||
std::min(p1[2], p2[2])}; | ||
} | ||
|
||
Point3f maxP(const Point3f &p1, const Point3f &p2) { | ||
return Point3f{std::max(p1[0], p2[0]), std::max(p1[1], p2[1]), | ||
std::max(p1[2], p2[2])}; | ||
} | ||
|
||
AABB AABB::Union(const AABB &other) const { | ||
Point3f min = minP(other.pMin, pMin), max = maxP(other.pMax, pMax); | ||
return AABB{min, max}; | ||
} | ||
|
||
void AABB::Expand(const AABB &other) { | ||
pMin = minP(pMin, other.pMin); | ||
pMax = maxP(pMax, other.pMax); | ||
} | ||
|
||
AABB AABB::Union(const Point3f &other) const { | ||
Point3f min = minP(other, pMin), max = maxP(other, pMax); | ||
return AABB{min, max}; | ||
} | ||
|
||
void AABB::Expand(const Point3f &other) { | ||
pMin = minP(pMin, other); | ||
pMax = maxP(pMax, other); | ||
} | ||
|
||
bool AABB::rayIntersect(const Ray &ray, float *tMin, float *tMax) const { | ||
float tNear = ray.tNear, tFar = ray.tFar; | ||
for (int i = 0; i < 3; ++i) { | ||
float invDir = 1.f / ray.direction[i]; | ||
float t0 = (pMin[i] - ray.origin[i]) * invDir, | ||
t1 = (pMax[i] - ray.origin[i]) * invDir; | ||
if (t0 > t1) | ||
std::swap(t0, t1); | ||
tNear = std::max(tNear, t0); | ||
tFar = std::min(tFar, t1); | ||
|
||
if (tNear > tFar) | ||
return false; | ||
} | ||
if (tMin) | ||
*tMin = tNear; | ||
if (tMax) | ||
*tMax = tFar; | ||
return true; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
#pragma once | ||
#include <CoreLayer/Math/Math.h> | ||
#include <FunctionLayer/Ray/Ray.h> | ||
// Axis-aligned Bounding box,即AABB | ||
class AABB { | ||
public: | ||
AABB() { | ||
pMin = Point3f{FLT_MAX, FLT_MAX, FLT_MAX}; | ||
pMax = Point3f{-FLT_MAX, -FLT_MAX, -FLT_MAX}; | ||
} | ||
|
||
AABB(const Point3f &_pMin, const Point3f &_pMax) : pMin(_pMin), pMax(_pMax) {} | ||
|
||
AABB Union(const AABB &other) const; | ||
|
||
void Expand(const AABB &other); | ||
|
||
AABB Union(const Point3f &other) const; | ||
|
||
void Expand(const Point3f &other); | ||
|
||
bool rayIntersect(const Ray &ray, float *tMin = nullptr, | ||
float *tMax = nullptr) const; | ||
|
||
public: | ||
Point3f pMin, pMax; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters