Search in sources :

Example 1 with ModelTexture

use of com.badlogic.gdx.graphics.g3d.model.data.ModelTexture in project nhglib by VoidZombie.

the class NhgModelLoader method getDependencies.

@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, P parameters) {
    final Array<AssetDescriptor> deps = new Array();
    ModelData data = loadModelData(file, parameters);
    if (data == null)
        return deps;
    ObjectMap.Entry<String, ModelData> item = new ObjectMap.Entry<String, ModelData>();
    item.key = fileName;
    item.value = data;
    synchronized (items) {
        items.add(item);
    }
    TextureLoader.TextureParameter textureParameter = (parameters != null) ? parameters.textureParameter : defaultParameters.textureParameter;
    for (final ModelMaterial modelMaterial : data.materials) {
        if (modelMaterial.textures != null) {
            for (final ModelTexture modelTexture : modelMaterial.textures) {
                String fName = modelTexture.fileName;
                if (fName.contains("/")) {
                    fName = fName.substring(fName.lastIndexOf("/") + 1);
                }
                deps.add(new AssetDescriptor(currentAsset.dependenciesPath + fName, Texture.class, textureParameter));
            }
        }
    }
    return deps;
}
Also used : ModelData(com.badlogic.gdx.graphics.g3d.model.data.ModelData) TextureLoader(com.badlogic.gdx.assets.loaders.TextureLoader) ModelTexture(com.badlogic.gdx.graphics.g3d.model.data.ModelTexture) Texture(com.badlogic.gdx.graphics.Texture) ModelTexture(com.badlogic.gdx.graphics.g3d.model.data.ModelTexture) Array(com.badlogic.gdx.utils.Array) ObjectMap(com.badlogic.gdx.utils.ObjectMap) ModelMaterial(com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor)

Example 2 with ModelTexture

use of com.badlogic.gdx.graphics.g3d.model.data.ModelTexture in project libgdx by libgdx.

the class ModelLoader method getDependencies.

@Override
public Array<AssetDescriptor> getDependencies(String fileName, FileHandle file, P parameters) {
    final Array<AssetDescriptor> deps = new Array();
    ModelData data = loadModelData(file, parameters);
    if (data == null)
        return deps;
    ObjectMap.Entry<String, ModelData> item = new ObjectMap.Entry<String, ModelData>();
    item.key = fileName;
    item.value = data;
    synchronized (items) {
        items.add(item);
    }
    TextureLoader.TextureParameter textureParameter = (parameters != null) ? parameters.textureParameter : defaultParameters.textureParameter;
    for (final ModelMaterial modelMaterial : data.materials) {
        if (modelMaterial.textures != null) {
            for (final ModelTexture modelTexture : modelMaterial.textures) deps.add(new AssetDescriptor(modelTexture.fileName, Texture.class, textureParameter));
        }
    }
    return deps;
}
Also used : Array(com.badlogic.gdx.utils.Array) ModelData(com.badlogic.gdx.graphics.g3d.model.data.ModelData) ObjectMap(com.badlogic.gdx.utils.ObjectMap) ModelMaterial(com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial) AssetDescriptor(com.badlogic.gdx.assets.AssetDescriptor) ModelTexture(com.badlogic.gdx.graphics.g3d.model.data.ModelTexture)

Example 3 with ModelTexture

use of com.badlogic.gdx.graphics.g3d.model.data.ModelTexture in project libgdx by libgdx.

the class G3dModelLoader method parseMaterials.

private void parseMaterials(ModelData model, JsonValue json, String materialDir) {
    JsonValue materials = json.get("materials");
    if (materials == null) {
    // we should probably create some default material in this case
    } else {
        model.materials.ensureCapacity(materials.size);
        for (JsonValue material = materials.child; material != null; material = material.next) {
            ModelMaterial jsonMaterial = new ModelMaterial();
            String id = material.getString("id", null);
            if (id == null)
                throw new GdxRuntimeException("Material needs an id.");
            jsonMaterial.id = id;
            // Read material colors
            final JsonValue diffuse = material.get("diffuse");
            if (diffuse != null)
                jsonMaterial.diffuse = parseColor(diffuse);
            final JsonValue ambient = material.get("ambient");
            if (ambient != null)
                jsonMaterial.ambient = parseColor(ambient);
            final JsonValue emissive = material.get("emissive");
            if (emissive != null)
                jsonMaterial.emissive = parseColor(emissive);
            final JsonValue specular = material.get("specular");
            if (specular != null)
                jsonMaterial.specular = parseColor(specular);
            final JsonValue reflection = material.get("reflection");
            if (reflection != null)
                jsonMaterial.reflection = parseColor(reflection);
            // Read shininess
            jsonMaterial.shininess = material.getFloat("shininess", 0.0f);
            // Read opacity
            jsonMaterial.opacity = material.getFloat("opacity", 1.0f);
            // Read textures
            JsonValue textures = material.get("textures");
            if (textures != null) {
                for (JsonValue texture = textures.child; texture != null; texture = texture.next) {
                    ModelTexture jsonTexture = new ModelTexture();
                    String textureId = texture.getString("id", null);
                    if (textureId == null)
                        throw new GdxRuntimeException("Texture has no id.");
                    jsonTexture.id = textureId;
                    String fileName = texture.getString("filename", null);
                    if (fileName == null)
                        throw new GdxRuntimeException("Texture needs filename.");
                    jsonTexture.fileName = materialDir + (materialDir.length() == 0 || materialDir.endsWith("/") ? "" : "/") + fileName;
                    jsonTexture.uvTranslation = readVector2(texture.get("uvTranslation"), 0f, 0f);
                    jsonTexture.uvScaling = readVector2(texture.get("uvScaling"), 1f, 1f);
                    String textureType = texture.getString("type", null);
                    if (textureType == null)
                        throw new GdxRuntimeException("Texture needs type.");
                    jsonTexture.usage = parseTextureUsage(textureType);
                    if (jsonMaterial.textures == null)
                        jsonMaterial.textures = new Array<ModelTexture>();
                    jsonMaterial.textures.add(jsonTexture);
                }
            }
            model.materials.add(jsonMaterial);
        }
    }
}
Also used : GdxRuntimeException(com.badlogic.gdx.utils.GdxRuntimeException) Array(com.badlogic.gdx.utils.Array) JsonValue(com.badlogic.gdx.utils.JsonValue) ModelMaterial(com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial) ModelTexture(com.badlogic.gdx.graphics.g3d.model.data.ModelTexture)

Example 4 with ModelTexture

use of com.badlogic.gdx.graphics.g3d.model.data.ModelTexture in project libgdx by libgdx.

the class MtlLoader method load.

/** loads .mtl file */
public void load(FileHandle file) {
    String line;
    String[] tokens;
    String curMatName = "default";
    Color difcolor = Color.WHITE;
    Color speccolor = Color.WHITE;
    float opacity = 1.f;
    float shininess = 0.f;
    String texFilename = null;
    if (file == null || file.exists() == false)
        return;
    BufferedReader reader = new BufferedReader(new InputStreamReader(file.read()), 4096);
    try {
        while ((line = reader.readLine()) != null) {
            if (line.length() > 0 && line.charAt(0) == '\t')
                line = line.substring(1).trim();
            tokens = line.split("\\s+");
            if (tokens[0].length() == 0) {
                continue;
            } else if (tokens[0].charAt(0) == '#')
                continue;
            else {
                final String key = tokens[0].toLowerCase();
                if (key.equals("newmtl")) {
                    ModelMaterial mat = new ModelMaterial();
                    mat.id = curMatName;
                    mat.diffuse = new Color(difcolor);
                    mat.specular = new Color(speccolor);
                    mat.opacity = opacity;
                    mat.shininess = shininess;
                    if (texFilename != null) {
                        ModelTexture tex = new ModelTexture();
                        tex.usage = ModelTexture.USAGE_DIFFUSE;
                        tex.fileName = new String(texFilename);
                        if (mat.textures == null)
                            mat.textures = new Array<ModelTexture>(1);
                        mat.textures.add(tex);
                    }
                    materials.add(mat);
                    if (tokens.length > 1) {
                        curMatName = tokens[1];
                        curMatName = curMatName.replace('.', '_');
                    } else
                        curMatName = "default";
                    difcolor = Color.WHITE;
                    speccolor = Color.WHITE;
                    opacity = 1.f;
                    shininess = 0.f;
                } else if (// diffuse or specular
                key.equals("kd") || key.equals("ks")) {
                    float r = Float.parseFloat(tokens[1]);
                    float g = Float.parseFloat(tokens[2]);
                    float b = Float.parseFloat(tokens[3]);
                    float a = 1;
                    if (tokens.length > 4)
                        a = Float.parseFloat(tokens[4]);
                    if (tokens[0].toLowerCase().equals("kd")) {
                        difcolor = new Color();
                        difcolor.set(r, g, b, a);
                    } else {
                        speccolor = new Color();
                        speccolor.set(r, g, b, a);
                    }
                } else if (key.equals("tr") || key.equals("d")) {
                    opacity = Float.parseFloat(tokens[1]);
                } else if (key.equals("ns")) {
                    shininess = Float.parseFloat(tokens[1]);
                } else if (key.equals("map_kd")) {
                    texFilename = file.parent().child(tokens[1]).path();
                }
            }
        }
        reader.close();
    } catch (IOException e) {
        return;
    }
    // last material
    ModelMaterial mat = new ModelMaterial();
    mat.id = curMatName;
    mat.diffuse = new Color(difcolor);
    mat.specular = new Color(speccolor);
    mat.opacity = opacity;
    mat.shininess = shininess;
    if (texFilename != null) {
        ModelTexture tex = new ModelTexture();
        tex.usage = ModelTexture.USAGE_DIFFUSE;
        tex.fileName = new String(texFilename);
        if (mat.textures == null)
            mat.textures = new Array<ModelTexture>(1);
        mat.textures.add(tex);
    }
    materials.add(mat);
    return;
}
Also used : Array(com.badlogic.gdx.utils.Array) FloatArray(com.badlogic.gdx.utils.FloatArray) InputStreamReader(java.io.InputStreamReader) Color(com.badlogic.gdx.graphics.Color) BufferedReader(java.io.BufferedReader) ModelMaterial(com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial) IOException(java.io.IOException) ModelTexture(com.badlogic.gdx.graphics.g3d.model.data.ModelTexture)

Example 5 with ModelTexture

use of com.badlogic.gdx.graphics.g3d.model.data.ModelTexture in project libgdx by libgdx.

the class Model method convertMaterial.

protected Material convertMaterial(ModelMaterial mtl, TextureProvider textureProvider) {
    Material result = new Material();
    result.id = mtl.id;
    if (mtl.ambient != null)
        result.set(new ColorAttribute(ColorAttribute.Ambient, mtl.ambient));
    if (mtl.diffuse != null)
        result.set(new ColorAttribute(ColorAttribute.Diffuse, mtl.diffuse));
    if (mtl.specular != null)
        result.set(new ColorAttribute(ColorAttribute.Specular, mtl.specular));
    if (mtl.emissive != null)
        result.set(new ColorAttribute(ColorAttribute.Emissive, mtl.emissive));
    if (mtl.reflection != null)
        result.set(new ColorAttribute(ColorAttribute.Reflection, mtl.reflection));
    if (mtl.shininess > 0f)
        result.set(new FloatAttribute(FloatAttribute.Shininess, mtl.shininess));
    if (mtl.opacity != 1.f)
        result.set(new BlendingAttribute(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA, mtl.opacity));
    ObjectMap<String, Texture> textures = new ObjectMap<String, Texture>();
    // FIXME uvScaling/uvTranslation totally ignored
    if (mtl.textures != null) {
        for (ModelTexture tex : mtl.textures) {
            Texture texture;
            if (textures.containsKey(tex.fileName)) {
                texture = textures.get(tex.fileName);
            } else {
                texture = textureProvider.load(tex.fileName);
                textures.put(tex.fileName, texture);
                disposables.add(texture);
            }
            TextureDescriptor descriptor = new TextureDescriptor(texture);
            descriptor.minFilter = texture.getMinFilter();
            descriptor.magFilter = texture.getMagFilter();
            descriptor.uWrap = texture.getUWrap();
            descriptor.vWrap = texture.getVWrap();
            float offsetU = tex.uvTranslation == null ? 0f : tex.uvTranslation.x;
            float offsetV = tex.uvTranslation == null ? 0f : tex.uvTranslation.y;
            float scaleU = tex.uvScaling == null ? 1f : tex.uvScaling.x;
            float scaleV = tex.uvScaling == null ? 1f : tex.uvScaling.y;
            switch(tex.usage) {
                case ModelTexture.USAGE_DIFFUSE:
                    result.set(new TextureAttribute(TextureAttribute.Diffuse, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_SPECULAR:
                    result.set(new TextureAttribute(TextureAttribute.Specular, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_BUMP:
                    result.set(new TextureAttribute(TextureAttribute.Bump, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_NORMAL:
                    result.set(new TextureAttribute(TextureAttribute.Normal, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_AMBIENT:
                    result.set(new TextureAttribute(TextureAttribute.Ambient, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_EMISSIVE:
                    result.set(new TextureAttribute(TextureAttribute.Emissive, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
                case ModelTexture.USAGE_REFLECTION:
                    result.set(new TextureAttribute(TextureAttribute.Reflection, descriptor, offsetU, offsetV, scaleU, scaleV));
                    break;
            }
        }
    }
    return result;
}
Also used : BlendingAttribute(com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute) ObjectMap(com.badlogic.gdx.utils.ObjectMap) ModelMaterial(com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial) FloatAttribute(com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute) TextureDescriptor(com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor) ColorAttribute(com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute) ModelTexture(com.badlogic.gdx.graphics.g3d.model.data.ModelTexture) Texture(com.badlogic.gdx.graphics.Texture) ModelTexture(com.badlogic.gdx.graphics.g3d.model.data.ModelTexture) TextureAttribute(com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute)

Aggregations

ModelMaterial (com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial)5 ModelTexture (com.badlogic.gdx.graphics.g3d.model.data.ModelTexture)5 Array (com.badlogic.gdx.utils.Array)4 ObjectMap (com.badlogic.gdx.utils.ObjectMap)3 AssetDescriptor (com.badlogic.gdx.assets.AssetDescriptor)2 Texture (com.badlogic.gdx.graphics.Texture)2 ModelData (com.badlogic.gdx.graphics.g3d.model.data.ModelData)2 TextureLoader (com.badlogic.gdx.assets.loaders.TextureLoader)1 Color (com.badlogic.gdx.graphics.Color)1 BlendingAttribute (com.badlogic.gdx.graphics.g3d.attributes.BlendingAttribute)1 ColorAttribute (com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute)1 FloatAttribute (com.badlogic.gdx.graphics.g3d.attributes.FloatAttribute)1 TextureAttribute (com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute)1 TextureDescriptor (com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor)1 FloatArray (com.badlogic.gdx.utils.FloatArray)1 GdxRuntimeException (com.badlogic.gdx.utils.GdxRuntimeException)1 JsonValue (com.badlogic.gdx.utils.JsonValue)1 BufferedReader (java.io.BufferedReader)1 IOException (java.io.IOException)1 InputStreamReader (java.io.InputStreamReader)1