feat: optimized triangle testing
This commit is contained in:
61
polygon.hpp
61
polygon.hpp
@@ -13,6 +13,8 @@ struct polygon {
|
||||
decimal bounding[4]; // min x, max x, min y, max y
|
||||
vec3 normals[3];
|
||||
vec3 colors[3];
|
||||
vec3 barycentrics;
|
||||
vec3 boundingBarycentrics;
|
||||
|
||||
polygon(const vec3 &v1, const vec3 &v2, const vec3 &v3)
|
||||
: points{v1, v2, v3}, delta{} {}
|
||||
@@ -61,13 +63,13 @@ struct polygon {
|
||||
|
||||
vec3 avgNormal() {
|
||||
vec3 result;
|
||||
for (int i = 0; i < 3;i++) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
result += normals[i];
|
||||
}
|
||||
return result * decimal(0.3333);
|
||||
}
|
||||
|
||||
const bool contains(const vec3 &p) {
|
||||
const bool depContains(const vec3 &p) {
|
||||
// if (skip)
|
||||
// return false;
|
||||
for (int i = 0; i < 3; i++) {
|
||||
@@ -80,6 +82,14 @@ struct polygon {
|
||||
}
|
||||
return true;
|
||||
}
|
||||
const bool contains(const vec3 &p) {
|
||||
if (small)
|
||||
return true;
|
||||
else
|
||||
return (barycentrics[0] >= decimal(-0.01)) &&
|
||||
(barycentrics[1] >= decimal(-0.01)) &&
|
||||
(barycentrics[2] >= decimal(-0.01));
|
||||
}
|
||||
friend std::ostream &operator<<(std::ostream &os, const polygon &p) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
os << p.points[i];
|
||||
@@ -87,40 +97,37 @@ struct polygon {
|
||||
return os;
|
||||
}
|
||||
|
||||
vec3 calcNormal(vec3 barycentrics) {
|
||||
vec3 result = normals[0] * barycentrics[0] +
|
||||
normals[1] * barycentrics[1] +
|
||||
normals[2] * barycentrics[2];
|
||||
if (result.isSmall())
|
||||
return normals[0];
|
||||
else
|
||||
return result.normalize();
|
||||
vec3 calcNormal() {
|
||||
return normals[0] * boundingBarycentrics[0] +
|
||||
normals[1] * boundingBarycentrics[1] +
|
||||
normals[2] * boundingBarycentrics[2];
|
||||
}
|
||||
|
||||
vec3 calcColor(vec3 barycentrics) {
|
||||
return colors[0] * barycentrics[0] + colors[1] * barycentrics[1] +
|
||||
colors[2] * barycentrics[2];
|
||||
vec3 calcColor() {
|
||||
return colors[0] * boundingBarycentrics[0] +
|
||||
colors[1] * boundingBarycentrics[1] +
|
||||
colors[2] * boundingBarycentrics[2];
|
||||
}
|
||||
|
||||
decimal calcDepth(vec3 barycentrics) {
|
||||
return points[0].z() * barycentrics[0] +
|
||||
points[1].z() * barycentrics[1] +
|
||||
points[2].z() * barycentrics[2];
|
||||
decimal calcDepth() {
|
||||
return points[0].z() * boundingBarycentrics[0] +
|
||||
points[1].z() * boundingBarycentrics[1] +
|
||||
points[2].z() * boundingBarycentrics[2];
|
||||
}
|
||||
|
||||
vec3 calcBarycentric(vec3 s) {
|
||||
void calcBarycentric(vec3 s) {
|
||||
// if (small)
|
||||
// return vec3(decimal(0.333), decimal(0.333), decimal(0.333));
|
||||
vec3 result;
|
||||
result[0] = (points[1].x() - s.x()) * (points[2].y() - s.y()) -
|
||||
(points[2].x() - s.x()) * (points[1].y() - s.y());
|
||||
result[1] = (points[2].x() - s.x()) * (points[0].y() - s.y()) -
|
||||
(points[0].x() - s.x()) * (points[2].y() - s.y());
|
||||
result = result * baryFactor;
|
||||
result[2] = decimal(1.0) - result[1] - result[0];
|
||||
|
||||
barycentrics[0] = (points[1].x() - s.x()) * (points[2].y() - s.y()) -
|
||||
(points[2].x() - s.x()) * (points[1].y() - s.y());
|
||||
barycentrics[1] = (points[2].x() - s.x()) * (points[0].y() - s.y()) -
|
||||
(points[0].x() - s.x()) * (points[2].y() - s.y());
|
||||
barycentrics = barycentrics * baryFactor;
|
||||
barycentrics[2] = decimal(1.0) - barycentrics[1] - barycentrics[0];
|
||||
// return result;
|
||||
return vec3::max(vec3::min(result, vec3(1.0, 1.0, 1.0)),
|
||||
vec3(0.0, 0.0, 0.0));
|
||||
boundingBarycentrics = vec3::max(
|
||||
vec3::min(barycentrics, vec3(1.0, 1.0, 1.0)), vec3(0.0, 0.0, 0.0));
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user