use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.
the class EntropyComputeUtil method computeLodEntropy.
public static float computeLodEntropy(Mesh terrainBlock, Buffer lodIndices) {
// Bounding box for the terrain block
BoundingBox bbox = (BoundingBox) terrainBlock.getBound();
// Vertex positions for the block
FloatBuffer positions = terrainBlock.getFloatBuffer(Type.Position);
// Prepare to cast rays
Vector3f pos = new Vector3f();
Vector3f dir = new Vector3f(0, -1, 0);
Ray ray = new Ray(pos, dir);
// Prepare collision results
CollisionResults results = new CollisionResults();
// Set the LOD indices on the block
VertexBuffer originalIndices = terrainBlock.getBuffer(Type.Index);
terrainBlock.clearBuffer(Type.Index);
if (lodIndices instanceof IntBuffer)
terrainBlock.setBuffer(Type.Index, 3, (IntBuffer) lodIndices);
else if (lodIndices instanceof ShortBuffer) {
terrainBlock.setBuffer(Type.Index, 3, (ShortBuffer) lodIndices);
}
// Recalculate collision mesh
terrainBlock.createCollisionData();
float entropy = 0;
for (int i = 0; i < positions.limit() / 3; i++) {
BufferUtils.populateFromBuffer(pos, positions, i);
float realHeight = pos.y;
pos.addLocal(0, bbox.getYExtent(), 0);
ray.setOrigin(pos);
results.clear();
terrainBlock.collideWith(ray, Matrix4f.IDENTITY, bbox, results);
if (results.size() > 0) {
Vector3f contactPoint = results.getClosestCollision().getContactPoint();
float delta = Math.abs(realHeight - contactPoint.y);
entropy = Math.max(delta, entropy);
}
}
// Restore original indices
terrainBlock.clearBuffer(Type.Index);
terrainBlock.setBuffer(originalIndices);
return entropy;
}
use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.
the class TerrainQuad method needToRecalculateNormals.
protected boolean needToRecalculateNormals() {
if (affectedAreaBBox != null)
return true;
if (!lastScale.equals(getWorldScale())) {
affectedAreaBBox = new BoundingBox(getWorldTranslation(), Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE);
lastScale = getWorldScale();
return true;
}
return false;
}
use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.
the class BoundingCollisionTest method testBoxRayCollision.
@Test
public void testBoxRayCollision() {
BoundingBox box = new BoundingBox(Vector3f.ZERO, 1, 1, 1);
Ray ray = new Ray(Vector3f.ZERO, Vector3f.UNIT_Z);
// XXX: seems incorrect, ray inside box should only generate
// one result...
checkCollision(box, ray, 2);
ray.setOrigin(new Vector3f(0, 0, -5));
checkCollision(box, ray, 2);
// XXX: is this right? the ray origin is on the box's side..
ray.setOrigin(new Vector3f(0, 0, 2f));
checkCollision(box, ray, 0);
ray.setOrigin(new Vector3f(0, 0, -2f));
checkCollision(box, ray, 2);
// parallel to the edge, touching the side
ray.setOrigin(new Vector3f(0, 1f, -2f));
checkCollision(box, ray, 2);
// still parallel, but not touching the side
ray.setOrigin(new Vector3f(0, 1f + FastMath.ZERO_TOLERANCE, -2f));
checkCollision(box, ray, 0);
}
use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.
the class BoundingCollisionTest method testBoxSphereCollision.
@Test
public void testBoxSphereCollision() {
BoundingBox box1 = new BoundingBox(Vector3f.ZERO, 1, 1, 1);
BoundingSphere sphere2 = new BoundingSphere(1, Vector3f.ZERO);
checkCollision(box1, sphere2, 1);
// Put it at the very edge - for sphere vs. box, it will not intersect
sphere2.setCenter(new Vector3f(2f, 0f, 0f));
checkCollision(box1, sphere2, 0);
// Put it a wee bit closer - should intersect.
sphere2.setCenter(new Vector3f(2f - FastMath.ZERO_TOLERANCE, 0, 0));
checkCollision(box1, sphere2, 1);
// Test if the algorithm converts the sphere
// to a box before testing the collision (incorrect)
float sqrt3 = FastMath.sqrt(3);
sphere2.setCenter(Vector3f.UNIT_XYZ.mult(2));
sphere2.setRadius(sqrt3);
checkCollision(box1, sphere2, 0);
// Make it a wee bit larger.
sphere2.setRadius(sqrt3 + FastMath.ZERO_TOLERANCE);
checkCollision(box1, sphere2, 1);
}
use of com.jme3.bounding.BoundingBox in project jmonkeyengine by jMonkeyEngine.
the class GeneratedTexture method triangulate.
/**
* This method triangulates the texture. In the result we get a set of small
* flat textures for each face of the given mesh. This can be later merged
* into one flat texture.
*
* @param mesh
* the mesh we create the texture for
* @param geometriesOMA
* the old memory address of the geometries group that the given
* mesh belongs to (required for bounding box calculations)
* @param coordinatesType
* the types of UV coordinates
* @param blenderContext
* the blender context
* @return triangulated texture
*/
public TriangulatedTexture triangulate(Mesh mesh, Long geometriesOMA, UVCoordinatesType coordinatesType, BlenderContext blenderContext) {
TemporalMesh geometries = (TemporalMesh) blenderContext.getLoadedFeature(geometriesOMA, LoadedDataType.TEMPORAL_MESH);
int[] coordinatesSwappingIndexes = new int[] { ((Number) mTex.getFieldValue("projx")).intValue(), ((Number) mTex.getFieldValue("projy")).intValue(), ((Number) mTex.getFieldValue("projz")).intValue() };
List<Vector3f> uvs = UVCoordinatesGenerator.generateUVCoordinatesFor3DTexture(mesh, coordinatesType, coordinatesSwappingIndexes, geometries);
Vector3f[] uvsArray = uvs.toArray(new Vector3f[uvs.size()]);
BoundingBox boundingBox = UVCoordinatesGenerator.getBoundingBox(geometries);
Set<TriangleTextureElement> triangleTextureElements = new TreeSet<TriangleTextureElement>(new Comparator<TriangleTextureElement>() {
public int compare(TriangleTextureElement o1, TriangleTextureElement o2) {
return o1.faceIndex - o2.faceIndex;
}
});
int[] indices = new int[3];
for (int i = 0; i < mesh.getTriangleCount(); ++i) {
mesh.getTriangle(i, indices);
triangleTextureElements.add(new TriangleTextureElement(i, boundingBox, this, uvsArray, indices, blenderContext));
}
return new TriangulatedTexture(triangleTextureElements, blenderContext);
}
Aggregations