use of org.rajawali3d.animation.mesh.VertexAnimationFrame in project Rajawali by Rajawali.
the class LoaderMD2 method parse.
public LoaderMD2 parse() throws ParsingException {
super.parse();
BufferedInputStream stream = null;
if (mFile == null) {
InputStream fileIn = mResources.openRawResource(mResourceId);
stream = new BufferedInputStream(fileIn);
} else {
try {
stream = new BufferedInputStream(new FileInputStream(mFile));
} catch (FileNotFoundException e) {
RajLog.e("[" + getClass().getCanonicalName() + "] Could not find file.");
throw new ParsingException(e);
}
}
mObject = new VertexAnimationObject3D();
mObject.setFps(10);
mHeader = new MD2Header();
try {
mHeader.parse(stream);
mFrames = new Stack<IAnimationFrame>();
for (int i = 0; i < mHeader.numFrames; ++i) mFrames.add(new VertexAnimationFrame());
byte[] bytes = new byte[mHeader.offsetEnd - 68];
stream.read(bytes);
getMaterials(stream, bytes);
float[] texCoords = getTexCoords(stream, bytes);
getFrames(stream, bytes);
getTriangles(stream, bytes, texCoords);
mObject.setFrames(mFrames);
IAnimationFrame firstFrame = mFrames.get(0);
Material material = new Material();
material.enableLighting(true);
material.setDiffuseMethod(new DiffuseMethod.Lambert());
material.addPlugin(new VertexAnimationMaterialPlugin());
mObject.getGeometry().copyFromGeometry3D(firstFrame.getGeometry());
mObject.setData(firstFrame.getGeometry().getVertexBufferInfo(), firstFrame.getGeometry().getNormalBufferInfo(), mTextureCoords, null, mIndices, false);
mObject.setMaterial(material);
mObject.setColor(0xffffffff);
if (mTexture != null) {
material.addTexture(new Texture(mCurrentTextureName, mTexture));
material.setColorInfluence(0);
}
stream.close();
} catch (Exception e) {
throw new ParsingException(e);
}
mObject.isContainer(false);
mRootObject = mObject;
return this;
}
use of org.rajawali3d.animation.mesh.VertexAnimationFrame in project Rajawali by Rajawali.
the class LoaderMD2 method getTriangles.
private void getTriangles(BufferedInputStream stream, byte[] bytes, float[] texCoords) throws IOException {
ByteArrayInputStream ba = new ByteArrayInputStream(bytes, mHeader.offsetTriangles - 68, bytes.length - mHeader.offsetTriangles);
LittleEndianDataInputStream is = new LittleEndianDataInputStream(ba);
int[] indices = new int[mHeader.numTriangles * 3];
int[] uvIndices = new int[mHeader.numTriangles * 3];
int index = 0, uvIndex = 0;
for (int i = 0; i < mHeader.numTriangles; i++) {
indices[index + 2] = is.readShort();
indices[index + 1] = is.readShort();
indices[index] = is.readShort();
index += 3;
uvIndices[uvIndex + 2] = is.readShort();
uvIndices[uvIndex + 1] = is.readShort();
uvIndices[uvIndex] = is.readShort();
uvIndex += 3;
}
is.close();
short newVertexIndex = (short) mHeader.numVerts;
int numIndices = indices.length;
Stack<VertexIndices> changedIndices = new Stack<LoaderMD2.VertexIndices>();
for (int i = 0; i < numIndices; i++) {
for (int j = i + 1; j < numIndices; j++) {
if (indices[i] == indices[j] && uvIndices[i] != uvIndices[j]) {
changedIndices.add(new VertexIndices((short) j, indices[j], newVertexIndex));
for (int k = j + 1; k < numIndices; k++) {
if (indices[j] == indices[k] && uvIndices[j] == uvIndices[k]) {
indices[k] = newVertexIndex;
}
}
indices[j] = newVertexIndex;
newVertexIndex++;
}
}
}
int[] cIndices = new int[changedIndices.size()];
for (int j = 0; j < changedIndices.size(); j++) cIndices[j] = changedIndices.get(j).oldVertexIndex;
float[] reorderedTexCoords = new float[(mHeader.numVerts + changedIndices.size()) * 2];
for (int i = 0; i < indices.length; i++) {
int fid = indices[i];
int uvid = uvIndices[i];
reorderedTexCoords[fid * 2] = texCoords[uvid * 2];
reorderedTexCoords[fid * 2 + 1] = texCoords[uvid * 2 + 1];
}
mTextureCoords = reorderedTexCoords;
mIndices = indices;
for (int i = 0; i < mHeader.numFrames; ++i) {
VertexAnimationFrame frame = (VertexAnimationFrame) mFrames.get(i);
duplicateAndAppendVertices(i, cIndices);
frame.getGeometry().setVertices(mFrameVerts[i]);
frame.getGeometry().setNormals(frame.calculateNormals(indices));
frame.getGeometry().createVertexAndNormalBuffersOnly();
}
}
Aggregations