Normand Briere
2019-09-08 4a303a7b3635adfee8f46ac76af4e1b7b4a7029b
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/*
 * SmoothSkin.java
 *
 *
 *  The Salamander Project - 2D and 3D graphics libraries in Java
 *  Copyright (C) 2004 Mark McKay
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU Lesser General Public
 *  License as published by the Free Software Foundation; either
 *  version 2.1 of the License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 *  Lesser General Public License for more details.
 *
 *  You should have received a copy of the GNU Lesser General Public
 *  License along with this library; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 *
 *  Mark McKay can be contacted at mark@kitfox.com.  Salamander and other
 *  projects can be found at http://www.kitfox.com
 *
 * Created on March 10, 2004, 5:39 AM
 */
 
import javax.media.j3d.*;
import javax.vecmath.*;
 
import java.util.*;
 
/**
 *
 * @author  kitfox
 */
//public class SmoothSkin extends Shape3D
public class SmoothSkin extends TriangleStripArray
{
//    TriangleStripArray targetSkin;
    Bone skelRoot;
 
//    int vertexFormat;
//    int vertexCount;
//    int[] stripVertexCounts;
    float[] bindCoordinates;
    float[] bindNormals;
    float[] targetCoordinates;
    float[] targetNormals;
    float[] texCoords;
    float[] colors;
 
    //Array of weights per bone per vertex.  Organized first by bones and then
    // by index of vertex into the targetCoordinates array.  Bones are in the
    // order of a depth first search of the skelRoot skeleton.
    float[] boneWeights;
    Transform3D worldToSkin = new Transform3D();
 
    private SmoothSkin(int vertexCount, int vertexFormat, int[] stripVertexCounts)
    {
        super(vertexCount, vertexFormat, stripVertexCounts);
    }
 
    private SmoothSkin(int vertexCount, int vertexFormat, int texCoordSetCount, int[] texCoordSetMap, int[] stripVertexCounts)
    {
        super(vertexCount, vertexFormat, texCoordSetCount, texCoordSetMap, stripVertexCounts);
    }
 
    /**
     * Create a new instance of SmoothSkin.  Special work needs to be done in
     * creation, so the constructor cannot be called directly.
     *
     * @param skelRoot - The root node of the skeleton system that will animate
     * this mesh
     * @param weights - A map of HashMaps that represent the distribution of
     * weights per bone.  That is, 'weights' will contain keys of Bone that map
     * to a hashmap describing weights for that bone.  This second hashmap
     * will contain keys of Point3f representing points in the source mesh to
     * Float, representing the weight for this point for this bone.
     * @param geometry - A triangle strip array that is the mesh this skeleton
     * will deform
     * @param skinToWorld - A transform that will map the skin mesh into world space,
     * if it is not already there.
     */
    public static SmoothSkin create(Bone skelRoot, HashMap weights, TriangleStripArray geometry, Transform3D skinToWorld)
    {
        //Make two copies of input geometry; one for holding bind information,
        // and the other being the target object that we render.
        SmoothSkin targetSkin;
 
        int vertexFormat = geometry.getVertexFormat();
        int vertexCount = geometry.getVertexCount();
        int numStrips = geometry.getNumStrips();
        int[] stripVertexCounts = new int[numStrips];
        geometry.getStripVertexCounts(stripVertexCounts);
 
        if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_2) != 0)
        {
            int texCoordSetCount = geometry.getTexCoordSetCount();
            int[] texCoordSetMap = new int[texCoordSetCount];
            geometry.getTexCoordSetMap(texCoordSetMap);
 
            targetSkin = new SmoothSkin(vertexCount, vertexFormat | GeometryArray.BY_REFERENCE, texCoordSetCount, texCoordSetMap, stripVertexCounts);
        } else
        {
            targetSkin = new SmoothSkin(vertexCount, vertexFormat | GeometryArray.BY_REFERENCE, stripVertexCounts);
        }
 
 
        targetSkin.prepareGeometry(skelRoot, geometry, weights, skinToWorld);
 
        return targetSkin;
    }
 
    private void prepareGeometry(Bone skelRoot, TriangleStripArray geom, HashMap weights, Transform3D skinToWorld)
    {
        int vertexCount = getVertexCount();
        int vertexFormat = getVertexFormat();
 
        bindCoordinates = new float[vertexCount * 3];
        geom.getCoordinates(0, bindCoordinates);
        targetCoordinates = new float[vertexCount * 3];
        geom.getCoordinates(0, targetCoordinates);
        setCoordRefFloat(targetCoordinates);
 
        setCapability(GeometryArray.ALLOW_REF_DATA_READ);
        setCapability(GeometryArray.ALLOW_REF_DATA_WRITE);
 
        if ((vertexFormat & GeometryArray.NORMALS) != 0)
        {
            bindNormals = new float[vertexCount * 3];
            geom.getNormals(0, bindNormals);
            targetNormals = new float[vertexCount * 3];
            geom.getNormals(0, targetNormals);
            setNormalRefFloat(targetNormals);
        }
 
        if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_2) != 0)
        {
            texCoords = new float[vertexCount * 2];
            geom.getTextureCoordinates(0, 0, texCoords);
            setTexCoordRefFloat(0, texCoords);
        } else if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_3) != 0)
        {
            texCoords = new float[vertexCount * 3];
            geom.getTextureCoordinates(0, 0, texCoords);
            setTexCoordRefFloat(0, texCoords);
        } else if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_4) != 0)
        {
            texCoords = new float[vertexCount * 4];
            geom.getTextureCoordinates(0, 0, texCoords);
            setTexCoordRefFloat(0, texCoords);
        }
 
        if ((vertexFormat & GeometryArray.COLOR_4) != 0)
        {
            colors = new float[vertexCount * 4];
            geom.getColors(0, colors);
            setColorRefFloat(colors);
        } else if ((vertexFormat & GeometryArray.COLOR_3) != 0)
        {
            colors = new float[vertexCount * 3];
            geom.getColors(0, colors);
            setColorRefFloat(colors);
        }
 
        //Now build skeleton and bone weights array
        this.skelRoot = skelRoot;
 
        boneWeights = new float[weights.size() * vertexCount];
        calcBoneWeights(skelRoot, weights, 0);
 
 
        //Map all points to world space
        Point3f mapPoint = new Point3f();
        for (int i = 0; i < bindCoordinates.length; i += 3)
        {
            mapPoint.set(bindCoordinates[i], bindCoordinates[i + 1], bindCoordinates[i + 2]);
            skinToWorld.transform(mapPoint);
            bindCoordinates[i] = mapPoint.x;
            bindCoordinates[i + 1] = mapPoint.y;
            bindCoordinates[i + 2] = mapPoint.z;
        }
 
        worldToSkin.invert(skinToWorld);
    }
 
    public NodeComponent cloneNodeComponent(boolean forceDuplicate)
    {
        SmoothSkin targetSkin;
 
        int vertexFormat = getVertexFormat();
        int vertexCount = getVertexCount();
        int numStrips = getNumStrips();
        int[] stripVertexCounts = new int[numStrips];
        getStripVertexCounts(stripVertexCounts);
 
        if ((vertexFormat & GeometryArray.TEXTURE_COORDINATE_2) != 0)
        {
            int texCoordSetCount = getTexCoordSetCount();
            int[] texCoordSetMap = new int[texCoordSetCount];
            getTexCoordSetMap(texCoordSetMap);
 
            targetSkin = new SmoothSkin(vertexCount, vertexFormat | GeometryArray.BY_REFERENCE, texCoordSetCount, texCoordSetMap, stripVertexCounts);
        } else
        {
            targetSkin = new SmoothSkin(vertexCount, vertexFormat | GeometryArray.BY_REFERENCE, stripVertexCounts);
        }
 
        targetSkin.duplicateNodeComponent(this, forceDuplicate);
        return targetSkin;
    }
 
    /**
     * Copies all information from the original node into this node
     */
    public void duplicateNodeComponent(NodeComponent originalNodeComponent, boolean forceDuplicate)
    {
        super.duplicateNodeComponent(originalNodeComponent, forceDuplicate);
 
        if (!forceDuplicate)
        {
            forceDuplicate = getDuplicateOnCloneTree();
        }
 
 
        SmoothSkin src = (SmoothSkin) originalNodeComponent;
 
        bindCoordinates = src.bindCoordinates;
        bindNormals = src.bindNormals;
        boneWeights = src.boneWeights;
 
        targetCoordinates = getCoordRefFloat();
        targetNormals = getNormalRefFloat();
        texCoords = getTexCoordRefFloat(0);
        colors = getColorRefFloat();
 
 
        if (forceDuplicate)
        {
            skelRoot = (Bone) src.skelRoot.cloneTree(true);
        } else
        {
            skelRoot = src.skelRoot;
        }
    }
 
    public Bone getSkeletonRoot()
    {
        return skelRoot;
    }
 
    /**
     * Returns number of points weighted
     */
    public int calcBoneWeights(Bone bone, HashMap weights, int offset)
    {
        HashMap vtxMap = (HashMap) weights.get(bone);
        Point3f vertex = new Point3f();
 
        int vertexCount = getVertexCount();
        for (int i = 0; i < vertexCount; i++)
        {
            vertex.set(bindCoordinates[i * 3], bindCoordinates[i * 3 + 1], bindCoordinates[i * 3 + 2]);
            Float weight = (Float) vtxMap.get(vertex);
 
            boneWeights[offset++] = weight.floatValue();
        }
 
        for (Iterator it = bone.children.iterator(); it.hasNext();)
        {
            Bone nextBone = (Bone) it.next();
            offset = calcBoneWeights(nextBone, weights, offset);
        }
 
        return offset;
    }
 
    /**
     * Causes the target skin to be reevaluated.  Call this after making changes
     * to the bone hierarchy to generate the new mesh.
     */
    public void updateSkin()
    {
        updateData(
                new GeometryUpdater()
                {
 
                    public void updateData(javax.media.j3d.Geometry geom)
                    {
                        evaluateSkin();
                    }
                });
    }
 
    /**
     * Recalculates target skin based on current bone animation positions.
     */
    private void evaluateSkin()
    {
        for (int i = 0; i < bindCoordinates.length; i++)
        {
            targetCoordinates[i] = targetNormals[i] = 0f;
        }
 
        //Identity change to root bone
        final Transform3D rootXform = new Transform3D();
        evalBoneWeights(skelRoot, 0, rootXform, rootXform);
 
        //Normal array needs to be converted from normal offsets to normals
        for (int i = 0; i < bindCoordinates.length; i++)
        {
            targetNormals[i] -= targetCoordinates[i];
        }
 
//Map all points to world space
        Point3f mapPoint = new Point3f();
        for (int i = 0; i < bindCoordinates.length; i += 3)
        {
            mapPoint.set(targetCoordinates[i], targetCoordinates[i + 1], targetCoordinates[i + 2]);
            worldToSkin.transform(mapPoint);
            targetCoordinates[i] = mapPoint.x;
            targetCoordinates[i + 1] = mapPoint.y;
            targetCoordinates[i + 2] = mapPoint.z;
        }
    }
 
//    Point4f ptN = new Point4f();
    public int evalBoneWeights(Bone bone, int offset, Transform3D parentWorldToLocal, Transform3D parentLocalToWorld)
    {
        Transform3D worldToLocal = new Transform3D();
        Transform3D localToWorld = new Transform3D();
        Transform3D sceneXform = new Transform3D();
 
        worldToLocal.mul(bone.bindInvXform, parentWorldToLocal);
        localToWorld.mul(parentLocalToWorld, bone.animXform);
        sceneXform.mul(localToWorld, worldToLocal);
 
        Point3f pt = new Point3f();
        Point3f norm = new Point3f();
 
        for (int i = 0; i < bindCoordinates.length; i += 3)
        {
//boneWeights[offset] = 1f;
            if (boneWeights[offset] < 0.01)
            {
                //Do not process transform if only a tiny amount of bone contribution
                // is present
                offset++;
                continue;
            }
 
            //Evaluate coorinate
            norm.x = pt.x = bindCoordinates[i];
            norm.y = pt.y = bindCoordinates[i + 1];
            norm.z = pt.z = bindCoordinates[i + 2];
 
            sceneXform.transform(pt);
            float boneWeight = boneWeights[offset];
            pt.scale(boneWeight);
 
            targetCoordinates[i] += pt.x;
            targetCoordinates[i + 1] += pt.y;
            targetCoordinates[i + 2] += pt.z;
 
            //Evaluate normal offset
            norm.x += bindNormals[i];
            norm.y += bindNormals[i + 1];
            norm.z += bindNormals[i + 2];
 
            sceneXform.transform(norm);
            norm.scale(boneWeights[offset++]);
 
            targetNormals[i] += norm.x;
            targetNormals[i + 1] += norm.y;
            targetNormals[i + 2] += norm.z;
        }
 
        for (Iterator it = bone.children.iterator(); it.hasNext();)
        {
            Bone nextBone = (Bone) it.next();
            offset = evalBoneWeights(nextBone, offset, worldToLocal, localToWorld);
        }
 
        return offset;
    }
}