* TGA loader now properly fails on grayscale formats git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7848 75d07b2b-3a1a-0410-a2c5-0572b91ccdca3.0
parent
7cc3592f0f
commit
bf72c7975b
@ -1,168 +1,169 @@ |
|||||||
/* |
/* |
||||||
* Copyright (c) 2009-2010 jMonkeyEngine |
* Copyright (c) 2009-2010 jMonkeyEngine |
||||||
* All rights reserved. |
* All rights reserved. |
||||||
* |
* |
||||||
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
||||||
* modification, are permitted provided that the following conditions are |
* modification, are permitted provided that the following conditions are |
||||||
* met: |
* met: |
||||||
* |
* |
||||||
* * Redistributions of source code must retain the above copyright |
* * Redistributions of source code must retain the above copyright |
||||||
* notice, this list of conditions and the following disclaimer. |
* notice, this list of conditions and the following disclaimer. |
||||||
* |
* |
||||||
* * Redistributions in binary form must reproduce the above copyright |
* * Redistributions in binary form must reproduce the above copyright |
||||||
* notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
||||||
* documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
||||||
* |
* |
||||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||||
* may be used to endorse or promote products derived from this software |
* may be used to endorse or promote products derived from this software |
||||||
* without specific prior written permission. |
* without specific prior written permission. |
||||||
* |
* |
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
*/ |
*/ |
||||||
package com.jme3.bullet.collision.shapes; |
package com.jme3.bullet.collision.shapes; |
||||||
|
|
||||||
import com.jme3.export.JmeExporter; |
import com.jme3.export.JmeExporter; |
||||||
import com.jme3.export.JmeImporter; |
import com.jme3.export.JmeImporter; |
||||||
import com.jme3.scene.Mesh; |
import com.jme3.scene.Mesh; |
||||||
import com.jme3.bullet.util.NativeMeshUtil; |
import com.jme3.bullet.util.NativeMeshUtil; |
||||||
import com.jme3.export.InputCapsule; |
import com.jme3.export.InputCapsule; |
||||||
import com.jme3.export.OutputCapsule; |
import com.jme3.export.OutputCapsule; |
||||||
import com.jme3.scene.VertexBuffer.Type; |
import com.jme3.scene.VertexBuffer.Type; |
||||||
import com.jme3.scene.mesh.IndexBuffer; |
import com.jme3.scene.mesh.IndexBuffer; |
||||||
import java.io.IOException; |
import com.jme3.util.BufferUtils; |
||||||
import java.nio.ByteBuffer; |
import java.io.IOException; |
||||||
import java.nio.ByteOrder; |
import java.nio.ByteBuffer; |
||||||
import java.nio.FloatBuffer; |
import java.nio.ByteOrder; |
||||||
import java.util.logging.Level; |
import java.nio.FloatBuffer; |
||||||
import java.util.logging.Logger; |
import java.util.logging.Level; |
||||||
|
import java.util.logging.Logger; |
||||||
/** |
|
||||||
* Basic mesh collision shape |
/** |
||||||
* @author normenhansen |
* Basic mesh collision shape |
||||||
*/ |
* @author normenhansen |
||||||
public class GImpactCollisionShape extends CollisionShape { |
*/ |
||||||
|
public class GImpactCollisionShape extends CollisionShape { |
||||||
// protected Vector3f worldScale;
|
|
||||||
protected int numVertices, numTriangles, vertexStride, triangleIndexStride; |
// protected Vector3f worldScale;
|
||||||
protected ByteBuffer triangleIndexBase, vertexBase; |
protected int numVertices, numTriangles, vertexStride, triangleIndexStride; |
||||||
protected long meshId = 0; |
protected ByteBuffer triangleIndexBase, vertexBase; |
||||||
// protected IndexedMesh bulletMesh;
|
protected long meshId = 0; |
||||||
|
// protected IndexedMesh bulletMesh;
|
||||||
public GImpactCollisionShape() { |
|
||||||
} |
public GImpactCollisionShape() { |
||||||
|
} |
||||||
/** |
|
||||||
* creates a collision shape from the given Mesh |
/** |
||||||
* @param mesh the Mesh to use |
* creates a collision shape from the given Mesh |
||||||
*/ |
* @param mesh the Mesh to use |
||||||
public GImpactCollisionShape(Mesh mesh) { |
*/ |
||||||
createCollisionMesh(mesh); |
public GImpactCollisionShape(Mesh mesh) { |
||||||
} |
createCollisionMesh(mesh); |
||||||
|
} |
||||||
private void createCollisionMesh(Mesh mesh) { |
|
||||||
triangleIndexBase = ByteBuffer.allocateDirect(mesh.getTriangleCount() * 3 * 4).order(ByteOrder.nativeOrder()); |
private void createCollisionMesh(Mesh mesh) { |
||||||
vertexBase = ByteBuffer.allocateDirect(mesh.getVertexCount() * 3 * 4).order(ByteOrder.nativeOrder()); |
triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4); |
||||||
// triangleIndexBase = ByteBuffer.allocate(mesh.getTriangleCount() * 3 * 4);
|
vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4); |
||||||
// vertexBase = ByteBuffer.allocate(mesh.getVertexCount() * 3 * 4);
|
// triangleIndexBase = ByteBuffer.allocate(mesh.getTriangleCount() * 3 * 4);
|
||||||
numVertices = mesh.getVertexCount(); |
// vertexBase = ByteBuffer.allocate(mesh.getVertexCount() * 3 * 4);
|
||||||
vertexStride = 12; //3 verts * 4 bytes per.
|
numVertices = mesh.getVertexCount(); |
||||||
numTriangles = mesh.getTriangleCount(); |
vertexStride = 12; //3 verts * 4 bytes per.
|
||||||
triangleIndexStride = 12; //3 index entries * 4 bytes each.
|
numTriangles = mesh.getTriangleCount(); |
||||||
|
triangleIndexStride = 12; //3 index entries * 4 bytes each.
|
||||||
IndexBuffer indices = mesh.getIndexBuffer(); |
|
||||||
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); |
IndexBuffer indices = mesh.getIndexBuffer(); |
||||||
vertices.rewind(); |
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); |
||||||
|
vertices.rewind(); |
||||||
int verticesLength = mesh.getVertexCount() * 3; |
|
||||||
for (int i = 0; i < verticesLength; i++) { |
int verticesLength = mesh.getVertexCount() * 3; |
||||||
float tempFloat = vertices.get(); |
for (int i = 0; i < verticesLength; i++) { |
||||||
vertexBase.putFloat(tempFloat); |
float tempFloat = vertices.get(); |
||||||
} |
vertexBase.putFloat(tempFloat); |
||||||
|
} |
||||||
int indicesLength = mesh.getTriangleCount() * 3; |
|
||||||
for (int i = 0; i < indicesLength; i++) { |
int indicesLength = mesh.getTriangleCount() * 3; |
||||||
triangleIndexBase.putInt(indices.get(i)); |
for (int i = 0; i < indicesLength; i++) { |
||||||
} |
triangleIndexBase.putInt(indices.get(i)); |
||||||
vertices.rewind(); |
} |
||||||
vertices.clear(); |
vertices.rewind(); |
||||||
|
vertices.clear(); |
||||||
createShape(); |
|
||||||
} |
createShape(); |
||||||
|
} |
||||||
// /**
|
|
||||||
// * creates a jme mesh from the collision shape, only needed for debugging
|
// /**
|
||||||
// */
|
// * creates a jme mesh from the collision shape, only needed for debugging
|
||||||
// public Mesh createJmeMesh() {
|
// */
|
||||||
// return Converter.convert(bulletMesh);
|
// public Mesh createJmeMesh() {
|
||||||
// }
|
// return Converter.convert(bulletMesh);
|
||||||
public void write(JmeExporter ex) throws IOException { |
// }
|
||||||
super.write(ex); |
public void write(JmeExporter ex) throws IOException { |
||||||
OutputCapsule capsule = ex.getCapsule(this); |
super.write(ex); |
||||||
// capsule.write(worldScale, "worldScale", new Vector3f(1, 1, 1));
|
OutputCapsule capsule = ex.getCapsule(this); |
||||||
capsule.write(numVertices, "numVertices", 0); |
// capsule.write(worldScale, "worldScale", new Vector3f(1, 1, 1));
|
||||||
capsule.write(numTriangles, "numTriangles", 0); |
capsule.write(numVertices, "numVertices", 0); |
||||||
capsule.write(vertexStride, "vertexStride", 0); |
capsule.write(numTriangles, "numTriangles", 0); |
||||||
capsule.write(triangleIndexStride, "triangleIndexStride", 0); |
capsule.write(vertexStride, "vertexStride", 0); |
||||||
|
capsule.write(triangleIndexStride, "triangleIndexStride", 0); |
||||||
capsule.write(triangleIndexBase.array(), "triangleIndexBase", new byte[0]); |
|
||||||
capsule.write(vertexBase.array(), "vertexBase", new byte[0]); |
capsule.write(triangleIndexBase.array(), "triangleIndexBase", new byte[0]); |
||||||
} |
capsule.write(vertexBase.array(), "vertexBase", new byte[0]); |
||||||
|
} |
||||||
public void read(JmeImporter im) throws IOException { |
|
||||||
super.read(im); |
public void read(JmeImporter im) throws IOException { |
||||||
InputCapsule capsule = im.getCapsule(this); |
super.read(im); |
||||||
// worldScale = (Vector3f) capsule.readSavable("worldScale", new Vector3f(1, 1, 1));
|
InputCapsule capsule = im.getCapsule(this); |
||||||
numVertices = capsule.readInt("numVertices", 0); |
// worldScale = (Vector3f) capsule.readSavable("worldScale", new Vector3f(1, 1, 1));
|
||||||
numTriangles = capsule.readInt("numTriangles", 0); |
numVertices = capsule.readInt("numVertices", 0); |
||||||
vertexStride = capsule.readInt("vertexStride", 0); |
numTriangles = capsule.readInt("numTriangles", 0); |
||||||
triangleIndexStride = capsule.readInt("triangleIndexStride", 0); |
vertexStride = capsule.readInt("vertexStride", 0); |
||||||
|
triangleIndexStride = capsule.readInt("triangleIndexStride", 0); |
||||||
triangleIndexBase = ByteBuffer.wrap(capsule.readByteArray("triangleIndexBase", new byte[0])); |
|
||||||
vertexBase = ByteBuffer.wrap(capsule.readByteArray("vertexBase", new byte[0])); |
triangleIndexBase = ByteBuffer.wrap(capsule.readByteArray("triangleIndexBase", new byte[0])); |
||||||
createShape(); |
vertexBase = ByteBuffer.wrap(capsule.readByteArray("vertexBase", new byte[0])); |
||||||
} |
createShape(); |
||||||
|
} |
||||||
protected void createShape() { |
|
||||||
// bulletMesh = new IndexedMesh();
|
protected void createShape() { |
||||||
// bulletMesh.numVertices = numVertices;
|
// bulletMesh = new IndexedMesh();
|
||||||
// bulletMesh.numTriangles = numTriangles;
|
// bulletMesh.numVertices = numVertices;
|
||||||
// bulletMesh.vertexStride = vertexStride;
|
// bulletMesh.numTriangles = numTriangles;
|
||||||
// bulletMesh.triangleIndexStride = triangleIndexStride;
|
// bulletMesh.vertexStride = vertexStride;
|
||||||
// bulletMesh.triangleIndexBase = triangleIndexBase;
|
// bulletMesh.triangleIndexStride = triangleIndexStride;
|
||||||
// bulletMesh.vertexBase = vertexBase;
|
// bulletMesh.triangleIndexBase = triangleIndexBase;
|
||||||
// bulletMesh.triangleIndexBase = triangleIndexBase;
|
// bulletMesh.vertexBase = vertexBase;
|
||||||
// TriangleIndexVertexArray tiv = new TriangleIndexVertexArray(numTriangles, triangleIndexBase, triangleIndexStride, numVertices, vertexBase, vertexStride);
|
// bulletMesh.triangleIndexBase = triangleIndexBase;
|
||||||
// objectId = new GImpactMeshShape(tiv);
|
// TriangleIndexVertexArray tiv = new TriangleIndexVertexArray(numTriangles, triangleIndexBase, triangleIndexStride, numVertices, vertexBase, vertexStride);
|
||||||
// objectId.setLocalScaling(Converter.convert(worldScale));
|
// objectId = new GImpactMeshShape(tiv);
|
||||||
// ((GImpactMeshShape)objectId).updateBound();
|
// objectId.setLocalScaling(Converter.convert(worldScale));
|
||||||
// objectId.setLocalScaling(Converter.convert(getScale()));
|
// ((GImpactMeshShape)objectId).updateBound();
|
||||||
// objectId.setMargin(margin);
|
// objectId.setLocalScaling(Converter.convert(getScale()));
|
||||||
meshId = NativeMeshUtil.createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride); |
// objectId.setMargin(margin);
|
||||||
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Mesh {0}", Long.toHexString(meshId)); |
meshId = NativeMeshUtil.createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride); |
||||||
objectId = createShape(meshId); |
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Mesh {0}", Long.toHexString(meshId)); |
||||||
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId)); |
objectId = createShape(meshId); |
||||||
setScale(scale); |
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId)); |
||||||
setMargin(margin); |
setScale(scale); |
||||||
} |
setMargin(margin); |
||||||
|
} |
||||||
private native long createShape(long meshId); |
|
||||||
|
private native long createShape(long meshId); |
||||||
@Override |
|
||||||
protected void finalize() throws Throwable { |
@Override |
||||||
super.finalize(); |
protected void finalize() throws Throwable { |
||||||
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Finalizing Mesh {0}", Long.toHexString(meshId)); |
super.finalize(); |
||||||
finalizeNative(meshId); |
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Finalizing Mesh {0}", Long.toHexString(meshId)); |
||||||
} |
finalizeNative(meshId); |
||||||
|
} |
||||||
private native void finalizeNative(long objectId); |
|
||||||
} |
private native void finalizeNative(long objectId); |
||||||
|
} |
||||||
|
@ -1,145 +1,146 @@ |
|||||||
/* |
/* |
||||||
* To change this template, choose Tools | Templates |
* To change this template, choose Tools | Templates |
||||||
* and open the template in the editor. |
* and open the template in the editor. |
||||||
*/ |
*/ |
||||||
package com.jme3.bullet.collision.shapes; |
package com.jme3.bullet.collision.shapes; |
||||||
|
|
||||||
import com.jme3.export.InputCapsule; |
import com.jme3.export.InputCapsule; |
||||||
import com.jme3.export.JmeExporter; |
import com.jme3.export.JmeExporter; |
||||||
import com.jme3.export.JmeImporter; |
import com.jme3.export.JmeImporter; |
||||||
import com.jme3.export.OutputCapsule; |
import com.jme3.export.OutputCapsule; |
||||||
import com.jme3.math.FastMath; |
import com.jme3.math.FastMath; |
||||||
import com.jme3.math.Vector3f; |
import com.jme3.math.Vector3f; |
||||||
import com.jme3.scene.Mesh; |
import com.jme3.scene.Mesh; |
||||||
import java.io.IOException; |
import com.jme3.util.BufferUtils; |
||||||
import java.nio.ByteBuffer; |
import java.io.IOException; |
||||||
import java.nio.ByteOrder; |
import java.nio.ByteBuffer; |
||||||
import java.util.logging.Level; |
import java.nio.ByteOrder; |
||||||
import java.util.logging.Logger; |
import java.util.logging.Level; |
||||||
|
import java.util.logging.Logger; |
||||||
/** |
|
||||||
* Uses Bullet Physics Heightfield terrain collision system. This is MUCH faster |
/** |
||||||
* than using a regular mesh. |
* Uses Bullet Physics Heightfield terrain collision system. This is MUCH faster |
||||||
* There are a couple tricks though: |
* than using a regular mesh. |
||||||
* -No rotation or translation is supported. |
* There are a couple tricks though: |
||||||
* -The collision bbox must be centered around 0,0,0 with the height above and below the y-axis being |
* -No rotation or translation is supported. |
||||||
* equal on either side. If not, the whole collision box is shifted vertically and things don't collide |
* -The collision bbox must be centered around 0,0,0 with the height above and below the y-axis being |
||||||
* as they should. |
* equal on either side. If not, the whole collision box is shifted vertically and things don't collide |
||||||
* |
* as they should. |
||||||
* @author Brent Owens |
* |
||||||
*/ |
* @author Brent Owens |
||||||
public class HeightfieldCollisionShape extends CollisionShape { |
*/ |
||||||
|
public class HeightfieldCollisionShape extends CollisionShape { |
||||||
protected int heightStickWidth; |
|
||||||
protected int heightStickLength; |
protected int heightStickWidth; |
||||||
protected float[] heightfieldData; |
protected int heightStickLength; |
||||||
protected float heightScale; |
protected float[] heightfieldData; |
||||||
protected float minHeight; |
protected float heightScale; |
||||||
protected float maxHeight; |
protected float minHeight; |
||||||
protected int upAxis; |
protected float maxHeight; |
||||||
protected boolean flipQuadEdges; |
protected int upAxis; |
||||||
protected ByteBuffer bbuf; |
protected boolean flipQuadEdges; |
||||||
// protected FloatBuffer fbuf;
|
protected ByteBuffer bbuf; |
||||||
|
// protected FloatBuffer fbuf;
|
||||||
public HeightfieldCollisionShape() { |
|
||||||
} |
public HeightfieldCollisionShape() { |
||||||
|
} |
||||||
public HeightfieldCollisionShape(float[] heightmap) { |
|
||||||
createCollisionHeightfield(heightmap, Vector3f.UNIT_XYZ); |
public HeightfieldCollisionShape(float[] heightmap) { |
||||||
} |
createCollisionHeightfield(heightmap, Vector3f.UNIT_XYZ); |
||||||
|
} |
||||||
public HeightfieldCollisionShape(float[] heightmap, Vector3f scale) { |
|
||||||
createCollisionHeightfield(heightmap, scale); |
public HeightfieldCollisionShape(float[] heightmap, Vector3f scale) { |
||||||
} |
createCollisionHeightfield(heightmap, scale); |
||||||
|
} |
||||||
protected void createCollisionHeightfield(float[] heightmap, Vector3f worldScale) { |
|
||||||
this.scale = worldScale; |
protected void createCollisionHeightfield(float[] heightmap, Vector3f worldScale) { |
||||||
this.heightScale = 1;//don't change away from 1, we use worldScale instead to scale
|
this.scale = worldScale; |
||||||
|
this.heightScale = 1;//don't change away from 1, we use worldScale instead to scale
|
||||||
this.heightfieldData = heightmap; |
|
||||||
|
this.heightfieldData = heightmap; |
||||||
float min = heightfieldData[0]; |
|
||||||
float max = heightfieldData[0]; |
float min = heightfieldData[0]; |
||||||
// calculate min and max height
|
float max = heightfieldData[0]; |
||||||
for (int i = 0; i < heightfieldData.length; i++) { |
// calculate min and max height
|
||||||
if (heightfieldData[i] < min) { |
for (int i = 0; i < heightfieldData.length; i++) { |
||||||
min = heightfieldData[i]; |
if (heightfieldData[i] < min) { |
||||||
} |
min = heightfieldData[i]; |
||||||
if (heightfieldData[i] > max) { |
} |
||||||
max = heightfieldData[i]; |
if (heightfieldData[i] > max) { |
||||||
} |
max = heightfieldData[i]; |
||||||
} |
} |
||||||
// we need to center the terrain collision box at 0,0,0 for BulletPhysics. And to do that we need to set the
|
} |
||||||
// min and max height to be equal on either side of the y axis, otherwise it gets shifted and collision is incorrect.
|
// we need to center the terrain collision box at 0,0,0 for BulletPhysics. And to do that we need to set the
|
||||||
if (max < 0) { |
// min and max height to be equal on either side of the y axis, otherwise it gets shifted and collision is incorrect.
|
||||||
max = -min; |
if (max < 0) { |
||||||
} else { |
max = -min; |
||||||
if (Math.abs(max) > Math.abs(min)) { |
} else { |
||||||
min = -max; |
if (Math.abs(max) > Math.abs(min)) { |
||||||
} else { |
min = -max; |
||||||
max = -min; |
} else { |
||||||
} |
max = -min; |
||||||
} |
} |
||||||
this.minHeight = min; |
} |
||||||
this.maxHeight = max; |
this.minHeight = min; |
||||||
|
this.maxHeight = max; |
||||||
this.upAxis = 1; |
|
||||||
this.flipQuadEdges = false; |
this.upAxis = 1; |
||||||
|
this.flipQuadEdges = false; |
||||||
heightStickWidth = (int) FastMath.sqrt(heightfieldData.length); |
|
||||||
heightStickLength = heightStickWidth; |
heightStickWidth = (int) FastMath.sqrt(heightfieldData.length); |
||||||
|
heightStickLength = heightStickWidth; |
||||||
|
|
||||||
createShape(); |
|
||||||
} |
createShape(); |
||||||
|
} |
||||||
protected void createShape() { |
|
||||||
bbuf = ByteBuffer.allocateDirect(heightfieldData.length * 4).order(ByteOrder.nativeOrder()); |
protected void createShape() { |
||||||
// fbuf = bbuf.asFloatBuffer();//FloatBuffer.wrap(heightfieldData);
|
bbuf = BufferUtils.createByteBuffer(heightfieldData.length * 4); |
||||||
// fbuf.rewind();
|
// fbuf = bbuf.asFloatBuffer();//FloatBuffer.wrap(heightfieldData);
|
||||||
// fbuf.put(heightfieldData);
|
// fbuf.rewind();
|
||||||
for (int i = 0; i < heightfieldData.length; i++) { |
// fbuf.put(heightfieldData);
|
||||||
float f = heightfieldData[i]; |
for (int i = 0; i < heightfieldData.length; i++) { |
||||||
bbuf.putFloat(f); |
float f = heightfieldData[i]; |
||||||
} |
bbuf.putFloat(f); |
||||||
// fbuf.rewind();
|
} |
||||||
objectId = createShape(heightStickWidth, heightStickLength, bbuf, heightScale, minHeight, maxHeight, upAxis, flipQuadEdges); |
// fbuf.rewind();
|
||||||
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId)); |
objectId = createShape(heightStickWidth, heightStickLength, bbuf, heightScale, minHeight, maxHeight, upAxis, flipQuadEdges); |
||||||
setScale(scale); |
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId)); |
||||||
setMargin(margin); |
setScale(scale); |
||||||
} |
setMargin(margin); |
||||||
|
} |
||||||
private native long createShape(int heightStickWidth, int heightStickLength, ByteBuffer heightfieldData, float heightScale, float minHeight, float maxHeight, int upAxis, boolean flipQuadEdges); |
|
||||||
|
private native long createShape(int heightStickWidth, int heightStickLength, ByteBuffer heightfieldData, float heightScale, float minHeight, float maxHeight, int upAxis, boolean flipQuadEdges); |
||||||
public Mesh createJmeMesh() { |
|
||||||
//TODO return Converter.convert(bulletMesh);
|
public Mesh createJmeMesh() { |
||||||
return null; |
//TODO return Converter.convert(bulletMesh);
|
||||||
} |
return null; |
||||||
|
} |
||||||
public void write(JmeExporter ex) throws IOException { |
|
||||||
super.write(ex); |
public void write(JmeExporter ex) throws IOException { |
||||||
OutputCapsule capsule = ex.getCapsule(this); |
super.write(ex); |
||||||
capsule.write(heightStickWidth, "heightStickWidth", 0); |
OutputCapsule capsule = ex.getCapsule(this); |
||||||
capsule.write(heightStickLength, "heightStickLength", 0); |
capsule.write(heightStickWidth, "heightStickWidth", 0); |
||||||
capsule.write(heightScale, "heightScale", 0); |
capsule.write(heightStickLength, "heightStickLength", 0); |
||||||
capsule.write(minHeight, "minHeight", 0); |
capsule.write(heightScale, "heightScale", 0); |
||||||
capsule.write(maxHeight, "maxHeight", 0); |
capsule.write(minHeight, "minHeight", 0); |
||||||
capsule.write(upAxis, "upAxis", 1); |
capsule.write(maxHeight, "maxHeight", 0); |
||||||
capsule.write(heightfieldData, "heightfieldData", new float[0]); |
capsule.write(upAxis, "upAxis", 1); |
||||||
capsule.write(flipQuadEdges, "flipQuadEdges", false); |
capsule.write(heightfieldData, "heightfieldData", new float[0]); |
||||||
} |
capsule.write(flipQuadEdges, "flipQuadEdges", false); |
||||||
|
} |
||||||
public void read(JmeImporter im) throws IOException { |
|
||||||
super.read(im); |
public void read(JmeImporter im) throws IOException { |
||||||
InputCapsule capsule = im.getCapsule(this); |
super.read(im); |
||||||
heightStickWidth = capsule.readInt("heightStickWidth", 0); |
InputCapsule capsule = im.getCapsule(this); |
||||||
heightStickLength = capsule.readInt("heightStickLength", 0); |
heightStickWidth = capsule.readInt("heightStickWidth", 0); |
||||||
heightScale = capsule.readFloat("heightScale", 0); |
heightStickLength = capsule.readInt("heightStickLength", 0); |
||||||
minHeight = capsule.readFloat("minHeight", 0); |
heightScale = capsule.readFloat("heightScale", 0); |
||||||
maxHeight = capsule.readFloat("maxHeight", 0); |
minHeight = capsule.readFloat("minHeight", 0); |
||||||
upAxis = capsule.readInt("upAxis", 1); |
maxHeight = capsule.readFloat("maxHeight", 0); |
||||||
heightfieldData = capsule.readFloatArray("heightfieldData", new float[0]); |
upAxis = capsule.readInt("upAxis", 1); |
||||||
flipQuadEdges = capsule.readBoolean("flipQuadEdges", false); |
heightfieldData = capsule.readFloatArray("heightfieldData", new float[0]); |
||||||
createShape(); |
flipQuadEdges = capsule.readBoolean("flipQuadEdges", false); |
||||||
} |
createShape(); |
||||||
} |
} |
||||||
|
} |
||||||
|
@ -1,98 +1,99 @@ |
|||||||
package com.jme3.bullet.collision.shapes; |
package com.jme3.bullet.collision.shapes; |
||||||
|
|
||||||
import java.nio.FloatBuffer; |
import java.nio.FloatBuffer; |
||||||
import com.jme3.export.InputCapsule; |
import com.jme3.export.InputCapsule; |
||||||
import com.jme3.export.JmeExporter; |
import com.jme3.export.JmeExporter; |
||||||
import com.jme3.export.JmeImporter; |
import com.jme3.export.JmeImporter; |
||||||
import com.jme3.export.OutputCapsule; |
import com.jme3.export.OutputCapsule; |
||||||
import com.jme3.scene.Mesh; |
import com.jme3.scene.Mesh; |
||||||
import com.jme3.scene.VertexBuffer.Type; |
import com.jme3.scene.VertexBuffer.Type; |
||||||
import java.io.IOException; |
import com.jme3.util.BufferUtils; |
||||||
import java.nio.ByteBuffer; |
import java.io.IOException; |
||||||
import java.nio.ByteOrder; |
import java.nio.ByteBuffer; |
||||||
import java.util.logging.Level; |
import java.nio.ByteOrder; |
||||||
import java.util.logging.Logger; |
import java.util.logging.Level; |
||||||
|
import java.util.logging.Logger; |
||||||
public class HullCollisionShape extends CollisionShape { |
|
||||||
|
public class HullCollisionShape extends CollisionShape { |
||||||
private float[] points; |
|
||||||
// protected FloatBuffer fbuf;
|
private float[] points; |
||||||
|
// protected FloatBuffer fbuf;
|
||||||
public HullCollisionShape() { |
|
||||||
} |
public HullCollisionShape() { |
||||||
|
} |
||||||
public HullCollisionShape(Mesh mesh) { |
|
||||||
this.points = getPoints(mesh); |
public HullCollisionShape(Mesh mesh) { |
||||||
createShape(); |
this.points = getPoints(mesh); |
||||||
} |
createShape(); |
||||||
|
} |
||||||
public HullCollisionShape(float[] points) { |
|
||||||
this.points = points; |
public HullCollisionShape(float[] points) { |
||||||
createShape(); |
this.points = points; |
||||||
} |
createShape(); |
||||||
|
} |
||||||
@Override |
|
||||||
public void write(JmeExporter ex) throws IOException { |
@Override |
||||||
super.write(ex); |
public void write(JmeExporter ex) throws IOException { |
||||||
|
super.write(ex); |
||||||
OutputCapsule capsule = ex.getCapsule(this); |
|
||||||
capsule.write(points, "points", null); |
OutputCapsule capsule = ex.getCapsule(this); |
||||||
} |
capsule.write(points, "points", null); |
||||||
|
} |
||||||
@Override |
|
||||||
public void read(JmeImporter im) throws IOException { |
@Override |
||||||
super.read(im); |
public void read(JmeImporter im) throws IOException { |
||||||
InputCapsule capsule = im.getCapsule(this); |
super.read(im); |
||||||
|
InputCapsule capsule = im.getCapsule(this); |
||||||
// for backwards compatability
|
|
||||||
Mesh mesh = (Mesh) capsule.readSavable("hullMesh", null); |
// for backwards compatability
|
||||||
if (mesh != null) { |
Mesh mesh = (Mesh) capsule.readSavable("hullMesh", null); |
||||||
this.points = getPoints(mesh); |
if (mesh != null) { |
||||||
} else { |
this.points = getPoints(mesh); |
||||||
this.points = capsule.readFloatArray("points", null); |
} else { |
||||||
|
this.points = capsule.readFloatArray("points", null); |
||||||
} |
|
||||||
// fbuf = ByteBuffer.allocateDirect(points.length * 4).asFloatBuffer();
|
} |
||||||
// fbuf.put(points);
|
// fbuf = ByteBuffer.allocateDirect(points.length * 4).asFloatBuffer();
|
||||||
// fbuf = FloatBuffer.wrap(points).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
// fbuf.put(points);
|
||||||
createShape(); |
// fbuf = FloatBuffer.wrap(points).order(ByteOrder.nativeOrder()).asFloatBuffer();
|
||||||
} |
createShape(); |
||||||
|
} |
||||||
protected void createShape() { |
|
||||||
// ObjectArrayList<Vector3f> pointList = new ObjectArrayList<Vector3f>();
|
protected void createShape() { |
||||||
// for (int i = 0; i < points.length; i += 3) {
|
// ObjectArrayList<Vector3f> pointList = new ObjectArrayList<Vector3f>();
|
||||||
// pointList.add(new Vector3f(points[i], points[i + 1], points[i + 2]));
|
// for (int i = 0; i < points.length; i += 3) {
|
||||||
// }
|
// pointList.add(new Vector3f(points[i], points[i + 1], points[i + 2]));
|
||||||
// objectId = new ConvexHullShape(pointList);
|
// }
|
||||||
// objectId.setLocalScaling(Converter.convert(getScale()));
|
// objectId = new ConvexHullShape(pointList);
|
||||||
// objectId.setMargin(margin);
|
// objectId.setLocalScaling(Converter.convert(getScale()));
|
||||||
ByteBuffer bbuf=ByteBuffer.allocateDirect(points.length * 4).order(ByteOrder.nativeOrder()); |
// objectId.setMargin(margin);
|
||||||
// fbuf = bbuf.asFloatBuffer();
|
ByteBuffer bbuf=BufferUtils.createByteBuffer(points.length * 4); |
||||||
// fbuf.rewind();
|
// fbuf = bbuf.asFloatBuffer();
|
||||||
// fbuf.put(points);
|
// fbuf.rewind();
|
||||||
for (int i = 0; i < points.length; i++) { |
// fbuf.put(points);
|
||||||
float f = points[i]; |
for (int i = 0; i < points.length; i++) { |
||||||
bbuf.putFloat(f); |
float f = points[i]; |
||||||
} |
bbuf.putFloat(f); |
||||||
bbuf.rewind(); |
} |
||||||
objectId = createShape(bbuf); |
bbuf.rewind(); |
||||||
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId)); |
objectId = createShape(bbuf); |
||||||
setScale(scale); |
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId)); |
||||||
setMargin(margin); |
setScale(scale); |
||||||
} |
setMargin(margin); |
||||||
|
} |
||||||
private native long createShape(ByteBuffer points); |
|
||||||
|
private native long createShape(ByteBuffer points); |
||||||
protected float[] getPoints(Mesh mesh) { |
|
||||||
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); |
protected float[] getPoints(Mesh mesh) { |
||||||
vertices.rewind(); |
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); |
||||||
int components = mesh.getVertexCount() * 3; |
vertices.rewind(); |
||||||
float[] pointsArray = new float[components]; |
int components = mesh.getVertexCount() * 3; |
||||||
for (int i = 0; i < components; i += 3) { |
float[] pointsArray = new float[components]; |
||||||
pointsArray[i] = vertices.get(); |
for (int i = 0; i < components; i += 3) { |
||||||
pointsArray[i + 1] = vertices.get(); |
pointsArray[i] = vertices.get(); |
||||||
pointsArray[i + 2] = vertices.get(); |
pointsArray[i + 1] = vertices.get(); |
||||||
} |
pointsArray[i + 2] = vertices.get(); |
||||||
return pointsArray; |
} |
||||||
} |
return pointsArray; |
||||||
} |
} |
||||||
|
} |
||||||
|
@ -1,160 +1,161 @@ |
|||||||
/* |
/* |
||||||
* Copyright (c) 2009-2010 jMonkeyEngine |
* Copyright (c) 2009-2010 jMonkeyEngine |
||||||
* All rights reserved. |
* All rights reserved. |
||||||
* |
* |
||||||
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
||||||
* modification, are permitted provided that the following conditions are |
* modification, are permitted provided that the following conditions are |
||||||
* met: |
* met: |
||||||
* |
* |
||||||
* * Redistributions of source code must retain the above copyright |
* * Redistributions of source code must retain the above copyright |
||||||
* notice, this list of conditions and the following disclaimer. |
* notice, this list of conditions and the following disclaimer. |
||||||
* |
* |
||||||
* * Redistributions in binary form must reproduce the above copyright |
* * Redistributions in binary form must reproduce the above copyright |
||||||
* notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
||||||
* documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
||||||
* |
* |
||||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||||
* may be used to endorse or promote products derived from this software |
* may be used to endorse or promote products derived from this software |
||||||
* without specific prior written permission. |
* without specific prior written permission. |
||||||
* |
* |
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
*/ |
*/ |
||||||
package com.jme3.bullet.collision.shapes; |
package com.jme3.bullet.collision.shapes; |
||||||
|
|
||||||
import com.jme3.bullet.util.NativeMeshUtil; |
import com.jme3.bullet.util.NativeMeshUtil; |
||||||
import com.jme3.export.JmeExporter; |
import com.jme3.export.JmeExporter; |
||||||
import com.jme3.export.JmeImporter; |
import com.jme3.export.JmeImporter; |
||||||
import com.jme3.scene.Mesh; |
import com.jme3.scene.Mesh; |
||||||
import com.jme3.export.InputCapsule; |
import com.jme3.export.InputCapsule; |
||||||
import com.jme3.export.OutputCapsule; |
import com.jme3.export.OutputCapsule; |
||||||
import com.jme3.scene.VertexBuffer.Type; |
import com.jme3.scene.VertexBuffer.Type; |
||||||
import com.jme3.scene.mesh.IndexBuffer; |
import com.jme3.scene.mesh.IndexBuffer; |
||||||
import java.io.IOException; |
import com.jme3.util.BufferUtils; |
||||||
import java.nio.ByteBuffer; |
import java.io.IOException; |
||||||
import java.nio.ByteOrder; |
import java.nio.ByteBuffer; |
||||||
import java.nio.FloatBuffer; |
import java.nio.ByteOrder; |
||||||
import java.util.logging.Level; |
import java.nio.FloatBuffer; |
||||||
import java.util.logging.Logger; |
import java.util.logging.Level; |
||||||
|
import java.util.logging.Logger; |
||||||
/** |
|
||||||
* Basic mesh collision shape |
/** |
||||||
* @author normenhansen |
* Basic mesh collision shape |
||||||
*/ |
* @author normenhansen |
||||||
public class MeshCollisionShape extends CollisionShape { |
*/ |
||||||
|
public class MeshCollisionShape extends CollisionShape { |
||||||
protected int numVertices, numTriangles, vertexStride, triangleIndexStride; |
|
||||||
protected ByteBuffer triangleIndexBase, vertexBase; |
protected int numVertices, numTriangles, vertexStride, triangleIndexStride; |
||||||
protected long meshId = 0; |
protected ByteBuffer triangleIndexBase, vertexBase; |
||||||
|
protected long meshId = 0; |
||||||
public MeshCollisionShape() { |
|
||||||
} |
public MeshCollisionShape() { |
||||||
|
} |
||||||
/** |
|
||||||
* creates a collision shape from the given TriMesh |
/** |
||||||
* @param mesh the TriMesh to use |
* creates a collision shape from the given TriMesh |
||||||
*/ |
* @param mesh the TriMesh to use |
||||||
public MeshCollisionShape(Mesh mesh) { |
*/ |
||||||
createCollisionMesh(mesh); |
public MeshCollisionShape(Mesh mesh) { |
||||||
} |
createCollisionMesh(mesh); |
||||||
|
} |
||||||
private void createCollisionMesh(Mesh mesh) { |
|
||||||
triangleIndexBase = ByteBuffer.allocateDirect(mesh.getTriangleCount() * 3 * 4).order(ByteOrder.nativeOrder()); |
private void createCollisionMesh(Mesh mesh) { |
||||||
vertexBase = ByteBuffer.allocateDirect(mesh.getVertexCount() * 3 * 4).order(ByteOrder.nativeOrder()); |
triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4); |
||||||
numVertices = mesh.getVertexCount(); |
vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4); |
||||||
vertexStride = 12; //3 verts * 4 bytes per.
|
numVertices = mesh.getVertexCount(); |
||||||
numTriangles = mesh.getTriangleCount(); |
vertexStride = 12; //3 verts * 4 bytes per.
|
||||||
triangleIndexStride = 12; //3 index entries * 4 bytes each.
|
numTriangles = mesh.getTriangleCount(); |
||||||
|
triangleIndexStride = 12; //3 index entries * 4 bytes each.
|
||||||
IndexBuffer indices = mesh.getIndexBuffer(); |
|
||||||
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); |
IndexBuffer indices = mesh.getIndexBuffer(); |
||||||
vertices.rewind(); |
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); |
||||||
|
vertices.rewind(); |
||||||
int verticesLength = mesh.getVertexCount() * 3; |
|
||||||
for (int i = 0; i < verticesLength; i++) { |
int verticesLength = mesh.getVertexCount() * 3; |
||||||
float tempFloat = vertices.get(); |
for (int i = 0; i < verticesLength; i++) { |
||||||
vertexBase.putFloat(tempFloat); |
float tempFloat = vertices.get(); |
||||||
} |
vertexBase.putFloat(tempFloat); |
||||||
|
} |
||||||
int indicesLength = mesh.getTriangleCount() * 3; |
|
||||||
for (int i = 0; i < indicesLength; i++) { |
int indicesLength = mesh.getTriangleCount() * 3; |
||||||
triangleIndexBase.putInt(indices.get(i)); |
for (int i = 0; i < indicesLength; i++) { |
||||||
} |
triangleIndexBase.putInt(indices.get(i)); |
||||||
vertices.rewind(); |
} |
||||||
vertices.clear(); |
vertices.rewind(); |
||||||
|
vertices.clear(); |
||||||
createShape(); |
|
||||||
} |
createShape(); |
||||||
|
} |
||||||
/** |
|
||||||
* creates a jme mesh from the collision shape, only needed for debugging |
/** |
||||||
*/ |
* creates a jme mesh from the collision shape, only needed for debugging |
||||||
// public Mesh createJmeMesh(){
|
*/ |
||||||
// return Converter.convert(bulletMesh);
|
// public Mesh createJmeMesh(){
|
||||||
// }
|
// return Converter.convert(bulletMesh);
|
||||||
public void write(JmeExporter ex) throws IOException { |
// }
|
||||||
super.write(ex); |
public void write(JmeExporter ex) throws IOException { |
||||||
OutputCapsule capsule = ex.getCapsule(this); |
super.write(ex); |
||||||
capsule.write(numVertices, "numVertices", 0); |
OutputCapsule capsule = ex.getCapsule(this); |
||||||
capsule.write(numTriangles, "numTriangles", 0); |
capsule.write(numVertices, "numVertices", 0); |
||||||
capsule.write(vertexStride, "vertexStride", 0); |
capsule.write(numTriangles, "numTriangles", 0); |
||||||
capsule.write(triangleIndexStride, "triangleIndexStride", 0); |
capsule.write(vertexStride, "vertexStride", 0); |
||||||
|
capsule.write(triangleIndexStride, "triangleIndexStride", 0); |
||||||
capsule.write(triangleIndexBase.array(), "triangleIndexBase", new byte[0]); |
|
||||||
capsule.write(vertexBase.array(), "vertexBase", new byte[0]); |
capsule.write(triangleIndexBase.array(), "triangleIndexBase", new byte[0]); |
||||||
} |
capsule.write(vertexBase.array(), "vertexBase", new byte[0]); |
||||||
|
} |
||||||
public void read(JmeImporter im) throws IOException { |
|
||||||
super.read(im); |
public void read(JmeImporter im) throws IOException { |
||||||
InputCapsule capsule = im.getCapsule(this); |
super.read(im); |
||||||
numVertices = capsule.readInt("numVertices", 0); |
InputCapsule capsule = im.getCapsule(this); |
||||||
numTriangles = capsule.readInt("numTriangles", 0); |
numVertices = capsule.readInt("numVertices", 0); |
||||||
vertexStride = capsule.readInt("vertexStride", 0); |
numTriangles = capsule.readInt("numTriangles", 0); |
||||||
triangleIndexStride = capsule.readInt("triangleIndexStride", 0); |
vertexStride = capsule.readInt("vertexStride", 0); |
||||||
|
triangleIndexStride = capsule.readInt("triangleIndexStride", 0); |
||||||
triangleIndexBase = ByteBuffer.wrap(capsule.readByteArray("triangleIndexBase", new byte[0])); |
|
||||||
vertexBase = ByteBuffer.wrap(capsule.readByteArray("vertexBase", new byte[0])).order(ByteOrder.nativeOrder()); |
triangleIndexBase = ByteBuffer.wrap(capsule.readByteArray("triangleIndexBase", new byte[0])); |
||||||
createShape(); |
vertexBase = ByteBuffer.wrap(capsule.readByteArray("vertexBase", new byte[0])).order(ByteOrder.nativeOrder()); |
||||||
} |
createShape(); |
||||||
|
} |
||||||
protected void createShape() { |
|
||||||
// bulletMesh = new IndexedMesh();
|
protected void createShape() { |
||||||
// bulletMesh.numVertices = numVertices;
|
// bulletMesh = new IndexedMesh();
|
||||||
// bulletMesh.numTriangles = numTriangles;
|
// bulletMesh.numVertices = numVertices;
|
||||||
// bulletMesh.vertexStride = vertexStride;
|
// bulletMesh.numTriangles = numTriangles;
|
||||||
// bulletMesh.triangleIndexStride = triangleIndexStride;
|
// bulletMesh.vertexStride = vertexStride;
|
||||||
// bulletMesh.triangleIndexBase = triangleIndexBase;
|
// bulletMesh.triangleIndexStride = triangleIndexStride;
|
||||||
// bulletMesh.vertexBase = vertexBase;
|
// bulletMesh.triangleIndexBase = triangleIndexBase;
|
||||||
// bulletMesh.triangleIndexBase = triangleIndexBase;
|
// bulletMesh.vertexBase = vertexBase;
|
||||||
// TriangleIndexVertexArray tiv = new TriangleIndexVertexArray(numTriangles, triangleIndexBase, triangleIndexStride, numVertices, vertexBase, vertexStride);
|
// bulletMesh.triangleIndexBase = triangleIndexBase;
|
||||||
// objectId = new BvhTriangleMeshShape(tiv, true);
|
// TriangleIndexVertexArray tiv = new TriangleIndexVertexArray(numTriangles, triangleIndexBase, triangleIndexStride, numVertices, vertexBase, vertexStride);
|
||||||
// objectId.setLocalScaling(Converter.convert(getScale()));
|
// objectId = new BvhTriangleMeshShape(tiv, true);
|
||||||
// objectId.setMargin(margin);
|
// objectId.setLocalScaling(Converter.convert(getScale()));
|
||||||
meshId = NativeMeshUtil.createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride); |
// objectId.setMargin(margin);
|
||||||
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Mesh {0}", Long.toHexString(meshId)); |
meshId = NativeMeshUtil.createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride); |
||||||
objectId = createShape(meshId); |
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Mesh {0}", Long.toHexString(meshId)); |
||||||
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId)); |
objectId = createShape(meshId); |
||||||
setScale(scale); |
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Created Shape {0}", Long.toHexString(objectId)); |
||||||
setMargin(margin); |
setScale(scale); |
||||||
} |
setMargin(margin); |
||||||
|
} |
||||||
private native long createShape(long meshId); |
|
||||||
|
private native long createShape(long meshId); |
||||||
@Override |
|
||||||
protected void finalize() throws Throwable { |
@Override |
||||||
super.finalize(); |
protected void finalize() throws Throwable { |
||||||
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Finalizing Mesh {0}", Long.toHexString(meshId)); |
super.finalize(); |
||||||
finalizeNative(meshId); |
Logger.getLogger(this.getClass().getName()).log(Level.INFO, "Finalizing Mesh {0}", Long.toHexString(meshId)); |
||||||
} |
finalizeNative(meshId); |
||||||
|
} |
||||||
private native void finalizeNative(long objectId); |
|
||||||
} |
private native void finalizeNative(long objectId); |
||||||
|
} |
||||||
|
@ -1,77 +1,77 @@ |
|||||||
/* |
/* |
||||||
* Copyright (c) 2009-2010 jMonkeyEngine |
* Copyright (c) 2009-2010 jMonkeyEngine |
||||||
* All rights reserved. |
* All rights reserved. |
||||||
* |
* |
||||||
* Redistribution and use in source and binary forms, with or without |
* Redistribution and use in source and binary forms, with or without |
||||||
* modification, are permitted provided that the following conditions are |
* modification, are permitted provided that the following conditions are |
||||||
* met: |
* met: |
||||||
* |
* |
||||||
* * Redistributions of source code must retain the above copyright |
* * Redistributions of source code must retain the above copyright |
||||||
* notice, this list of conditions and the following disclaimer. |
* notice, this list of conditions and the following disclaimer. |
||||||
* |
* |
||||||
* * Redistributions in binary form must reproduce the above copyright |
* * Redistributions in binary form must reproduce the above copyright |
||||||
* notice, this list of conditions and the following disclaimer in the |
* notice, this list of conditions and the following disclaimer in the |
||||||
* documentation and/or other materials provided with the distribution. |
* documentation and/or other materials provided with the distribution. |
||||||
* |
* |
||||||
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
* * Neither the name of 'jMonkeyEngine' nor the names of its contributors |
||||||
* may be used to endorse or promote products derived from this software |
* may be used to endorse or promote products derived from this software |
||||||
* without specific prior written permission. |
* without specific prior written permission. |
||||||
* |
* |
||||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
||||||
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED |
||||||
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
||||||
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR |
||||||
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
||||||
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
||||||
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
||||||
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF |
||||||
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
||||||
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
||||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
||||||
*/ |
*/ |
||||||
package com.jme3.bullet.util; |
package com.jme3.bullet.util; |
||||||
|
|
||||||
import com.jme3.scene.Mesh; |
import com.jme3.scene.Mesh; |
||||||
import com.jme3.scene.VertexBuffer.Type; |
import com.jme3.scene.VertexBuffer.Type; |
||||||
import com.jme3.scene.mesh.IndexBuffer; |
import com.jme3.scene.mesh.IndexBuffer; |
||||||
import java.nio.ByteBuffer; |
import com.jme3.util.BufferUtils; |
||||||
import java.nio.ByteOrder; |
import java.nio.ByteBuffer; |
||||||
import java.nio.FloatBuffer; |
import java.nio.FloatBuffer; |
||||||
|
|
||||||
/** |
/** |
||||||
* |
* |
||||||
* @author normenhansen |
* @author normenhansen |
||||||
*/ |
*/ |
||||||
public class NativeMeshUtil { |
public class NativeMeshUtil { |
||||||
|
|
||||||
public static long getTriangleIndexVertexArray(Mesh mesh){ |
public static long getTriangleIndexVertexArray(Mesh mesh){ |
||||||
ByteBuffer triangleIndexBase = ByteBuffer.allocateDirect(mesh.getTriangleCount() * 3 * 4).order(ByteOrder.nativeOrder()); |
ByteBuffer triangleIndexBase = BufferUtils.createByteBuffer(mesh.getTriangleCount() * 3 * 4); |
||||||
ByteBuffer vertexBase = ByteBuffer.allocateDirect(mesh.getVertexCount() * 3 * 4).order(ByteOrder.nativeOrder()); |
ByteBuffer vertexBase = BufferUtils.createByteBuffer(mesh.getVertexCount() * 3 * 4); |
||||||
int numVertices = mesh.getVertexCount(); |
int numVertices = mesh.getVertexCount(); |
||||||
int vertexStride = 12; //3 verts * 4 bytes per.
|
int vertexStride = 12; //3 verts * 4 bytes per.
|
||||||
int numTriangles = mesh.getTriangleCount(); |
int numTriangles = mesh.getTriangleCount(); |
||||||
int triangleIndexStride = 12; //3 index entries * 4 bytes each.
|
int triangleIndexStride = 12; //3 index entries * 4 bytes each.
|
||||||
|
|
||||||
IndexBuffer indices = mesh.getIndicesAsList(); |
IndexBuffer indices = mesh.getIndicesAsList(); |
||||||
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); |
FloatBuffer vertices = mesh.getFloatBuffer(Type.Position); |
||||||
vertices.rewind(); |
vertices.rewind(); |
||||||
|
|
||||||
int verticesLength = mesh.getVertexCount() * 3; |
int verticesLength = mesh.getVertexCount() * 3; |
||||||
for (int i = 0; i < verticesLength; i++) { |
for (int i = 0; i < verticesLength; i++) { |
||||||
float tempFloat = vertices.get(); |
float tempFloat = vertices.get(); |
||||||
vertexBase.putFloat(tempFloat); |
vertexBase.putFloat(tempFloat); |
||||||
} |
} |
||||||
|
|
||||||
int indicesLength = mesh.getTriangleCount() * 3; |
int indicesLength = mesh.getTriangleCount() * 3; |
||||||
for (int i = 0; i < indicesLength; i++) { |
for (int i = 0; i < indicesLength; i++) { |
||||||
triangleIndexBase.putInt(indices.get(i)); |
triangleIndexBase.putInt(indices.get(i)); |
||||||
} |
} |
||||||
vertices.rewind(); |
vertices.rewind(); |
||||||
vertices.clear(); |
vertices.clear(); |
||||||
|
|
||||||
return createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride); |
return createTriangleIndexVertexArray(triangleIndexBase, vertexBase, numTriangles, numVertices, vertexStride, triangleIndexStride); |
||||||
} |
} |
||||||
|
|
||||||
public static native long createTriangleIndexVertexArray(ByteBuffer triangleIndexBase, ByteBuffer vertexBase, int numTraingles, int numVertices, int vertextStride, int triangleIndexStride); |
public static native long createTriangleIndexVertexArray(ByteBuffer triangleIndexBase, ByteBuffer vertexBase, int numTraingles, int numVertices, int vertextStride, int triangleIndexStride); |
||||||
|
|
||||||
} |
} |
||||||
|
Loading…
Reference in new issue