From bd0556cf22504473371c3f161569dfed109fe2d1 Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Thu, 9 Feb 2017 00:00:50 -0700 Subject: [PATCH] light: switch to new Face_Centroid --- common/bsputils.cc | 5 +++ common/mathlib.cc | 30 ++++++++++++++++++ include/common/bsputils.hh | 1 + include/common/mathlib.hh | 2 ++ include/light/ltface.hh | 1 - light/light.cc | 14 ++++---- light/ltface.cc | 65 ++------------------------------------ light/ltface2.cc | 13 +------- 8 files changed, 47 insertions(+), 84 deletions(-) diff --git a/common/bsputils.cc b/common/bsputils.cc index db6c5219..854a2fef 100644 --- a/common/bsputils.cc +++ b/common/bsputils.cc @@ -296,3 +296,8 @@ std::vector GLM_FacePoints(const bsp2_t *bsp, const bsp2_dface_t *f) } return points; } + +glm::vec3 Face_Centroid(const bsp2_t *bsp, const bsp2_dface_t *face) +{ + return GLM_PolyCentroid(GLM_FacePoints(bsp, face)); +} diff --git a/common/mathlib.cc b/common/mathlib.cc index c561c726..d0f93007 100644 --- a/common/mathlib.cc +++ b/common/mathlib.cc @@ -350,6 +350,12 @@ GLM_EdgePlanes_PointInside(const vector &edgeplanes, const vec3 &point) return minDist >= -POINT_EQUAL_EPSILON; } +vec3 +GLM_TriangleCentroid(const vec3 &v0, const vec3 &v1, const vec3 &v2) +{ + return (v0 + v1 + v2) / 3.0f; +} + float GLM_TriangleArea(const vec3 &v0, const vec3 &v1, const vec3 &v2) { @@ -360,3 +366,27 @@ float GLM_DistAbovePlane(const glm::vec4 &plane, const glm::vec3 &point) { return dot(vec3(plane), point) - plane.w; } + +glm::vec3 GLM_PolyCentroid(std::vector points) +{ + Q_assert(points.size() >= 3); + + vec3 poly_centroid(0); + float poly_area = 0; + + const vec3 v0 = points.at(0); + for (int i = 2; i < points.size(); i++) { + const vec3 v1 = points.at(i-1); + const vec3 v2 = points.at(i); + + const float triarea = GLM_TriangleArea(v0, v1, v2); + const vec3 tricentroid = GLM_TriangleCentroid(v0, v1, v2); + + poly_area += triarea; + poly_centroid = poly_centroid + (triarea * tricentroid); + } + + poly_centroid /= poly_area; + + return poly_centroid; +} diff --git a/include/common/bsputils.hh b/include/common/bsputils.hh index cdfc8e51..189a7c1d 100644 --- a/include/common/bsputils.hh +++ b/include/common/bsputils.hh @@ -47,5 +47,6 @@ glm::vec3 Face_PointAtIndex_E(const bsp2_t *bsp, const bsp2_dface_t *f, int v); glm::vec3 Vertex_GetPos_E(const bsp2_t *bsp, int num); glm::vec3 Face_Normal_E(const bsp2_t *bsp, const bsp2_dface_t *f); std::vector GLM_FacePoints(const bsp2_t *bsp, const bsp2_dface_t *face); +glm::vec3 Face_Centroid(const bsp2_t *bsp, const bsp2_dface_t *face); #endif /* __COMMON_BSPUTILS_HH__ */ diff --git a/include/common/mathlib.hh b/include/common/mathlib.hh index 3b04300c..7626afed 100644 --- a/include/common/mathlib.hh +++ b/include/common/mathlib.hh @@ -321,7 +321,9 @@ glm::vec3 GLM_FaceNormal(std::vector points); std::vector GLM_MakeInwardFacingEdgePlanes(std::vector points); bool GLM_EdgePlanes_PointInside(const std::vector &edgeplanes, const glm::vec3 &point); float GLM_EdgePlanes_PointInsideDist(const std::vector &edgeplanes, const glm::vec3 &point); +glm::vec3 GLM_TriangleCentroid(const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2); float GLM_TriangleArea(const glm::vec3 &v0, const glm::vec3 &v1, const glm::vec3 &v2); float GLM_DistAbovePlane(const glm::vec4 &plane, const glm::vec3 &point); +glm::vec3 GLM_PolyCentroid(std::vector points); #endif /* __COMMON_MATHLIB_H__ */ diff --git a/include/light/ltface.hh b/include/light/ltface.hh index 6924a0b7..028cc6de 100644 --- a/include/light/ltface.hh +++ b/include/light/ltface.hh @@ -42,7 +42,6 @@ extern std::atomic total_light_rays, total_light_ray_hits, total_samplepoints; extern std::atomic total_bounce_rays, total_bounce_ray_hits; -void FaceCentroid(const bsp2_dface_t *face, const bsp2_t *bsp, vec3_t out); void WorldToTexCoord(const vec3_t world, const texinfo_t *tex, vec_t coord[2]); void PrintFaceInfo(const bsp2_dface_t *face, const bsp2_t *bsp); // FIXME: remove light param. add normal param and dir params. diff --git a/light/light.cc b/light/light.cc index 9865fc07..5de11651 100644 --- a/light/light.cc +++ b/light/light.cc @@ -568,20 +568,18 @@ CheckNoDebugModeSet() // returns the face with a centroid nearest the given point. static const bsp2_dface_t * -Face_NearestCentroid(const bsp2_t *bsp, const vec3_t point) +Face_NearestCentroid(const bsp2_t *bsp, const glm::vec3 &point) { const bsp2_dface_t *nearest_face = NULL; - vec_t nearest_dist = VECT_MAX; + float nearest_dist = VECT_MAX; for (int i=0; inumfaces; i++) { const bsp2_dface_t *f = &bsp->dfaces[i]; - vec3_t fc; - FaceCentroid(f, bsp, fc); + const glm::vec3 fc = Face_Centroid(bsp, f); - vec3_t distvec; - VectorSubtract(fc, point, distvec); - vec_t dist = VectorLength(distvec); + const glm::vec3 distvec = fc - point; + const float dist = glm::length(distvec); if (dist < nearest_dist) { nearest_dist = dist; @@ -598,7 +596,7 @@ FindDebugFace(const bsp2_t *bsp) if (!dump_face) return; - const bsp2_dface_t *f = Face_NearestCentroid(bsp, dump_face_point); + const bsp2_dface_t *f = Face_NearestCentroid(bsp, vec3_t_to_glm(dump_face_point)); if (f == NULL) Error("FindDebugFace: f == NULL\n"); diff --git a/light/ltface.cc b/light/ltface.cc index 5efb92d6..194c0c04 100644 --- a/light/ltface.cc +++ b/light/ltface.cc @@ -23,6 +23,7 @@ #include #include #include +#include #include #include @@ -55,68 +56,6 @@ std::atomic total_bounce_rays, total_bounce_ray_hits; * ============================================================================ */ -/* - * Functions to aid in calculation of polygon centroid - */ -static void -TriCentroid(const dvertex_t *v0, const dvertex_t *v1, const dvertex_t *v2, - vec3_t out) -{ - for (int i = 0; i < 3; i++) - out[i] = (v0->point[i] + v1->point[i] + v2->point[i]) / 3.0; -} - -static vec_t -TriArea(const dvertex_t *v0, const dvertex_t *v1, const dvertex_t *v2) -{ - vec3_t edge0, edge1, cross; - - for (int i =0; i < 3; i++) { - edge0[i] = v1->point[i] - v0->point[i]; - edge1[i] = v2->point[i] - v0->point[i]; - } - CrossProduct(edge0, edge1, cross); - - return VectorLength(cross) * 0.5; -} - -void -FaceCentroid(const bsp2_dface_t *face, const bsp2_t *bsp, vec3_t out) -{ - int edgenum; - dvertex_t *v0, *v1, *v2; - vec3_t centroid, poly_centroid; - vec_t area, poly_area; - - VectorCopy(vec3_origin, poly_centroid); - poly_area = 0; - - edgenum = bsp->dsurfedges[face->firstedge]; - if (edgenum >= 0) - v0 = bsp->dvertexes + bsp->dedges[edgenum].v[0]; - else - v0 = bsp->dvertexes + bsp->dedges[-edgenum].v[1]; - - for (int i = 1; i < face->numedges - 1; i++) { - edgenum = bsp->dsurfedges[face->firstedge + i]; - if (edgenum >= 0) { - v1 = bsp->dvertexes + bsp->dedges[edgenum].v[0]; - v2 = bsp->dvertexes + bsp->dedges[edgenum].v[1]; - } else { - v1 = bsp->dvertexes + bsp->dedges[-edgenum].v[1]; - v2 = bsp->dvertexes + bsp->dedges[-edgenum].v[0]; - } - - area = TriArea(v0, v1, v2); - poly_area += area; - - TriCentroid(v0, v1, v2, centroid); - VectorMA(poly_centroid, area, centroid, poly_centroid); - } - - VectorScale(poly_centroid, 1.0 / poly_area, out); -} - static void TexCoordToWorld(vec_t s, vec_t t, const texorg_t *texorg, vec3_t world) { @@ -221,7 +160,7 @@ CalcFaceExtents(const bsp2_dface_t *face, } vec3_t worldpoint; - FaceCentroid(face, bsp, worldpoint); + glm_to_vec3_t(Face_Centroid(bsp, face), worldpoint); WorldToTexCoord(worldpoint, tex, surf->exactmid); // calculate a bounding sphere for the face diff --git a/light/ltface2.cc b/light/ltface2.cc index fd00c22d..6c59c5b5 100644 --- a/light/ltface2.cc +++ b/light/ltface2.cc @@ -36,6 +36,7 @@ #include using namespace std; +using namespace glm; static glm::vec2 WorldToTexCoord_HighPrecision(const bsp2_t *bsp, const bsp2_dface_t *face, @@ -347,18 +348,6 @@ static glm::vec3 randomColor() { return glm::vec3(Random(), Random(), Random()) * 255.0f; } -static glm::vec3 Face_Centroid(const bsp2_t *bsp, const bsp2_dface_t *face) { - glm::vec3 res(0); - - for (int i=0; inumedges; i++) { - res += Face_PointAtIndex_E(bsp, face, i); - } - - res /= static_cast(face->numedges); - - return res; -} - struct sample_pos_t { const bsp2_dface_t *face; glm::vec3 worldpos;