From 72f360710a846c45d253ca05090edea8d27095d1 Mon Sep 17 00:00:00 2001 From: Krishty Date: Wed, 18 Jan 2023 00:08:38 +0100 Subject: [PATCH 1/3] =?UTF-8?q?Fix=20MSVC=20Warnings=20With=20=E2=80=9Cemp?= =?UTF-8?q?lace=5Fback()=E2=80=9D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Several places in the code call `std::vector.emplace_back(0, 0, 0)`. The constructor of `aiVector3D` actually expects arguments of the type `ai_real`, (alias of `float` if compiling without `ASSIMP_DOUBLE_PRECISION`) but the literal `0` is of type `int`. `emplace_back()` does support promotion, but `int` to `float` is a potentially lossy conversion. tl;dr: On warning level 4, MSVC spits out a very deeply nested `warning C4244: 'argument': conversion from '_Ty' to 'TReal', possible loss of data with _Ty=int and TReal=ai_real`. --- code/AssetLib/MMD/MMDImporter.cpp | 2 +- code/AssetLib/X3D/X3DGeoHelper.cpp | 8 ++++---- code/AssetLib/X3D/X3DImporter_Geometry2D.cpp | 16 ++++++++-------- 3 files changed, 13 insertions(+), 13 deletions(-) diff --git a/code/AssetLib/MMD/MMDImporter.cpp b/code/AssetLib/MMD/MMDImporter.cpp index 97b04f4eb..0905ce1e0 100644 --- a/code/AssetLib/MMD/MMDImporter.cpp +++ b/code/AssetLib/MMD/MMDImporter.cpp @@ -269,7 +269,7 @@ aiMesh *MMDImporter::CreateMesh(const pmx::PmxModel *pModel, dynamic_cast(v->skinning.get()); switch (v->skinning_type) { case pmx::PmxVertexSkinningType::BDEF1: - bone_vertex_map[vsBDEF1_ptr->bone_index].emplace_back(index, 1.0); + bone_vertex_map[vsBDEF1_ptr->bone_index].emplace_back(index, static_cast(1)); break; case pmx::PmxVertexSkinningType::BDEF2: bone_vertex_map[vsBDEF2_ptr->bone_index1].emplace_back(index, vsBDEF2_ptr->bone_weight); diff --git a/code/AssetLib/X3D/X3DGeoHelper.cpp b/code/AssetLib/X3D/X3DGeoHelper.cpp index e89aeb428..0a62ff9b0 100644 --- a/code/AssetLib/X3D/X3DGeoHelper.cpp +++ b/code/AssetLib/X3D/X3DGeoHelper.cpp @@ -193,7 +193,7 @@ void X3DGeoHelper::add_color(aiMesh &pMesh, const std::list &pColors, // create RGBA array from RGB. for (std::list::const_iterator it = pColors.begin(); it != pColors.end(); ++it) - tcol.emplace_back((*it).r, (*it).g, (*it).b, 1); + tcol.emplace_back((*it).r, (*it).g, (*it).b, static_cast(1)); // call existing function for adding RGBA colors add_color(pMesh, tcol, pColorPerVertex); @@ -238,7 +238,7 @@ void X3DGeoHelper::add_color(aiMesh &pMesh, const std::vector &pCoordId // create RGBA array from RGB. for (std::list::const_iterator it = pColors.begin(); it != pColors.end(); ++it) { - tcol.emplace_back((*it).r, (*it).g, (*it).b, 1); + tcol.emplace_back((*it).r, (*it).g, (*it).b, static_cast(1)); } // call existing function for adding RGBA colors @@ -440,7 +440,7 @@ void X3DGeoHelper::add_tex_coord(aiMesh &pMesh, const std::vector &pCoo // copy list to array because we are need indexed access to normals. texcoord_arr_copy.reserve(pTexCoords.size()); for (std::list::const_iterator it = pTexCoords.begin(); it != pTexCoords.end(); ++it) { - texcoord_arr_copy.emplace_back((*it).x, (*it).y, 0); + texcoord_arr_copy.emplace_back((*it).x, (*it).y, static_cast(0)); } if (pTexCoordIdx.size() > 0) { @@ -480,7 +480,7 @@ void X3DGeoHelper::add_tex_coord(aiMesh &pMesh, const std::list &pTe // copy list to array because we are need convert aiVector2D to aiVector3D and also get indexed access as a bonus. tc_arr_copy.reserve(pTexCoords.size()); for (std::list::const_iterator it = pTexCoords.begin(); it != pTexCoords.end(); ++it) { - tc_arr_copy.emplace_back((*it).x, (*it).y, 0); + tc_arr_copy.emplace_back((*it).x, (*it).y, static_cast(0)); } // copy texture coordinates to mesh diff --git a/code/AssetLib/X3D/X3DImporter_Geometry2D.cpp b/code/AssetLib/X3D/X3DImporter_Geometry2D.cpp index 653203b4e..8e33decee 100644 --- a/code/AssetLib/X3D/X3DImporter_Geometry2D.cpp +++ b/code/AssetLib/X3D/X3DImporter_Geometry2D.cpp @@ -151,7 +151,7 @@ void X3DImporter::readArcClose2D(XmlNode &node) { std::list &vlist = ((X3DNodeElementGeometry2D *)ne)->Vertices; // just short alias. if ((closureType == "PIE") || (closureType == "\"PIE\"")) - vlist.emplace_back(0, 0, 0); // center point - first radial line + vlist.emplace_back(static_cast(0), static_cast(0), static_cast(0)); // center point - first radial line else if ((closureType != "CHORD") && (closureType != "\"CHORD\"")) Throw_IncorrectAttrValue("ArcClose2D", "closureType"); @@ -323,7 +323,7 @@ void X3DImporter::readPolyline2D(XmlNode &node) { // convert vec2 to vec3 for (std::list::iterator it2 = lineSegments.begin(); it2 != lineSegments.end(); ++it2) - tlist.emplace_back(it2->x, it2->y, 0); + tlist.emplace_back(it2->x, it2->y, static_cast(0)); // convert point set to line set X3DGeoHelper::extend_point_to_line(tlist, ((X3DNodeElementGeometry2D *)ne)->Vertices); @@ -361,7 +361,7 @@ void X3DImporter::readPolypoint2D(XmlNode &node) { // convert vec2 to vec3 for (std::list::iterator it2 = point.begin(); it2 != point.end(); ++it2) { - ((X3DNodeElementGeometry2D *)ne)->Vertices.emplace_back(it2->x, it2->y, 0); + ((X3DNodeElementGeometry2D *)ne)->Vertices.emplace_back(it2->x, it2->y, static_cast(0)); } ((X3DNodeElementGeometry2D *)ne)->NumIndices = 1; @@ -405,10 +405,10 @@ void X3DImporter::readRectangle2D(XmlNode &node) { float y2 = size.y / 2.0f; std::list &vlist = ((X3DNodeElementGeometry2D *)ne)->Vertices; // just short alias. - vlist.emplace_back(x2, y1, 0); // 1st point - vlist.emplace_back(x2, y2, 0); // 2nd point - vlist.emplace_back(x1, y2, 0); // 3rd point - vlist.emplace_back(x1, y1, 0); // 4th point + vlist.emplace_back(x2, y1, static_cast(0)); // 1st point + vlist.emplace_back(x2, y2, static_cast(0)); // 2nd point + vlist.emplace_back(x1, y2, static_cast(0)); // 3rd point + vlist.emplace_back(x1, y1, static_cast(0)); // 4th point ((X3DNodeElementGeometry2D *)ne)->Solid = solid; ((X3DNodeElementGeometry2D *)ne)->NumIndices = 4; // check for X3DMetadataObject childs. @@ -449,7 +449,7 @@ void X3DImporter::readTriangleSet2D(XmlNode &node) { // convert vec2 to vec3 for (std::list::iterator it2 = vertices.begin(); it2 != vertices.end(); ++it2) { - ((X3DNodeElementGeometry2D *)ne)->Vertices.emplace_back(it2->x, it2->y, 0); + ((X3DNodeElementGeometry2D *)ne)->Vertices.emplace_back(it2->x, it2->y, static_cast(0)); } ((X3DNodeElementGeometry2D *)ne)->Solid = solid; From eb5d3c51e8987a6c687657503a30481e45a90de1 Mon Sep 17 00:00:00 2001 From: lsnoel <52174215+lsnoel@users.noreply.github.com> Date: Thu, 19 Jan 2023 16:46:29 +0000 Subject: [PATCH 2/3] Correctly consider aiProcess_FlipWindingOrder AND aiProcess_MakeLeftHanded when generating normals --- code/PostProcessing/GenFaceNormalsProcess.cpp | 5 +++-- code/PostProcessing/GenFaceNormalsProcess.h | 1 + code/PostProcessing/GenVertexNormalsProcess.cpp | 5 +++-- code/PostProcessing/GenVertexNormalsProcess.h | 1 + 4 files changed, 8 insertions(+), 4 deletions(-) diff --git a/code/PostProcessing/GenFaceNormalsProcess.cpp b/code/PostProcessing/GenFaceNormalsProcess.cpp index f104b98b6..0edbd9c6f 100644 --- a/code/PostProcessing/GenFaceNormalsProcess.cpp +++ b/code/PostProcessing/GenFaceNormalsProcess.cpp @@ -67,6 +67,7 @@ GenFaceNormalsProcess::~GenFaceNormalsProcess() = default; bool GenFaceNormalsProcess::IsActive(unsigned int pFlags) const { force_ = (pFlags & aiProcess_ForceGenNormals) != 0; flippedWindingOrder_ = (pFlags & aiProcess_FlipWindingOrder) != 0; + leftHanded_ = (pFlags & aiProcess_MakeLeftHanded) != 0; return (pFlags & aiProcess_GenNormals) != 0; } @@ -131,8 +132,8 @@ bool GenFaceNormalsProcess::GenMeshFaceNormals(aiMesh *pMesh) { const aiVector3D *pV1 = &pMesh->mVertices[face.mIndices[0]]; const aiVector3D *pV2 = &pMesh->mVertices[face.mIndices[1]]; const aiVector3D *pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices - 1]]; - if (flippedWindingOrder_) - std::swap( pV2, pV3 ); + if (flippedWindingOrder_ != leftHanded_) // Boolean XOR + std::swap(pV2, pV3); const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe(); for (unsigned int i = 0; i < face.mNumIndices; ++i) { diff --git a/code/PostProcessing/GenFaceNormalsProcess.h b/code/PostProcessing/GenFaceNormalsProcess.h index 586c4902e..c2f157e20 100644 --- a/code/PostProcessing/GenFaceNormalsProcess.h +++ b/code/PostProcessing/GenFaceNormalsProcess.h @@ -81,6 +81,7 @@ private: bool GenMeshFaceNormals(aiMesh* pcMesh); mutable bool force_ = false; mutable bool flippedWindingOrder_ = false; + mutable bool leftHanded_ = false; }; } // end of namespace Assimp diff --git a/code/PostProcessing/GenVertexNormalsProcess.cpp b/code/PostProcessing/GenVertexNormalsProcess.cpp index 0cb2bddb1..e9541c7d9 100644 --- a/code/PostProcessing/GenVertexNormalsProcess.cpp +++ b/code/PostProcessing/GenVertexNormalsProcess.cpp @@ -69,6 +69,7 @@ GenVertexNormalsProcess::~GenVertexNormalsProcess() = default; bool GenVertexNormalsProcess::IsActive(unsigned int pFlags) const { force_ = (pFlags & aiProcess_ForceGenNormals) != 0; flippedWindingOrder_ = (pFlags & aiProcess_FlipWindingOrder) != 0; + leftHanded_ = (pFlags & aiProcess_MakeLeftHanded) != 0; return (pFlags & aiProcess_GenSmoothNormals) != 0; } @@ -141,8 +142,8 @@ bool GenVertexNormalsProcess::GenMeshVertexNormals(aiMesh *pMesh, unsigned int m const aiVector3D *pV1 = &pMesh->mVertices[face.mIndices[0]]; const aiVector3D *pV2 = &pMesh->mVertices[face.mIndices[1]]; const aiVector3D *pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices - 1]]; - if (flippedWindingOrder_) - std::swap( pV2, pV3 ); + if (flippedWindingOrder_ != leftHanded_) // Boolean XOR + std::swap(pV2, pV3); const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe(); for (unsigned int i = 0; i < face.mNumIndices; ++i) { diff --git a/code/PostProcessing/GenVertexNormalsProcess.h b/code/PostProcessing/GenVertexNormalsProcess.h index 0dcae793a..370bf42b1 100644 --- a/code/PostProcessing/GenVertexNormalsProcess.h +++ b/code/PostProcessing/GenVertexNormalsProcess.h @@ -105,6 +105,7 @@ private: ai_real configMaxAngle; mutable bool force_ = false; mutable bool flippedWindingOrder_ = false; + mutable bool leftHanded_ = false; }; } // end of namespace Assimp From 8d1256f472702cc22292abcb8bbd6f15b1edda64 Mon Sep 17 00:00:00 2001 From: lsnoel <52174215+lsnoel@users.noreply.github.com> Date: Fri, 20 Jan 2023 14:55:06 +0000 Subject: [PATCH 3/3] Comments about winding order fix for gen normals --- code/PostProcessing/GenFaceNormalsProcess.cpp | 4 +++- code/PostProcessing/GenVertexNormalsProcess.cpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/code/PostProcessing/GenFaceNormalsProcess.cpp b/code/PostProcessing/GenFaceNormalsProcess.cpp index 0edbd9c6f..d3520d4b2 100644 --- a/code/PostProcessing/GenFaceNormalsProcess.cpp +++ b/code/PostProcessing/GenFaceNormalsProcess.cpp @@ -132,7 +132,9 @@ bool GenFaceNormalsProcess::GenMeshFaceNormals(aiMesh *pMesh) { const aiVector3D *pV1 = &pMesh->mVertices[face.mIndices[0]]; const aiVector3D *pV2 = &pMesh->mVertices[face.mIndices[1]]; const aiVector3D *pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices - 1]]; - if (flippedWindingOrder_ != leftHanded_) // Boolean XOR + // Boolean XOR - if either but not both of these flags is set, then the winding order has + // changed and the cross product to calculate the normal needs to be reversed + if (flippedWindingOrder_ != leftHanded_) std::swap(pV2, pV3); const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe(); diff --git a/code/PostProcessing/GenVertexNormalsProcess.cpp b/code/PostProcessing/GenVertexNormalsProcess.cpp index e9541c7d9..5b9033383 100644 --- a/code/PostProcessing/GenVertexNormalsProcess.cpp +++ b/code/PostProcessing/GenVertexNormalsProcess.cpp @@ -142,7 +142,9 @@ bool GenVertexNormalsProcess::GenMeshVertexNormals(aiMesh *pMesh, unsigned int m const aiVector3D *pV1 = &pMesh->mVertices[face.mIndices[0]]; const aiVector3D *pV2 = &pMesh->mVertices[face.mIndices[1]]; const aiVector3D *pV3 = &pMesh->mVertices[face.mIndices[face.mNumIndices - 1]]; - if (flippedWindingOrder_ != leftHanded_) // Boolean XOR + // Boolean XOR - if either but not both of these flags is set, then the winding order has + // changed and the cross product to calculate the normal needs to be reversed + if (flippedWindingOrder_ != leftHanded_) std::swap(pV2, pV3); const aiVector3D vNor = ((*pV2 - *pV1) ^ (*pV3 - *pV1)).NormalizeSafe();