Search in sources :

Example 26 with Point

use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.

the class Ray method intersects.

/**
     * <code>intersects</code> does the actual intersection work.
     *
     * @param v0
     *            first point of the triangle.
     * @param v1
     *            second point of the triangle.
     * @param v2
     *            third point of the triangle.
     * @param store
     *            storage vector - if null, no intersection is calc'd
     * @param doPlanar
     *            true if we are calcing planar results.
     * @param quad
     * @return true if ray intersects triangle
     */
private boolean intersects(Vector3f v0, Vector3f v1, Vector3f v2, Vector3f store, boolean doPlanar, boolean quad) {
    TempVars vars = TempVars.get();
    Vector3f tempVa = vars.vect1, tempVb = vars.vect2, tempVc = vars.vect3, tempVd = vars.vect4;
    Vector3f diff = origin.subtract(v0, tempVa);
    Vector3f edge1 = v1.subtract(v0, tempVb);
    Vector3f edge2 = v2.subtract(v0, tempVc);
    Vector3f norm = edge1.cross(edge2, tempVd);
    float dirDotNorm = direction.dot(norm);
    float sign;
    if (dirDotNorm > FastMath.FLT_EPSILON) {
        sign = 1;
    } else if (dirDotNorm < -FastMath.FLT_EPSILON) {
        sign = -1f;
        dirDotNorm = -dirDotNorm;
    } else {
        // ray and triangle/quad are parallel
        vars.release();
        return false;
    }
    float dirDotDiffxEdge2 = sign * direction.dot(diff.cross(edge2, edge2));
    if (dirDotDiffxEdge2 >= 0.0f) {
        float dirDotEdge1xDiff = sign * direction.dot(edge1.crossLocal(diff));
        if (dirDotEdge1xDiff >= 0.0f) {
            if (!quad ? dirDotDiffxEdge2 + dirDotEdge1xDiff <= dirDotNorm : dirDotEdge1xDiff <= dirDotNorm) {
                float diffDotNorm = -sign * diff.dot(norm);
                if (diffDotNorm >= 0.0f) {
                    // this method always returns
                    vars.release();
                    // if storage vector is null, just return true,
                    if (store == null) {
                        return true;
                    }
                    // else fill in.
                    float inv = 1f / dirDotNorm;
                    float t = diffDotNorm * inv;
                    if (!doPlanar) {
                        store.set(origin).addLocal(direction.x * t, direction.y * t, direction.z * t);
                    } else {
                        // these weights can be used to determine
                        // interpolated values, such as texture coord.
                        // eg. texcoord s,t at intersection point:
                        // s = w0*s0 + w1*s1 + w2*s2;
                        // t = w0*t0 + w1*t1 + w2*t2;
                        float w1 = dirDotDiffxEdge2 * inv;
                        float w2 = dirDotEdge1xDiff * inv;
                        //float w0 = 1.0f - w1 - w2;
                        store.set(t, w1, w2);
                    }
                    return true;
                }
            }
        }
    }
    vars.release();
    return false;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 27 with Point

use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.

the class Ray method distanceSquared.

public float distanceSquared(Vector3f point) {
    TempVars vars = TempVars.get();
    Vector3f tempVa = vars.vect1, tempVb = vars.vect2;
    point.subtract(origin, tempVa);
    float rayParam = direction.dot(tempVa);
    if (rayParam > 0) {
        origin.add(direction.mult(rayParam, tempVb), tempVb);
    } else {
        tempVb.set(origin);
        rayParam = 0.0f;
    }
    tempVb.subtract(point, tempVa);
    float len = tempVa.lengthSquared();
    vars.release();
    return len;
}
Also used : TempVars(com.jme3.util.TempVars)

Example 28 with Point

use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.

the class CurvesTemporalMesh method getValueAlongCurve.

/**
     * The method computes the value of a point at the certain relational distance from its beggining.
     * @param alongRatio
     *            the relative distance along the curve; should be a value between 0 and 1 inclusive;
     *            if the value exceeds the boundaries it is truncated to them
     * @return computed value along the curve
     */
private Vector3f getValueAlongCurve(float alongRatio) {
    alongRatio = FastMath.clamp(alongRatio, 0, 1);
    Vector3f result = new Vector3f();
    float probeLength = this.getLength() * alongRatio, length = 0;
    for (BezierLine bezier : beziers) {
        float edgeLength = bezier.getLength();
        if (length + edgeLength >= probeLength) {
            float ratioAlongEdge = (probeLength - length) / edgeLength;
            return bezier.getValueAlongCurve(ratioAlongEdge);
        }
        length += edgeLength;
    }
    return result;
}
Also used : Vector3f(com.jme3.math.Vector3f)

Example 29 with Point

use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.

the class CurvesHelper method transformToFirstLineOfBevelPoints.

/**
     * This method transforms the first line of the bevel points positioning it
     * on the first point of the curve.
     * 
     * @param startingLinePoints
     *            the vbevel shape points
     * @param firstCurvePoint
     *            the first curve's point
     * @param secondCurvePoint
     *            the second curve's point
     * @return points of transformed bevel
     */
protected Vector3f[] transformToFirstLineOfBevelPoints(Vector3f[] startingLinePoints, Vector3f firstCurvePoint, Vector3f secondCurvePoint) {
    Vector3f planeNormal = secondCurvePoint.subtract(firstCurvePoint).normalizeLocal();
    float angle = FastMath.acos(planeNormal.dot(Vector3f.UNIT_X));
    Vector3f rotationVector = Vector3f.UNIT_X.cross(planeNormal).normalizeLocal();
    Matrix4f m = new Matrix4f();
    m.setRotationQuaternion(new Quaternion().fromAngleAxis(angle, rotationVector));
    m.setTranslation(firstCurvePoint);
    Vector3f temp = new Vector3f();
    Vector3f[] verts = new Vector3f[startingLinePoints.length];
    for (int i = 0; i < verts.length; ++i) {
        verts[i] = m.mult(startingLinePoints[i], temp).clone();
    }
    return verts;
}
Also used : Matrix4f(com.jme3.math.Matrix4f) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f)

Example 30 with Point

use of com.jme3.scene.plugins.blender.meshes.Point in project jmonkeyengine by jMonkeyEngine.

the class TangentBinormalGenerator method genTangentLines.

private static Mesh genTangentLines(Mesh mesh, float scale) {
    FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData();
    FloatBuffer normalBuffer = (FloatBuffer) mesh.getBuffer(Type.Normal).getData();
    FloatBuffer tangentBuffer = (FloatBuffer) mesh.getBuffer(Type.Tangent).getData();
    FloatBuffer binormalBuffer = null;
    if (mesh.getBuffer(Type.Binormal) != null) {
        binormalBuffer = (FloatBuffer) mesh.getBuffer(Type.Binormal).getData();
    }
    ColorRGBA originColor = ColorRGBA.White;
    ColorRGBA tangentColor = ColorRGBA.Red;
    ColorRGBA binormalColor = ColorRGBA.Green;
    ColorRGBA normalColor = ColorRGBA.Blue;
    Mesh lineMesh = new Mesh();
    lineMesh.setMode(Mesh.Mode.Lines);
    Vector3f origin = new Vector3f();
    Vector3f point = new Vector3f();
    Vector3f tangent = new Vector3f();
    Vector3f normal = new Vector3f();
    IntBuffer lineIndex = BufferUtils.createIntBuffer(vertexBuffer.limit() / 3 * 6);
    FloatBuffer lineVertex = BufferUtils.createFloatBuffer(vertexBuffer.limit() * 4);
    FloatBuffer lineColor = BufferUtils.createFloatBuffer(vertexBuffer.limit() / 3 * 4 * 4);
    boolean hasParity = mesh.getBuffer(Type.Tangent).getNumComponents() == 4;
    float tangentW = 1;
    for (int i = 0; i < vertexBuffer.limit() / 3; i++) {
        populateFromBuffer(origin, vertexBuffer, i);
        populateFromBuffer(normal, normalBuffer, i);
        if (hasParity) {
            tangent.x = tangentBuffer.get(i * 4);
            tangent.y = tangentBuffer.get(i * 4 + 1);
            tangent.z = tangentBuffer.get(i * 4 + 2);
            tangentW = tangentBuffer.get(i * 4 + 3);
        } else {
            populateFromBuffer(tangent, tangentBuffer, i);
        }
        int index = i * 4;
        int id = i * 6;
        lineIndex.put(id, index);
        lineIndex.put(id + 1, index + 1);
        lineIndex.put(id + 2, index);
        lineIndex.put(id + 3, index + 2);
        lineIndex.put(id + 4, index);
        lineIndex.put(id + 5, index + 3);
        setInBuffer(origin, lineVertex, index);
        setInBuffer(originColor, lineColor, index);
        point.set(tangent);
        point.multLocal(scale);
        point.addLocal(origin);
        setInBuffer(point, lineVertex, index + 1);
        setInBuffer(tangentColor, lineColor, index + 1);
        if (binormalBuffer == null) {
            normal.cross(tangent, point);
            point.multLocal(-tangentW);
            point.normalizeLocal();
        } else {
            populateFromBuffer(point, binormalBuffer, i);
        }
        point.multLocal(scale);
        point.addLocal(origin);
        setInBuffer(point, lineVertex, index + 2);
        setInBuffer(binormalColor, lineColor, index + 2);
        point.set(normal);
        point.multLocal(scale);
        point.addLocal(origin);
        setInBuffer(point, lineVertex, index + 3);
        setInBuffer(normalColor, lineColor, index + 3);
    }
    lineMesh.setBuffer(Type.Index, 1, lineIndex);
    lineMesh.setBuffer(Type.Position, 3, lineVertex);
    lineMesh.setBuffer(Type.Color, 4, lineColor);
    lineMesh.setStatic();
    //lineMesh.setInterleaved();
    return lineMesh;
}
Also used : ColorRGBA(com.jme3.math.ColorRGBA) Vector3f(com.jme3.math.Vector3f) IntBuffer(java.nio.IntBuffer) FloatBuffer(java.nio.FloatBuffer)

Aggregations

Vector3f (com.jme3.math.Vector3f)27 TempVars (com.jme3.util.TempVars)19 FloatBuffer (java.nio.FloatBuffer)6 ColorRGBA (com.jme3.math.ColorRGBA)5 DirectionalLight (com.jme3.light.DirectionalLight)4 PointLight (com.jme3.light.PointLight)4 SpotLight (com.jme3.light.SpotLight)4 Quaternion (com.jme3.math.Quaternion)4 Spatial (com.jme3.scene.Spatial)4 ArrayList (java.util.ArrayList)4 CollisionResult (com.jme3.collision.CollisionResult)3 Light (com.jme3.light.Light)3 Triangle (com.jme3.math.Triangle)3 Vector2f (com.jme3.math.Vector2f)3 Geometry (com.jme3.scene.Geometry)3 Mesh (com.jme3.scene.Mesh)3 BoundingSphere (com.jme3.bounding.BoundingSphere)2 MotionPath (com.jme3.cinematic.MotionPath)2 MotionPathListener (com.jme3.cinematic.MotionPathListener)2 MotionEvent (com.jme3.cinematic.events.MotionEvent)2