light: switch to new Face_Centroid
This commit is contained in:
parent
f09cadbc76
commit
bd0556cf22
|
|
@ -296,3 +296,8 @@ std::vector<glm::vec3> 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));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -350,6 +350,12 @@ GLM_EdgePlanes_PointInside(const vector<vec4> &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<vec3> 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;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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::vec3> 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__ */
|
||||
|
|
|
|||
|
|
@ -321,7 +321,9 @@ glm::vec3 GLM_FaceNormal(std::vector<glm::vec3> points);
|
|||
std::vector<glm::vec4> GLM_MakeInwardFacingEdgePlanes(std::vector<glm::vec3> points);
|
||||
bool GLM_EdgePlanes_PointInside(const std::vector<glm::vec4> &edgeplanes, const glm::vec3 &point);
|
||||
float GLM_EdgePlanes_PointInsideDist(const std::vector<glm::vec4> &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<glm::vec3> points);
|
||||
|
||||
#endif /* __COMMON_MATHLIB_H__ */
|
||||
|
|
|
|||
|
|
@ -42,7 +42,6 @@
|
|||
extern std::atomic<uint32_t> total_light_rays, total_light_ray_hits, total_samplepoints;
|
||||
extern std::atomic<uint32_t> 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.
|
||||
|
|
|
|||
|
|
@ -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; i<bsp->numfaces; 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");
|
||||
|
||||
|
|
|
|||
|
|
@ -23,6 +23,7 @@
|
|||
#include <light/entities.hh>
|
||||
#include <light/trace.hh>
|
||||
#include <light/ltface.hh>
|
||||
#include <light/ltface2.hh>
|
||||
#include <light/light2.hh>
|
||||
|
||||
#include <common/bsputils.hh>
|
||||
|
|
@ -55,68 +56,6 @@ std::atomic<uint32_t> 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
|
||||
|
|
|
|||
|
|
@ -36,6 +36,7 @@
|
|||
#include <glm/gtx/string_cast.hpp>
|
||||
|
||||
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; i<face->numedges; i++) {
|
||||
res += Face_PointAtIndex_E(bsp, face, i);
|
||||
}
|
||||
|
||||
res /= static_cast<float>(face->numedges);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
struct sample_pos_t {
|
||||
const bsp2_dface_t *face;
|
||||
glm::vec3 worldpos;
|
||||
|
|
|
|||
Loading…
Reference in New Issue