/**
*
* @author eagle.sakura
* @version 2010/07/08 :
*/
package eagle.android.fbx;
import java.io.IOException;
import eagle.android.gles11.GLManager;
import eagle.android.gles11.ITexture;
import eagle.android.math.Matrix4x4;
import eagle.io.DataInputStream;
import eagle.util.Disposable;
import eagle.util.EagleException;
import eagle.util.EagleUtil;
/**
*
*
* @author eagle.sakura
* @version 2010/07/08 :
*/
public class Figure implements Disposable {
/**
*
*/
private Node[] nodes; // !<
/**
*
*/
private GLManager glManager;
/**
*
* @author eagle.sakura
* @version 2010/07/08 :
*/
private Figure() {
}
/**
* gc
*
* @author eagle.sakura
* @throws Throwable
* @version 2010/07/12 :
*/
@Override
protected void finalize() throws Throwable {
// TODO
super.finalize();
dispose();
}
/**
* GL
*
* @author eagle.sakura
* @version 2010/07/12 :
*/
@Override
public void dispose() {
if (nodes != null) {
for (Node node : nodes) {
node.dispose();
}
nodes = null;
}
}
/**
*
* @author eagle.sakura
* @return
* @version 2010/07/08 :
*/
public GLManager getGLManager() {
return glManager;
}
/**
*
* @author eagle.sakura
* @param node
* @version 2010/07/08 :
*/
private void drawNodeTree(Node node) {
node.draw();
int num = node.getChildCount();
for (int i = 0; i < num; ++i) {
drawNodeTree(node.getChild(i));
}
}
/**
*
* @author eagle.sakura
* @version 2010/07/09 :
*/
private void updateNodeTree(Node node) {
node.updateMatrix();
int num = node.getChildCount();
for (int i = 0; i < num; ++i) {
updateNodeTree(node.getChild(i));
}
}
/**
*
*
* @author eagle.sakura
* @param name
* @param texture
* @version 2010/07/11 :
*/
public void bindTexture(String name, ITexture texture) {
for (Node node : nodes) {
// !
if (node.getNodeType() == Node.eNodeTypeSkin || node.getNodeType() == Node.eNodeTypeMesh) {
Frame frame = (Frame) node;
frame.bindTexture(name, texture);
}
}
}
/**
*
* @author eagle.sakura
* @param trans
* @version 2010/07/08 :
*/
public void draw(Matrix4x4 trans) {
/*
* //! for( Node node : nodes ) { // node.draw(); }
*/
updateNodeTree(getRootNode());
drawNodeTree(getRootNode());
}
/**
*
*
* @author eagle.sakura
* @param name
* @return
* @version 2010/07/10 :
*/
public Node getNode(String name) {
for (Node node : nodes) {
if (node.getName().equals(name)) {
return node;
}
}
return null;
}
/**
*
*
* @author eagle.sakura
* @param index
* @return
* @version 2010/07/11 :
*/
public Node getNode(int index) {
return nodes[index];
}
/**
*
*
* @author eagle.sakura
* @return
* @version 2010/07/08 :
*/
public Node getRootNode() {
return nodes[0];
}
/**
* <BR>
* null
*
* @author eagle.sakura
* @param name
* @return
* @version 2010/07/08 :
*/
public Node getBone(String name) {
for (Node node : nodes) {
if (node.getNodeType() == Node.eNodeTypeBone && node.getName().equals(name)
// && name.startsWith( node.getName( )
) {
return node;
}
}
for (Node node : nodes) {
if (node.getNodeType() == Node.eNodeTypeBone
// && node.getName().equals( name )
&& name.startsWith(node.getName()) && (node.getName().length() + 3) > name.length()) {
return node;
}
}
return null;
}
/**
*
*
* @author eagle.sakura
* @param action
* @param frame
* @version 2010/07/11 :
*/
public void bindAction(FigureAction action, float frame) {
frame = action.normalizeFrame(frame);
// !
for (Node node : nodes) {
NodeAction na = action.getNodeAction(node.getNodeNumber());
na.bind(frame, node.getBindKey());
}
}
/**
*
* @author eagle.sakura
* @param parent
* @param dis
* @throws IOException
* @throws EagleException
* @version 2010/07/08 :
*/
private Node createNode(Node parent, DataInputStream dis, GLResourceCreater glCreater) throws IOException, EagleException {
{
int version = dis.readS32();
if (version != 0x1) {
throw new EagleException(EagleException.eStatusUnknownFileVersion);
}
}
// !
int type = dis.readS32();
int number = dis.readS32();
// EagleUtil.log( "type : " + type + " number : " + number );
// EagleUtil.log( "Memory : " + ( Runtime.getRuntime().freeMemory() /
// 1024 ) + " KB" );
if (type == Node.eNodeTypeMesh || type == Node.eNodeTypeSkin) {
Frame frame = new Frame(this, parent, number);
nodes[number] = frame;
} else {
Node node = null;
if (type == Node.eNodeTypeBone) {
node = new Bone(parent, number);
} else {
node = new Node(parent, number);
}
nodes[number] = node;
}
nodes[number].initialize(dis, glCreater);
if (nodes[number].getNodeType() == Node.eNodeTypeBone) {
EagleUtil.log("Bone : " + nodes[number].getName());
}
{
int childs = dis.readS16();
for (int i = 0; i < childs; ++i) {
Node node = createNode(nodes[number], dis, glCreater);
nodes[number].addChild(node);
}
}
return nodes[number];
}
/**
* <BR>
*
*
* @author eagle.sakura
* @version 2010/07/10 :
*/
private void bindBones() {
for (Node node : nodes) {
if (node.getNodeType() == Node.eNodeTypeSkin) {
Frame frame = (Frame) node;
Deformer deformer = frame.getDeformer();
for (int i = 0; i < deformer.getNumBones(); ++i) {
Node bone = getBone(deformer.getBoneName(i));
deformer.setBone(i, (Bone) bone);
}
}
// !
if (node.getPoseInvertMatrixGlobal() != null
// && node.getParent() != null
) {
node.getMatrix().invert(node.getPoseInvertMatrixGlobal());
// node.getMatrix().invert( node.getPoseInvertMatrixGlobal( )
// ).multiply( getRootNode().getChild( 0 ).getMatrix() );
// node.getMatrix().invert( node.getDefaultPoseInvertMatrix( )
// );
}
}
}
/**
*
*
* @author eagle.sakura
* @version 2010/07/10 :
*/
private void calcInvertMatrix() {
for (Node node : nodes) {
if (node.getNodeType() == Node.eNodeTypeBone) {
Bone bone = (Bone) node;
bone.getMatrix().invert(bone.getInvertMatrix());
}
}
}
/**
*
* @author eagle.sakura
* @param glManager
* @param dis
* @return
* @throws IOException
* @throws EagleException
* @version 2010/07/08 :
*/
public static Figure createInstance(GLManager glManager, DataInputStream dis, GLResourceCreater glCreater) throws IOException, EagleException {
Figure figure = new Figure();
figure.glManager = glManager;
{
int version = dis.readS32();
if (version != 0x1) {
throw new EagleException(EagleException.eStatusUnknownFileVersion);
}
}
// !
{
int nodes = dis.readS32();
EagleUtil.log("Nodes : " + nodes);
figure.nodes = new Node[nodes];
}
// !
{
figure.createNode(null, dis, glCreater);
EagleUtil.log("node create complete!");
String[] str = new String[figure.nodes.length];
{
int i = 0;
for (Node node : figure.nodes) {
str[i] = node.getName();
++i;
}
}
// !
figure.updateNodeTree(figure.getRootNode());
// !
figure.bindBones();
// !
figure.calcInvertMatrix();
}
return figure;
}
}
|