Search in sources :

Example 1 with IAnimationFrame

use of org.rajawali3d.animation.mesh.IAnimationFrame 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;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianDataInputStream(org.rajawali3d.util.LittleEndianDataInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IAnimationFrame(org.rajawali3d.animation.mesh.IAnimationFrame) FileNotFoundException(java.io.FileNotFoundException) Material(org.rajawali3d.materials.Material) Texture(org.rajawali3d.materials.textures.Texture) FileInputStream(java.io.FileInputStream) VertexAnimationMaterialPlugin(org.rajawali3d.materials.plugins.VertexAnimationMaterialPlugin) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) BufferedInputStream(java.io.BufferedInputStream) VertexAnimationObject3D(org.rajawali3d.animation.mesh.VertexAnimationObject3D) DiffuseMethod(org.rajawali3d.materials.methods.DiffuseMethod) VertexAnimationFrame(org.rajawali3d.animation.mesh.VertexAnimationFrame)

Example 2 with IAnimationFrame

use of org.rajawali3d.animation.mesh.IAnimationFrame in project Rajawali by Rajawali.

the class LoaderMD2 method getFrames.

private void getFrames(BufferedInputStream stream, byte[] bytes) throws IOException {
    ByteArrayInputStream ba = new ByteArrayInputStream(bytes, mHeader.offsetFrames - 68, bytes.length - mHeader.offsetFrames);
    LittleEndianDataInputStream is = new LittleEndianDataInputStream(ba);
    mFrameVerts = new float[mHeader.numFrames][];
    for (int i = 0; i < mHeader.numFrames; i++) {
        float scaleX = is.readFloat();
        float scaleY = is.readFloat();
        float scaleZ = is.readFloat();
        float translateX = is.readFloat();
        float translateY = is.readFloat();
        float translateZ = is.readFloat();
        String name = is.readString(16);
        IAnimationFrame frame = mFrames.get(i);
        if (name.indexOf("_") > 0)
            name = name.subSequence(0, name.lastIndexOf("_")).toString();
        else
            name = name.trim().replaceAll("[0-9]{1,2}$", "");
        frame.setName(name);
        float[] vertices = new float[mHeader.numVerts * 3];
        int index = 0;
        Vector3 v = new Vector3();
        for (int j = 0; j < mHeader.numVerts; j++) {
            v.x = scaleX * is.readUnsignedByte() + translateX;
            v.y = scaleY * is.readUnsignedByte() + translateY;
            v.z = scaleZ * is.readUnsignedByte() + translateZ;
            v.rotateZ(-90);
            v.rotateX(-90);
            vertices[index + 0] = (float) v.x;
            vertices[index + 1] = (float) v.y;
            vertices[index + 2] = (float) v.z;
            index += 3;
            is.readUnsignedByte();
        }
        mFrameVerts[i] = vertices;
    }
    is.close();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianDataInputStream(org.rajawali3d.util.LittleEndianDataInputStream) IAnimationFrame(org.rajawali3d.animation.mesh.IAnimationFrame) Vector3(org.rajawali3d.math.vector.Vector3)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)2 IAnimationFrame (org.rajawali3d.animation.mesh.IAnimationFrame)2 LittleEndianDataInputStream (org.rajawali3d.util.LittleEndianDataInputStream)2 BufferedInputStream (java.io.BufferedInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 VertexAnimationFrame (org.rajawali3d.animation.mesh.VertexAnimationFrame)1 VertexAnimationObject3D (org.rajawali3d.animation.mesh.VertexAnimationObject3D)1 Material (org.rajawali3d.materials.Material)1 DiffuseMethod (org.rajawali3d.materials.methods.DiffuseMethod)1 VertexAnimationMaterialPlugin (org.rajawali3d.materials.plugins.VertexAnimationMaterialPlugin)1 Texture (org.rajawali3d.materials.textures.Texture)1 Vector3 (org.rajawali3d.math.vector.Vector3)1