From 304f8047052d3a772a6d3232f82e334393516daf Mon Sep 17 00:00:00 2001 From: Eric Wasylishen Date: Sun, 30 Apr 2017 10:49:03 -0600 Subject: [PATCH] wip --- common/mesh.cc | 79 ++++++++++++++++++++++++++++++------------ include/common/mesh.hh | 13 +++++-- 2 files changed, 67 insertions(+), 25 deletions(-) diff --git a/common/mesh.cc b/common/mesh.cc index aef348c8..82d86ffa 100644 --- a/common/mesh.cc +++ b/common/mesh.cc @@ -39,18 +39,53 @@ std::vector qvecsToGlm(std::vector qvecs) { return res; } -mesh_t buildMesh(const vector> &faces) -{ +class vertex_uniquer { +private: // FIXME: this is ugly using pos_t = tuple; - int nextVert = 0; + int nextVert; map posToVertIndex; +public: + vertex_uniquer() : nextVert(0), posToVertIndex() {} + + int addExactPoint(const qvec3f &point) { + const pos_t pos = make_tuple(vert[0], vert[1], vert[2]); + const auto it = posToVertIndex.find(pos); + + if (it == posToVertIndex.end()) { + posToVertIndex[pos] = nextVert; + return nextVert++; + } else { + int vertIndex = it->second; + return vertIndex; + } + } + + vector getVerts() const { + // convert posToVertIndex to a vector + vector vertsVec; + vertsVec.resize(posToVertIndex.size()); + for (const auto &posIndex : posToVertIndex) { + const pos_t &pos = posIndex.first; + vertsVec.at(posIndex.second) = qvec3f(std::get<0>(pos), std::get<1>(pos), std::get<2>(pos)); + } + } +}; + +mesh_t buildMeshWithNormals(const std::vector> &faces, + const std::vector> *faceVertNormals=nullptr) +{ + vertex_uniquer uniquer; + vector faceplanes; vector> facesWithIndices; + vector> facesWithNormals; + for (const auto &face : faces) { vector vertIndices; + vector vertNormals; // compute face plane const auto glmvecs = qvecsToGlm(face); @@ -58,41 +93,37 @@ mesh_t buildMesh(const vector> &faces) qplane3f qp(qvec3f(gp[0], gp[1], gp[2]), gp[3]); faceplanes.push_back(qp); - for (const auto &vert : face) { + for (const qvec3f &vert : face) { + const qvec3f &vert = vertNormalPair.first; + const qvec3f &normal = vertNormalPair.second; + float distOff = qp.distAbove(vert); Q_assert(fabs(distOff) < 0.001); - const pos_t pos = make_tuple(vert[0], vert[1], vert[2]); - const auto it = posToVertIndex.find(pos); - - if (it == posToVertIndex.end()) { - posToVertIndex[pos] = nextVert; - vertIndices.push_back(nextVert); - nextVert++; - } else { - int vertIndex = it->second; - vertIndices.push_back(vertIndex); - } + int vertIndex = uniquer.addExactPoint(vert) + vertIndices.push_back(vertIndex); + vertNormals.push_back(normal); } facesWithIndices.push_back(vertIndices); - } - - // convert posToVertIndex to a vector - vector vertsVec; - vertsVec.resize(posToVertIndex.size()); - for (const auto &posIndex : posToVertIndex) { - const pos_t &pos = posIndex.first; - vertsVec.at(posIndex.second) = qvec3f(std::get<0>(pos), std::get<1>(pos), std::get<2>(pos)); + facesWithNormals.push_back(vertNormals); } mesh_t res; res.verts = vertsVec; res.faces = facesWithIndices; res.faceplanes = faceplanes; + res.facevertnormals = facesWithNormals; + return res; } +// wrapper around buildMesh +mesh_t buildMesh(const vector> &faces) +{ + std::vector>> +} + mesh_t buildMeshFromBSP(const bsp2_t *bsp) { mesh_t res; @@ -255,6 +286,7 @@ void cleanupMesh(mesh_t &mesh) // sample point positioning +#if 0 class position_t { public: bool m_unoccluded; @@ -426,3 +458,4 @@ sample_position_t positionSample(const mesh_t &mesh, facenum_t startingFace, con // call the recursive version return positionSample_r(mesh, startingFace, startingPos, 0); } +#endif diff --git a/include/common/mesh.hh b/include/common/mesh.hh index 10e13055..0ebfd0fc 100644 --- a/include/common/mesh.hh +++ b/include/common/mesh.hh @@ -34,14 +34,17 @@ using meshface_t = std::vector; class mesh_t { public: std::vector verts; - std::vector faces; + std::vector faceverts; + std::vector> facevertnormals; + // this is redundant data with the verts, but we know the planes in advance // and this saves having to estimate them from the verts std::vector faceplanes; }; // Welds vertices at exactly the same position -mesh_t buildMesh(const std::vector> &faces); +mesh_t buildMeshWithNormals(const std::vector> &faces, + const std::vector> *faceVertNormals=nullptr); mesh_t buildMeshFromBSP(const bsp2_t *bsp); std::vector> meshToFaces(const mesh_t &mesh); @@ -74,6 +77,12 @@ public: m_interpolatedNormal(interpolatedNormal) {}; }; +#if 0 +/** + * "Corrects" a point so it's on the front side of the + * mesh + */ sample_position_t positionSample(const mesh_t &mesh, facenum_t startingFace, const qvec3f &startingPos); +#endif #endif /* __COMMON_MESH_HH__ */