Search in sources :

Example 81 with Sphere

use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.

the class EnvMapUtils method getSphericalHarmonicsCoefficents.

/**
     * Returns the Spherical Harmonics coefficients for this cube map.
     *
     * The method used is the one from this article :
     * http://graphics.stanford.edu/papers/envmap/envmap.pdf
     *
     * Also good resources on spherical harmonics
     * http://dickyjim.wordpress.com/2013/09/04/spherical-harmonics-for-beginners/
     *
     * @param cubeMap the environment cube map to compute SH for
     * @param fixSeamsMethod method to fix seams when computing the SH
     * coefficients
     * @return an array of 9 vector3f representing thos coefficients for each
     * r,g,b channnel
     */
public static Vector3f[] getSphericalHarmonicsCoefficents(TextureCubeMap cubeMap, FixSeamsMethod fixSeamsMethod) {
    Vector3f[] shCoef = new Vector3f[NUM_SH_COEFFICIENT];
    float[] shDir = new float[9];
    float weightAccum = 0.0f;
    float weight;
    if (cubeMap.getImage().getData(0) == null) {
        throw new IllegalStateException("The cube map must contain Efficient data, if you rendered the cube map on the GPU plase use renderer.readFrameBuffer, to create a CPU image");
    }
    int width = cubeMap.getImage().getWidth();
    int height = cubeMap.getImage().getHeight();
    Vector3f texelVect = new Vector3f();
    ColorRGBA color = new ColorRGBA();
    CubeMapWrapper envMapReader = new CubeMapWrapper(cubeMap);
    for (int face = 0; face < 6; face++) {
        for (int y = 0; y < height; y++) {
            for (int x = 0; x < width; x++) {
                weight = getSolidAngleAndVector(x, y, width, face, texelVect, fixSeamsMethod);
                evalShBasis(texelVect, shDir);
                envMapReader.getPixel(x, y, face, color);
                for (int i = 0; i < NUM_SH_COEFFICIENT; i++) {
                    if (shCoef[i] == null) {
                        shCoef[i] = new Vector3f();
                    }
                    shCoef[i].setX(shCoef[i].x + color.r * shDir[i] * weight);
                    shCoef[i].setY(shCoef[i].y + color.g * shDir[i] * weight);
                    shCoef[i].setZ(shCoef[i].z + color.b * shDir[i] * weight);
                }
                weightAccum += weight;
            }
        }
    }
    /* Normalization - The sum of solid angle should be equal to the solid angle of the sphere (4 PI), so
         * normalize in order our weightAccum exactly match 4 PI. */
    for (int i = 0; i < NUM_SH_COEFFICIENT; ++i) {
        shCoef[i].multLocal(4.0f * PI / weightAccum);
    }
    return shCoef;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) Vector3f(com.jme3.math.Vector3f)

Example 82 with Sphere

use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.

the class BIHTree method collideWithBoundingVolume.

private int collideWithBoundingVolume(BoundingVolume bv, Matrix4f worldMatrix, CollisionResults results) {
    BoundingBox bbox;
    if (bv instanceof BoundingSphere) {
        BoundingSphere sphere = (BoundingSphere) bv;
        bbox = new BoundingBox(bv.getCenter().clone(), sphere.getRadius(), sphere.getRadius(), sphere.getRadius());
    } else if (bv instanceof BoundingBox) {
        bbox = new BoundingBox((BoundingBox) bv);
    } else {
        throw new UnsupportedCollisionException("BoundingVolume:" + bv);
    }
    bbox.transform(worldMatrix.invert(), bbox);
    return root.intersectWhere(bv, bbox, worldMatrix, this, results);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) BoundingBox(com.jme3.bounding.BoundingBox) UnsupportedCollisionException(com.jme3.collision.UnsupportedCollisionException)

Example 83 with Sphere

use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.

the class BoundingCollisionTest method testSphereTriangleCollision.

@Test
public void testSphereTriangleCollision() {
    BoundingSphere sphere = new BoundingSphere(1, Vector3f.ZERO);
    Geometry geom = new Geometry("geom", new Quad(1, 1));
    checkCollision(sphere, geom, 2);
    // The box touches the edges of the triangles.
    sphere.setCenter(new Vector3f(-1f + FastMath.ZERO_TOLERANCE, 0, 0));
    checkCollision(sphere, geom, 2);
    // Move it slightly farther..
    sphere.setCenter(new Vector3f(-1f - FastMath.ZERO_TOLERANCE, 0, 0));
    checkCollision(sphere, geom, 0);
    // Parallel triangle / box side, touching
    sphere.setCenter(new Vector3f(0, 0, -1f));
    checkCollision(sphere, geom, 2);
    // Not touching
    sphere.setCenter(new Vector3f(0, 0, -1f - FastMath.ZERO_TOLERANCE));
    checkCollision(sphere, geom, 0);
    // Test collisions only against one of the triangles
    sphere.setCenter(new Vector3f(-0.9f, 1.2f, 0f));
    checkCollision(sphere, geom, 1);
    sphere.setCenter(new Vector3f(1.2f, -0.9f, 0f));
    checkCollision(sphere, geom, 1);
}
Also used : Geometry(com.jme3.scene.Geometry) Quad(com.jme3.scene.shape.Quad) BoundingSphere(com.jme3.bounding.BoundingSphere) Vector3f(com.jme3.math.Vector3f) Test(org.junit.Test)

Example 84 with Sphere

use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.

the class LightFilterTest method testAmbientFiltering.

@Test
public void testAmbientFiltering() {
    geom.addLight(new AmbientLight());
    // Ambient lights must never be filtered
    checkFilteredLights(1);
    // Test for bounding Sphere
    geom.setModelBound(new BoundingSphere(0.5f, Vector3f.ZERO));
    // Ambient lights must never be filtered
    checkFilteredLights(1);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) Test(org.junit.Test)

Example 85 with Sphere

use of com.jme3.scene.shape.Sphere in project jmonkeyengine by jMonkeyEngine.

the class LightFilterTest method testPointFiltering.

@Test
public void testPointFiltering() {
    PointLight pl = new PointLight(Vector3f.ZERO);
    geom.addLight(pl);
    // Infinite point lights must never be filtered
    checkFilteredLights(1);
    // Light at origin does not intersect geom which is at Z=10
    pl.setRadius(1);
    checkFilteredLights(0);
    // Put it closer to geom, the very edge of the sphere touches the box.
    // Still not considered an intersection though.
    pl.setPosition(new Vector3f(0, 0, 8f));
    checkFilteredLights(0);
    // And more close - now its an intersection.
    pl.setPosition(new Vector3f(0, 0, 8f + FastMath.ZERO_TOLERANCE));
    checkFilteredLights(1);
    // Move the geometry away
    geom.move(0, 0, FastMath.ZERO_TOLERANCE);
    checkFilteredLights(0);
    // Test if the algorithm converts the sphere 
    // to a box before testing the collision (incorrect)
    float sqrt3 = FastMath.sqrt(3);
    pl.setPosition(new Vector3f(2, 2, 8));
    pl.setRadius(sqrt3);
    checkFilteredLights(0);
    // Make it a wee bit larger.
    pl.setRadius(sqrt3 + FastMath.ZERO_TOLERANCE);
    checkFilteredLights(1);
    // Rotate the camera so it is up, light is outside frustum.
    cam.lookAtDirection(Vector3f.UNIT_Y, Vector3f.UNIT_Y);
    checkFilteredLights(0);
    // ==================================
    // Tests for bounding Sphere
    geom.setModelBound(new BoundingSphere(1f, Vector3f.ZERO));
    geom.setLocalTranslation(0, 0, 2);
    pl.setPosition(new Vector3f(0, 0, 2f));
    // Infinite point lights must never be filtered
    pl.setRadius(0);
    checkFilteredLights(1);
    pl.setRadius(1f);
    // Put the light at the very close to the geom,
    // the very edge of the sphere touches the other bounding sphere
    // Still not considered an intersection though.
    pl.setPosition(new Vector3f(0, 0, 0));
    checkFilteredLights(0);
    // And more close - now its an intersection.
    pl.setPosition(new Vector3f(0, 0, 0f + FastMath.ZERO_TOLERANCE));
    checkFilteredLights(1);
    geom.setLocalTranslation(0, 0, 0);
    // In this case its an intersection for pointLight v. box
    // But not for pointLight v. sphere
    // Vector3f(0, 0.5f, 0.5f).normalize().mult(2) ~ >= (0.0, 1.4142135, 1.4142135)
    //pl.setPosition(new Vector3f(0, 0.5f, 0.5f).normalizeLocal().multLocal(2 + FastMath.ZERO_TOLERANCE));
    pl.setPosition(new Vector3f(0f, 1.4142135f, 1.4142135f).multLocal(1 + FastMath.ZERO_TOLERANCE));
    checkFilteredLights(0);
    // Make the distance a wee bit closer, now its an intersection
    //pl.setPosition(new Vector3f(0, 0.5f, 0.5f).normalizeLocal().multLocal(2 - FastMath.ZERO_TOLERANCE));
    pl.setPosition(new Vector3f(0f, 1.4142135f, 1.4142135f).multLocal(1 - FastMath.ZERO_TOLERANCE));
    checkFilteredLights(1);
    // it's a point light, also test for the other corner
    pl.setPosition(new Vector3f(0f, -1.4142135f, -1.4142135f).multLocal(1 - FastMath.ZERO_TOLERANCE));
    checkFilteredLights(0);
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) Vector3f(com.jme3.math.Vector3f) Test(org.junit.Test)

Aggregations

Sphere (com.jme3.scene.shape.Sphere)63 Geometry (com.jme3.scene.Geometry)58 Vector3f (com.jme3.math.Vector3f)57 Material (com.jme3.material.Material)46 DirectionalLight (com.jme3.light.DirectionalLight)23 Box (com.jme3.scene.shape.Box)22 RigidBodyControl (com.jme3.bullet.control.RigidBodyControl)17 Node (com.jme3.scene.Node)17 PointLight (com.jme3.light.PointLight)15 BulletAppState (com.jme3.bullet.BulletAppState)13 SphereCollisionShape (com.jme3.bullet.collision.shapes.SphereCollisionShape)13 BoundingSphere (com.jme3.bounding.BoundingSphere)12 AmbientLight (com.jme3.light.AmbientLight)12 Quaternion (com.jme3.math.Quaternion)11 ColorRGBA (com.jme3.math.ColorRGBA)10 Spatial (com.jme3.scene.Spatial)9 TempVars (com.jme3.util.TempVars)9 KeyTrigger (com.jme3.input.controls.KeyTrigger)8 Vector2f (com.jme3.math.Vector2f)8 MeshCollisionShape (com.jme3.bullet.collision.shapes.MeshCollisionShape)7