This commit is contained in:
Eric Wasylishen 2017-04-30 10:49:03 -06:00
parent 3068be1a74
commit 304f804705
2 changed files with 67 additions and 25 deletions

View File

@ -39,18 +39,53 @@ std::vector<qvec3f> qvecsToGlm(std::vector<qvec3f> qvecs) {
return res; return res;
} }
mesh_t buildMesh(const vector<vector<qvec3f>> &faces) class vertex_uniquer {
{ private:
// FIXME: this is ugly // FIXME: this is ugly
using pos_t = tuple<float, float, float>; using pos_t = tuple<float, float, float>;
int nextVert = 0; int nextVert;
map<pos_t, int> posToVertIndex; map<pos_t, int> 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<qvec3f> getVerts() const {
// convert posToVertIndex to a vector
vector<qvec3f> 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<std::vector<qvec3f>> &faces,
const std::vector<std::vector<qvec3f>> *faceVertNormals=nullptr)
{
vertex_uniquer uniquer;
vector<qplane3f> faceplanes; vector<qplane3f> faceplanes;
vector<vector<int>> facesWithIndices; vector<vector<int>> facesWithIndices;
vector<vector<qvec3f>> facesWithNormals;
for (const auto &face : faces) { for (const auto &face : faces) {
vector<int> vertIndices; vector<int> vertIndices;
vector<qvec3f> vertNormals;
// compute face plane // compute face plane
const auto glmvecs = qvecsToGlm(face); const auto glmvecs = qvecsToGlm(face);
@ -58,41 +93,37 @@ mesh_t buildMesh(const vector<vector<qvec3f>> &faces)
qplane3f qp(qvec3f(gp[0], gp[1], gp[2]), gp[3]); qplane3f qp(qvec3f(gp[0], gp[1], gp[2]), gp[3]);
faceplanes.push_back(qp); 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); float distOff = qp.distAbove(vert);
Q_assert(fabs(distOff) < 0.001); Q_assert(fabs(distOff) < 0.001);
const pos_t pos = make_tuple(vert[0], vert[1], vert[2]); int vertIndex = uniquer.addExactPoint(vert)
const auto it = posToVertIndex.find(pos); vertIndices.push_back(vertIndex);
vertNormals.push_back(normal);
if (it == posToVertIndex.end()) {
posToVertIndex[pos] = nextVert;
vertIndices.push_back(nextVert);
nextVert++;
} else {
int vertIndex = it->second;
vertIndices.push_back(vertIndex);
}
} }
facesWithIndices.push_back(vertIndices); facesWithIndices.push_back(vertIndices);
} facesWithNormals.push_back(vertNormals);
// convert posToVertIndex to a vector
vector<qvec3f> 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 res; mesh_t res;
res.verts = vertsVec; res.verts = vertsVec;
res.faces = facesWithIndices; res.faces = facesWithIndices;
res.faceplanes = faceplanes; res.faceplanes = faceplanes;
res.facevertnormals = facesWithNormals;
return res; return res;
} }
// wrapper around buildMesh
mesh_t buildMesh(const vector<vector<qvec3f>> &faces)
{
std::vector<std::vector<std::pair<qvec3f, qvec3f>>>
}
mesh_t buildMeshFromBSP(const bsp2_t *bsp) mesh_t buildMeshFromBSP(const bsp2_t *bsp)
{ {
mesh_t res; mesh_t res;
@ -255,6 +286,7 @@ void cleanupMesh(mesh_t &mesh)
// sample point positioning // sample point positioning
#if 0
class position_t { class position_t {
public: public:
bool m_unoccluded; bool m_unoccluded;
@ -426,3 +458,4 @@ sample_position_t positionSample(const mesh_t &mesh, facenum_t startingFace, con
// call the recursive version // call the recursive version
return positionSample_r(mesh, startingFace, startingPos, 0); return positionSample_r(mesh, startingFace, startingPos, 0);
} }
#endif

View File

@ -34,14 +34,17 @@ using meshface_t = std::vector<vertnum_t>;
class mesh_t { class mesh_t {
public: public:
std::vector<qvec3f> verts; std::vector<qvec3f> verts;
std::vector<meshface_t> faces; std::vector<meshface_t> faceverts;
std::vector<std::vector<qvec3f>> facevertnormals;
// this is redundant data with the verts, but we know the planes in advance // this is redundant data with the verts, but we know the planes in advance
// and this saves having to estimate them from the verts // and this saves having to estimate them from the verts
std::vector<qplane3f> faceplanes; std::vector<qplane3f> faceplanes;
}; };
// Welds vertices at exactly the same position // Welds vertices at exactly the same position
mesh_t buildMesh(const std::vector<std::vector<qvec3f>> &faces); mesh_t buildMeshWithNormals(const std::vector<std::vector<qvec3f>> &faces,
const std::vector<std::vector<qvec3f>> *faceVertNormals=nullptr);
mesh_t buildMeshFromBSP(const bsp2_t *bsp); mesh_t buildMeshFromBSP(const bsp2_t *bsp);
std::vector<std::vector<qvec3f>> meshToFaces(const mesh_t &mesh); std::vector<std::vector<qvec3f>> meshToFaces(const mesh_t &mesh);
@ -74,6 +77,12 @@ public:
m_interpolatedNormal(interpolatedNormal) {}; 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); sample_position_t positionSample(const mesh_t &mesh, facenum_t startingFace, const qvec3f &startingPos);
#endif
#endif /* __COMMON_MESH_HH__ */ #endif /* __COMMON_MESH_HH__ */