FileDocCategorySizeDatePackage
ColladaParser.javaAPI DocAndroid 5.1 API20967Thu Mar 12 22:22:44 GMT 2015com.android.scenegraph

ColladaParser

public class ColladaParser extends Object

Fields Summary
static final String
TAG
Document
mDom
HashMap
mLights
HashMap
mCameras
HashMap
mEffectsParams
HashMap
mImages
HashMap
mSamplerImageMap
HashMap
mMeshIdNameMap
Scene
mScene
String
mRootDir
Constructors Summary
public ColladaParser()

        mLights = new HashMap<String, LightBase>();
        mCameras = new HashMap<String, Camera>();
        mEffectsParams = new HashMap<String, ArrayList<ShaderParam> >();
        mImages = new HashMap<String, Texture2D>();
        mMeshIdNameMap = new HashMap<String, String>();
    
Methods Summary
private voidconvertCamera(org.w3c.dom.Element camera)

        String name = camera.getAttribute("name");
        String id = camera.getAttribute("id");
        float fov = 30.0f;
        if (getString(camera, "yfov") != null) {
            fov = getFloat(camera, "yfov");
        } else if(getString(camera, "xfov") != null) {
            float aspect = getFloat(camera, "aspect_ratio");
            fov = getFloat(camera, "xfov") / aspect;
        }

        float near = getFloat(camera, "znear");
        float far = getFloat(camera, "zfar");

        Camera sceneCamera = new Camera();
        sceneCamera.setFOV(fov);
        sceneCamera.setNear(near);
        sceneCamera.setFar(far);
        sceneCamera.setName(name);
        mScene.appendCamera(sceneCamera);
        mCameras.put(id, sceneCamera);
    
private voidconvertEffects(org.w3c.dom.Element fx)

        String id = fx.getAttribute("id");
        ArrayList<ShaderParam> params = new ArrayList<ShaderParam>();

        NodeList nl = fx.getElementsByTagName("newparam");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element field = (Element)nl.item(i);
                field.setIdAttribute("sid", true);
            }
        }

        nl = fx.getElementsByTagName("blinn");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element field = (Element)nl.item(i);
                //Log.v(TAG, "blinn");
                extractParams(field, params);
            }
        }
        nl = fx.getElementsByTagName("lambert");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element field = (Element)nl.item(i);
                //Log.v(TAG, "lambert");
                extractParams(field, params);
            }
        }
        nl = fx.getElementsByTagName("phong");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element field = (Element)nl.item(i);
                //Log.v(TAG, "phong");
                extractParams(field, params);
            }
        }
        mEffectsParams.put(id, params);
    
private voidconvertGeometries(org.w3c.dom.Element geo)

        String id = geo.getAttribute("id");
        String name = geo.getAttribute("name");
        if (!id.equals(name)) {
            mMeshIdNameMap.put(id, name);
        }
    
private voidconvertImage(org.w3c.dom.Element img)

        String name = img.getAttribute("name");
        String id = img.getAttribute("id");
        String file = getString(img, "init_from");

        Texture2D tex = new Texture2D();
        tex.setFileName(file);
        tex.setFileDir(mRootDir);
        mScene.appendTextures(tex);
        mImages.put(id, tex);
    
private voidconvertLight(org.w3c.dom.Element light)

        String name = light.getAttribute("name");
        String id = light.getAttribute("id");

        // Determine type
        String[] knownTypes = { "point", "spot", "directional" };
        final int POINT_LIGHT = 0;
        final int SPOT_LIGHT = 1;
        final int DIR_LIGHT = 2;
        int type = -1;
        for (int i = 0; i < knownTypes.length; i ++) {
            NodeList nl = light.getElementsByTagName(knownTypes[i]);
            if (nl != null && nl.getLength() != 0) {
                type = i;
                break;
            }
        }

        //Log.v(TAG, "Found Light Type " + type);

        LightBase sceneLight = null;
        switch (type) {
        case POINT_LIGHT:
            sceneLight = new PointLight();
            break;
        case SPOT_LIGHT: // TODO: finish light types
            break;
        case DIR_LIGHT: // TODO: finish light types
            break;
        }

        if (sceneLight == null) {
            return;
        }

        Float3 color = getFloat3(light, "color");
        sceneLight.setColor(color.x, color.y, color.z);
        sceneLight.setName(name);
        mScene.appendLight(sceneLight);
        mLights.put(id, sceneLight);

        //Log.v(TAG, "Light " + name + " color " + toString(color));
    
private voidconvertMaterials(org.w3c.dom.Element mat)

        String id = mat.getAttribute("id");
        NodeList nl = mat.getElementsByTagName("instance_effect");
        if (nl != null && nl.getLength() == 1) {
            Element ref = (Element)nl.item(0);
            String url = ref.getAttribute("url");
            ArrayList<ShaderParam> params = mEffectsParams.get(url.substring(1));
            mEffectsParams.put(id, params);
        }
    
private voidexportSceneData()

        mScene = new Scene();

        Element docEle = mDom.getDocumentElement();
        NodeList nl = docEle.getElementsByTagName("light");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element l = (Element)nl.item(i);
                convertLight(l);
            }
        }

        nl = docEle.getElementsByTagName("camera");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element c = (Element)nl.item(i);
                convertCamera(c);
            }
        }

        nl = docEle.getElementsByTagName("image");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element img = (Element)nl.item(i);
                convertImage(img);
            }
        }

        nl = docEle.getElementsByTagName("effect");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element e = (Element)nl.item(i);
                convertEffects(e);
            }
        }

        // Material is just a link to the effect
        nl = docEle.getElementsByTagName("material");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element m = (Element)nl.item(i);
                convertMaterials(m);
            }
        }

        // Look through the geometry list and build up a correlation between id's and names
        nl = docEle.getElementsByTagName("geometry");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element m = (Element)nl.item(i);
                convertGeometries(m);
            }
        }


        nl = docEle.getElementsByTagName("visual_scene");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element s = (Element)nl.item(i);
                getScene(s);
            }
        }
    
voidextractParams(org.w3c.dom.Element fx, java.util.ArrayList params)

        Node paramNode = fx.getFirstChild();
        while (paramNode != null) {
            if (paramNode.getNodeType() == Node.ELEMENT_NODE) {
                String name = paramNode.getNodeName();
                // Now find what type it is
                Node typeNode = paramNode.getFirstChild();
                while (typeNode != null && typeNode.getNodeType() != Node.ELEMENT_NODE) {
                    typeNode = typeNode.getNextSibling();
                }
                String paramType = typeNode.getNodeName();
                Element typeElem = (Element)typeNode;
                ShaderParam sceneParam = null;
                if (paramType.equals("color")) {
                    Float4Param f4p = new Float4Param(name);
                    Float4 value = getFloat4(typeElem);
                    f4p.setValue(value);
                    sceneParam = f4p;
                    //Log.v(TAG, "Extracted " + sceneParam.getParamName() + " value " + toString(value));
                } else if (paramType.equals("float")) {
                    Float4Param f4p = new Float4Param(name);
                    float value = getFloat(typeElem);
                    f4p.setValue(new Float4(value, value, value, value));
                    sceneParam = f4p;
                    //Log.v(TAG, "Extracted " + sceneParam.getParamName() + " value " + value);
                }  else if (paramType.equals("texture")) {
                    String samplerName = typeElem.getAttribute("texture");
                    Texture2D tex = getTexture(samplerName);
                    TextureParam texP = new TextureParam(name);
                    texP.setTexture(tex);
                    sceneParam = texP;
                    //Log.v(TAG, "Extracted texture " + tex);
                }
                if (sceneParam != null) {
                    params.add(sceneParam);
                }
            }
            paramNode = paramNode.getNextSibling();
        }
    
private floatgetFloat(org.w3c.dom.Element elem, java.lang.String name)

        return Float.parseFloat(getString(elem, name));
    
private floatgetFloat(org.w3c.dom.Element elem)

        return Float.parseFloat(getString(elem));
    
private Float3getFloat3(org.w3c.dom.Element elem, java.lang.String name)

        String valueString = getString(elem, name);
        return parseFloat3(valueString);
    
private Float3getFloat3(org.w3c.dom.Element elem)

        String valueString = getString(elem);
        return parseFloat3(valueString);
    
private Float4getFloat4(org.w3c.dom.Element elem, java.lang.String name)

        String valueString = getString(elem, name);
        return parseFloat4(valueString);
    
private Float4getFloat4(org.w3c.dom.Element elem)

        String valueString = getString(elem);
        return parseFloat4(valueString);
    
private intgetInt(org.w3c.dom.Element elem, java.lang.String name)

        return Integer.parseInt(getString(elem, name));
    
private voidgetNode(org.w3c.dom.Element node, Transform parent, java.lang.String indent)

        String name = node.getAttribute("name");
        String id = node.getAttribute("id");
        CompoundTransform current = new CompoundTransform();
        current.setName(name);
        if (parent != null) {
            parent.appendChild(current);
        } else {
            mScene.appendTransform(current);
        }

        mScene.addToTransformMap(current);

        //Log.v(TAG, indent + "|");
        //Log.v(TAG, indent + "[" + name + "]");

        Node childNode = node.getFirstChild();
        while (childNode != null) {
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                Element field = (Element)childNode;
                String fieldName = field.getTagName();
                String description = field.getAttribute("sid");
                if (fieldName.equals("translate")) {
                    Float3 value = getFloat3(field);
                    current.addTranslate(description, value);
                    //Log.v(TAG, indent + " translate " + description + toString(value));
                } else if (fieldName.equals("rotate")) {
                    Float4 value = getFloat4(field);
                    //Log.v(TAG, indent + " rotate " + description + toString(value));
                    Float3 axis = new Float3(value.x, value.y, value.z);
                    current.addRotate(description, axis, value.w);
                } else if (fieldName.equals("scale")) {
                    Float3 value = getFloat3(field);
                    //Log.v(TAG, indent + " scale " + description + toString(value));
                    current.addScale(description, value);
                } else if (fieldName.equals("instance_geometry")) {
                    getRenderable(field, current);
                } else if (fieldName.equals("instance_light")) {
                    updateLight(field, current);
                } else if (fieldName.equals("instance_camera")) {
                    updateCamera(field, current);
                } else if (fieldName.equals("node")) {
                    getNode(field, current, indent + "   ");
                }
            }
            childNode = childNode.getNextSibling();
        }
    
private voidgetRenderable(org.w3c.dom.Element shape, Transform t)

        String geoURL = shape.getAttribute("url").substring(1);
        String geoName = mMeshIdNameMap.get(geoURL);
        if (geoName != null) {
            geoURL = geoName;
        }
        //RenderableGroup group = new RenderableGroup();
        //group.setName(geoURL.substring(1));
        //mScene.appendRenderable(group);
        NodeList nl = shape.getElementsByTagName("instance_material");
        if (nl != null) {
            for(int i = 0; i < nl.getLength(); i++) {
                Element materialRef = (Element)nl.item(i);
                String meshIndexName = materialRef.getAttribute("symbol");
                String materialName = materialRef.getAttribute("target");

                Renderable d = new Renderable();
                d.setMesh(geoURL, meshIndexName);
                d.setMaterialName(materialName.substring(1));
                d.setName(geoURL);

                //Log.v(TAG, "Created drawable geo " + geoURL + " index " + meshIndexName + " material " + materialName);

                d.setTransform(t);
                //Log.v(TAG, "Set source param " + t.getName());

                // Now find all the parameters that exist on the material
                ArrayList<ShaderParam> materialParams;
                materialParams = mEffectsParams.get(materialName.substring(1));
                for (int pI = 0; pI < materialParams.size(); pI ++) {
                    d.appendSourceParams(materialParams.get(pI));
                    //Log.v(TAG, "Set source param i: " + pI + " name " + materialParams.get(pI).getParamName());
                }
                mScene.appendRenderable(d);
                //group.appendChildren(d);
            }
        }
    
private voidgetScene(org.w3c.dom.Element scene)

        String name = scene.getAttribute("name");
        String id = scene.getAttribute("id");

        Node childNode = scene.getFirstChild();
        while (childNode != null) {
            if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                String indent = "";
                getNode((Element)childNode, null, indent);
            }
            childNode = childNode.getNextSibling();
        }
    
ScenegetScene()

        return mScene;
    
private java.lang.StringgetString(org.w3c.dom.Element elem, java.lang.String name)

        String text = null;
        NodeList nl = elem.getElementsByTagName(name);
        if (nl != null && nl.getLength() != 0) {
            text = ((Element)nl.item(0)).getFirstChild().getNodeValue();
        }
        return text;
    
private java.lang.StringgetString(org.w3c.dom.Element elem)

        String text = null;
        text = elem.getFirstChild().getNodeValue();
        return text;
    
Texture2DgetTexture(java.lang.String samplerName)

        String texName = samplerName;

        // Check to see if the image file is hidden by a sampler surface link combo
        Element sampler = mDom.getElementById(samplerName);
        if (sampler != null) {
            NodeList nl = sampler.getElementsByTagName("source");
            if (nl != null && nl.getLength() == 1) {
                Element ref = (Element)nl.item(0);
                String surfaceName = getString(ref);
                if (surfaceName == null) {
                    return null;
                }

                Element surface = mDom.getElementById(surfaceName);
                if (surface == null) {
                    return null;
                }
                nl = surface.getElementsByTagName("init_from");
                if (nl != null && nl.getLength() == 1) {
                    ref = (Element)nl.item(0);
                    texName = getString(ref);
                }
            }
        }

        //Log.v(TAG, "Extracted texture name " + texName);
        return mImages.get(texName);
    
public voidinit(java.io.InputStream is, java.lang.String rootDir)

        mLights.clear();
        mCameras.clear();
        mEffectsParams.clear();

        mRootDir = rootDir;

        long start = System.currentTimeMillis();
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder db = dbf.newDocumentBuilder();
            mDom = db.parse(is);
        } catch(ParserConfigurationException e) {
            e.printStackTrace();
        } catch(SAXException e) {
            e.printStackTrace();
        } catch(IOException e) {
            e.printStackTrace();
        }
        long end = System.currentTimeMillis();
        Log.v("TIMER", "    Parse time: " + (end - start));
        exportSceneData();
    
private Float3parseFloat3(java.lang.String valueString)

        StringTokenizer st = new StringTokenizer(valueString);
        float x = Float.parseFloat(st.nextToken());
        float y = Float.parseFloat(st.nextToken());
        float z = Float.parseFloat(st.nextToken());
        return new Float3(x, y, z);
    
private Float4parseFloat4(java.lang.String valueString)

        StringTokenizer st = new StringTokenizer(valueString);
        float x = Float.parseFloat(st.nextToken());
        float y = Float.parseFloat(st.nextToken());
        float z = Float.parseFloat(st.nextToken());
        float w = Float.parseFloat(st.nextToken());
        return new Float4(x, y, z, w);
    
java.lang.StringtoString(Float3 v)


       
        String valueStr = v.x + " " + v.y + " " + v.z;
        return valueStr;
    
java.lang.StringtoString(Float4 v)

        String valueStr = v.x + " " + v.y + " " + v.z + " " + v.w;
        return valueStr;
    
private voidupdateCamera(org.w3c.dom.Element shape, Transform t)

        String camURL = shape.getAttribute("url");
        // collada uses a uri structure to link things,
        // but we ignore it for now and do a simple search
        Camera cam = mCameras.get(camURL.substring(1));
        if (cam != null) {
            cam.setTransform(t);
            //Log.v(TAG, "Set Camera " + cam.getName() + " " + t.getName());
        }
    
private voidupdateLight(org.w3c.dom.Element shape, Transform t)

        String lightURL = shape.getAttribute("url");
        // collada uses a uri structure to link things,
        // but we ignore it for now and do a simple search
        LightBase light = mLights.get(lightURL.substring(1));
        if (light != null) {
            light.setTransform(t);
            //Log.v(TAG, "Set Light " + light.getName() + " " + t.getName());
        }