Search in sources :

Example 16 with BoundingSphere

use of com.jme3.bounding.BoundingSphere in project jmonkeyengine by jMonkeyEngine.

the class LodGenerator method build.

private void build() {
    BoundingSphere bs = new BoundingSphere();
    bs.computeFromPoints(mesh.getFloatBuffer(VertexBuffer.Type.Position));
    meshBoundingSphereRadius = bs.getRadius();
    List<Vertex> vertexLookup = new ArrayList<Vertex>();
    initialize();
    gatherVertexData(mesh, vertexLookup);
    gatherIndexData(mesh, vertexLookup);
    computeCosts();
// assert (assertValidMesh());
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) ArrayList(java.util.ArrayList)

Example 17 with BoundingSphere

use of com.jme3.bounding.BoundingSphere in project TeachingInSimulation by ScOrPiOzzy.

the class TestPBRLighting method simpleUpdate.

@Override
public void simpleUpdate(float tpf) {
    frame++;
    if (frame == 2) {
        // modelNode.removeFromParent();
        final LightProbe probe = LightProbeFactory.makeProbe(stateManager.getState(EnvironmentCamera.class), rootNode);
        ((BoundingSphere) probe.getBounds()).setRadius(100);
        rootNode.addLight(probe);
    // getStateManager().getState(EnvironmentManager.class).addEnvProbe(probe);
    }
// if (frame > 10 && modelNode.getParent() == null) {
// rootNode.attachChild(modelNode);
// }
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) LightProbe(com.jme3.light.LightProbe) EnvironmentCamera(com.jme3.environment.EnvironmentCamera)

Example 18 with BoundingSphere

use of com.jme3.bounding.BoundingSphere in project jmonkeyengine by jMonkeyEngine.

the class BoundingSphere method merge.

//    /**
//     * Merges this sphere with the given OBB.
//     *
//     * @param volume
//     *            The OBB to merge.
//     * @return This sphere, after merging.
//     */
//    private BoundingSphere mergeOBB(OrientedBoundingBox volume) {
//        // compute edge points from the obb
//        if (!volume.correctCorners)
//            volume.computeCorners();
//        _mergeBuf.rewind();
//        for (int i = 0; i < 8; i++) {
//            _mergeBuf.put(volume.vectorStore[i].x);
//            _mergeBuf.put(volume.vectorStore[i].y);
//            _mergeBuf.put(volume.vectorStore[i].z);
//        }
//
//        // remember old radius and center
//        float oldRadius = radius;
//        Vector3f oldCenter = _compVect2.set( center );
//
//        // compute new radius and center from obb points
//        computeFromPoints(_mergeBuf);
//        Vector3f newCenter = _compVect3.set( center );
//        float newRadius = radius;
//
//        // restore old center and radius
//        center.set( oldCenter );
//        radius = oldRadius;
//
//        //merge obb points result
//        merge( newRadius, newCenter, this );
//
//        return this;
//    }
private BoundingVolume merge(float temp_radius, Vector3f temp_center, BoundingSphere rVal) {
    TempVars vars = TempVars.get();
    Vector3f diff = temp_center.subtract(center, vars.vect1);
    float lengthSquared = diff.lengthSquared();
    float radiusDiff = temp_radius - radius;
    float fRDiffSqr = radiusDiff * radiusDiff;
    if (fRDiffSqr >= lengthSquared) {
        if (radiusDiff <= 0.0f) {
            vars.release();
            return this;
        }
        Vector3f rCenter = rVal.center;
        if (rCenter == null) {
            rVal.setCenter(rCenter = new Vector3f());
        }
        rCenter.set(temp_center);
        rVal.setRadius(temp_radius);
        vars.release();
        return rVal;
    }
    float length = (float) Math.sqrt(lengthSquared);
    Vector3f rCenter = rVal.center;
    if (rCenter == null) {
        rVal.setCenter(rCenter = new Vector3f());
    }
    if (length > RADIUS_EPSILON) {
        float coeff = (length + radiusDiff) / (2.0f * length);
        rCenter.set(center.addLocal(diff.multLocal(coeff)));
    } else {
        rCenter.set(center);
    }
    rVal.setRadius(0.5f * (length + radius + temp_radius));
    vars.release();
    return rVal;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 19 with BoundingSphere

use of com.jme3.bounding.BoundingSphere in project jmonkeyengine by jMonkeyEngine.

the class Intersection method intersect.

public static boolean intersect(BoundingSphere sphere, Vector3f center, float radius) {
    assert Vector3f.isValidVector(center) && Vector3f.isValidVector(sphere.center);
    TempVars vars = TempVars.get();
    try {
        Vector3f diff = center.subtract(sphere.center, vars.vect1);
        float rsum = sphere.getRadius() + radius;
        return (diff.dot(diff) <= rsum * rsum);
    } finally {
        vars.release();
    }
}
Also used : Vector3f(com.jme3.math.Vector3f) TempVars(com.jme3.util.TempVars)

Example 20 with BoundingSphere

use of com.jme3.bounding.BoundingSphere in project jmonkeyengine by jMonkeyEngine.

the class SinglePassAndImageBasedLightingLogic method updateLightListUniforms.

/**
     * Uploads the lights in the light list as two uniform arrays.<br/><br/> *
     * <p>
     * <code>uniform vec4 g_LightColor[numLights];</code><br/> //
     * g_LightColor.rgb is the diffuse/specular color of the light.<br/> //
     * g_Lightcolor.a is the type of light, 0 = Directional, 1 = Point, <br/> //
     * 2 = Spot. <br/> <br/>
     * <code>uniform vec4 g_LightPosition[numLights];</code><br/> //
     * g_LightPosition.xyz is the position of the light (for point lights)<br/>
     * // or the direction of the light (for directional lights).<br/> //
     * g_LightPosition.w is the inverse radius (1/r) of the light (for
     * attenuation) <br/> </p>
     */
protected int updateLightListUniforms(Shader shader, Geometry g, LightList lightList, int numLights, RenderManager rm, int startIndex, int lastTexUnit) {
    if (numLights == 0) {
        // this shader does not do lighting, ignore.
        return 0;
    }
    Uniform lightData = shader.getUniform("g_LightData");
    //8 lights * max 3
    lightData.setVector4Length(numLights * 3);
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    Uniform lightProbeData = shader.getUniform("g_LightProbeData");
    lightProbeData.setVector4Length(1);
    Uniform lightProbeIrrMap = shader.getUniform("g_IrradianceMap");
    Uniform lightProbePemMap = shader.getUniform("g_PrefEnvMap");
    lightProbe = null;
    if (startIndex != 0) {
        // apply additive blending for 2nd and future passes
        rm.getRenderer().applyRenderState(ADDITIVE_LIGHT);
        ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
    } else {
        lightProbe = extractIndirectLights(lightList, true);
        ambientColor.setValue(VarType.Vector4, ambientLightColor);
    }
    //If there is a lightProbe in the list we force it's render on the first pass
    if (lightProbe != null) {
        BoundingSphere s = (BoundingSphere) lightProbe.getBounds();
        lightProbeData.setVector4InArray(lightProbe.getPosition().x, lightProbe.getPosition().y, lightProbe.getPosition().z, 1f / s.getRadius(), 0);
        //assigning new texture indexes
        int irrUnit = lastTexUnit++;
        int pemUnit = lastTexUnit++;
        rm.getRenderer().setTexture(irrUnit, lightProbe.getIrradianceMap());
        lightProbeIrrMap.setValue(VarType.Int, irrUnit);
        rm.getRenderer().setTexture(pemUnit, lightProbe.getPrefilteredEnvMap());
        lightProbePemMap.setValue(VarType.Int, pemUnit);
    } else {
        //Disable IBL for this pass
        lightProbeData.setVector4InArray(0, 0, 0, -1, 0);
    }
    int lightDataIndex = 0;
    TempVars vars = TempVars.get();
    Vector4f tmpVec = vars.vect4f1;
    int curIndex;
    int endIndex = numLights + startIndex;
    for (curIndex = startIndex; curIndex < endIndex && curIndex < lightList.size(); curIndex++) {
        Light l = lightList.get(curIndex);
        if (l.getType() == Light.Type.Ambient) {
            endIndex++;
            continue;
        }
        ColorRGBA color = l.getColor();
        if (l.getType() != Light.Type.Probe) {
            lightData.setVector4InArray(color.getRed(), color.getGreen(), color.getBlue(), l.getType().getId(), lightDataIndex);
            lightDataIndex++;
        }
        switch(l.getType()) {
            case Directional:
                DirectionalLight dl = (DirectionalLight) l;
                Vector3f dir = dl.getDirection();
                //Data directly sent in view space to avoid a matrix mult for each pixel
                tmpVec.set(dir.getX(), dir.getY(), dir.getZ(), 0.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), -1, lightDataIndex);
                lightDataIndex++;
                //PADDING
                lightData.setVector4InArray(0, 0, 0, 0, lightDataIndex);
                lightDataIndex++;
                break;
            case Point:
                PointLight pl = (PointLight) l;
                Vector3f pos = pl.getPosition();
                float invRadius = pl.getInvRadius();
                tmpVec.set(pos.getX(), pos.getY(), pos.getZ(), 1.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), invRadius, lightDataIndex);
                lightDataIndex++;
                //PADDING
                lightData.setVector4InArray(0, 0, 0, 0, lightDataIndex);
                lightDataIndex++;
                break;
            case Spot:
                SpotLight sl = (SpotLight) l;
                Vector3f pos2 = sl.getPosition();
                Vector3f dir2 = sl.getDirection();
                float invRange = sl.getInvSpotRange();
                float spotAngleCos = sl.getPackedAngleCos();
                tmpVec.set(pos2.getX(), pos2.getY(), pos2.getZ(), 1.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), invRange, lightDataIndex);
                lightDataIndex++;
                tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0.0f);
                lightData.setVector4InArray(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos, lightDataIndex);
                lightDataIndex++;
                break;
            default:
                throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
        }
    }
    vars.release();
    //Padding of unsued buffer space
    while (lightDataIndex < numLights * 3) {
        lightData.setVector4InArray(0f, 0f, 0f, 0f, lightDataIndex);
        lightDataIndex++;
    }
    return curIndex;
}
Also used : BoundingSphere(com.jme3.bounding.BoundingSphere) TempVars(com.jme3.util.TempVars)

Aggregations

BoundingSphere (com.jme3.bounding.BoundingSphere)24 Vector3f (com.jme3.math.Vector3f)9 BoundingBox (com.jme3.bounding.BoundingBox)8 TempVars (com.jme3.util.TempVars)7 Test (org.junit.Test)7 BoundingVolume (com.jme3.bounding.BoundingVolume)5 Geometry (com.jme3.scene.Geometry)5 LightProbe (com.jme3.light.LightProbe)4 EnvironmentCamera (com.jme3.environment.EnvironmentCamera)3 Material (com.jme3.material.Material)3 Node (com.jme3.scene.Node)2 ArrayList (java.util.ArrayList)2 UnsupportedCollisionException (com.jme3.collision.UnsupportedCollisionException)1 InputCapsule (com.jme3.export.InputCapsule)1 OutputCapsule (com.jme3.export.OutputCapsule)1 Light (com.jme3.light.Light)1 Triangle (com.jme3.math.Triangle)1 Vector2f (com.jme3.math.Vector2f)1 Mesh (com.jme3.scene.Mesh)1 Spatial (com.jme3.scene.Spatial)1