* Made MTLLoader more compatible

* Fixed bug where MTLLoader would skip lines
 * Fixed exception in Mesh.setInterleaved

git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@7463 75d07b2b-3a1a-0410-a2c5-0572b91ccdca
3.0
sha..rd 14 years ago
parent 7938b776de
commit 5bf644ff90
  1. 126
      engine/src/core-plugins/com/jme3/scene/plugins/MTLLoader.java
  2. 1
      engine/src/core/com/jme3/material/Material.java
  3. 6
      engine/src/core/com/jme3/scene/Mesh.java

@ -40,7 +40,6 @@ import com.jme3.material.MatParam;
import com.jme3.material.Material; import com.jme3.material.Material;
import com.jme3.material.MaterialList; import com.jme3.material.MaterialList;
import com.jme3.material.RenderState.BlendMode; import com.jme3.material.RenderState.BlendMode;
import com.jme3.material.RenderState.FaceCullMode;
import com.jme3.math.ColorRGBA; import com.jme3.math.ColorRGBA;
import com.jme3.texture.Image.Format; import com.jme3.texture.Image.Format;
import com.jme3.texture.Texture; import com.jme3.texture.Texture;
@ -58,7 +57,7 @@ public class MTLLoader implements AssetLoader {
protected Material material; protected Material material;
protected AssetManager assetManager; protected AssetManager assetManager;
protected String folderName; protected String folderName;
public void reset(){ public void reset(){
scan = null; scan = null;
matList = null; matList = null;
@ -86,107 +85,130 @@ public class MTLLoader implements AssetLoader {
material.setFloat("Shininess", 16f); // prevents "premature culling" bug material.setFloat("Shininess", 16f); // prevents "premature culling" bug
matList.put(name, material); matList.put(name, material);
} }
protected Texture loadTexture(String path){
String name = new File(path).getName();
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
if (texture != null){
texture.setWrap(WrapMode.Repeat);
}
return texture;
}
protected boolean readLine(){ protected boolean readLine(){
if (!scan.hasNext()){ if (!scan.hasNext()){
return false; return false;
} }
String cmd = scan.next(); String cmd = scan.next().toLowerCase();
if (cmd.startsWith("#")){ if (cmd.startsWith("#")){
// skip entire comment until next line // skip entire comment until next line
nextStatement();
}else if (cmd.equals("newmtl")){ }else if (cmd.equals("newmtl")){
String name = scan.next(); String name = scan.next();
startMaterial(name); startMaterial(name);
}else if (cmd.equals("Ka") || cmd.equals("Ke") || cmd.equals("Ni") || cmd.equals("illum")){ }else if (cmd.equals("ka")){
// ignore it for now material.setColor("Ambient", readColor());
}else if (cmd.equals("Kd")){ }else if (cmd.equals("kd")){
ColorRGBA color = readColor(); ColorRGBA color = readColor();
MatParam param = material.getParam("Diffuse"); MatParam param = material.getParam("Diffuse");
if (param != null){ if (param != null){
color.a = ((ColorRGBA) param.getValue()).getAlpha(); color.a = ((ColorRGBA) param.getValue()).getAlpha();
} }
material.setColor("Diffuse", color); material.setColor("Diffuse", color);
}else if (cmd.equals("Ks")){ }else if (cmd.equals("ks")){
material.setColor("Specular", readColor()); material.setColor("Specular", readColor());
}else if (cmd.equals("Ns")){ }else if (cmd.equals("ns")){
material.setFloat("Shininess", scan.nextFloat() /* (128f / 1000f)*/ ); material.setFloat("Shininess", scan.nextFloat() /* (128f / 1000f)*/ );
}else if (cmd.equals("d")){ }else if (cmd.equals("d") || cmd.equals("tr")){
float alpha = scan.nextFloat(); float alpha = scan.nextFloat();
// if (alpha < 1f){ if (alpha < 1f){
// MatParam param = material.getParam("Diffuse"); MatParam param = material.getParam("Diffuse");
// ColorRGBA color; ColorRGBA color;
// if (param != null) if (param != null)
// color = (ColorRGBA) param.getValue(); color = (ColorRGBA) param.getValue();
// else else
// color = new ColorRGBA(ColorRGBA.White); color = new ColorRGBA(ColorRGBA.White);
//
// color.a = scan.nextFloat(); color.a = scan.nextFloat();
// material.setColor("Diffuse", color); material.setColor("Diffuse", color);
// material.setBoolean("UseAlpha", true); material.setTransparent(true);
// material.setTransparent(true); material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
// material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); }
// } }else if (cmd.equals("map_ka")){
}else if (cmd.equals("map_Ka")){
// ignore it for now // ignore it for now
}else if (cmd.equals("map_Kd")){ }else if (cmd.equals("map_kd")){
String path = scan.next(); String path = scan.next();
String name = new File(path).getName(); material.setTexture("DiffuseMap", loadTexture(path));
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
if (texture != null){
texture.setWrap(WrapMode.Repeat);
material.setTexture("DiffuseMap", texture);
}
}else if (cmd.equals("map_bump") || cmd.equals("bump")){ }else if (cmd.equals("map_bump") || cmd.equals("bump")){
if (material.getParam("NormalMap") == null){ if (material.getParam("NormalMap") == null){
String path = scan.next(); String path = scan.next();
String name = new File(path).getName(); Texture texture = loadTexture(path);
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
if (texture != null){ if (texture != null){
texture.setWrap(WrapMode.Repeat);
material.setTexture("NormalMap", texture); material.setTexture("NormalMap", texture);
if (texture.getImage().getFormat() == Format.LATC){ if (texture.getImage().getFormat() == Format.LATC){
material.setBoolean("LATC", true); material.setBoolean("LATC", true);
} }
} }
} }
}else if (cmd.equals("map_Ks")){ }else if (cmd.equals("map_ks")){
String path = scan.next(); String path = scan.next();
String name = new File(path).getName(); Texture texture = loadTexture(path);
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
if (texture != null){ if (texture != null){
texture.setWrap(WrapMode.Repeat);
material.setTexture("SpecularMap", texture); material.setTexture("SpecularMap", texture);
// NOTE: since specular color is modulated with specmap // NOTE: since specular color is modulated with specmap
// make sure we have it set // make sure we have it set
material.setColor("Specular", ColorRGBA.White); MatParam specParam = material.getParam("Specular");
if (specParam == null){
material.setColor("Specular", ColorRGBA.White);
}else{
ColorRGBA spec = (ColorRGBA) specParam.getValue();
if (spec.equals(ColorRGBA.Black)){
material.setColor("Specular", ColorRGBA.White);
}
}
} }
}else if (cmd.equals("map_d")){ }else if (cmd.equals("map_d")){
String path = scan.next(); String path = scan.next();
String name = new File(path).getName(); Texture texture = loadTexture(path);
TextureKey key = new TextureKey(folderName + name);
key.setGenerateMips(true);
Texture texture = assetManager.loadTexture(key);
if (texture != null){ if (texture != null){
texture.setWrap(WrapMode.Repeat);
material.setTexture("AlphaMap", texture); material.setTexture("AlphaMap", texture);
material.setTransparent(true); material.setTransparent(true);
material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha); material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
material.getAdditionalRenderState().setAlphaTest(true); material.getAdditionalRenderState().setAlphaTest(true);
material.getAdditionalRenderState().setAlphaFallOff(0.01f); material.getAdditionalRenderState().setAlphaFallOff(0.01f);
} }
}else if (cmd.equals("illum")){
int mode = scan.nextInt();
switch (mode){
case 0:
// no ambient
material.setColor("Ambient", ColorRGBA.Black);
break;
case 4:
case 6:
case 7:
case 9:
// Enable transparency
material.setTransparent(true);
material.getAdditionalRenderState().setBlendMode(BlendMode.Alpha);
// Works best if diffuse map has an alpha channel
break;
}
}else if (cmd.equals("ke") || cmd.equals("ni")){
// Ni: index of refraction - unsupported in jME
// Ke: emission color
nextStatement();
}else{ }else{
System.out.println("Unknown statement in MTL! "+cmd); System.out.println("Unknown statement in MTL! "+cmd);
nextStatement();
} }
nextStatement();
return true; return true;
} }

@ -279,7 +279,6 @@ public class Material implements Cloneable, Savable, Comparable<Material> {
if (param instanceof MatParam) { if (param instanceof MatParam) {
return (MatParam) param; return (MatParam) param;
} }
return null; return null;
} }

@ -336,7 +336,7 @@ public class Mesh implements Savable, Cloneable {
VertexBuffer allData = new VertexBuffer(Type.InterleavedData); VertexBuffer allData = new VertexBuffer(Type.InterleavedData);
ByteBuffer dataBuf = BufferUtils.createByteBuffer(stride * getVertexCount()); ByteBuffer dataBuf = BufferUtils.createByteBuffer(stride * getVertexCount());
allData.setupData(Usage.Static, -1, Format.UnsignedByte, dataBuf); allData.setupData(Usage.Static, 1, Format.UnsignedByte, dataBuf);
// adding buffer directly so that no update counts is forced // adding buffer directly so that no update counts is forced
buffers.put(Type.InterleavedData.ordinal(), allData); buffers.put(Type.InterleavedData.ordinal(), allData);
@ -387,8 +387,8 @@ public class Mesh implements Savable, Cloneable {
vb.setOffset(offset); vb.setOffset(offset);
vb.setStride(stride); vb.setStride(stride);
// discard old buffer vb.updateData(null);
vb.setupData(vb.usage, vb.components, vb.format, null); //vb.setupData(vb.usage, vb.components, vb.format, null);
offset += vb.componentsLength; offset += vb.componentsLength;
} }
} }

Loading…
Cancel
Save