~ubuntu-branches/debian/wheezy/java3ds-fileloader/wheezy

« back to all changes in this revision

Viewing changes to com/microcrowd/loader/java3d/max3ds/chunks/RotationChunk.java

  • Committer: Bazaar Package Importer
  • Author(s): Gabriele Giacone
  • Date: 2009-11-24 16:27:06 UTC
  • Revision ID: james.westby@ubuntu.com-20091124162706-yvyj5k4igsyct81k
Tags: 1.2-1
Initial release (Closes: #558259)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*8
 
2
 * Make a donation http://sourceforge.net/donate/index.php?group_id=98797
 
3
 * Microcrowd.com
 
4
 *
 
5
 * This library is free software; you can redistribute it and/or
 
6
 * modify it under the terms of the GNU Lesser General Public
 
7
 * License as published by the Free Software Foundation; either
 
8
 * version 2.1 of the License, or (at your option) any later version.
 
9
 * 
 
10
 * This library is distributed in the hope that it will be useful,
 
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
13
 * Lesser General Public License for more details.
 
14
 * 
 
15
 * You should have received a copy of the GNU Lesser General Public
 
16
 * License along with this library; if not, write to the Free Software
 
17
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 
18
 * 
 
19
 * Contact Josh DeFord jdeford@microcrowd.com
 
20
 */
 
21
 
 
22
package com.microcrowd.loader.java3d.max3ds.chunks;
 
23
 
 
24
import java.util.ArrayList;
 
25
import java.util.List;
 
26
import javax.vecmath.Quat4f;
 
27
import javax.vecmath.Vector3f;
 
28
import com.microcrowd.loader.java3d.max3ds.ChunkChopper;
 
29
 
 
30
/**
 
31
 * Extracts the rotation information from the 3ds file.
 
32
 * Rotations occur about a pivot at a position within
 
33
 * a local coordinate system. The rotation information
 
34
 * is provided to the KeyFramerInfoChunk which then converts
 
35
 * it to a global coordinate system and applies animation
 
36
 * information.
 
37
 * {@see KeyFramerInfoChunk} for more information about using
 
38
 * animations from a 3ds file
 
39
 */
 
40
public class RotationChunk extends Chunk
 
41
{
 
42
    /** 
 
43
     *  String that will be used as a data object in the transform that the 
 
44
     *  RotationInterpolator will be a child of so it may be look up later.
 
45
     **/
 
46
    public static String ROTATION_TAG = "ROTATION_INTERPOLATOR";
 
47
 
 
48
    /**
 
49
     * Loads the quaternion for a rotation of a shape
 
50
     * and notifies mesh info chunk.
 
51
     *
 
52
     * @param chopper the ChunkChopper containing the state of the parser.  
 
53
     */
 
54
    public void loadData(ChunkChopper chopper)
 
55
    {
 
56
        int flags = chopper.getUnsignedShort();
 
57
        chopper.getLong();
 
58
        int numKeys = chopper.getUnsignedInt();
 
59
 
 
60
        Quat4f previousQuat = null;
 
61
 
 
62
        List quats = new ArrayList();
 
63
        for(int i =0; i < numKeys; i++)
 
64
        {
 
65
            long         frameNumber = chopper.getUnsignedInt();//Part of the track header
 
66
            int   accelerationData = chopper.getUnsignedShort();//Part of the track header
 
67
            getSplineTerms(accelerationData, chopper);//Part of the track header
 
68
 
 
69
            float            angle = chopper.getFloat();
 
70
            Vector3f        vector = chopper.getVector(); 
 
71
 
 
72
            Quat4f quat = getQuaternion(vector, angle);
 
73
            if(previousQuat != null) {
 
74
                quat.mul(previousQuat, quat);
 
75
            }
 
76
            previousQuat = quat; 
 
77
 
 
78
            quats.add(quat); 
 
79
            if(i==0)
 
80
            {
 
81
                chopper.getKeyFramer().setRotation(quat);
 
82
            }
 
83
        }
 
84
        chopper.getKeyFramer().setOrientationKeys(quats);
 
85
    }
 
86
 
 
87
    /**
 
88
     * This only reads the spline data and should be part
 
89
     * of the track header when it gets invented.
 
90
     * @param chopper an integer representing the bits that 
 
91
     * determine which of the five possible spline terms are present in the 
 
92
     * data and should be read.
 
93
     * @param chopper what to read the data from
 
94
     * The possible spline values are are 
 
95
     * <ol>
 
96
     * <li> Tension
 
97
     * <li> Continuity
 
98
     * <li> Bias
 
99
     * <li> EaseTo
 
100
     * <li> EaseFrom
 
101
     * </ol>
 
102
     */
 
103
    private void getSplineTerms(final int accelerationData, ChunkChopper chopper)
 
104
    {
 
105
        int bits = accelerationData;
 
106
        for(int i=0; i < 5; i++)
 
107
        {
 
108
            bits = bits >>> i;
 
109
            if((bits & 1) == 1)
 
110
            {
 
111
                chopper.getFloat();
 
112
            }
 
113
        }
 
114
    }
 
115
 
 
116
    /**
 
117
     * This converts a 3ds angle and axis to 
 
118
     * a quaternion rotation.  Successive
 
119
     * rotations are relative to the first so each
 
120
     * rotation must be made absolute by multiplying
 
121
     * it with its predecessor
 
122
     */
 
123
    public Quat4f getQuaternion(Vector3f axis, float angle)
 
124
    {
 
125
        float sinA  = (float)(java.lang.Math.sin(angle/2.0f));
 
126
        float cosA  = (float)(java.lang.Math.cos(angle/2.0f));
 
127
        return new Quat4f(axis.x * sinA, axis.y * sinA, axis.z * sinA, cosA);
 
128
    }
 
129
}