From fe6be2100803b10f7954a28df25261ef1eca6411 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Wed, 9 Aug 2023 22:08:18 -0700 Subject: [PATCH 1/2] fix raw reading --- dNavigation/dTerrain/RawFile.cpp | 24 +++++++++++++----------- dNavigation/dTerrain/RawFile.h | 2 +- 2 files changed, 14 insertions(+), 12 deletions(-) diff --git a/dNavigation/dTerrain/RawFile.cpp b/dNavigation/dTerrain/RawFile.cpp index d4496b2f..dfad9ca4 100644 --- a/dNavigation/dTerrain/RawFile.cpp +++ b/dNavigation/dTerrain/RawFile.cpp @@ -32,13 +32,15 @@ RawFile::RawFile(std::string fileName) { m_Chunks.push_back(chunk); } + m_FinalMesh = new RawMesh(); + this->GenerateFinalMeshFromChunks(); } RawFile::~RawFile() { - delete m_FinalMesh; + if (m_FinalMesh) delete m_FinalMesh; for (const auto* item : m_Chunks) { - delete item; + if (item) delete item; } } @@ -49,10 +51,14 @@ void RawFile::GenerateFinalMeshFromChunks() { for (const auto& vert : chunk->m_Mesh->m_Vertices) { auto tempVert = vert; - tempVert.SetX(tempVert.GetX() + (chunk->m_X / 4)); - tempVert.SetZ(tempVert.GetZ() + (chunk->m_Z / 4)); + // Scale X and Z by the chunk's position in the world + // Scale Y by the chunk's heightmap scale factor + tempVert.SetX(tempVert.GetX() + (chunk->m_X / chunk->m_HeightMap->m_ScaleFactor)); + tempVert.SetY(tempVert.GetY() / chunk->m_HeightMap->m_ScaleFactor); + tempVert.SetZ(tempVert.GetZ() + (chunk->m_Z / chunk->m_HeightMap->m_ScaleFactor)); - tempVert* chunk->m_HeightMap->m_ScaleFactor; + // Then scale it again for some reason + tempVert *= chunk->m_HeightMap->m_ScaleFactor; m_FinalMesh->m_Vertices.push_back(tempVert); } @@ -67,16 +73,12 @@ void RawFile::GenerateFinalMeshFromChunks() { void RawFile::WriteFinalMeshToOBJ(std::string path) { std::ofstream file(path); - std::string vertData; for (const auto& v : m_FinalMesh->m_Vertices) { - vertData += "v " + std::to_string(v.x) + " " + std::to_string(v.y) + " " + std::to_string(v.z) + "\n"; + file << "v " << v.x << ' ' << v.y << ' ' << v.z << '\n'; } for (int i = 0; i < m_FinalMesh->m_Triangles.size(); i += 3) { - vertData += "f " + std::to_string(*std::next(m_FinalMesh->m_Triangles.begin(), i) + 1) + " " + std::to_string(*std::next(m_FinalMesh->m_Triangles.begin(), i + 1) + 1) + " " + std::to_string(*std::next(m_FinalMesh->m_Triangles.begin(), i + 2) + 1) + "\n"; + file << "f " << *std::next(m_FinalMesh->m_Triangles.begin(), i) + 1 << ' ' << *std::next(m_FinalMesh->m_Triangles.begin(), i + 1) + 1 << ' ' << *std::next(m_FinalMesh->m_Triangles.begin(), i + 2) + 1 << '\n'; } - - file.write(vertData.c_str(), vertData.size()); - file.close(); } diff --git a/dNavigation/dTerrain/RawFile.h b/dNavigation/dTerrain/RawFile.h index 2a702c53..b1fb4024 100644 --- a/dNavigation/dTerrain/RawFile.h +++ b/dNavigation/dTerrain/RawFile.h @@ -24,5 +24,5 @@ private: uint32_t m_Height; std::vector m_Chunks; - RawMesh* m_FinalMesh; + RawMesh* m_FinalMesh = nullptr; }; From ad81e341da474cfa9209bd59c93d4d9074259878 Mon Sep 17 00:00:00 2001 From: David Markowitz Date: Wed, 9 Aug 2023 22:13:20 -0700 Subject: [PATCH 2/2] add operator *= for vectors --- dCommon/NiPoint3.cpp | 7 +++++++ dCommon/NiPoint3.h | 2 ++ 2 files changed, 9 insertions(+) diff --git a/dCommon/NiPoint3.cpp b/dCommon/NiPoint3.cpp index 20780815..5ad06f30 100644 --- a/dCommon/NiPoint3.cpp +++ b/dCommon/NiPoint3.cpp @@ -136,6 +136,13 @@ NiPoint3& NiPoint3::operator+=(const NiPoint3& point) { return *this; } +NiPoint3& NiPoint3::operator*=(const float scalar) { + this->x *= scalar; + this->y *= scalar; + this->z *= scalar; + return *this; +} + //! Operator for subtraction of vectors NiPoint3 NiPoint3::operator-(const NiPoint3& point) const { return NiPoint3(this->x - point.x, this->y - point.y, this->z - point.z); diff --git a/dCommon/NiPoint3.h b/dCommon/NiPoint3.h index c956b654..44c3c383 100644 --- a/dCommon/NiPoint3.h +++ b/dCommon/NiPoint3.h @@ -138,6 +138,8 @@ public: //! Operator for addition of vectors NiPoint3& operator+=(const NiPoint3& point); + NiPoint3& operator*=(const float scalar); + //! Operator for subtraction of vectors NiPoint3 operator-(const NiPoint3& point) const;