// Read material name newmtl name
if (tokenizer.nextToken() == StreamTokenizer.TT_WORD) {
currentAppearance = new Appearance();
appearances.put(tokenizer.sval, currentAppearance);
} else {
throw new IncorrectFormatException("Expected material name at line " + tokenizer.lineno());
}
} else if ("Ka".equals(tokenizer.sval)) {
// Read ambient color Ka r g b
Color3f ambiantColor = new Color3f(parseNumber(tokenizer),
parseNumber(tokenizer), parseNumber(tokenizer));
if (currentAppearance != null) {
Material material = getMaterial(currentAppearance);
material.setAmbientColor(ambiantColor);
}
} else if ("Kd".equals(tokenizer.sval)) {
// Read diffuse or emissive color Kd r g b
Color3f diffuseColor = new Color3f(parseNumber(tokenizer),
parseNumber(tokenizer), parseNumber(tokenizer));
if (currentAppearance != null) {
OBJMaterial material = getMaterial(currentAppearance);
material.setDiffuseColor(diffuseColor);
currentAppearance.setColoringAttributes(
new ColoringAttributes(diffuseColor, ColoringAttributes.SHADE_GOURAUD));
}
} else if ("Ks".equals(tokenizer.sval)) {
// Read specular color Ks r g b
Color3f specularColor = new Color3f(parseNumber(tokenizer),
parseNumber(tokenizer), parseNumber(tokenizer));
if (currentAppearance != null) {
OBJMaterial material = getMaterial(currentAppearance);
if (!material.isIlluminationModelSet()
|| material.getIlluminationModel() >= 2) {
material.setSpecularColor(specularColor);
} else {
material.setSpecularColor(0, 0, 0);
}
}
} else if ("Ns".equals(tokenizer.sval)) {
// Read shininess Ns val with 0 <= val <= 1000
float shininess = parseNumber(tokenizer);
if (currentAppearance != null) {
OBJMaterial material = getMaterial(currentAppearance);
if (!material.isIlluminationModelSet()
|| material.getIlluminationModel() >= 2) {
// Use shininess at a max value equal to 128
material.setShininess(Math.max(1f, Math.min(shininess, 128f)));
} else {
material.setShininess(1f);
}
}
} else if ("Ni".equals(tokenizer.sval)) {
// Read optical density Ni val
float opticalDensity = parseNumber(tokenizer);
if (currentAppearance != null) {
OBJMaterial material = getMaterial(currentAppearance);
material.setOpticalDensity(opticalDensity);
}
} else if ("sharpness".equals(tokenizer.sval)) {
// Read sharpness sharpness val
float sharpness = parseNumber(tokenizer);
if (currentAppearance != null) {
OBJMaterial material = getMaterial(currentAppearance);
material.setSharpness(sharpness);
}
} else if ("d".equals(tokenizer.sval)) {
// Read transparency d val with 0 <= val <= 1
if (tokenizer.nextToken() == StreamTokenizer.TT_WORD) {
if ("-halo".equals(tokenizer.sval)) {
// Ignore halo transparency
parseNumber(tokenizer);
} else {
tokenizer.pushBack();
float transparency = parseNumber(tokenizer);
if (currentAppearance != null) {
if (transparency >= 1) {
currentAppearance.setTransparencyAttributes(null);
} else {
currentAppearance.setTransparencyAttributes(new TransparencyAttributes(
TransparencyAttributes.NICEST, 1f - Math.max(0f, transparency)));
}
}
}
} else {
throw new IncorrectFormatException("Expected transparency factor at line " + tokenizer.lineno());
}
} else if ("illum".equals(tokenizer.sval)) {
// Read illumination setting illum n
int illumination = parseInteger(tokenizer);
if (currentAppearance != null) {
OBJMaterial material = getMaterial(currentAppearance);
material.setIlluminationModel(illumination);
material.setLightingEnable(illumination >= 1);
if (illumination <= 1) {
material.setSpecularColor(0, 0, 0);
material.setShininess(1f);
}
}
} else if ("map_Kd".equals(tokenizer.sval)) {
// Read material texture map_Kd name
// Search last parameter that matches image file name
String imageFileName = null;
while (tokenizer.nextToken() != StreamTokenizer.TT_EOL) {
if (tokenizer.ttype == StreamTokenizer.TT_WORD) {
imageFileName = tokenizer.sval;
}
}
if (imageFileName != null) {
URL textureImageUrl = baseUrl != null
? new URL(baseUrl, imageFileName)
: new File(imageFileName).toURI().toURL();
BufferedImage textureImage = null;
try {
textureImage = ImageIO.read(textureImageUrl);
} catch (IOException ex) {
// Ignore images at other format
}
if (textureImage != null) {
TextureLoader textureLoader = new TextureLoader(textureImage);
Texture texture = textureLoader.getTexture();
// Keep in user data the URL of the texture image
texture.setUserData(textureImageUrl);
currentAppearance.setTexture(texture);
}
} else {
throw new IncorrectFormatException("Expected image file name at line " + tokenizer.lineno());
}
tokenizer.pushBack();
} else {
int token;
do {
token = tokenizer.nextToken();
} while (token != StreamTokenizer.TT_EOL && token != StreamTokenizer.TT_EOF);
tokenizer.pushBack();
}
int token = tokenizer.nextToken();
if (token != StreamTokenizer.TT_EOL && token != StreamTokenizer.TT_EOF) {
throw new IncorrectFormatException("Expected end of line at line " + tokenizer.lineno());
}
return currentAppearance;
}