use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.
the class LoaderMD5Anim method parse.
public LoaderMD5Anim parse() throws ParsingException {
super.parse();
BufferedReader buffer = null;
if (mFile == null) {
InputStream fileIn = mResources.openRawResource(mResourceId);
buffer = new BufferedReader(new InputStreamReader(fileIn));
} else {
try {
buffer = new BufferedReader(new FileReader(mFile));
} catch (FileNotFoundException e) {
RajLog.e("[" + getClass().getCanonicalName() + "] Could not find file.");
e.printStackTrace();
}
}
mSequence = new SkeletalAnimationSequence(mAnimationName);
SkeletalAnimationFrame[] frames = null;
String line;
try {
while ((line = buffer.readLine()) != null) {
line = line.replace("\t", " ");
StringTokenizer parts = new StringTokenizer(line, " ");
int numTokens = parts.countTokens();
if (numTokens == 0)
continue;
String type = parts.nextToken();
if (type.equalsIgnoreCase(MD5_VERSION)) {
} else if (type.equalsIgnoreCase(COMMAND_LINE)) {
} else if (type.equalsIgnoreCase(NUM_JOINTS)) {
mNumJoints = Integer.parseInt(parts.nextToken());
mJoints = new SkeletonJoint[mNumJoints];
} else if (type.equalsIgnoreCase(NUM_FRAMES)) {
mSequence.setNumFrames(Integer.parseInt(parts.nextToken()));
frames = new SkeletalAnimationFrame[mSequence.getNumFrames()];
} else if (type.equalsIgnoreCase(FRAME_RATE)) {
mSequence.setFrameRate(Integer.parseInt(parts.nextToken()));
} else if (type.equalsIgnoreCase(NUM_ANIMATED_COMPONENTS)) {
mNumAnimatedComponents = Integer.parseInt(parts.nextToken());
} else if (type.equalsIgnoreCase(HIERARCHY)) {
parseHierarchy(buffer);
} else if (type.equalsIgnoreCase(BOUNDS)) {
parseBounds(frames, buffer);
} else if (type.equalsIgnoreCase(FRAME)) {
parseFrame(frames, Integer.parseInt(parts.nextToken()), buffer);
} else if (type.equalsIgnoreCase(BASEFRAME)) {
mBaseFrame = new SkeletonJoint[mNumJoints];
parseBaseFrame(buffer);
}
}
buffer.close();
} catch (Exception e) {
e.printStackTrace();
}
mSequence.setFrames(frames);
return this;
}
use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.
the class LoaderMD5Anim method buildFrameSkeleton.
private void buildFrameSkeleton(float[] frameData, Skeleton skeleton) {
for (int i = 0; i < mNumJoints; ++i) {
SkeletonJoint joint = new SkeletonJoint(mBaseFrame[i]);
SkeletonJoint jointInfo = mJoints[i];
joint.setParentIndex(jointInfo.getParentIndex());
int j = 0;
int startIndex = jointInfo.getStartIndex();
if ((jointInfo.getFlags() & 1) == 1)
joint.getPosition().x = frameData[startIndex + j++];
if ((jointInfo.getFlags() & 2) == 2)
joint.getPosition().z = frameData[startIndex + j++];
if ((jointInfo.getFlags() & 4) == 4)
joint.getPosition().y = frameData[startIndex + j++];
if ((jointInfo.getFlags() & 8) == 8)
joint.getOrientation().x = frameData[startIndex + j++];
if ((jointInfo.getFlags() & 16) == 16)
joint.getOrientation().z = frameData[startIndex + j++];
if ((jointInfo.getFlags() & 32) == 32)
joint.getOrientation().y = frameData[startIndex + j++];
joint.getOrientation().computeW();
if (// Has a parent joint
joint.getParentIndex() >= 0) {
SkeletonJoint parentJoint = skeleton.getJoint(joint.getParentIndex());
Vector3 rotPos = parentJoint.getOrientation().multiply(joint.getPosition());
//We don't clone here because nothing will be able to use the quaternion scratch before we do
joint.getPosition().setAll(Vector3.addAndCreate(parentJoint.getPosition(), rotPos));
joint.getOrientation().multiply(parentJoint.getOrientation());
joint.getOrientation().normalize();
}
skeleton.setJoint(i, joint);
}
}
use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.
the class LoaderMD5Anim method parseHierarchy.
private void parseHierarchy(BufferedReader buffer) {
try {
String line;
int index = 0;
while ((line = buffer.readLine()) != null) {
line = line.replace("\t", " ");
StringTokenizer parts = new StringTokenizer(line, " ");
int numTokens = parts.countTokens();
if (line.indexOf('}') > -1)
return;
if (numTokens == 0)
continue;
SkeletonJoint joint = new SkeletonJoint();
joint.setIndex(index);
joint.setName(parts.nextToken());
joint.setParentIndex(Integer.parseInt(parts.nextToken()));
joint.setFlags(Integer.parseInt(parts.nextToken()));
joint.setStartIndex(Integer.parseInt(parts.nextToken()));
mJoints[index++] = joint;
}
} catch (Exception e) {
e.printStackTrace();
}
}
use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.
the class LoaderMD5Mesh method calculateNormals.
private void calculateNormals() {
for (int i = 0; i < mNumMeshes; ++i) {
SkeletonMeshData mesh = mMeshes[i];
int numTriangles = mesh.numTriangles;
mesh.indices = new int[numTriangles * 3];
int index = 0;
for (int j = 0; j < numTriangles; ++j) {
int[] triangle = mesh.triangles[j];
int index0 = triangle[0];
int index1 = triangle[2];
int index2 = triangle[1];
mesh.indices[index++] = index0;
mesh.indices[index++] = index1;
mesh.indices[index++] = index2;
int index03 = index0 * 3;
int index13 = index1 * 3;
int index23 = index2 * 3;
Vector3 v0 = new Vector3(mesh.vertices[index03], mesh.vertices[index03 + 1], mesh.vertices[index03 + 2]);
Vector3 v1 = new Vector3(mesh.vertices[index13], mesh.vertices[index13 + 1], mesh.vertices[index13 + 2]);
Vector3 v2 = new Vector3(mesh.vertices[index23], mesh.vertices[index23 + 1], mesh.vertices[index23 + 2]);
Vector3 normal = Vector3.crossAndCreate(Vector3.subtractAndCreate(v2, v0), Vector3.subtractAndCreate(v1, v0));
normal.inverse();
mesh.boneVertices[index0].normal.add(normal);
mesh.boneVertices[index1].normal.add(normal);
mesh.boneVertices[index2].normal.add(normal);
}
int numVertices = mesh.numVertices;
if (mesh.normals == null)
mesh.normals = new float[numVertices * 3];
for (int j = 0; j < numVertices; ++j) {
BoneVertex vert = mesh.boneVertices[j];
Vector3 normal = vert.normal.clone();
vert.normal.normalize();
normal.normalize();
int normIndex = j * 3;
mesh.normals[normIndex] = (float) normal.x;
mesh.normals[normIndex + 1] = (float) normal.y;
mesh.normals[normIndex + 2] = (float) normal.z;
vert.normal.setAll(0, 0, 0);
// so the animated normal can be computed faster
for (int k = 0; k < vert.numWeights; ++k) {
BoneWeight weight = mesh.boneWeights[vert.weightIndex + k];
SkeletonJoint joint = mJoints[weight.jointIndex];
//We don't clone here because nothing will be able to use the quaternion scratch before we do
vert.normal.add(Vector3.scaleAndCreate(joint.getOrientation().multiply(normal), weight.weightValue));
}
}
}
}
use of org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint in project Rajawali by Rajawali.
the class LoaderMD5Mesh method parse.
@Override
public LoaderMD5Mesh parse() throws ParsingException {
super.parse();
BufferedReader buffer = null;
if (mFile == null) {
InputStream fileIn = mResources.openRawResource(mResourceId);
buffer = new BufferedReader(new InputStreamReader(fileIn));
} else {
try {
buffer = new BufferedReader(new FileReader(mFile));
} catch (FileNotFoundException e) {
RajLog.e("[" + getClass().getCanonicalName() + "] Could not find file.");
throw new ParsingException(e);
}
}
String line;
try {
while ((line = buffer.readLine()) != null) {
StringTokenizer parts = new StringTokenizer(line, " ");
int numTokens = parts.countTokens();
if (numTokens == 0)
continue;
String type = parts.nextToken();
if (type.equalsIgnoreCase(MD5_VERSION)) {
if (RajLog.isDebugEnabled())
RajLog.d("MD5 Version: " + parts.nextToken());
} else if (type.equalsIgnoreCase(COMMAND_LINE)) {
} else if (type.equalsIgnoreCase(NUM_JOINTS)) {
mNumJoints = Integer.parseInt(parts.nextToken());
mJoints = new SkeletonJoint[mNumJoints];
} else if (type.equalsIgnoreCase(NUM_MESHES)) {
mNumMeshes = Integer.parseInt(parts.nextToken());
mMeshes = new SkeletonMeshData[mNumMeshes];
} else if (type.equalsIgnoreCase(JOINTS)) {
parseJoints(buffer);
} else if (type.equals(MESH)) {
parseMesh(buffer);
}
}
buffer.close();
buildBindPose();
buildMeshes();
calculateNormals();
createObjects();
} catch (Exception tme) {
try {
buffer.close();
} catch (Exception ex) {
}
throw new ParsingException(tme);
} finally {
mMeshes = null;
mJoints = null;
mBindPoseMatrix = null;
mInverseBindPoseMatrix = null;
}
return this;
}
Aggregations