Search in sources :

Example 1 with LittleEndianDataInputStream

use of org.rajawali3d.util.LittleEndianDataInputStream in project Rajawali by Rajawali.

the class LoaderMD2 method getTexCoords.

private float[] getTexCoords(BufferedInputStream stream, byte[] bytes) throws IOException {
    ByteArrayInputStream ba = new ByteArrayInputStream(bytes, mHeader.offsetTexCoord - 68, bytes.length - mHeader.offsetTexCoord);
    LittleEndianDataInputStream is = new LittleEndianDataInputStream(ba);
    float[] coords = new float[mHeader.numTexCoord * 2];
    int buffIndex = 0;
    for (int i = 0; i < mHeader.numTexCoord; i++) {
        buffIndex = i * 2;
        coords[buffIndex] = (float) is.readShort() / (float) mHeader.skinWidth;
        coords[buffIndex + 1] = (float) is.readShort() / (float) mHeader.skinHeight;
    }
    is.close();
    return coords;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianDataInputStream(org.rajawali3d.util.LittleEndianDataInputStream)

Example 2 with LittleEndianDataInputStream

use of org.rajawali3d.util.LittleEndianDataInputStream in project Rajawali by Rajawali.

the class LoaderMD2 method getMaterials.

private void getMaterials(BufferedInputStream stream, byte[] bytes) throws IOException {
    ByteArrayInputStream ba = new ByteArrayInputStream(bytes, mHeader.offsetSkins - 68, bytes.length - mHeader.offsetSkins);
    LittleEndianDataInputStream is = new LittleEndianDataInputStream(ba);
    for (int i = 0; i < mHeader.numSkins; i++) {
        String skinPath = is.readString(64);
        skinPath = skinPath.substring(skinPath.lastIndexOf("/") + 1, skinPath.length());
        StringBuffer textureName = new StringBuffer(skinPath.toLowerCase(Locale.ENGLISH));
        mCurrentTextureName = textureName.toString().trim();
        if (mFile != null)
            continue;
        int dotIndex = textureName.lastIndexOf(".");
        if (dotIndex > -1)
            textureName = new StringBuffer(textureName.substring(0, dotIndex));
        mCurrentTextureName = textureName.toString();
    }
    is.close();
    if (mFile == null) {
        if (mCurrentTextureName == null) {
            RajLog.e("[" + getClass().getCanonicalName() + "] No texture name was specified. No material will be created.");
            return;
        }
        int identifier = mResources.getIdentifier(mCurrentTextureName, "drawable", mResources.getResourcePackageName(mResourceId));
        mTexture = BitmapFactory.decodeResource(mResources, identifier);
    } else {
        try {
            String filePath = mFile.getParent() + File.separatorChar + mCurrentTextureName;
            mTexture = BitmapFactory.decodeFile(filePath);
        } catch (Exception e) {
            RajLog.e("[" + getClass().getCanonicalName() + "] Could not find file " + mCurrentTextureName);
            e.printStackTrace();
            return;
        }
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianDataInputStream(org.rajawali3d.util.LittleEndianDataInputStream) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Example 3 with LittleEndianDataInputStream

use of org.rajawali3d.util.LittleEndianDataInputStream in project Rajawali by Rajawali.

the class LoaderSTL method parse.

public AMeshLoader parse(StlType type) throws ParsingException {
    super.parse();
    try {
        // Open the file
        BufferedReader buffer = null;
        LittleEndianDataInputStream dis = null;
        switch(type) {
            case UNKNOWN:
                buffer = getBufferedReader();
                // Determine if ASCII or Binary
                boolean isASCII = isASCII(buffer);
                // Determine ASCII or Binary format
                if (isASCII) {
                    readASCII(buffer);
                } else {
                    // Switch to a LittleEndianDataInputStream (all values in binary are stored in little endian format)
                    buffer.close();
                    dis = getLittleEndianInputStream();
                    readBinary(dis);
                }
                break;
            case ASCII:
                buffer = getBufferedReader();
                readASCII(buffer);
                break;
            case BINARY:
                dis = getLittleEndianInputStream();
                readBinary(dis);
                break;
        }
        // Cleanup
        if (buffer != null)
            buffer.close();
        if (dis != null)
            dis.close();
    } catch (FileNotFoundException e) {
        RajLog.e("[" + getClass().getCanonicalName() + "] Could not find file.");
        throw new ParsingException("File not found.", e);
    } catch (NumberFormatException e) {
        RajLog.e(e.getMessage());
        throw new ParsingException("Unexpected value.", e);
    } catch (IOException e) {
        RajLog.e(e.getMessage());
        throw new ParsingException("File reading failed.", e);
    } catch (Exception e) {
        RajLog.e(e.getMessage());
        throw new ParsingException("Unexpected exception occured.", e);
    }
    return this;
}
Also used : LittleEndianDataInputStream(org.rajawali3d.util.LittleEndianDataInputStream) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NotFoundException(android.content.res.Resources.NotFoundException)

Example 4 with LittleEndianDataInputStream

use of org.rajawali3d.util.LittleEndianDataInputStream 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();
    }
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) LittleEndianDataInputStream(org.rajawali3d.util.LittleEndianDataInputStream) VertexAnimationFrame(org.rajawali3d.animation.mesh.VertexAnimationFrame) Stack(java.util.Stack)

Example 5 with LittleEndianDataInputStream

use of org.rajawali3d.util.LittleEndianDataInputStream 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

LittleEndianDataInputStream (org.rajawali3d.util.LittleEndianDataInputStream)5 ByteArrayInputStream (java.io.ByteArrayInputStream)4 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 NotFoundException (android.content.res.Resources.NotFoundException)1 BufferedReader (java.io.BufferedReader)1 Stack (java.util.Stack)1 IAnimationFrame (org.rajawali3d.animation.mesh.IAnimationFrame)1 VertexAnimationFrame (org.rajawali3d.animation.mesh.VertexAnimationFrame)1 Vector3 (org.rajawali3d.math.vector.Vector3)1