From 49c47f8174c8f331a4d1f3f669c3f3965587ce40 Mon Sep 17 00:00:00 2001 From: "Kae..pl" Date: Tue, 28 Aug 2012 11:56:14 +0000 Subject: [PATCH] Fixing a bug that caused the loader to crash when the mesh (traditional one and not BMesh) had no faces and only edges or points instead (without support for loading edges and points yet). git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9692 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../plugins/blender/meshes/MeshBuilder.java | 23 ++++++++++++ .../plugins/blender/meshes/MeshHelper.java | 37 ++++++++++++++++--- 2 files changed, 55 insertions(+), 5 deletions(-) diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshBuilder.java b/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshBuilder.java index 1c1a6bd6c..72d61aed6 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshBuilder.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshBuilder.java @@ -5,6 +5,7 @@ import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.logging.Logger; import com.jme3.math.FastMath; import com.jme3.math.Vector2f; @@ -12,6 +13,8 @@ import com.jme3.math.Vector3f; import com.jme3.util.BufferUtils; /*package*/ class MeshBuilder { + private static final Logger LOGGER = Logger.getLogger(MeshBuilder.class.getName()); + /** An array of reference vertices. */ private Vector3f[][] verticesAndNormals; /** An list of vertices colors. */ @@ -50,6 +53,26 @@ import com.jme3.util.BufferUtils; globalVertexReferenceMap = new HashMap>>(verticesAndNormals.length); } + /** + * This method adds a point to the mesh. + * @param coordinates the coordinates of the point + * @param normal the point's normal vector + * @param materialNumber the material number for this point + */ + public void appendPoint(Vector3f coordinates, Vector3f normal, int materialNumber) { + LOGGER.warning("Appending single point not yet supported!");//TODO + } + + /** + * This method adds a line to the mesh. + * @param v1 index of the 1'st vertex from the reference vertex table + * @param v2 index of the 2'nd vertex from the reference vertex table + * @param smooth indicates if this face should have smooth shading or flat shading + */ + public void appendEdge(int v1, int v2, boolean smooth) { + LOGGER.warning("Appending single line not yet supported!");//TODO + } + /** * This method adds a face to the mesh. * @param v1 index of the 1'st vertex from the reference vertex table diff --git a/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java b/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java index cf740f7bb..45851bc7f 100644 --- a/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java +++ b/engine/src/blender/com/jme3/scene/plugins/blender/meshes/MeshHelper.java @@ -111,12 +111,12 @@ public class MeshHelper extends AbstractBlenderHelper { MeshBuilder meshBuilder = new MeshBuilder(verticesAndNormals, verticesColors, this.areGeneratedTexturesPresent(materials)); - Pointer pMFace = (Pointer) structure.getFieldValue("mface"); - if(pMFace.isNotNull()) { - this.readTraditionalFaces(meshBuilder, structure, blenderContext); - } else { + if(this.isBMeshCompatible(structure)) { this.readBMesh(meshBuilder, structure, blenderContext); + } else { + this.readTraditionalFaces(meshBuilder, structure, blenderContext); } + if(meshBuilder.isEmpty()) { geometries = new ArrayList(0); blenderContext.addLoadedFeatures(structure.getOldMemoryAddress(), structure.getName(), structure, geometries); @@ -235,6 +235,19 @@ public class MeshHelper extends AbstractBlenderHelper { return geometries; } + /** + * Tells if the given mesh structure supports BMesh. + * + * @param meshStructure + * the mesh structure + * @return true if BMesh is supported and false otherwise + */ + private boolean isBMeshCompatible(Structure meshStructure) { + Pointer pMLoop = (Pointer) meshStructure.getFieldValue("mloop"); + Pointer pMPoly = (Pointer) meshStructure.getFieldValue("mpoly"); + return pMLoop != null && pMPoly != null; + } + /** * This method reads the mesh from the new BMesh system. * @@ -316,7 +329,7 @@ public class MeshHelper extends AbstractBlenderHelper { @SuppressWarnings("unchecked") private void readTraditionalFaces(MeshBuilder meshBuilder, Structure meshStructure, BlenderContext blenderContext) throws BlenderFileException { Pointer pMFace = (Pointer) meshStructure.getFieldValue("mface"); - List mFaces = pMFace.fetchData(blenderContext.getInputStream()); + List mFaces = pMFace.isNotNull() ? pMFace.fetchData(blenderContext.getInputStream()) : null; if (mFaces != null && mFaces.size() > 0) { Pointer pMTFace = (Pointer) meshStructure.getFieldValue("mtface"); List mtFaces = null; @@ -362,6 +375,20 @@ public class MeshHelper extends AbstractBlenderHelper { meshBuilder.appendFace(v1, v3, v4, smooth, materialNumber, uvs == null ? null : uvCoordinatesForFace, true, i); } } + } else { + Pointer pMEdge = (Pointer) meshStructure.getFieldValue("medge"); + List mEdges = pMEdge.isNotNull() ? pMEdge.fetchData(blenderContext.getInputStream()) : null; + if (mEdges != null && mEdges.size() > 0) { + for (int i = 0; i < mEdges.size(); ++i) { + Structure mEdge = mEdges.get(i); + boolean smooth = (((Number) mEdge.getFieldValue("flag")).byteValue() & 0x01) != 0x00; + + int v1 = ((Number) mEdge.getFieldValue("v1")).intValue(); + int v2 = ((Number) mEdge.getFieldValue("v2")).intValue(); + + meshBuilder.appendEdge(v1, v2, smooth); + } + } } }