159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/*
259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Copyright (c) 2009-2010 jMonkeyEngine
359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * All rights reserved.
459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * Redistribution and use in source and binary forms, with or without
659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * modification, are permitted provided that the following conditions are
759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * met:
859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions of source code must retain the above copyright
1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer.
1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Redistributions in binary form must reproduce the above copyright
1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   notice, this list of conditions and the following disclaimer in the
1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   documentation and/or other materials provided with the distribution.
1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * * Neither the name of 'jMonkeyEngine' nor the names of its contributors
1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   may be used to endorse or promote products derived from this software
1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *   without specific prior written permission.
1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta *
2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage com.jme3.animation;
3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.export.*;
3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Mesh;
3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.VertexBuffer;
3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.VertexBuffer.Type;
3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.util.TempVars;
4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.IOException;
4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.FloatBuffer;
4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/**
4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * A single track of pose animation associated with a certain mesh.
4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */
4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta@Deprecated
4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic final class PoseTrack implements Track {
4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private int targetMeshIndex;
5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private PoseFrame[] frames;
5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private float[] times;
5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public static class PoseFrame implements Savable, Cloneable {
5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Pose[] poses;
5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        float[] weights;
5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        public PoseFrame(Pose[] poses, float[] weights) {
5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            this.poses = poses;
6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            this.weights = weights;
6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        /**
6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta         * This method creates a clone of the current object.
6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta         * @return a clone of the current object
6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta         */
6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        @Override
6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        public PoseFrame clone() {
6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            try {
7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                PoseFrame result = (PoseFrame) super.clone();
7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                result.weights = this.weights.clone();
7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                if (this.poses != null) {
7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    result.poses = new Pose[this.poses.length];
7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    for (int i = 0; i < this.poses.length; ++i) {
7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                        result.poses[i] = this.poses[i].clone();
7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    }
7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                }
7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                return result;
7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            } catch (CloneNotSupportedException e) {
8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                throw new AssertionError();
8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        public void write(JmeExporter e) throws IOException {
8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            OutputCapsule out = e.getCapsule(this);
8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            out.write(poses, "poses", null);
8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            out.write(weights, "weights", null);
8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        public void read(JmeImporter i) throws IOException {
9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            InputCapsule in = i.getCapsule(this);
9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            poses = (Pose[]) in.readSavableArray("poses", null);
9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            weights = in.readFloatArray("weights", null);
9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public PoseTrack(int targetMeshIndex, float[] times, PoseFrame[] frames){
9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.targetMeshIndex = targetMeshIndex;
9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.times = times;
10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        this.frames = frames;
10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    private void applyFrame(Mesh target, int frameIndex, float weight){
10459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        PoseFrame frame = frames[frameIndex];
10559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        VertexBuffer pb = target.getBuffer(Type.Position);
10659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        for (int i = 0; i < frame.poses.length; i++){
10759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            Pose pose = frame.poses[i];
10859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            float poseWeight = frame.weights[i] * weight;
10959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            pose.apply(poseWeight, (FloatBuffer) pb.getData());
11159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
11259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // force to re-upload data to gpu
11459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        pb.updateData(pb.getData());
11559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
11659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
11759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void setTime(float time, float weight, AnimControl control, AnimChannel channel, TempVars vars) {
11859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // TODO: When MeshControl is created, it will gather targets
11959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        // list automatically which is then retrieved here.
12059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
12159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        /*
12259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        Mesh target = targets[targetMeshIndex];
12359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        if (time < times[0]) {
12459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            applyFrame(target, 0, weight);
12559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else if (time > times[times.length - 1]) {
12659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            applyFrame(target, times.length - 1, weight);
12759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } else {
12859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            int startFrame = 0;
12959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            for (int i = 0; i < times.length; i++) {
13059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                if (times[i] < time) {
13159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    startFrame = i;
13259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                }
13359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
13459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
13559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            int endFrame = startFrame + 1;
13659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            float blend = (time - times[startFrame]) / (times[endFrame] - times[startFrame]);
13759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            applyFrame(target, startFrame, blend * weight);
13859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            applyFrame(target, endFrame, (1f - blend) * weight);
13959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
14059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        */
14159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
14259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
14359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
14459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return the length of the track
14559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
14659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public float getLength() {
14759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        return times == null ? 0 : times[times.length - 1] - times[0];
14859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
14959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
15059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    /**
15159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * This method creates a clone of the current object.
15259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     * @return a clone of the current object
15359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta     */
15459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
15559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public PoseTrack clone() {
15659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        try {
15759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            PoseTrack result = (PoseTrack) super.clone();
15859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            result.times = this.times.clone();
15959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            if (this.frames != null) {
16059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                result.frames = new PoseFrame[this.frames.length];
16159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                for (int i = 0; i < this.frames.length; ++i) {
16259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                    result.frames[i] = this.frames[i].clone();
16359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta                }
16459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            }
16559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            return result;
16659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        } catch (CloneNotSupportedException e) {
16759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta            throw new AssertionError();
16859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        }
16959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
17059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
17159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
17259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void write(JmeExporter e) throws IOException {
17359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        OutputCapsule out = e.getCapsule(this);
17459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        out.write(targetMeshIndex, "meshIndex", 0);
17559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        out.write(frames, "frames", null);
17659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        out.write(times, "times", null);
17759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
17859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta
17959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    @Override
18059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    public void read(JmeImporter i) throws IOException {
18159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        InputCapsule in = i.getCapsule(this);
18259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        targetMeshIndex = in.readInt("meshIndex", 0);
18359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        frames = (PoseFrame[]) in.readSavableArray("frames", null);
18459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta        times = in.readFloatArray("times", null);
18559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta    }
18659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}
187