Search in sources :

Example 1 with FBXFloatBuffer

use of org.rajawali3d.loader.fbx.FBXValues.FBXFloatBuffer in project Rajawali by Rajawali.

the class LoaderFBX method readLine.

private void readLine(BufferedReader buffer, String line) throws IllegalArgumentException, SecurityException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, IOException {
    if (line.replaceAll(REGEX_CLEAN, REPLACE_EMPTY).length() == 0)
        return;
    if (line.contains("{")) {
        // -- found new object
        Object last = mObjStack.peek();
        if (line.contains(":")) {
            if (line.contains(OBJECT_TYPE)) {
                String val = line.split(":")[1].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
                Object ot = last.getClass().getDeclaredMethod("addObjectType", String.class).invoke(last, val);
                mObjStack.push(ot);
                return;
            } else if (line.contains(MODEL)) {
                String[] vals = line.split(",");
                if (vals.length < 2) {
                    // TODO add model object for Take
                    mObjStack.push(new Object());
                    return;
                }
                vals[0] = vals[0].split(": ")[1].replaceAll(REGEX_NO_QUOTE, REPLACE_EMPTY);
                vals[1] = vals[1].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
                Object mo = last.getClass().getDeclaredMethod("addModel", String.class, String.class).invoke(last, vals[0], vals[1]);
                mObjStack.push(mo);
                return;
            } else if (line.contains(MATERIAL) && !line.contains(LAYER_ELEMENT)) {
                String[] vals = line.split(": ")[1].split(",");
                vals[0] = vals[0].replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY);
                Object ma = last.getClass().getDeclaredMethod("addMaterial", String.class).invoke(last, vals[0]);
                mObjStack.push(ma);
                return;
            } else if (line.contains(POSE_NODE)) {
                Object pn = last.getClass().getDeclaredMethod("addPoseNode").invoke(last);
                mObjStack.push(pn);
                return;
            } else if (line.contains(PROPERTIES)) {
                line = "Properties";
            } else if (line.contains(LAYER_ELEMENT)) {
                line = line.replaceAll("\\W|\\d", REPLACE_EMPTY);
            } else if (line.contains(LAYER)) {
                line = LAYER;
            } else if (line.contains(POSE)) {
                String val = line.split(":")[1];
                String[] vals = val.split(",");
                last.getClass().getDeclaredMethod("setPoseName", String.class).invoke(last, vals[0].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY));
                line = POSE;
            } else if (line.contains(TEXTURE)) {
                String val = line.split(": ")[1];
                String[] vals = val.split(",");
                Object te = last.getClass().getDeclaredMethod("addTexture", String.class, String.class).invoke(last, vals[0].replaceAll(REGEX_NO_QUOTE, REPLACE_EMPTY), vals[1].replace(REGEX_NO_QUOTE, REPLACE_EMPTY));
                mObjStack.push(te);
                return;
            }
        }
        line = line.replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
        line = line.replaceAll(FBX_U, FBX_L);
        line = line.substring(0, 1).toLowerCase(Locale.US) + line.substring(1);
        try {
            Field field = last.getClass().getField(line);
            mObjStack.push(field.get(last));
        } catch (NoSuchFieldException e) {
            // -- create a generic object
            mObjStack.push(new Object());
            return;
        }
    } else if (line.contains("}")) {
        // -- end of object
        mObjStack.pop();
    } else {
        // -- found property
        Object last = mObjStack.peek();
        String[] spl = line.split(": ");
        if (spl.length == 0)
            return;
        String prop = spl[0].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
        prop = prop.replaceAll(FBX_U, FBX_L);
        prop = prop.substring(0, 1).toLowerCase(Locale.US) + prop.substring(1);
        boolean processNextLine = false;
        Object obj = mObjStack.peek();
        try {
            if (spl.length < 2)
                return;
            String val = spl[1];
            if (line.contains(PROPERTY)) {
                String[] vals = val.split(",");
                prop = vals[0].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
                prop = prop.substring(0, 1).toLowerCase(Locale.US) + prop.substring(1);
                String type = vals[1].replaceAll(REGEX_NO_FUNNY_CHARS, REPLACE_EMPTY);
                if (type.equals(TYPE_VECTOR3D) || type.equals(TYPE_COLOR) || type.equals(TYPE_COLOR_RGB) || type.equals(TYPE_LCL_ROTATION) || type.equals(TYPE_LCL_SCALING) || type.equals(TYPE_LCL_TRANSLATION) || type.equals(TYPE_VECTOR)) {
                    val = vals[3] + "," + vals[4] + "," + vals[5];
                } else {
                    if (vals.length < 4)
                        return;
                    val = vals[3].replaceAll(REGEX_NO_QUOTE, REPLACE_EMPTY);
                }
            } else if (line.contains(CONNECT)) {
                String[] vals = line.substring(line.indexOf(':')).split(",");
                last.getClass().getDeclaredMethod("addConnection", String.class, String.class, String.class).invoke(last, vals[0].replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY), vals[1].replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY), vals[2].replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY));
                return;
            }
            Field field = obj.getClass().getField(prop);
            Class<?> clazz = field.getType();
            if (clazz.equals(Integer.class)) {
                // TODO investigate why there are multiple values in TextureId sometimes
                if (val.split(",").length > 0)
                    val = val.split(",")[0];
                field.set(obj, Integer.valueOf(val.replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY)));
            } else if (clazz.equals(String.class))
                field.set(obj, val.replaceAll(REGEX_NO_QUOTE, REPLACE_EMPTY));
            else if (clazz.equals(Long.class))
                field.set(obj, Long.valueOf(val.replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY)));
            else if (clazz.equals(Float.class))
                field.set(obj, Float.valueOf(val.replaceAll(REGEX_NO_SPACE_NO_QUOTE, REPLACE_EMPTY)));
            else if (clazz.equals(Vector3.class)) {
                field.set(obj, new Vector3(val.split(",")));
            } else if (clazz.equals(FBXFloatBuffer.class)) {
                StringBuffer sb = new StringBuffer(val);
                String noSpace;
                while ((line = buffer.readLine()) != null) {
                    noSpace = line.replaceAll("\\s", REPLACE_EMPTY);
                    if (noSpace.length() > 0 && noSpace.charAt(0) == ',')
                        sb.append(noSpace);
                    else {
                        processNextLine = true;
                        break;
                    }
                }
                field.set(obj, new FBXFloatBuffer(sb.toString()));
            } else if (clazz.equals(FBXIntBuffer.class)) {
                StringBuffer sb = new StringBuffer(val);
                String noSpace;
                while ((line = buffer.readLine()) != null) {
                    noSpace = line.replaceAll("\\s", REPLACE_EMPTY);
                    if (noSpace.length() > 0 && noSpace.charAt(0) == ',')
                        sb.append(noSpace);
                    else {
                        processNextLine = true;
                        break;
                    }
                }
                field.set(obj, new FBXIntBuffer(sb.toString()));
            } else if (clazz.equals(FBXMatrix.class)) {
                StringBuffer sb = new StringBuffer(val);
                String noSpace;
                while ((line = buffer.readLine()) != null) {
                    noSpace = line.replaceAll(REGEX_CLEAN, REPLACE_EMPTY);
                    if (noSpace.length() > 0 && noSpace.charAt(0) == ',')
                        sb.append(noSpace);
                    else {
                        processNextLine = true;
                        break;
                    }
                }
                field.set(obj, new FBXMatrix(sb.toString()));
            } else if (clazz.equals(FBXColor4.class)) {
                field.set(obj, new FBXColor4(val));
            } else if (clazz.equals(Vector2.class)) {
                field.set(obj, new Vector2(val.replaceAll("\\s", REPLACE_EMPTY).split(",")));
            }
            if (processNextLine && line.replaceAll(REGEX_CLEAN, REPLACE_EMPTY).length() > 0)
                readLine(buffer, line);
        } catch (NoSuchFieldException e) {
            return;
        }
    }
}
Also used : FBXColor4(org.rajawali3d.loader.fbx.FBXValues.FBXColor4) FBXMatrix(org.rajawali3d.loader.fbx.FBXValues.FBXMatrix) Vector3(org.rajawali3d.math.vector.Vector3) Field(java.lang.reflect.Field) FBXFloatBuffer(org.rajawali3d.loader.fbx.FBXValues.FBXFloatBuffer) FBXIntBuffer(org.rajawali3d.loader.fbx.FBXValues.FBXIntBuffer) Vector2(org.rajawali3d.math.vector.Vector2)

Aggregations

Field (java.lang.reflect.Field)1 FBXColor4 (org.rajawali3d.loader.fbx.FBXValues.FBXColor4)1 FBXFloatBuffer (org.rajawali3d.loader.fbx.FBXValues.FBXFloatBuffer)1 FBXIntBuffer (org.rajawali3d.loader.fbx.FBXValues.FBXIntBuffer)1 FBXMatrix (org.rajawali3d.loader.fbx.FBXValues.FBXMatrix)1 Vector2 (org.rajawali3d.math.vector.Vector2)1 Vector3 (org.rajawali3d.math.vector.Vector3)1