I've got my mesh skinning engine nearly working; the shader associates each vertex with the correct matrix and transforms them properly. However, since a bone's offset it not applied to its matrix, all rotations pivot around the origin. I have bone offsets available to the code, but I can't figure out hot to apply them in such a way that they affect only the pivot point of the bone, without translating vertices attached to them. Here's my Bone class:
//==============================================================================
private class Bone {
//----------------------------------------------------------------------
private final Bone PARENT;
private final List<Bone> CHILDREN = new ArrayList<Bone>();
private final String NAME;
public final int INDEX;
//private boolean use_physics = false; // For later
private Vector3f offset;
//----------------------------------------------------------------------
private Bone( Bone parent, String name, int index ) {
PARENT = parent;
NAME = name;
INDEX = index;
if ( PARENT != null ) PARENT.addChild( this );
}
//----------------------------------------------------------------------
public void addChild( Bone child ) { CHILDREN.add( child ); }
//----------------------------------------------------------------------
public void savePose( Pose p, Stack<Matrix4f> stack, FloatBuffer b ) {
stack.push( stack.isEmpty() ? new Matrix4f() : new Matrix4f( stack.peek() ));
Matrix4f temp_mat = new Matrix4f();
Matrix4f.mul( p.get( NAME ), temp_mat, temp_mat );
Matrix4f.mul( stack.peek(), temp_mat, stack.peek() );
b.position( INDEX * 16 );
stack.peek().store( b );
for ( Bone c : CHILDREN ) { c.savePose( p, stack, b ); }
stack.pop();
}
//----------------------------------------------------------------------
}
//------------------------------------------------------------------------------
savePose() is the method in question; Pose is a tiny class extending HashMap<String,Matrix4f> which contains a matrix for the rotation data associated with the name of each bone.
This will be the first time I've gotten a rendering engine to work with skinning, and this is pretty much the last hurdle. Google goes completely insane when I ask for code examples, so I've resorted to this. Thanks in advance.