use of com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial 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;
}
use of com.badlogic.gdx.graphics.g3d.model.data.ModelMaterial 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;
}
Aggregations