Search in sources :

Example 71 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class SimulationNode method computeAnimationTimeBoundaries.

/**
     * Computes the maximum frame and time for the animation. Different tracks
     * can have different lengths so here the maximum one is being found.
     * 
     * @param animation
     *            the animation
     * @return maximum frame and time of the animation
     */
private float[] computeAnimationTimeBoundaries(Animation animation) {
    int maxFrame = Integer.MIN_VALUE;
    float maxTime = -Float.MAX_VALUE;
    for (Track track : animation.getTracks()) {
        if (track instanceof BoneTrack) {
            maxFrame = Math.max(maxFrame, ((BoneTrack) track).getTranslations().length);
            maxTime = Math.max(maxTime, ((BoneTrack) track).getTimes()[((BoneTrack) track).getTimes().length - 1]);
        } else if (track instanceof SpatialTrack) {
            maxFrame = Math.max(maxFrame, ((SpatialTrack) track).getTranslations().length);
            maxTime = Math.max(maxTime, ((SpatialTrack) track).getTimes()[((SpatialTrack) track).getTimes().length - 1]);
        } else {
            throw new IllegalStateException("Unsupported track type for simuation: " + track);
        }
    }
    return new float[] { maxFrame, maxTime };
}
Also used : SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) Track(com.jme3.animation.Track)

Example 72 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class AnimationHelper method fromIpoStructure.

/**
     * This method creates an ipo object used for interpolation calculations.
     * 
     * @param ipoStructure
     *            the structure with ipo definition
     * @param blenderContext
     *            the blender context
     * @return the ipo object
     * @throws BlenderFileException
     *             this exception is thrown when the blender file is somehow
     *             corrupted
     */
public Ipo fromIpoStructure(Structure ipoStructure, BlenderContext blenderContext) throws BlenderFileException {
    Structure curvebase = (Structure) ipoStructure.getFieldValue("curve");
    // preparing bezier curves
    Ipo result = null;
    // IpoCurve
    List<Structure> curves = curvebase.evaluateListBase();
    if (curves.size() > 0) {
        BezierCurve[] bezierCurves = new BezierCurve[curves.size()];
        int frame = 0;
        for (Structure curve : curves) {
            Pointer pBezTriple = (Pointer) curve.getFieldValue("bezt");
            List<Structure> bezTriples = pBezTriple.fetchData();
            int type = ((Number) curve.getFieldValue("adrcode")).intValue();
            bezierCurves[frame++] = new BezierCurve(type, bezTriples, 2);
        }
        curves.clear();
        result = new Ipo(bezierCurves, fixUpAxis, blenderContext.getBlenderVersion());
        Long ipoOma = ipoStructure.getOldMemoryAddress();
        blenderContext.addLoadedFeatures(ipoOma, LoadedDataType.STRUCTURE, ipoStructure);
        blenderContext.addLoadedFeatures(ipoOma, LoadedDataType.FEATURE, result);
    }
    return result;
}
Also used : ConstIpo(com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure) BezierCurve(com.jme3.scene.plugins.blender.curves.BezierCurve)

Example 73 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class AnimationHelper method getTracks250.

/**
     * This method retuns the bone tracks for animation for blender version 2.50
     * and higher.
     * 
     * @param actionStructure
     *            the structure containing the tracks
     * @param blenderContext
     *            the blender context
     * @return a list of tracks for the specified animation
     * @throws BlenderFileException
     *             an exception is thrown when there are problems with the blend
     *             file
     */
private BlenderAction getTracks250(Structure actionStructure, BlenderContext blenderContext) throws BlenderFileException {
    LOGGER.log(Level.FINE, "Getting tracks!");
    Structure groups = (Structure) actionStructure.getFieldValue("groups");
    // bActionGroup
    List<Structure> actionGroups = groups.evaluateListBase();
    BlenderAction blenderAction = new BlenderAction(actionStructure.getName(), blenderContext.getBlenderKey().getFps());
    int lastFrame = 1;
    for (Structure actionGroup : actionGroups) {
        String name = actionGroup.getFieldValue("name").toString();
        List<Structure> channels = ((Structure) actionGroup.getFieldValue("channels")).evaluateListBase();
        BezierCurve[] bezierCurves = new BezierCurve[channels.size()];
        int channelCounter = 0;
        for (Structure c : channels) {
            int type = this.getCurveType(c, blenderContext);
            Pointer pBezTriple = (Pointer) c.getFieldValue("bezt");
            List<Structure> bezTriples = pBezTriple.fetchData();
            bezierCurves[channelCounter++] = new BezierCurve(type, bezTriples, 2);
        }
        Ipo ipo = new Ipo(bezierCurves, fixUpAxis, blenderContext.getBlenderVersion());
        lastFrame = Math.max(lastFrame, ipo.getLastFrame());
        blenderAction.featuresTracks.put(name, ipo);
    }
    blenderAction.stopFrame = lastFrame;
    return blenderAction;
}
Also used : ConstIpo(com.jme3.scene.plugins.blender.animations.Ipo.ConstIpo) Pointer(com.jme3.scene.plugins.blender.file.Pointer) Structure(com.jme3.scene.plugins.blender.file.Structure) BezierCurve(com.jme3.scene.plugins.blender.curves.BezierCurve)

Example 74 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class Ipo method calculateTrack.

/**
     * This method calculates the value of the curves as a bone track between
     * the specified frames.
     * 
     * @param targetIndex
     *            the index of the target for which the method calculates the
     *            tracks IMPORTANT! Aet to -1 (or any negative number) if you
     *            want to load spatial animation.
     * @param localTranslation
     *            the local translation of the object/bone that will be animated by
     *            the track
     * @param localRotation
     *            the local rotation of the object/bone that will be animated by
     *            the track
     * @param localScale
     *            the local scale of the object/bone that will be animated by
     *            the track
     * @param startFrame
     *            the first frame of tracks (inclusive)
     * @param stopFrame
     *            the last frame of the tracks (inclusive)
     * @param fps
     *            frame rate (frames per second)
     * @param spatialTrack
     *            this flag indicates if the track belongs to a spatial or to a
     *            bone; the difference is important because it appears that bones
     *            in blender have the same type of coordinate system (Y as UP)
     *            as jme while other features have different one (Z is UP)
     * @return bone track for the specified bone
     */
public Track calculateTrack(int targetIndex, BoneContext boneContext, Vector3f localTranslation, Quaternion localRotation, Vector3f localScale, int startFrame, int stopFrame, int fps, boolean spatialTrack) {
    if (calculatedTrack == null) {
        // preparing data for track
        int framesAmount = stopFrame - startFrame;
        float timeBetweenFrames = 1.0f / fps;
        float[] times = new float[framesAmount + 1];
        Vector3f[] translations = new Vector3f[framesAmount + 1];
        float[] translation = new float[3];
        Quaternion[] rotations = new Quaternion[framesAmount + 1];
        float[] quaternionRotation = new float[] { localRotation.getX(), localRotation.getY(), localRotation.getZ(), localRotation.getW() };
        float[] eulerRotation = localRotation.toAngles(null);
        Vector3f[] scales = new Vector3f[framesAmount + 1];
        float[] scale = new float[] { localScale.x, localScale.y, localScale.z };
        float degreeToRadiansFactor = 1;
        if (blenderVersion < 250) {
            // in blender earlier than 2.50 the values are stored in degrees
            // the values in blender are divided by 10, so we need to mult it here
            degreeToRadiansFactor *= FastMath.DEG_TO_RAD * 10;
        }
        int yIndex = 1, zIndex = 2;
        boolean swapAxes = spatialTrack && fixUpAxis;
        if (swapAxes) {
            yIndex = 2;
            zIndex = 1;
        }
        boolean eulerRotationUsed = false, queternionRotationUsed = false;
        // calculating track data
        for (int frame = startFrame; frame <= stopFrame; ++frame) {
            boolean translationSet = false;
            translation[0] = translation[1] = translation[2] = 0;
            int index = frame - startFrame;
            // start + (frame - 1) * timeBetweenFrames;
            times[index] = index * timeBetweenFrames;
            for (int j = 0; j < bezierCurves.length; ++j) {
                double value = bezierCurves[j].evaluate(frame, BezierCurve.Y_VALUE);
                switch(bezierCurves[j].getType()) {
                    // LOCATION
                    case AC_LOC_X:
                        translation[0] = (float) value;
                        translationSet = true;
                        break;
                    case AC_LOC_Y:
                        if (swapAxes && value != 0) {
                            value = -value;
                        }
                        translation[yIndex] = (float) value;
                        translationSet = true;
                        break;
                    case AC_LOC_Z:
                        translation[zIndex] = (float) value;
                        translationSet = true;
                        break;
                    // EULER ROTATION
                    case OB_ROT_X:
                        eulerRotationUsed = true;
                        eulerRotation[0] = (float) value * degreeToRadiansFactor;
                        break;
                    case OB_ROT_Y:
                        eulerRotationUsed = true;
                        if (swapAxes && value != 0) {
                            value = -value;
                        }
                        eulerRotation[yIndex] = (float) value * degreeToRadiansFactor;
                        break;
                    case OB_ROT_Z:
                        eulerRotationUsed = true;
                        eulerRotation[zIndex] = (float) value * degreeToRadiansFactor;
                        break;
                    // SIZE
                    case AC_SIZE_X:
                        scale[0] = (float) value;
                        break;
                    case AC_SIZE_Y:
                        scale[yIndex] = (float) value;
                        break;
                    case AC_SIZE_Z:
                        scale[zIndex] = (float) value;
                        break;
                    // QUATERNION ROTATION (used with bone animation)
                    case AC_QUAT_W:
                        queternionRotationUsed = true;
                        quaternionRotation[3] = (float) value;
                        break;
                    case AC_QUAT_X:
                        queternionRotationUsed = true;
                        quaternionRotation[0] = (float) value;
                        break;
                    case AC_QUAT_Y:
                        queternionRotationUsed = true;
                        if (swapAxes && value != 0) {
                            value = -value;
                        }
                        quaternionRotation[yIndex] = (float) value;
                        break;
                    case AC_QUAT_Z:
                        quaternionRotation[zIndex] = (float) value;
                        break;
                    default:
                        LOGGER.log(Level.WARNING, "Unknown ipo curve type: {0}.", bezierCurves[j].getType());
                }
            }
            if (translationSet) {
                translations[index] = localRotation.multLocal(new Vector3f(translation[0], translation[1], translation[2]));
            } else {
                translations[index] = new Vector3f();
            }
            if (boneContext != null) {
                if (boneContext.getBone().getParent() == null && boneContext.is(BoneContext.NO_LOCAL_LOCATION)) {
                    float temp = translations[index].z;
                    translations[index].z = -translations[index].y;
                    translations[index].y = temp;
                }
            }
            if (queternionRotationUsed) {
                rotations[index] = new Quaternion(quaternionRotation[0], quaternionRotation[1], quaternionRotation[2], quaternionRotation[3]);
            } else {
                rotations[index] = new Quaternion().fromAngles(eulerRotation);
            }
            scales[index] = new Vector3f(scale[0], scale[1], scale[2]);
        }
        if (spatialTrack) {
            calculatedTrack = new SpatialTrack(times, translations, rotations, scales);
        } else {
            calculatedTrack = new BoneTrack(targetIndex, times, translations, rotations, scales);
        }
        if (queternionRotationUsed && eulerRotationUsed) {
            LOGGER.warning("Animation uses both euler and quaternion tracks for rotations. Quaternion rotation is applied. Make sure that this is what you wanted!");
        }
    }
    return calculatedTrack;
}
Also used : SpatialTrack(com.jme3.animation.SpatialTrack) BoneTrack(com.jme3.animation.BoneTrack) Quaternion(com.jme3.math.Quaternion) Vector3f(com.jme3.math.Vector3f)

Example 75 with Type

use of com.jme3.scene.VertexBuffer.Type in project jmonkeyengine by jMonkeyEngine.

the class CameraHelper method toCamera250.

/**
     * This method converts the given structure to jme camera. Should be used form blender 2.5+.
     * 
     * @param structure
     *            camera structure
     * @param sceneStructure
     *            scene structure
     * @return jme camera object
     * @throws BlenderFileException
     *             an exception is thrown when there are problems with the
     *             blender file
     */
private Camera toCamera250(Structure structure, Structure sceneStructure) throws BlenderFileException {
    int width = DEFAULT_CAM_WIDTH;
    int height = DEFAULT_CAM_HEIGHT;
    if (sceneStructure != null) {
        Structure renderData = (Structure) sceneStructure.getFieldValue("r");
        width = ((Number) renderData.getFieldValue("xsch")).shortValue();
        height = ((Number) renderData.getFieldValue("ysch")).shortValue();
    }
    Camera camera = new Camera(width, height);
    int type = ((Number) structure.getFieldValue("type")).intValue();
    if (type != 0 && type != 1) {
        LOGGER.log(Level.WARNING, "Unknown camera type: {0}. Perspective camera is being used!", type);
        type = 0;
    }
    // type==0 - perspective; type==1 - orthographic; perspective is used as default
    camera.setParallelProjection(type == 1);
    float aspect = width / (float) height;
    // Vertical field of view in degrees
    float fovY;
    float clipsta = ((Number) structure.getFieldValue("clipsta")).floatValue();
    float clipend = ((Number) structure.getFieldValue("clipend")).floatValue();
    if (type == 0) {
        // Convert lens MM to vertical degrees in fovY, see Blender rna_Camera_angle_get()
        // Default sensor size prior to 2.60 was 32.
        float sensor = 32.0f;
        boolean sensorVertical = false;
        Number sensorFit = (Number) structure.getFieldValue("sensor_fit");
        if (sensorFit != null) {
            // If sensor_fit is vert (2), then sensor_y is used
            sensorVertical = sensorFit.byteValue() == 2;
            String sensorName = "sensor_x";
            if (sensorVertical) {
                sensorName = "sensor_y";
            }
            sensor = ((Number) structure.getFieldValue(sensorName)).floatValue();
        }
        float focalLength = ((Number) structure.getFieldValue("lens")).floatValue();
        float fov = 2.0f * FastMath.atan(sensor / 2.0f / focalLength);
        if (sensorVertical) {
            fovY = fov * FastMath.RAD_TO_DEG;
        } else {
            // Convert fov from horizontal to vertical
            fovY = 2.0f * FastMath.atan(FastMath.tan(fov / 2.0f) / aspect) * FastMath.RAD_TO_DEG;
        }
    } else {
        // This probably is not correct.
        fovY = ((Number) structure.getFieldValue("ortho_scale")).floatValue();
    }
    camera.setFrustumPerspective(fovY, aspect, clipsta, clipend);
    camera.setName(structure.getName());
    return camera;
}
Also used : Camera(com.jme3.renderer.Camera) Structure(com.jme3.scene.plugins.blender.file.Structure)

Aggregations

ArrayList (java.util.ArrayList)18 Structure (com.jme3.scene.plugins.blender.file.Structure)12 Vector3f (com.jme3.math.Vector3f)11 Pointer (com.jme3.scene.plugins.blender.file.Pointer)10 IOException (java.io.IOException)10 List (java.util.List)9 ColorRGBA (com.jme3.math.ColorRGBA)8 Texture (com.jme3.texture.Texture)8 Geometry (com.jme3.scene.Geometry)6 Image (com.jme3.texture.Image)6 BoundingBox (com.jme3.bounding.BoundingBox)5 BoundingSphere (com.jme3.bounding.BoundingSphere)5 Light (com.jme3.light.Light)5 TemporalMesh (com.jme3.scene.plugins.blender.meshes.TemporalMesh)5 Uniform (com.jme3.shader.Uniform)5 Material (com.jme3.material.Material)4 Quaternion (com.jme3.math.Quaternion)4 Transform (com.jme3.math.Transform)4 Mesh (com.jme3.scene.Mesh)4 BlenderFileException (com.jme3.scene.plugins.blender.file.BlenderFileException)4