Search in sources :

Example 1 with OgreMaterialKey

use of com.jme3.scene.plugins.ogre.matext.OgreMaterialKey in project jmonkeyengine by jMonkeyEngine.

the class SceneMaterialLoader method startElement.

@Override
public void startElement(String uri, String localName, String qName, Attributes attribs) throws SAXException {
    if (qName.equals("externals")) {
        checkTopNode("scene");
        // Has an externals block, create material list.
        materialList = new MaterialList();
    } else if (qName.equals("item")) {
        checkTopNode("externals");
        if (!attribs.getValue("type").equals("material")) {
            // This is not a material external. Ignore it.
            ignoreItem = true;
        }
    } else if (qName.equals("file")) {
        checkTopNode("item");
        if (!ignoreItem) {
            String materialPath = attribs.getValue("name");
            String materialName = new File(materialPath).getName();
            String matFile = folderName + materialName;
            try {
                MaterialList loadedMaterialList = (MaterialList) assetManager.loadAsset(new OgreMaterialKey(matFile));
                materialList.putAll(loadedMaterialList);
            } catch (AssetNotFoundException ex) {
                logger.log(Level.WARNING, "Cannot locate material file: {0}", matFile);
            }
        }
    }
    elementStack.push(qName);
}
Also used : MaterialList(com.jme3.material.MaterialList) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) AssetNotFoundException(com.jme3.asset.AssetNotFoundException) File(java.io.File)

Example 2 with OgreMaterialKey

use of com.jme3.scene.plugins.ogre.matext.OgreMaterialKey in project jmonkeyengine by jMonkeyEngine.

the class MeshLoader method load.

public Object load(AssetInfo info) throws IOException {
    try {
        key = info.getKey();
        meshName = key.getName();
        folderName = key.getFolder();
        String ext = key.getExtension();
        meshName = meshName.substring(0, meshName.length() - ext.length() - 1);
        if (folderName != null && folderName.length() > 0) {
            meshName = meshName.substring(folderName.length());
        }
        assetManager = info.getManager();
        if (key instanceof OgreMeshKey) {
            // OgreMeshKey is being used, try getting the material list
            // from it
            OgreMeshKey meshKey = (OgreMeshKey) key;
            materialList = meshKey.getMaterialList();
            String materialName = meshKey.getMaterialName();
            // Material list not set but material name is available
            if (materialList == null && materialName != null) {
                OgreMaterialKey materialKey = new OgreMaterialKey(folderName + materialName + ".material");
                try {
                    materialList = (MaterialList) assetManager.loadAsset(materialKey);
                } catch (AssetNotFoundException e) {
                    logger.log(Level.WARNING, "Cannot locate {0} for model {1}", new Object[] { materialKey, key });
                }
            }
        } else {
            // Make sure to reset it to null so that previous state
            // doesn't leak onto this one
            materialList = null;
        }
        // default method.
        if (materialList == null) {
            OgreMaterialKey materialKey = new OgreMaterialKey(folderName + meshName + ".material");
            try {
                materialList = (MaterialList) assetManager.loadAsset(materialKey);
            } catch (AssetNotFoundException e) {
                logger.log(Level.WARNING, "Cannot locate {0} for model {1}", new Object[] { materialKey, key });
            }
        }
        // Added by larynx 25.06.2011
        // Android needs the namespace aware flag set to true                 
        // Kirill 30.06.2011
        // Now, hack is applied for both desktop and android to avoid
        // checking with JmeSystem.
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader xr = factory.newSAXParser().getXMLReader();
        xr.setContentHandler(this);
        xr.setErrorHandler(this);
        InputStreamReader r = null;
        try {
            r = new InputStreamReader(info.openStream());
            xr.parse(new InputSource(r));
        } finally {
            if (r != null) {
                r.close();
            }
        }
        return compileModel();
    } catch (SAXException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D mesh.xml");
        ioEx.initCause(ex);
        throw ioEx;
    } catch (ParserConfigurationException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D mesh.xml");
        ioEx.initCause(ex);
        throw ioEx;
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 3 with OgreMaterialKey

use of com.jme3.scene.plugins.ogre.matext.OgreMaterialKey in project jmonkeyengine by jMonkeyEngine.

the class SceneLoader method load.

public Object load(AssetInfo info) throws IOException {
    try {
        key = info.getKey();
        assetManager = info.getManager();
        sceneName = key.getName();
        String ext = key.getExtension();
        folderName = key.getFolder();
        sceneName = sceneName.substring(0, sceneName.length() - ext.length() - 1);
        reset();
        // == Run 1st pass over XML file to determine material list ==
        materialList = materialLoader.load(assetManager, folderName, info.openStream());
        if (materialList == null || materialList.isEmpty()) {
            // NOTE: No materials were found by searching the externals section.
            // Try finding a similarly named material file in the same folder.
            // (Backward compatibility only!)
            OgreMaterialKey materialKey = new OgreMaterialKey(sceneName + ".material");
            try {
                materialList = (MaterialList) assetManager.loadAsset(materialKey);
            } catch (AssetNotFoundException ex) {
                logger.log(Level.WARNING, "Cannot locate {0} for scene {1}", new Object[] { materialKey, key });
                materialList = null;
            }
        }
        // == Run 2nd pass to load entities and other objects ==
        // Added by larynx 25.06.2011
        // Android needs the namespace aware flag set to true 
        // Kirill 30.06.2011
        // Now, hack is applied for both desktop and android to avoid
        // checking with JmeSystem.
        SAXParserFactory factory = SAXParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XMLReader xr = factory.newSAXParser().getXMLReader();
        xr.setContentHandler(this);
        xr.setErrorHandler(this);
        InputStreamReader r = null;
        try {
            r = new InputStreamReader(info.openStream());
            xr.parse(new InputSource(r));
        } finally {
            if (r != null) {
                r.close();
            }
        }
        return root;
    } catch (SAXException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D dotScene");
        ioEx.initCause(ex);
        throw ioEx;
    } catch (ParserConfigurationException ex) {
        IOException ioEx = new IOException("Error while parsing Ogre3D dotScene");
        ioEx.initCause(ex);
        throw ioEx;
    }
}
Also used : InputSource(org.xml.sax.InputSource) InputStreamReader(java.io.InputStreamReader) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) IOException(java.io.IOException) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) XMLReader(org.xml.sax.XMLReader) SAXParserFactory(javax.xml.parsers.SAXParserFactory) SAXException(org.xml.sax.SAXException)

Example 4 with OgreMaterialKey

use of com.jme3.scene.plugins.ogre.matext.OgreMaterialKey in project jmonkeyengine by jMonkeyEngine.

the class MaterialLoader method load.

private MaterialList load(AssetManager assetManager, AssetKey key, InputStream in) throws IOException {
    folderName = key.getFolder();
    this.assetManager = assetManager;
    MaterialList list = null;
    List<Statement> statements = BlockLanguageParser.parse(in);
    for (Statement statement : statements) {
        if (statement.getLine().startsWith("import")) {
            MaterialExtensionSet matExts = null;
            if (key instanceof OgreMaterialKey) {
                matExts = ((OgreMaterialKey) key).getMaterialExtensionSet();
            }
            if (matExts == null) {
                throw new IOException("Must specify MaterialExtensionSet when loading\n" + "Ogre3D materials with extended materials");
            }
            list = new MaterialExtensionLoader().load(assetManager, key, matExts, statements);
            break;
        } else if (statement.getLine().startsWith("material")) {
            if (list == null) {
                list = new MaterialList();
            }
            String[] split = statement.getLine().split(" ", 2);
            matName = split[1].trim();
            if (matName.startsWith("\"") && matName.endsWith("\"")) {
                matName = matName.substring(1, matName.length() - 1);
            }
            readMaterial(statement);
            Material mat = compileMaterial();
            list.put(mat.getName(), mat);
        }
    }
    return list;
}
Also used : MaterialExtensionLoader(com.jme3.scene.plugins.ogre.matext.MaterialExtensionLoader) Statement(com.jme3.util.blockparser.Statement) MaterialList(com.jme3.material.MaterialList) OgreMaterialKey(com.jme3.scene.plugins.ogre.matext.OgreMaterialKey) Material(com.jme3.material.Material) IOException(java.io.IOException) MaterialExtensionSet(com.jme3.scene.plugins.ogre.matext.MaterialExtensionSet)

Aggregations

OgreMaterialKey (com.jme3.scene.plugins.ogre.matext.OgreMaterialKey)4 IOException (java.io.IOException)3 MaterialList (com.jme3.material.MaterialList)2 InputStreamReader (java.io.InputStreamReader)2 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)2 SAXParserFactory (javax.xml.parsers.SAXParserFactory)2 InputSource (org.xml.sax.InputSource)2 SAXException (org.xml.sax.SAXException)2 XMLReader (org.xml.sax.XMLReader)2 AssetNotFoundException (com.jme3.asset.AssetNotFoundException)1 Material (com.jme3.material.Material)1 MaterialExtensionLoader (com.jme3.scene.plugins.ogre.matext.MaterialExtensionLoader)1 MaterialExtensionSet (com.jme3.scene.plugins.ogre.matext.MaterialExtensionSet)1 Statement (com.jme3.util.blockparser.Statement)1 File (java.io.File)1