diff --git a/src/tools/trackgen/ac3d.cpp b/src/tools/trackgen/ac3d.cpp index 0663e989c..32e431f39 100644 --- a/src/tools/trackgen/ac3d.cpp +++ b/src/tools/trackgen/ac3d.cpp @@ -54,6 +54,13 @@ double Ac3d::V3d::length() const return sqrt((*this)[0] * (*this)[0] + (*this)[1] * (*this)[1] + (*this)[2] * (*this)[2]); } +void Ac3d::V3d::normalize() +{ + const double l = length(); + if (l != 0.0) + *this = *this / length(); +} + double Ac3d::V3d::dot(const V3d &other) const { return (*this)[0] * other[0] + (*this)[1] * other[1] + (*this)[2] * other[2]; @@ -692,7 +699,21 @@ void Ac3d::Object::parse(std::ifstream &fin, const std::string &objType) { const int numsurf = std::stoi(tokens.at(1)); for (int i = 0; i < numsurf; i++) + { surfaces.emplace_back(fin); + + Surface &surface = surfaces.back(); + + if (surface.isPolygon() && surface.refs.size() >= 3) + { + const V3d &p0 = vertices[surface.refs[0].index]; + const V3d &p1 = vertices[surface.refs[1].index]; + const V3d &p2 = vertices[surface.refs[2].index]; + + surface.normal = ((p1 - p0).cross(p2 - p1)); + surface.normal.normalize(); + } + } } else if (tokens.at(0) == "kids") { @@ -918,12 +939,12 @@ void Ac3d::Object::splitBySURF() for (std::list::iterator it = kids.begin(); it != kids.end(); ++it) { - const Object &kid = *it; + Object &kid = *it; - std::set surfTypes; if (kid.type == "poly") { // get the different SURFs + std::set surfTypes; for (auto surface : kid.surfaces) surfTypes.insert(surface.surf); @@ -952,6 +973,8 @@ void Ac3d::Object::splitBySURF() it = last; } } + else + kid.splitBySURF(); } } @@ -962,12 +985,12 @@ void Ac3d::Object::splitByMaterial() for (std::list::iterator it = kids.begin(); it != kids.end(); ++it) { - const Object &kid = *it; + Object &kid = *it; - std::set materialTypes; if (kid.type == "poly") { // get the different SURFs + std::set materialTypes; for (auto surface : kid.surfaces) materialTypes.insert(surface.surf); @@ -996,6 +1019,41 @@ void Ac3d::Object::splitByMaterial() it = last; } } + else + kid.splitByMaterial(); + } +} + +void Ac3d::Object::splitByUV() +{ + if (type == "poly") + return; + + for (std::list::iterator it = kids.begin(); it != kids.end(); ++it) + { + Object &kid = *it; + if (kid.type == "poly") + { + bool needSplit = false; + std::vector> uvs(vertices.size()); + for (const auto &surface : surfaces) + { + for (const auto &ref : surface.refs) + { + uvs[ref.index].insert(ref.coords[0]); + + if (uvs[ref.index].size() > 1) + needSplit = true; + } + } + + if (needSplit) + { + // TODO + } + } + else + kid.splitByUV(); } } @@ -1397,3 +1455,8 @@ void Ac3d::splitByMaterial() { root.splitByMaterial(); } + +void Ac3d::splitByUV() +{ + root.splitByUV(); +} diff --git a/src/tools/trackgen/ac3d.h b/src/tools/trackgen/ac3d.h index feaafe709..82502e0cc 100644 --- a/src/tools/trackgen/ac3d.h +++ b/src/tools/trackgen/ac3d.h @@ -72,6 +72,7 @@ struct Ac3d double dot(const V3d &other) const; V3d cross(const V3d &other) const; double length() const; + void normalize(); }; struct Color : public std::array @@ -165,6 +166,7 @@ struct Ac3d SURF surf = PolygonSingleSidedFlat; int mat = 0; std::vector refs; + V3d normal = { 0, 0, 0 }; Surface() = default; explicit Surface(std::ifstream &fin); @@ -285,6 +287,7 @@ struct Ac3d void flipAxes(bool in); void splitBySURF(); void splitByMaterial(); + void splitByUV(); void removeSurfacesNotSURF(int SURF); void removeSurfacesNotMaterial(int material); void removeUnusedVertices(); @@ -312,6 +315,7 @@ struct Ac3d void generateTriangles(); void splitBySURF(); void splitByMaterial(); + void splitByUV(); void merge(const Ac3d &ac3d, bool mergeMaterials); double getTerrainHeight(double x, double y) const; double getTerrainAngle(double x, double y) const; diff --git a/src/tools/trackgen/main.cpp b/src/tools/trackgen/main.cpp index 8381ba401..54b6b020e 100644 --- a/src/tools/trackgen/main.cpp +++ b/src/tools/trackgen/main.cpp @@ -98,7 +98,7 @@ public: //! Constructor. Application::Application() -: GfApplication("TrackGen", "1.6.0.32", "Terrain generator for tracks") +: GfApplication("TrackGen", "1.6.0.33", "Terrain generator for tracks") , HeightSteps(30) , Bump(false) , Raceline(false) diff --git a/src/tools/trackgen/objects.cpp b/src/tools/trackgen/objects.cpp index 93b67a3cc..21b675f4b 100644 --- a/src/tools/trackgen/objects.cpp +++ b/src/tools/trackgen/objects.cpp @@ -297,6 +297,7 @@ AddObject(tTrack *track, void *trackHandle, const Ac3d &terrainRoot, const Ac3d obj.transform(m); obj.splitBySURF(); obj.splitByMaterial(); + obj.splitByUV(); objectsRoot.merge(obj, multipleMaterials);