浏览代码

Merge pull request #4359 from assimp/kimkulling-hmp_fix_override_issue4235

HMP: Fix override during copying position data
Kim Kulling 3 年之前
父节点
当前提交
a214b0833f
共有 1 个文件被更改,包括 48 次插入35 次删除
  1. 48 35
      code/AssetLib/HMP/HMPLoader.cpp

+ 48 - 35
code/AssetLib/HMP/HMPLoader.cpp

@@ -275,7 +275,9 @@ void HMPImporter::InternReadFile_HMP7() {
 
 
     // now load all vertices from the file
     // now load all vertices from the file
     aiVector3D *pcVertOut = pcMesh->mVertices;
     aiVector3D *pcVertOut = pcMesh->mVertices;
+    ai_assert(pcVertOut != nullptr);
     aiVector3D *pcNorOut = pcMesh->mNormals;
     aiVector3D *pcNorOut = pcMesh->mNormals;
+    ai_assert(pcNorOut != nullptr);
     const HMP::Vertex_HMP7 *src = (const HMP::Vertex_HMP7 *)szCurrent;
     const HMP::Vertex_HMP7 *src = (const HMP::Vertex_HMP7 *)szCurrent;
     for (unsigned int y = 0; y < height; ++y) {
     for (unsigned int y = 0; y < height; ++y) {
         for (unsigned int x = 0; x < width; ++x) {
         for (unsigned int x = 0; x < width; ++x) {
@@ -327,29 +329,31 @@ void HMPImporter::CreateMaterial(const unsigned char *szCurrent,
 
 
         // now read the first skin and skip all others
         // now read the first skin and skip all others
         ReadFirstSkin(pcHeader->numskins, szCurrent, &szCurrent);
         ReadFirstSkin(pcHeader->numskins, szCurrent, &szCurrent);
-    } else {
-        // generate a default material
-        const int iMode = (int)aiShadingMode_Gouraud;
-        aiMaterial *pcHelper = new aiMaterial();
-        pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
-
-        aiColor3D clr;
-        clr.b = clr.g = clr.r = 0.6f;
-        pcHelper->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
-        pcHelper->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_SPECULAR);
-
-        clr.b = clr.g = clr.r = 0.05f;
-        pcHelper->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_AMBIENT);
-
-        aiString szName;
-        szName.Set(AI_DEFAULT_MATERIAL_NAME);
-        pcHelper->AddProperty(&szName, AI_MATKEY_NAME);
-
-        // add the material to the scene
-        pScene->mNumMaterials = 1;
-        pScene->mMaterials = new aiMaterial *[1];
-        pScene->mMaterials[0] = pcHelper;
-    }
+        *szCurrentOut = szCurrent;
+        return;
+    } 
+
+    // generate a default material
+    const int iMode = (int)aiShadingMode_Gouraud;
+    aiMaterial *pcHelper = new aiMaterial();
+    pcHelper->AddProperty<int>(&iMode, 1, AI_MATKEY_SHADING_MODEL);
+
+    aiColor3D clr;
+    clr.b = clr.g = clr.r = 0.6f;
+    pcHelper->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_DIFFUSE);
+    pcHelper->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_SPECULAR);
+
+    clr.b = clr.g = clr.r = 0.05f;
+    pcHelper->AddProperty<aiColor3D>(&clr, 1, AI_MATKEY_COLOR_AMBIENT);
+
+    aiString szName;
+    szName.Set(AI_DEFAULT_MATERIAL_NAME);
+    pcHelper->AddProperty(&szName, AI_MATKEY_NAME);
+
+    // add the material to the scene
+    pScene->mNumMaterials = 1;
+    pScene->mMaterials = new aiMaterial *[1];
+    pScene->mMaterials[0] = pcHelper;
     *szCurrentOut = szCurrent;
     *szCurrentOut = szCurrent;
 }
 }
 
 
@@ -373,27 +377,36 @@ void HMPImporter::CreateOutputFaceList(unsigned int width, unsigned int height)
     aiVector3D *pcUVOut(pcUVs);
     aiVector3D *pcUVOut(pcUVs);
 
 
     // Build the terrain square
     // Build the terrain square
+    const unsigned int upperBound = pcMesh->mNumVertices;
     unsigned int iCurrent = 0;
     unsigned int iCurrent = 0;
     for (unsigned int y = 0; y < height - 1; ++y) {
     for (unsigned int y = 0; y < height - 1; ++y) {
+        const size_t offset0 = y * width;
+        const size_t offset1 = (y + 1) * width;
         for (unsigned int x = 0; x < width - 1; ++x, ++pcFaceOut) {
         for (unsigned int x = 0; x < width - 1; ++x, ++pcFaceOut) {
             pcFaceOut->mNumIndices = 4;
             pcFaceOut->mNumIndices = 4;
             pcFaceOut->mIndices = new unsigned int[4];
             pcFaceOut->mIndices = new unsigned int[4];
+            if ((offset0 + x + 1) >= upperBound){
+                continue;
+            }
+            if ((offset1 + x + 1) >= upperBound){
+                continue;
+            }
 
 
-            *pcVertOut++ = pcMesh->mVertices[y * width + x];
-            *pcVertOut++ = pcMesh->mVertices[(y + 1) * width + x];
-            *pcVertOut++ = pcMesh->mVertices[(y + 1) * width + x + 1];
-            *pcVertOut++ = pcMesh->mVertices[y * width + x + 1];
+            *pcVertOut++ = pcMesh->mVertices[offset0 + x];
+            *pcVertOut++ = pcMesh->mVertices[offset1 + x];
+            *pcVertOut++ = pcMesh->mVertices[offset1 + x + 1];
+            *pcVertOut++ = pcMesh->mVertices[offset0 + x + 1];
 
 
-            *pcNorOut++ = pcMesh->mNormals[y * width + x];
-            *pcNorOut++ = pcMesh->mNormals[(y + 1) * width + x];
-            *pcNorOut++ = pcMesh->mNormals[(y + 1) * width + x + 1];
-            *pcNorOut++ = pcMesh->mNormals[y * width + x + 1];
+            *pcNorOut++ = pcMesh->mNormals[offset0 + x];
+            *pcNorOut++ = pcMesh->mNormals[offset1 + x];
+            *pcNorOut++ = pcMesh->mNormals[offset1 + x + 1];
+            *pcNorOut++ = pcMesh->mNormals[offset0 + x + 1];
 
 
             if (pcMesh->mTextureCoords[0]) {
             if (pcMesh->mTextureCoords[0]) {
-                *pcUVOut++ = pcMesh->mTextureCoords[0][y * width + x];
-                *pcUVOut++ = pcMesh->mTextureCoords[0][(y + 1) * width + x];
-                *pcUVOut++ = pcMesh->mTextureCoords[0][(y + 1) * width + x + 1];
-                *pcUVOut++ = pcMesh->mTextureCoords[0][y * width + x + 1];
+                *pcUVOut++ = pcMesh->mTextureCoords[0][offset0 + x];
+                *pcUVOut++ = pcMesh->mTextureCoords[0][offset1 + x];
+                *pcUVOut++ = pcMesh->mTextureCoords[0][offset1 + x + 1];
+                *pcUVOut++ = pcMesh->mTextureCoords[0][offset0 + x + 1];
             }
             }
 
 
             for (unsigned int i = 0; i < 4; ++i)
             for (unsigned int i = 0; i < 4; ++i)