Search in sources :

Example 11 with BoundingBox

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;
}
Also used : CollisionResults(com.jme3.collision.CollisionResults) VertexBuffer(com.jme3.scene.VertexBuffer) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer) Ray(com.jme3.math.Ray) ShortBuffer(java.nio.ShortBuffer)

Example 12 with BoundingBox

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;
}
Also used : BoundingBox(com.jme3.bounding.BoundingBox)

Example 13 with BoundingBox

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);
}
Also used : BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) Ray(com.jme3.math.Ray) Test(org.junit.Test)

Example 14 with BoundingBox

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);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) BoundingBox(com.jme3.bounding.BoundingBox) Vector3f(com.jme3.math.Vector3f) Test(org.junit.Test)

Example 15 with BoundingBox

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);
}
Also used : TriangleTextureElement(com.jme3.scene.plugins.blender.textures.TriangulatedTexture.TriangleTextureElement) TemporalMesh(com.jme3.scene.plugins.blender.meshes.TemporalMesh) TreeSet(java.util.TreeSet) Vector3f(com.jme3.math.Vector3f) BoundingBox(com.jme3.bounding.BoundingBox)

Aggregations

BoundingBox (com.jme3.bounding.BoundingBox)42 Vector3f (com.jme3.math.Vector3f)35 Geometry (com.jme3.scene.Geometry)14 TempVars (com.jme3.util.TempVars)14 BoundingVolume (com.jme3.bounding.BoundingVolume)10 BoundingSphere (com.jme3.bounding.BoundingSphere)8 Material (com.jme3.material.Material)7 Vector2f (com.jme3.math.Vector2f)4 Texture (com.jme3.texture.Texture)4 Test (org.junit.Test)4 DirectionalLight (com.jme3.light.DirectionalLight)3 Matrix4f (com.jme3.math.Matrix4f)3 Ray (com.jme3.math.Ray)3 Node (com.jme3.scene.Node)3 Spatial (com.jme3.scene.Spatial)3 TerrainLodControl (com.jme3.terrain.geomipmap.TerrainLodControl)3 TerrainQuad (com.jme3.terrain.geomipmap.TerrainQuad)3 DistanceLodCalculator (com.jme3.terrain.geomipmap.lodcalc.DistanceLodCalculator)3 AbstractHeightMap (com.jme3.terrain.heightmap.AbstractHeightMap)3 ImageBasedHeightMap (com.jme3.terrain.heightmap.ImageBasedHeightMap)3