Search in sources :

Example 16 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class Spatial method rotateUpTo.

/**
     * <code>rotateUpTo</code> is a utility function that alters the
     * local rotation to point the Y axis in the direction given by newUp.
     *
     * @param newUp
     *            the up vector to use - assumed to be a unit vector.
     */
public void rotateUpTo(Vector3f newUp) {
    TempVars vars = TempVars.get();
    Vector3f compVecA = vars.vect1;
    Quaternion q = vars.quat1;
    // First figure out the current up vector.
    Vector3f upY = compVecA.set(Vector3f.UNIT_Y);
    Quaternion rot = localTransform.getRotation();
    rot.multLocal(upY);
    // get angle between vectors
    float angle = upY.angleBetween(newUp);
    // figure out rotation axis by taking cross product
    Vector3f rotAxis = upY.crossLocal(newUp).normalizeLocal();
    // Build a rotation quat and apply current local rotation.
    q.fromAngleNormalAxis(angle, rotAxis);
    q.mult(rot, rot);
    vars.release();
    setTransformRefresh();
}
Also used : TempVars(com.jme3.util.TempVars)

Example 17 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class MultiPassLightingLogic method render.

@Override
public void render(RenderManager renderManager, Shader shader, Geometry geometry, LightList lights, int lastTexUnit) {
    Renderer r = renderManager.getRenderer();
    Uniform lightDir = shader.getUniform("g_LightDirection");
    Uniform lightColor = shader.getUniform("g_LightColor");
    Uniform lightPos = shader.getUniform("g_LightPosition");
    Uniform ambientColor = shader.getUniform("g_AmbientLightColor");
    boolean isFirstLight = true;
    boolean isSecondLight = false;
    getAmbientColor(lights, false, ambientLightColor);
    for (int i = 0; i < lights.size(); i++) {
        Light l = lights.get(i);
        if (l instanceof AmbientLight) {
            continue;
        }
        if (isFirstLight) {
            // set ambient color for first light only
            ambientColor.setValue(VarType.Vector4, ambientLightColor);
            isFirstLight = false;
            isSecondLight = true;
        } else if (isSecondLight) {
            ambientColor.setValue(VarType.Vector4, ColorRGBA.Black);
            // apply additive blending for 2nd and future lights
            r.applyRenderState(ADDITIVE_LIGHT);
            isSecondLight = false;
        }
        TempVars vars = TempVars.get();
        Quaternion tmpLightDirection = vars.quat1;
        Quaternion tmpLightPosition = vars.quat2;
        ColorRGBA tmpLightColor = vars.color;
        Vector4f tmpVec = vars.vect4f1;
        ColorRGBA color = l.getColor();
        tmpLightColor.set(color);
        tmpLightColor.a = l.getType().getId();
        lightColor.setValue(VarType.Vector4, tmpLightColor);
        switch(l.getType()) {
            case Directional:
                DirectionalLight dl = (DirectionalLight) l;
                Vector3f dir = dl.getDirection();
                //FIXME : there is an inconstency here due to backward
                //compatibility of the lighting shader.
                //The directional light direction is passed in the
                //LightPosition uniform. The lighting shader needs to be
                //reworked though in order to fix this.
                tmpLightPosition.set(dir.getX(), dir.getY(), dir.getZ(), -1);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                tmpLightDirection.set(0, 0, 0, 0);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Point:
                PointLight pl = (PointLight) l;
                Vector3f pos = pl.getPosition();
                float invRadius = pl.getInvRadius();
                tmpLightPosition.set(pos.getX(), pos.getY(), pos.getZ(), invRadius);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                tmpLightDirection.set(0, 0, 0, 0);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Spot:
                SpotLight sl = (SpotLight) l;
                Vector3f pos2 = sl.getPosition();
                Vector3f dir2 = sl.getDirection();
                float invRange = sl.getInvSpotRange();
                float spotAngleCos = sl.getPackedAngleCos();
                tmpLightPosition.set(pos2.getX(), pos2.getY(), pos2.getZ(), invRange);
                lightPos.setValue(VarType.Vector4, tmpLightPosition);
                //We transform the spot direction in view space here to save 5 varying later in the lighting shader
                //one vec4 less and a vec4 that becomes a vec3
                //the downside is that spotAngleCos decoding happens now in the frag shader.
                tmpVec.set(dir2.getX(), dir2.getY(), dir2.getZ(), 0);
                renderManager.getCurrentCamera().getViewMatrix().mult(tmpVec, tmpVec);
                tmpLightDirection.set(tmpVec.getX(), tmpVec.getY(), tmpVec.getZ(), spotAngleCos);
                lightDir.setValue(VarType.Vector4, tmpLightDirection);
                break;
            case Probe:
                break;
            default:
                throw new UnsupportedOperationException("Unknown type of light: " + l.getType());
        }
        vars.release();
        r.setShader(shader);
        renderMeshFromGeometry(r, geometry);
    }
    if (isFirstLight) {
        // Either there are no lights at all, or only ambient lights.
        // Render a dummy "normal light" so we can see the ambient color.
        ambientColor.setValue(VarType.Vector4, getAmbientColor(lights, false, ambientLightColor));
        lightColor.setValue(VarType.Vector4, ColorRGBA.BlackNoAlpha);
        lightPos.setValue(VarType.Vector4, NULL_DIR_LIGHT);
        r.setShader(shader);
        renderMeshFromGeometry(r, geometry);
    }
}
Also used : Quaternion(com.jme3.math.Quaternion) Uniform(com.jme3.shader.Uniform) TempVars(com.jme3.util.TempVars) SpotLight(com.jme3.light.SpotLight) ColorRGBA(com.jme3.math.ColorRGBA) Vector4f(com.jme3.math.Vector4f) DirectionalLight(com.jme3.light.DirectionalLight) SpotLight(com.jme3.light.SpotLight) Light(com.jme3.light.Light) PointLight(com.jme3.light.PointLight) AmbientLight(com.jme3.light.AmbientLight) DirectionalLight(com.jme3.light.DirectionalLight) Vector3f(com.jme3.math.Vector3f) Renderer(com.jme3.renderer.Renderer) PointLight(com.jme3.light.PointLight) AmbientLight(com.jme3.light.AmbientLight)

Example 18 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class LineSegment method distanceSquared.

public float distanceSquared(LineSegment test) {
    TempVars vars = TempVars.get();
    Vector3f compVec1 = vars.vect1;
    origin.subtract(test.getOrigin(), compVec1);
    float negativeDirectionDot = -(direction.dot(test.getDirection()));
    float diffThisDot = compVec1.dot(direction);
    float diffTestDot = -(compVec1.dot(test.getDirection()));
    float lengthOfDiff = compVec1.lengthSquared();
    vars.release();
    float determinant = FastMath.abs(1.0f - negativeDirectionDot * negativeDirectionDot);
    float s0, s1, squareDistance, extentDeterminant0, extentDeterminant1, tempS0, tempS1;
    if (determinant >= FastMath.FLT_EPSILON) {
        // segments are not parallel
        s0 = negativeDirectionDot * diffTestDot - diffThisDot;
        s1 = negativeDirectionDot * diffThisDot - diffTestDot;
        extentDeterminant0 = extent * determinant;
        extentDeterminant1 = test.getExtent() * determinant;
        if (s0 >= -extentDeterminant0) {
            if (s0 <= extentDeterminant0) {
                if (s1 >= -extentDeterminant1) {
                    if (// region 0 (interior)
                    s1 <= extentDeterminant1) {
                        // minimum at two interior points of 3D lines
                        float inverseDeterminant = ((float) 1.0) / determinant;
                        s0 *= inverseDeterminant;
                        s1 *= inverseDeterminant;
                        squareDistance = s0 * (s0 + negativeDirectionDot * s1 + (2.0f) * diffThisDot) + s1 * (negativeDirectionDot * s0 + s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                    } else // region 3 (side)
                    {
                        s1 = test.getExtent();
                        tempS0 = -(negativeDirectionDot * s1 + diffThisDot);
                        if (tempS0 < -extent) {
                            s0 = -extent;
                            squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                        } else if (tempS0 <= extent) {
                            s0 = tempS0;
                            squareDistance = -s0 * s0 + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                        } else {
                            s0 = extent;
                            squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                        }
                    }
                } else // region 7 (side)
                {
                    s1 = -test.getExtent();
                    tempS0 = -(negativeDirectionDot * s1 + diffThisDot);
                    if (tempS0 < -extent) {
                        s0 = -extent;
                        squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                    } else if (tempS0 <= extent) {
                        s0 = tempS0;
                        squareDistance = -s0 * s0 + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                    } else {
                        s0 = extent;
                        squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                    }
                }
            } else {
                if (s1 >= -extentDeterminant1) {
                    if (// region 1 (side)
                    s1 <= extentDeterminant1) {
                        s0 = extent;
                        tempS1 = -(negativeDirectionDot * s0 + diffTestDot);
                        if (tempS1 < -test.getExtent()) {
                            s1 = -test.getExtent();
                            squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                        } else if (tempS1 <= test.getExtent()) {
                            s1 = tempS1;
                            squareDistance = -s1 * s1 + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                        } else {
                            s1 = test.getExtent();
                            squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                        }
                    } else // region 2 (corner)
                    {
                        s1 = test.getExtent();
                        tempS0 = -(negativeDirectionDot * s1 + diffThisDot);
                        if (tempS0 < -extent) {
                            s0 = -extent;
                            squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                        } else if (tempS0 <= extent) {
                            s0 = tempS0;
                            squareDistance = -s0 * s0 + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                        } else {
                            s0 = extent;
                            tempS1 = -(negativeDirectionDot * s0 + diffTestDot);
                            if (tempS1 < -test.getExtent()) {
                                s1 = -test.getExtent();
                                squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                            } else if (tempS1 <= test.getExtent()) {
                                s1 = tempS1;
                                squareDistance = -s1 * s1 + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                            } else {
                                s1 = test.getExtent();
                                squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                            }
                        }
                    }
                } else // region 8 (corner)
                {
                    s1 = -test.getExtent();
                    tempS0 = -(negativeDirectionDot * s1 + diffThisDot);
                    if (tempS0 < -extent) {
                        s0 = -extent;
                        squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                    } else if (tempS0 <= extent) {
                        s0 = tempS0;
                        squareDistance = -s0 * s0 + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                    } else {
                        s0 = extent;
                        tempS1 = -(negativeDirectionDot * s0 + diffTestDot);
                        if (tempS1 > test.getExtent()) {
                            s1 = test.getExtent();
                            squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                        } else if (tempS1 >= -test.getExtent()) {
                            s1 = tempS1;
                            squareDistance = -s1 * s1 + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                        } else {
                            s1 = -test.getExtent();
                            squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                        }
                    }
                }
            }
        } else {
            if (s1 >= -extentDeterminant1) {
                if (// region 5 (side)
                s1 <= extentDeterminant1) {
                    s0 = -extent;
                    tempS1 = -(negativeDirectionDot * s0 + diffTestDot);
                    if (tempS1 < -test.getExtent()) {
                        s1 = -test.getExtent();
                        squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                    } else if (tempS1 <= test.getExtent()) {
                        s1 = tempS1;
                        squareDistance = -s1 * s1 + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                    } else {
                        s1 = test.getExtent();
                        squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                    }
                } else // region 4 (corner)
                {
                    s1 = test.getExtent();
                    tempS0 = -(negativeDirectionDot * s1 + diffThisDot);
                    if (tempS0 > extent) {
                        s0 = extent;
                        squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                    } else if (tempS0 >= -extent) {
                        s0 = tempS0;
                        squareDistance = -s0 * s0 + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                    } else {
                        s0 = -extent;
                        tempS1 = -(negativeDirectionDot * s0 + diffTestDot);
                        if (tempS1 < -test.getExtent()) {
                            s1 = -test.getExtent();
                            squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                        } else if (tempS1 <= test.getExtent()) {
                            s1 = tempS1;
                            squareDistance = -s1 * s1 + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                        } else {
                            s1 = test.getExtent();
                            squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                        }
                    }
                }
            } else // region 6 (corner)
            {
                s1 = -test.getExtent();
                tempS0 = -(negativeDirectionDot * s1 + diffThisDot);
                if (tempS0 > extent) {
                    s0 = extent;
                    squareDistance = s0 * (s0 - (2.0f) * tempS0) + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                } else if (tempS0 >= -extent) {
                    s0 = tempS0;
                    squareDistance = -s0 * s0 + s1 * (s1 + (2.0f) * diffTestDot) + lengthOfDiff;
                } else {
                    s0 = -extent;
                    tempS1 = -(negativeDirectionDot * s0 + diffTestDot);
                    if (tempS1 < -test.getExtent()) {
                        s1 = -test.getExtent();
                        squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                    } else if (tempS1 <= test.getExtent()) {
                        s1 = tempS1;
                        squareDistance = -s1 * s1 + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                    } else {
                        s1 = test.getExtent();
                        squareDistance = s1 * (s1 - (2.0f) * tempS1) + s0 * (s0 + (2.0f) * diffThisDot) + lengthOfDiff;
                    }
                }
            }
        }
    } else {
        // The segments are parallel. The average b0 term is designed to
        // ensure symmetry of the function. That is, dist(seg0,seg1) and
        // dist(seg1,seg0) should produce the same number.get
        float extentSum = extent + test.getExtent();
        float sign = (negativeDirectionDot > 0.0f ? -1.0f : 1.0f);
        float averageB0 = (0.5f) * (diffThisDot - sign * diffTestDot);
        float lambda = -averageB0;
        if (lambda < -extentSum) {
            lambda = -extentSum;
        } else if (lambda > extentSum) {
            lambda = extentSum;
        }
        squareDistance = lambda * (lambda + (2.0f) * averageB0) + lengthOfDiff;
    }
    return FastMath.abs(squareDistance);
}
Also used : TempVars(com.jme3.util.TempVars)

Example 19 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class BillboardControl method rotateScreenAligned.

/**
     * Rotate the billboard so it points directly opposite the direction the
     * camera's facing
     *
     * @param camera
     *            Camera
     */
private void rotateScreenAligned(Camera camera) {
    // coopt diff for our in direction:
    look.set(camera.getDirection()).negateLocal();
    // coopt loc for our left direction:
    left.set(camera.getLeft()).negateLocal();
    orient.fromAxes(left, camera.getUp(), look);
    Node parent = spatial.getParent();
    Quaternion rot = new Quaternion().fromRotationMatrix(orient);
    if (parent != null) {
        rot = parent.getWorldRotation().inverse().multLocal(rot);
        rot.normalizeLocal();
    }
    spatial.setLocalRotation(rot);
    fixRefreshFlags();
}
Also used : Quaternion(com.jme3.math.Quaternion) Node(com.jme3.scene.Node)

Example 20 with Direction

use of com.jme3.terrain.geomipmap.picking.BresenhamYUpGridTracer.Direction in project jmonkeyengine by jMonkeyEngine.

the class Quaternion method lookAt.

/**
     * <code>lookAt</code> is a convienence method for auto-setting the
     * quaternion based on a direction and an up vector. It computes
     * the rotation to transform the z-axis to point into 'direction'
     * and the y-axis to 'up'.
     *
     * @param direction
     *            where to look at in terms of local coordinates
     * @param up
     *            a vector indicating the local up direction.
     *            (typically {0, 1, 0} in jME.)
     */
public void lookAt(Vector3f direction, Vector3f up) {
    TempVars vars = TempVars.get();
    vars.vect3.set(direction).normalizeLocal();
    vars.vect1.set(up).crossLocal(direction).normalizeLocal();
    vars.vect2.set(direction).crossLocal(vars.vect1).normalizeLocal();
    fromAxes(vars.vect1, vars.vect2, vars.vect3);
    vars.release();
}
Also used : TempVars(com.jme3.util.TempVars)

Aggregations

Vector3f (com.jme3.math.Vector3f)26 TempVars (com.jme3.util.TempVars)19 CollisionResult (com.jme3.collision.CollisionResult)6 ColorRGBA (com.jme3.math.ColorRGBA)6 Quaternion (com.jme3.math.Quaternion)6 Geometry (com.jme3.scene.Geometry)4 CollisionResults (com.jme3.collision.CollisionResults)3 DirectionalLight (com.jme3.light.DirectionalLight)3 Ray (com.jme3.math.Ray)3 Vector2f (com.jme3.math.Vector2f)3 Renderer (com.jme3.renderer.Renderer)3 CameraNode (com.jme3.scene.CameraNode)3 Node (com.jme3.scene.Node)3 BoundingSphere (com.jme3.bounding.BoundingSphere)2 VehicleWheel (com.jme3.bullet.objects.VehicleWheel)2 InputCapsule (com.jme3.export.InputCapsule)2 OutputCapsule (com.jme3.export.OutputCapsule)2 AmbientLight (com.jme3.light.AmbientLight)2 Light (com.jme3.light.Light)2 PointLight (com.jme3.light.PointLight)2