Skip to content

Commit

Permalink
Merge pull request #2704 from MalcolmTyrrell/fix-gltf-importer-crash
Browse files Browse the repository at this point in the history
Fix gltf importer crash
  • Loading branch information
kimkulling committed Oct 23, 2019
2 parents 85bde11 + 932acf4 commit bd25cc7
Show file tree
Hide file tree
Showing 8 changed files with 651 additions and 0 deletions.
18 changes: 18 additions & 0 deletions code/glTF/glTFImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,8 @@ void glTFImporter::ImportMaterials(glTF::Asset& r) {

if (mScene->mNumMaterials == 0) {
mScene->mNumMaterials = 1;
// Delete the array of length zero created above.
delete[] mScene->mMaterials;
mScene->mMaterials = new aiMaterial*[1];
mScene->mMaterials[0] = new aiMaterial();
}
Expand Down Expand Up @@ -330,6 +332,10 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)

case PrimitiveMode_LINES: {
nFaces = count / 2;
if (nFaces * 2 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped.");
count = nFaces * 2;
}
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 2) {
SetFace(faces[i / 2], data.GetUInt(i), data.GetUInt(i + 1));
Expand All @@ -353,6 +359,10 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)

case PrimitiveMode_TRIANGLES: {
nFaces = count / 3;
if (nFaces * 3 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped.");
count = nFaces * 3;
}
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 3) {
SetFace(faces[i / 3], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2));
Expand Down Expand Up @@ -395,6 +405,10 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)

case PrimitiveMode_LINES: {
nFaces = count / 2;
if (nFaces * 2 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped.");
count = nFaces * 2;
}
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 2) {
SetFace(faces[i / 2], i, i + 1);
Expand All @@ -418,6 +432,10 @@ void glTFImporter::ImportMeshes(glTF::Asset& r)

case PrimitiveMode_TRIANGLES: {
nFaces = count / 3;
if (nFaces * 3 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped.");
count = nFaces * 3;
}
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 3) {
SetFace(faces[i / 3], i, i + 1, i + 2);
Expand Down
16 changes: 16 additions & 0 deletions code/glTF2/glTF2Importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)

case PrimitiveMode_LINES: {
nFaces = count / 2;
if (nFaces * 2 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped.");
count = nFaces * 2;
}
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 2) {
SetFace(faces[i / 2], data.GetUInt(i), data.GetUInt(i + 1));
Expand All @@ -553,6 +557,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)

case PrimitiveMode_TRIANGLES: {
nFaces = count / 3;
if (nFaces * 3 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped.");
count = nFaces * 3;
}
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 3) {
SetFace(faces[i / 3], data.GetUInt(i), data.GetUInt(i + 1), data.GetUInt(i + 2));
Expand Down Expand Up @@ -604,6 +612,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)

case PrimitiveMode_LINES: {
nFaces = count / 2;
if (nFaces * 2 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the LINES mode. Some vertices were dropped.");
count = nFaces * 2;
}
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 2) {
SetFace(faces[i / 2], i, i + 1);
Expand All @@ -627,6 +639,10 @@ void glTF2Importer::ImportMeshes(glTF2::Asset& r)

case PrimitiveMode_TRIANGLES: {
nFaces = count / 3;
if (nFaces * 3 != count) {
ASSIMP_LOG_WARN("The number of vertices was not compatible with the TRIANGLES mode. Some vertices were dropped.");
count = nFaces * 3;
}
faces = new aiFace[nFaces];
for (unsigned int i = 0; i < count; i += 3) {
SetFace(faces[i / 3], i, i + 1, i + 2);
Expand Down
Binary file added test/models/glTF/IncorrectVertexArrays/Cube.bin
Binary file not shown.
283 changes: 283 additions & 0 deletions test/models/glTF/IncorrectVertexArrays/Cube_v1.gltf
Original file line number Diff line number Diff line change
@@ -0,0 +1,283 @@
{
"accessors" : {
"accessor_0" : {
"bufferView" : "bufferView_0",
"byteOffset" : 0,
"componentType" : 5123,
"count" : 36,
"max" : [
35
],
"min" : [
0
],
"type" : "SCALAR"
},
"accessor_1" : {
"bufferView" : "bufferView_1",
"byteOffset" : 0,
"componentType" : 5126,
"count" : 36,
"max" : [
1.000000,
1.000000,
1.000001
],
"min" : [
-1.000000,
-1.000000,
-1.000000
],
"type" : "VEC3"
},
"accessor_2" : {
"bufferView" : "bufferView_2",
"byteOffset" : 0,
"componentType" : 5126,
"count" : 36,
"max" : [
1.000000,
1.000000,
1.000000
],
"min" : [
-1.000000,
-1.000000,
-1.000000
],
"type" : "VEC3"
},
"accessor_3" : {
"bufferView" : "bufferView_3",
"byteOffset" : 0,
"componentType" : 5126,
"count" : 36,
"max" : [
1.000000,
-0.000000,
-0.000000,
1.000000
],
"min" : [
0.000000,
-0.000000,
-1.000000,
-1.000000
],
"type" : "VEC4"
},
"accessor_4" : {
"bufferView" : "bufferView_4",
"byteOffset" : 0,
"componentType" : 5126,
"count" : 36,
"max" : [
1.000000,
1.000000
],
"min" : [
-1.000000,
-1.000000
],
"type" : "VEC2"
},
"accessor_5" : {
"bufferView" : "bufferView_1",
"byteOffset" : 0,
"componentType" : 5126,
"count" : 36,
"type" : "VEC3"
},
"accessor_6" : {
"bufferView" : "bufferView_1",
"byteOffset" : 0,
"componentType" : 5126,
"count" : 35,
"type" : "VEC3"
},
"accessor_7" : {
"bufferView" : "bufferView_0",
"byteOffset" : 0,
"componentType" : 5123,
"count" : 35,
"max" : [
35
],
"min" : [
0
],
"type" : "SCALAR"
}
},
"asset" : {
"generator" : "VKTS glTF 2.0 exporter",
"version" : "1.0"
},
"bufferViews" : {
"bufferView_0" : {
"buffer" : "buffer_0",
"byteLength" : 72,
"byteOffset" : 0,
"target" : 34963
},
"bufferView_1" : {
"buffer" : "buffer_0",
"byteLength" : 432,
"byteOffset" : 72,
"target" : 34962
},
"bufferView_2" : {
"buffer" : "buffer_0",
"byteLength" : 432,
"byteOffset" : 504,
"target" : 34962
},
"bufferView_3" : {
"buffer" : "buffer_0",
"byteLength" : 576,
"byteOffset" : 936,
"target" : 34962
},
"bufferView_4" : {
"buffer" : "buffer_0",
"byteLength" : 288,
"byteOffset" : 1512,
"target" : 34962
}
},
"buffers" : {
"buffer_0" : {
"byteLength" : 514,
"uri" : "Cube.bin"
}
},
"meshes" : {
"mesh_0" : {
"name" : "Cube",
"primitives" : [
{
"attributes" : {
"POSITION" : "accessor_1"
},
"mode" : 4
}
]
},
"mesh_1" : {
"name" : "TruncatedCube",
"primitives" : [ {
"attributes" : {
"POSITION" : "accessor_6"
},
"mode" : 4
} ]
},
"mesh_2" : {
"name" : "Lines",
"primitives" : [ {
"attributes" : {
"POSITION" : "accessor_5"
},
"mode" : 1
} ]
},
"mesh_3" : {
"name" : "TruncatedLines",
"primitives" : [ {
"attributes" : {
"POSITION" : "accessor_6"
},
"mode" : 1
} ]
},
"mesh_4" : {
"name" : "IndexedCube",
"primitives" : [ {
"attributes" : {
"POSITION" : "accessor_1"
},
"mode" : 4,
"indices" : "accessor_0"
} ]
},
"mesh_5" : {
"name" : "TruncatedIndexedCube",
"primitives" : [ {
"attributes" : {
"POSITION" : "accessor_6"
},
"mode" : 4,
"indices" : "accessor_7"
} ]
},
"mesh_6" : {
"name" : "IndexedLines",
"primitives" : [ {
"attributes" : {
"POSITION" : "accessor_5"
},
"mode" : 1,
"indices" : "accessor_0"
} ]
},
"mesh_7" : {
"name" : "TruncatedIndexedLines",
"primitives" : [ {
"attributes" : {
"POSITION" : "accessor_6"
},
"mode" : 1,
"indices" : "accessor_7"
} ]
}
},
"nodes" : {
"node_0" : {
"meshes" : [ "mesh_0" ],
"name" : "Cube"
},
"node_1" : {
"meshes" : [ "mesh_1" ],
"name" : "TruncatedCube",
"translation": [ 2.5, 0.0, 2.5 ]
},
"node_2" : {
"meshes" : [ "mesh_2" ],
"name" : "Lines",
"translation": [ 2.5, 0.0, 0.0 ]
},
"node_3" : {
"meshes" : [ "mesh_3" ],
"name" : "TruncatedLines",
"translation": [ 2.5, 0.0, -2.5 ]
},
"node_4" : {
"meshes" : [ "mesh_4" ],
"name" : "IndexedCube",
"translation": [ -2.5, 0.0, 2.5 ]
},
"node_5" : {
"meshes" : [ "mesh_5" ],
"name" : "TruncatedIndexedCube",
"translation": [ -2.5, 0.0, 0.0 ]
},
"node_6" : {
"meshes" : [ "mesh_6" ],
"name" : "IndexedLines",
"translation": [ -2.5, 0.0, -2.5 ]
},
"node_7" : {
"meshes" : [ "mesh_7" ],
"name" : "TruncatedIndexedLines",
"translation": [ 0.0, 0.0, -2.5 ]
}
},
"scene" : "defaultScene",
"scenes" : {
"defaultScene" : {
"nodes" : [
"node_0", "node_1", "node_2", "node_3", "node_4", "node_5", "node_6", "node_7"
]
}
}
}
Binary file added test/models/glTF2/IncorrectVertexArrays/Cube.bin
Binary file not shown.

0 comments on commit bd25cc7

Please sign in to comment.