• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  */
32 package com.jme3.bullet.collision.shapes;
33 
34 import com.bulletphysics.collision.shapes.BvhTriangleMeshShape;
35 import com.bulletphysics.collision.shapes.IndexedMesh;
36 import com.bulletphysics.collision.shapes.TriangleIndexVertexArray;
37 import com.jme3.bullet.util.Converter;
38 import com.jme3.export.InputCapsule;
39 import com.jme3.export.JmeExporter;
40 import com.jme3.export.JmeImporter;
41 import com.jme3.export.OutputCapsule;
42 import com.jme3.math.Vector3f;
43 import com.jme3.scene.Mesh;
44 import java.io.IOException;
45 import java.nio.ByteBuffer;
46 
47 /**
48  * Basic mesh collision shape
49  * @author normenhansen
50  */
51 public class MeshCollisionShape extends CollisionShape {
52 
53     protected int numVertices, numTriangles, vertexStride, triangleIndexStride;
54     protected ByteBuffer triangleIndexBase, vertexBase;
55     protected IndexedMesh bulletMesh;
56 
MeshCollisionShape()57     public MeshCollisionShape() {
58     }
59 
60     /**
61      * creates a collision shape from the given TriMesh
62      * @param mesh the TriMesh to use
63      */
MeshCollisionShape(Mesh mesh)64     public MeshCollisionShape(Mesh mesh) {
65         createCollisionMesh(mesh, new Vector3f(1, 1, 1));
66     }
67 
createCollisionMesh(Mesh mesh, Vector3f worldScale)68     private void createCollisionMesh(Mesh mesh, Vector3f worldScale) {
69         this.scale = worldScale;
70         bulletMesh = Converter.convert(mesh);
71         this.numVertices = bulletMesh.numVertices;
72         this.numTriangles = bulletMesh.numTriangles;
73         this.vertexStride = bulletMesh.vertexStride;
74         this.triangleIndexStride = bulletMesh.triangleIndexStride;
75         this.triangleIndexBase = bulletMesh.triangleIndexBase;
76         this.vertexBase = bulletMesh.vertexBase;
77         createShape();
78     }
79 
80     /**
81      * creates a jme mesh from the collision shape, only needed for debugging
82      */
createJmeMesh()83     public Mesh createJmeMesh(){
84         return Converter.convert(bulletMesh);
85     }
86 
write(JmeExporter ex)87     public void write(JmeExporter ex) throws IOException {
88         super.write(ex);
89         OutputCapsule capsule = ex.getCapsule(this);
90         capsule.write(numVertices, "numVertices", 0);
91         capsule.write(numTriangles, "numTriangles", 0);
92         capsule.write(vertexStride, "vertexStride", 0);
93         capsule.write(triangleIndexStride, "triangleIndexStride", 0);
94 
95         capsule.write(triangleIndexBase.array(), "triangleIndexBase", new byte[0]);
96         capsule.write(vertexBase.array(), "vertexBase", new byte[0]);
97     }
98 
read(JmeImporter im)99     public void read(JmeImporter im) throws IOException {
100         super.read(im);
101         InputCapsule capsule = im.getCapsule(this);
102         numVertices = capsule.readInt("numVertices", 0);
103         numTriangles = capsule.readInt("numTriangles", 0);
104         vertexStride = capsule.readInt("vertexStride", 0);
105         triangleIndexStride = capsule.readInt("triangleIndexStride", 0);
106 
107         triangleIndexBase = ByteBuffer.wrap(capsule.readByteArray("triangleIndexBase", new byte[0]));
108         vertexBase = ByteBuffer.wrap(capsule.readByteArray("vertexBase", new byte[0]));
109         createShape();
110     }
111 
createShape()112     protected void createShape() {
113         bulletMesh = new IndexedMesh();
114         bulletMesh.numVertices = numVertices;
115         bulletMesh.numTriangles = numTriangles;
116         bulletMesh.vertexStride = vertexStride;
117         bulletMesh.triangleIndexStride = triangleIndexStride;
118         bulletMesh.triangleIndexBase = triangleIndexBase;
119         bulletMesh.vertexBase = vertexBase;
120         bulletMesh.triangleIndexBase = triangleIndexBase;
121         TriangleIndexVertexArray tiv = new TriangleIndexVertexArray(numTriangles, triangleIndexBase, triangleIndexStride, numVertices, vertexBase, vertexStride);
122         cShape = new BvhTriangleMeshShape(tiv, true);
123         cShape.setLocalScaling(Converter.convert(getScale()));
124         cShape.setMargin(margin);
125     }
126 }
127