Skip to content

Commit

Permalink
Add more vertex index checks
Browse files Browse the repository at this point in the history
  • Loading branch information
zbx1425 committed Aug 27, 2023
1 parent a8a8477 commit 149c7c4
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 12 deletions.
11 changes: 7 additions & 4 deletions common/src/main/java/cn/zbx1425/sowcerext/model/RawMesh.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

public class RawMesh {

public MaterialProp materialProp;
public final MaterialProp materialProp;
public List<Vertex> vertices = new ArrayList<>();
public List<Face> faces = new ArrayList<>();

Expand Down Expand Up @@ -82,13 +82,15 @@ public void clear() {
faces.clear();
}

public boolean checkVertIndex() {
public void validateVertIndex() {
for (Face face : faces) {
for (int vertIndex : face.vertices) {
if (vertIndex < 0 || vertIndex >= vertices.size()) return false;
if (vertIndex < 0 || vertIndex >= vertices.size()) {
throw new IndexOutOfBoundsException("RawMesh contains invalid vertex index "
+ vertIndex + " (Should be 0 to " + (vertices.size() - 1) + ")");
}
}
}
return true;
}

/** Removes duplicate vertices and faces from the mesh. */
Expand Down Expand Up @@ -216,6 +218,7 @@ public void upload(Mesh mesh, VertAttrMapping mapping) {
}

public Mesh upload(VertAttrMapping mapping) {
validateVertIndex();
VertBuf vertBufObj = new VertBuf();
IndexBuf indexBufObj = new IndexBuf(faces.size(), GL11.GL_UNSIGNED_INT);
Mesh target = new Mesh(vertBufObj, indexBufObj, materialProp);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,7 @@ public RawModel(DataInputStream dis) throws IOException {
public Model upload(VertAttrMapping mapping) {
Model model = new Model();
for (RawMesh mesh : meshList.values()) {
if (mesh.faces.size() == 0) continue;
if (!mesh.checkVertIndex()) throw new IndexOutOfBoundsException("RawModel contains invalid vertex index");
if (mesh.faces.isEmpty()) continue;
model.meshList.add(mesh.upload(mapping));
}
return model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ public static RawModel loadModel(ResourceManager resourceManager, ResourceLocati
if (StringUtils.isEmpty(tokens[0])) continue;
switch (tokens[0]) {
case "createmeshbuilder":
if (!buildingMesh.checkVertIndex())
throw new IndexOutOfBoundsException("Invalid vertex index in AddFace/AddFace2.");
if (buildingMesh.faces.size() > 0) builtMeshList.add(buildingMesh);
buildingMesh.validateVertIndex();
if (!buildingMesh.faces.isEmpty()) builtMeshList.add(buildingMesh);
buildingMesh = new RawMesh(new MaterialProp("rendertype_entity_cutout"));
break;
case "addvertex":
Expand Down Expand Up @@ -216,9 +215,8 @@ public static RawModel loadModel(ResourceManager resourceManager, ResourceLocati
}

}
if (!buildingMesh.checkVertIndex())
throw new IndexOutOfBoundsException("Vertex index out of bound in " + objLocation);
if (buildingMesh.faces.size() > 0) builtMeshList.add(buildingMesh);
buildingMesh.validateVertIndex();
if (!buildingMesh.faces.isEmpty()) builtMeshList.add(buildingMesh);

RawModel model = new RawModel();
model.sourceLocation = objLocation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,7 @@ private static RawModel loadModel(Obj srcObj, ResourceLocation objLocation, Map<
mesh.faces.add(new Face(new int[] {face.getVertexIndex(0), face.getVertexIndex(1), face.getVertexIndex(2)}));
}
if (atlasManager != null) atlasManager.applyToMesh(mesh);
mesh.validateVertIndex();
model.append(mesh);
}

Expand Down

0 comments on commit 149c7c4

Please sign in to comment.