OGRE MaterialLoader extended

Hello,



It is only a small problem, but I got it fixed today:

When using MaterialLoader from package com.jmex.model.ogrexml,

It always annoyed me, that all materials must be inside one text file.



So instead of a material loader, I implemented a small material adder, derived from the loader class.

Maybe someone finds it useful.

I guess, I will have to plan a material manager, when my project leaves the early test phases.



By the way - good work implementing the ogre import!  :slight_smile:



My little class:





import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.Map;
import java.util.logging.Logger;

import com.jme.util.resource.RelativeResourceLocator;
import com.jme.util.resource.ResourceLocator;
import com.jme.util.resource.ResourceLocatorTool;
import com.jmex.model.ogrexml.Material;
import com.jmex.model.ogrexml.MaterialLoader;


public class OgreMaterialAdder extends MaterialLoader {
    private static final Logger logger =
        Logger.getLogger(OgreMaterialAdder.class.getName());

   
    /**
     * ADD the materials from the given stream to this MaterialAdder's map of available materials.
     */
    public void add(InputStream in) throws IOException {
       Map<String, Material> materials = this.getMaterials();
        load(in);
        if(materials != null)
           this.getMaterials().putAll(materials);
    }

    /**
     * Convenience wrapper.
     *
     * @see #add(URL)
     */
    public void add(URI uri) throws IOException{
        add(uri.toURL());
    }

    /**
     * ADD the materials from the url to the materials of this MaterialAdder,
     * automatically adding the containing directory to the resource locator
     * paths for the duration of the add.
     */
    public void add(URL url) throws IOException{
        ResourceLocator locator = null;
        try {
            locator = new RelativeResourceLocator(url);
        } catch (URISyntaxException use) {
            throw new IllegalArgumentException("Bad URL: " + use);
        }
        ResourceLocatorTool.addResourceLocator(
                ResourceLocatorTool.TYPE_TEXTURE, locator);
        InputStream stream = null;
        try {
            stream = url.openStream();
            if (stream == null) {
                throw new IOException("Failed to add materials file '"
                        + url + "'");
            }
            logger.fine("adding materials from '" + url + "'...");
            add(stream);
        } finally {
            if (stream != null) stream.close();
            ResourceLocatorTool.removeResourceLocator(
                    ResourceLocatorTool.TYPE_TEXTURE, locator);
            locator = null;  // Just to encourage GC
        }
    }
}




Use case:
You have many models with seperate .material files.
Load in all the materials (adding all file contents to the material adder).
Load in all the models, assigning them the materials map.


Would be nice to read some replies that give a little more impulse to this topic of material / model management...