• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.jme3.bullet.collision.shapes;
2 
3 import com.jme3.export.InputCapsule;
4 import com.jme3.export.JmeExporter;
5 import com.jme3.export.JmeImporter;
6 import com.jme3.export.OutputCapsule;
7 import com.jme3.scene.Mesh;
8 import com.jme3.scene.VertexBuffer.Type;
9 import com.jme3.util.BufferUtils;
10 import java.io.IOException;
11 import java.nio.ByteBuffer;
12 import java.nio.FloatBuffer;
13 import java.util.logging.Level;
14 import java.util.logging.Logger;
15 
16 public class HullCollisionShape extends CollisionShape {
17 
18     private float[] points;
19 //    protected FloatBuffer fbuf;
20 
HullCollisionShape()21     public HullCollisionShape() {
22     }
23 
HullCollisionShape(Mesh mesh)24     public HullCollisionShape(Mesh mesh) {
25         this.points = getPoints(mesh);
26         createShape();
27     }
28 
HullCollisionShape(float[] points)29     public HullCollisionShape(float[] points) {
30         this.points = points;
31         createShape();
32     }
33 
34     @Override
write(JmeExporter ex)35     public void write(JmeExporter ex) throws IOException {
36         super.write(ex);
37 
38         OutputCapsule capsule = ex.getCapsule(this);
39         capsule.write(points, "points", null);
40     }
41 
42     @Override
read(JmeImporter im)43     public void read(JmeImporter im) throws IOException {
44         super.read(im);
45         InputCapsule capsule = im.getCapsule(this);
46 
47         // for backwards compatability
48         Mesh mesh = (Mesh) capsule.readSavable("hullMesh", null);
49         if (mesh != null) {
50             this.points = getPoints(mesh);
51         } else {
52             this.points = capsule.readFloatArray("points", null);
53 
54         }
55 //        fbuf = ByteBuffer.allocateDirect(points.length * 4).asFloatBuffer();
56 //        fbuf.put(points);
57 //        fbuf = FloatBuffer.wrap(points).order(ByteOrder.nativeOrder()).asFloatBuffer();
58         createShape();
59     }
60 
createShape()61     protected void createShape() {
62 //        ObjectArrayList<Vector3f> pointList = new ObjectArrayList<Vector3f>();
63 //        for (int i = 0; i < points.length; i += 3) {
64 //            pointList.add(new Vector3f(points[i], points[i + 1], points[i + 2]));
65 //        }
66 //        objectId = new ConvexHullShape(pointList);
67 //        objectId.setLocalScaling(Converter.convert(getScale()));
68 //        objectId.setMargin(margin);
69         ByteBuffer bbuf=BufferUtils.createByteBuffer(points.length * 4);
70 //        fbuf = bbuf.asFloatBuffer();
71 //        fbuf.rewind();
72 //        fbuf.put(points);
73         for (int i = 0; i < points.length; i++) {
74             float f = points[i];
75             bbuf.putFloat(f);
76         }
77         bbuf.rewind();
78         objectId = createShape(bbuf);
79         Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId));
80         setScale(scale);
81         setMargin(margin);
82     }
83 
createShape(ByteBuffer points)84     private native long createShape(ByteBuffer points);
85 
getPoints(Mesh mesh)86     protected float[] getPoints(Mesh mesh) {
87         FloatBuffer vertices = mesh.getFloatBuffer(Type.Position);
88         vertices.rewind();
89         int components = mesh.getVertexCount() * 3;
90         float[] pointsArray = new float[components];
91         for (int i = 0; i < components; i += 3) {
92             pointsArray[i] = vertices.get();
93             pointsArray[i + 1] = vertices.get();
94             pointsArray[i + 2] = vertices.get();
95         }
96         return pointsArray;
97     }
98 }
99