Search in sources :

Example 1 with ParsingException

use of org.rajawali3d.loader.ParsingException in project Rajawali by Rajawali.

the class LoaderFBX method setMeshTextures.

private void setMeshTextures(Object3D o, String name) throws TextureException, ParsingException {
    Stack<FBXValues.Objects.Texture> textures = mFbx.objects.textures;
    Stack<Connect> connections = mFbx.connections.connections;
    int numTex = textures.size();
    int numCon = connections.size();
    for (int i = 0; i < numTex; ++i) {
        FBXValues.Objects.Texture tex = textures.get(i);
        for (int j = 0; j < numCon; ++j) {
            Connect conn = connections.get(j);
            if (conn.object2.equals(name) && conn.object1.equals(tex.textureName)) {
                // -- one texture for now
                String textureName = tex.fileName;
                Bitmap bitmap = null;
                if (mFile == null) {
                    int identifier = mResources.getIdentifier(getFileNameWithoutExtension(textureName).toLowerCase(Locale.US), "drawable", mResources.getResourcePackageName(mResourceId));
                    bitmap = BitmapFactory.decodeResource(mResources, identifier);
                } else {
                    try {
                        String filePath = mFile.getParent() + File.separatorChar + getOnlyFileName(textureName);
                        bitmap = BitmapFactory.decodeFile(filePath);
                    } catch (Exception e) {
                        throw new ParsingException("[" + getClass().getCanonicalName() + "] Could not find file " + getOnlyFileName(textureName));
                    }
                }
                o.getMaterial().setColorInfluence(0);
                o.getMaterial().addTexture(new Texture(textureName.replaceAll("[\\W]|_", ""), bitmap));
                return;
            }
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) Connect(org.rajawali3d.loader.fbx.FBXValues.Connections.Connect) ParsingException(org.rajawali3d.loader.ParsingException) Texture(org.rajawali3d.materials.textures.Texture) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) ParsingException(org.rajawali3d.loader.ParsingException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) InvocationTargetException(java.lang.reflect.InvocationTargetException)

Example 2 with ParsingException

use of org.rajawali3d.loader.ParsingException 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;
}
Also used : SkeletalAnimationSequence(org.rajawali3d.animation.mesh.SkeletalAnimationSequence) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) SkeletalAnimationFrame(org.rajawali3d.animation.mesh.SkeletalAnimationFrame) FileNotFoundException(java.io.FileNotFoundException) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) ParsingException(org.rajawali3d.loader.ParsingException) FileNotFoundException(java.io.FileNotFoundException) StringTokenizer(java.util.StringTokenizer) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) BufferedReader(java.io.BufferedReader) FileReader(java.io.FileReader)

Example 3 with ParsingException

use of org.rajawali3d.loader.ParsingException 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;
}
Also used : StringTokenizer(java.util.StringTokenizer) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) ParsingException(org.rajawali3d.loader.ParsingException) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) FileReader(java.io.FileReader) SkeletonJoint(org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint) TextureException(org.rajawali3d.materials.textures.ATexture.TextureException) ParsingException(org.rajawali3d.loader.ParsingException) FileNotFoundException(java.io.FileNotFoundException) SkeletalAnimationException(org.rajawali3d.animation.mesh.SkeletalAnimationObject3D.SkeletalAnimationException)

Example 4 with ParsingException

use of org.rajawali3d.loader.ParsingException in project Rajawali by Rajawali.

the class BlockMeshInstance method parseBlock.

public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
    // Parse scene block
    RajLog.d("Parsing SceneGraph Block at position: " + dis.getPosition());
    mSceneGraphBlock = new SceneGraphBlock();
    mSceneGraphBlock.readGraphData(blockHeader, dis);
    // Block id for geometry
    mGeometryID = dis.readUnsignedInt();
    // Lookup the geometry or create it if it does not exist.
    final BlockHeader geomHeader = blockHeader.blockHeaders.get((short) mGeometryID);
    if (geomHeader == null) {
        mGeometry = new Object3D(mSceneGraphBlock.lookupName);
    } else {
        if (geomHeader.parser == null || !(geomHeader.parser instanceof ABaseObjectBlockParser))
            throw new ParsingException("Invalid block reference.");
        mGeometry = ((ABaseObjectBlockParser) geomHeader.parser).getBaseObject3D().clone(false, true);
        mGeometry.setName(mSceneGraphBlock.lookupName);
    }
    // Apply the materials
    final int materialCount = dis.readUnsignedShort();
    final Material[] materials = new Material[materialCount];
    for (int i = 0; i < materialCount; ++i) {
        final long materialID = dis.readUnsignedInt();
        if (materialID == 0) {
            materials[i] = getDefaultMaterial();
            materials[i].addTexture(getDefaultTexture());
        } else {
            final BlockHeader materialHeader = blockHeader.blockHeaders.get((short) materialID);
            if (materialHeader == null || materialHeader.parser == null || !(materialHeader.parser instanceof ATextureBlockParser))
                throw new ParsingException("Invalid block reference " + materialID);
            materials[i] = ((ATextureBlockParser) materialHeader.parser).getMaterial();
        }
    }
    // mesh instance properties; does it cast a shadow?
    AwdProperties properties = dis.readProperties(EXPECTED_PROPS);
    mCastsShadow = (boolean) properties.get(PROP_CASTS_SHADOW, true);
    final Matrix4 matrix = new Matrix4(mSceneGraphBlock.transformMatrix);
    // Set translation
    mGeometry.setPosition(matrix.getTranslation());
    // Set scale
    final Vector3 scale = matrix.getScaling();
    mGeometry.setScale(scale.y, scale.x, scale.z);
    // Set rotation
    mGeometry.setOrientation(new Quaternion().fromMatrix(matrix));
    int m = 0;
    if (!mGeometry.isContainer())
        mGeometry.setMaterial(materials[m++]);
    for (int i = 0; i < mGeometry.getNumChildren(); i++) mGeometry.getChildAt(i).setMaterial(materials[Math.min(materials.length - 1, m++)]);
    // ignore user properties, skip to end of block
    dis.skip(blockHeader.blockEnd - dis.getPosition());
}
Also used : Quaternion(org.rajawali3d.math.Quaternion) Material(org.rajawali3d.materials.Material) Vector3(org.rajawali3d.math.vector.Vector3) AwdProperties(org.rajawali3d.loader.LoaderAWD.AwdProperties) Matrix4(org.rajawali3d.math.Matrix4) Object3D(org.rajawali3d.Object3D) ParsingException(org.rajawali3d.loader.ParsingException) BlockHeader(org.rajawali3d.loader.LoaderAWD.BlockHeader)

Example 5 with ParsingException

use of org.rajawali3d.loader.ParsingException in project Rajawali by Rajawali.

the class BlockPrimitiveGeometry method parseBlock.

@SuppressWarnings("unused")
public void parseBlock(AWDLittleEndianDataInputStream dis, BlockHeader blockHeader) throws Exception {
    // FIXME The primitive type requires more features than what the Rajawali primitives provide.
    // Lookup name, not sure why this is useful.
    mLookupName = dis.readVarString();
    // Read the primitive type
    mPrimitiveType = dis.readUnsignedByte();
    if (RajLog.isDebugEnabled()) {
        RajLog.d("  Lookup Name: " + mLookupName);
        RajLog.d("  Primitive Type: " + mPrimitiveType);
    }
    // Read primitive properties and construct a new base object determined by the primitive type
    switch(PrimitiveType.values()[mPrimitiveType - 1]) {
        case CUBE:
            {
                final float width = dis.readFloat();
                final float height = dis.readFloat();
                final float depth = dis.readFloat();
                final int segmentsW = dis.readUnsignedShort();
                final int segmentsH = dis.readUnsignedShort();
                final int segmentsD = dis.readUnsignedShort();
                final boolean tile6 = dis.readBoolean();
                throw new ParsingException("Type of Cube is not yet supported!");
            }
        case CAPSULE:
            throw new ParsingException("Type of Capsule is not yet supported!");
        case CONE:
            throw new ParsingException("Type of Cone is not yet supported!");
        case CYLINDER:
            {
                final float radiusTop = dis.readFloat();
                final float radiusBottom = dis.readFloat();
                final float height = dis.readFloat();
                final int segmentsW = dis.readUnsignedShort();
                final int segmentsH = dis.readUnsignedShort();
                final boolean topClosed = dis.readBoolean();
                final boolean bottomClosed = dis.readBoolean();
                final boolean yUp = dis.readBoolean();
                final boolean surfaceClosed = dis.readBoolean();
                throw new ParsingException("Type of Cylinder is not yet supported!");
            }
        case PLANE:
            {
                final float width = dis.readFloat();
                final float height = dis.readFloat();
                final int segmentsW = dis.readUnsignedShort();
                final int segmentsH = dis.readUnsignedShort();
                final boolean yUp = dis.readBoolean();
                final boolean doubleSided = dis.readBoolean();
                throw new ParsingException("Type of Plane is not yet supported!");
            }
        case SPHERE:
            {
                final float radius = dis.readFloat();
                final int segmentsW = dis.readUnsignedShort();
                final int segmentsH = dis.readUnsignedShort();
                final boolean yUp = dis.readBoolean();
                throw new ParsingException("Type of Cylinder is not yet supported!");
            }
        case TORUS:
            throw new ParsingException("Type of Torus is not yet supported!");
    }
}
Also used : ParsingException(org.rajawali3d.loader.ParsingException)

Aggregations

ParsingException (org.rajawali3d.loader.ParsingException)9 FileNotFoundException (java.io.FileNotFoundException)4 BufferedReader (java.io.BufferedReader)3 FileReader (java.io.FileReader)3 InputStream (java.io.InputStream)3 InputStreamReader (java.io.InputStreamReader)3 SkeletonJoint (org.rajawali3d.animation.mesh.SkeletalAnimationFrame.SkeletonJoint)3 Material (org.rajawali3d.materials.Material)3 TextureException (org.rajawali3d.materials.textures.ATexture.TextureException)3 Texture (org.rajawali3d.materials.textures.Texture)3 IOException (java.io.IOException)2 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 StringTokenizer (java.util.StringTokenizer)2 Object3D (org.rajawali3d.Object3D)2 SkeletalAnimationChildObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationChildObject3D)2 SkeletalAnimationObject3D (org.rajawali3d.animation.mesh.SkeletalAnimationObject3D)2 AwdProperties (org.rajawali3d.loader.LoaderAWD.AwdProperties)2 Vector3 (org.rajawali3d.math.vector.Vector3)2 Bitmap (android.graphics.Bitmap)1 SparseArray (android.util.SparseArray)1