1/* 2 * Copyright (c) 2009-2010 jMonkeyEngine 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 12 * * Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 16 * * Neither the name of 'jMonkeyEngine' nor the names of its contributors 17 * may be used to endorse or promote products derived from this software 18 * without specific prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 24 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 25 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 26 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 27 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 29 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 30 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 31 */ 32package com.jme3.bullet.collision.shapes; 33 34import com.jme3.bullet.util.NativeMeshUtil; 35import com.jme3.export.InputCapsule; 36import com.jme3.export.JmeExporter; 37import com.jme3.export.JmeImporter; 38import com.jme3.export.OutputCapsule; 39import com.jme3.scene.Mesh; 40import com.jme3.scene.VertexBuffer.Type; 41import com.jme3.scene.mesh.IndexBuffer; 42import com.jme3.util.BufferUtils; 43import java.io.IOException; 44import java.nio.ByteBuffer; 45import java.nio.ByteOrder; 46import java.nio.FloatBuffer; 47import java.util.logging.Level; 48import java.util.logging.Logger; 49 50/** 51 * Basic mesh collision shape 52 * @author normenhansen 53 */ 54public class MeshCollisionShape extends CollisionShape { 55 56 protected int numVertices, numTriangles, vertexStride, triangleIndexStride; 57 protected ByteBuffer triangleIndexBase, vertexBase; 58 protected long meshId = 0; 59 60 public MeshCollisionShape() { 61 } 62 63 /** 64 * creates a collision shape from the given TriMesh 65 * @param mesh the TriMesh to use 66 */ 67 public MeshCollisionShape(Mesh mesh) { 68 createCollisionMesh(mesh); 69 } 70 71 private void createCollisionMesh(Mesh mesh) { 72 triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4); 73 vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4); 74 numVertices = mesh.getVertexCount(); 75 vertexStride = 12; //3 verts * 4 bytes per. 76 numTriangles = mesh.getTriangleCount(); 77 triangleIndexStride = 12; //3 index entries * 4 bytes each. 78 79 IndexBuffer indices = mesh.getIndexBuffer(); 80 FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); 81 vertices.rewind(); 82 83 int verticesLength = mesh.getVertexCount() * 3; 84 for (int i = 0; i < verticesLength; i++) { 85 float tempFloat = vertices.get(); 86 vertexBase.putFloat(tempFloat); 87 } 88 89 int indicesLength = mesh.getTriangleCount() * 3; 90 for (int i = 0; i < indicesLength; i++) { 91 triangleIndexBase.putInt(indices.get(i)); 92 } 93 vertices.rewind(); 94 vertices.clear(); 95 96 createShape(); 97 } 98 99 /** 100 * creates a jme mesh from the collision shape, only needed for debugging 101 */ 102// public Mesh createJmeMesh(){ 103// return Converter.convert(bulletMesh); 104// } 105 public void write(JmeExporter ex) throws IOException { 106 super.write(ex); 107 OutputCapsule capsule = ex.getCapsule(this); 108 capsule.write(numVertices, "numVertices", 0); 109 capsule.write(numTriangles, "numTriangles", 0); 110 capsule.write(vertexStride, "vertexStride", 0); 111 capsule.write(triangleIndexStride, "triangleIndexStride", 0); 112 113 capsule.write(triangleIndexBase.array(), "triangleIndexBase", new byte[0]); 114 capsule.write(vertexBase.array(), "vertexBase", new byte[0]); 115 } 116 117 public void read(JmeImporter im) throws IOException { 118 super.read(im); 119 InputCapsule capsule = im.getCapsule(this); 120 numVertices = capsule.readInt("numVertices", 0); 121 numTriangles = capsule.readInt("numTriangles", 0); 122 vertexStride = capsule.readInt("vertexStride", 0); 123 triangleIndexStride = capsule.readInt("triangleIndexStride", 0); 124 125 triangleIndexBase = ByteBuffer.wrap(capsule.readByteArray("triangleIndexBase", new byte[0])); 126 vertexBase = ByteBuffer.wrap(capsule.readByteArray("vertexBase", new byte[0])).order(ByteOrder.nativeOrder()); 127 createShape(); 128 } 129 130 protected void createShape() { 131// bulletMesh = new IndexedMesh(); 132// bulletMesh.numVertices = numVertices; 133// bulletMesh.numTriangles = numTriangles; 134// bulletMesh.vertexStride = vertexStride; 135// bulletMesh.triangleIndexStride = triangleIndexStride; 136// bulletMesh.triangleIndexBase = triangleIndexBase; 137// bulletMesh.vertexBase = vertexBase; 138// bulletMesh.triangleIndexBase = triangleIndexBase; 139// TriangleIndexVertexArray tiv = new TriangleIndexVertexArray(numTriangles, triangleIndexBase, triangleIndexStride, numVertices, vertexBase, vertexStride); 140// objectId = new BvhTriangleMeshShape(tiv, true); 141// objectId.setLocalScaling(Converter.convert(getScale())); 142// objectId.setMargin(margin); 143 meshId = NativeMeshUtil.createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride); 144 Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Mesh {0}", Long.toHexString(meshId)); 145 objectId = createShape(meshId); 146 Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId)); 147 setScale(scale); 148 setMargin(margin); 149 } 150 151 private native long createShape(long meshId); 152 153 @Override 154 protected void finalize() throws Throwable { 155 super.finalize(); 156 Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Finalizing Mesh {0}", Long.toHexString(meshId)); 157 finalizeNative(meshId); 158 } 159 160 private native void finalizeNative(long objectId); 161} 162