Merge pull request #1182 from DarkflameUniverse/fixRawReading

fix: RAW to OBJ parsing
This commit is contained in:
Gie "Max" Vanommeslaeghe 2023-08-10 23:50:11 +02:00 committed by GitHub
commit d233c7e2aa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 12 deletions

View File

@ -136,6 +136,13 @@ NiPoint3& NiPoint3::operator+=(const NiPoint3& point) {
return *this; return *this;
} }
NiPoint3& NiPoint3::operator*=(const float scalar) {
this->x *= scalar;
this->y *= scalar;
this->z *= scalar;
return *this;
}
//! Operator for subtraction of vectors //! Operator for subtraction of vectors
NiPoint3 NiPoint3::operator-(const NiPoint3& point) const { NiPoint3 NiPoint3::operator-(const NiPoint3& point) const {
return NiPoint3(this->x - point.x, this->y - point.y, this->z - point.z); return NiPoint3(this->x - point.x, this->y - point.y, this->z - point.z);

View File

@ -138,6 +138,8 @@ public:
//! Operator for addition of vectors //! Operator for addition of vectors
NiPoint3& operator+=(const NiPoint3& point); NiPoint3& operator+=(const NiPoint3& point);
NiPoint3& operator*=(const float scalar);
//! Operator for subtraction of vectors //! Operator for subtraction of vectors
NiPoint3 operator-(const NiPoint3& point) const; NiPoint3 operator-(const NiPoint3& point) const;

View File

@ -32,13 +32,15 @@ RawFile::RawFile(std::string fileName) {
m_Chunks.push_back(chunk); m_Chunks.push_back(chunk);
} }
m_FinalMesh = new RawMesh();
this->GenerateFinalMeshFromChunks(); this->GenerateFinalMeshFromChunks();
} }
RawFile::~RawFile() { RawFile::~RawFile() {
delete m_FinalMesh; if (m_FinalMesh) delete m_FinalMesh;
for (const auto* item : m_Chunks) { 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) { for (const auto& vert : chunk->m_Mesh->m_Vertices) {
auto tempVert = vert; auto tempVert = vert;
tempVert.SetX(tempVert.GetX() + (chunk->m_X / 4)); // Scale X and Z by the chunk's position in the world
tempVert.SetZ(tempVert.GetZ() + (chunk->m_Z / 4)); // 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); m_FinalMesh->m_Vertices.push_back(tempVert);
} }
@ -67,16 +73,12 @@ void RawFile::GenerateFinalMeshFromChunks() {
void RawFile::WriteFinalMeshToOBJ(std::string path) { void RawFile::WriteFinalMeshToOBJ(std::string path) {
std::ofstream file(path); std::ofstream file(path);
std::string vertData;
for (const auto& v : m_FinalMesh->m_Vertices) { 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) { 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();
} }

View File

@ -24,5 +24,5 @@ private:
uint32_t m_Height; uint32_t m_Height;
std::vector<RawChunk*> m_Chunks; std::vector<RawChunk*> m_Chunks;
RawMesh* m_FinalMesh; RawMesh* m_FinalMesh = nullptr;
}; };