📄 objtojme.java
字号:
} else if ("Ks".equals(parts[0])) {
curGroup.m.setSpecular(new ColorRGBA(Float.parseFloat(parts[1]),
Float.parseFloat(parts[2]), Float.parseFloat(parts[3]), 1));
return;
} else if ("Ns".equals(parts[0])) {
float shine = Float.parseFloat(parts[1]);
if (shine > 128) {
shine = 128;
} else if (shine < 0) {
shine = 0;
}
curGroup.m.setShininess(shine);
return;
} else if ("d".equals(parts[0])) {
float alpha = Float.parseFloat(parts[1]);
curGroup.m.getAmbient().a *= alpha;
curGroup.m.getDiffuse().a *= alpha;
curGroup.m.getSpecular().a *= alpha;
if (alpha < 1.0f)
curGroup.createBlendState();
return;
} else if ("map_d".equals(parts[0])) {
curGroup.createBlendState();
return;
} else if ("map_Kd".equals(parts[0]) || "map_Ka".equals(parts[0])) {
URL texdir = (URL) properties.get("texdir");
URL texurl = null;
if (texdir != null) {
texurl = new URL(texdir, s.trim().substring(7));
} else {
texurl = new File(s.trim().substring(7)).toURI().toURL();
}
TextureKey tkey = new TextureKey(texurl, true,
TextureManager.COMPRESS_BY_DEFAULT ? Image.Format.Guess
: Image.Format.GuessNoCompression);
Texture t = new Texture2D();
t.setAnisotropicFilterPercent(0.0f);
t.setMinificationFilter(Texture.MinificationFilter.BilinearNearestMipMap);
t.setMagnificationFilter(Texture.MagnificationFilter.Bilinear);
t.setTextureKey(tkey);
t.setWrap(Texture.WrapMode.Repeat);
t.setImageLocation(texurl.toString());
curGroup.ts = renderer.createTextureState();
curGroup.ts.setTexture(t);
curGroup.ts.setEnabled(true);
return;
} else if ("o".equals(parts[0])) {
curObjectName = parts[1];
logger.info("Object:" + curObjectName);
}
}
private String[] removeEmpty(String[] parts) {
int cnt = 0;
for (int i = 0; i < parts.length; i++) {
if (!parts[i].equals(""))
cnt++;
}
String[] toReturn = new String[cnt];
int index = 0;
for (int i = 0; i < parts.length; i++) {
if (!parts[i].equals("")) {
toReturn[index++] = parts[i].trim();
}
}
return toReturn;
}
private void addMaterial(String[] parts) {
MaterialGrouping newMat = new MaterialGrouping();
materialNames.put(parts[1], newMat);
materialSets.put(newMat, new ArraySet());
curGroup = newMat;
}
private void loadMaterials(String[] fileNames) throws IOException {
URL matURL = (URL) properties.get("mtllib");
if (matURL == null)
return;
for (int i = 1; i < fileNames.length; i++) {
processMaterialFile(new URL(matURL, fileNames[i]).openStream());
}
}
private void processMaterialFile(InputStream inputStream)
throws IOException {
BufferedReader matFile = new BufferedReader(new InputStreamReader(
inputStream));
String in;
while ((in = matFile.readLine()) != null) {
processLine(in);
}
}
private void addFaces(String[] parts) {
ArraySet thisMat = materialSets.get(curGroup);
if (thisMat.objName == null) {
if (curObjectName != null) {
thisMat.objName = curObjectName;
}
else if (curGroupName != null) {
thisMat.objName = curGroupName;
}
}
IndexSet first = new IndexSet(parts[1]);
int firstIndex = thisMat.findSet(first);
IndexSet second = new IndexSet(parts[2]);
int secondIndex = thisMat.findSet(second);
for (int i = 3; i < parts.length; i++) {
IndexSet third = new IndexSet();
third.parseStringArray(parts[i]);
int thirdIndex = thisMat.findSet(third);
thisMat.indexes.add(firstIndex);
thisMat.indexes.add(secondIndex);
thisMat.indexes.add(thirdIndex);
if (first.nIndex == -1 || second.nIndex == -1 || third.nIndex == -1) {
// Generate flat face normal. TODO: Smoothed normals?
Vector3f v = new Vector3f(vertexList.get(second.vIndex));
Vector3f w = new Vector3f(vertexList.get(third.vIndex));
v.subtractLocal(vertexList.get(first.vIndex));
w.subtractLocal(vertexList.get(first.vIndex));
v.crossLocal(w);
v.normalizeLocal();
genNormalList.add(v);
int genIndex = (-1 * (genNormalList.size() - 1)) - 2;
if (first.nIndex == -1) {
first.nIndex = genIndex;
}
if (second.nIndex == -1) {
second.nIndex = genIndex;
}
if (third.nIndex == -1) {
third.nIndex = genIndex;
}
}
secondIndex = thirdIndex; // The second will be the same as the
// last third
}
}
private void setDefaultGroup() {
curGroup = defaultMaterialGroup;
}
private void addNormalToList(String[] parts) {
Vector3f norm = new Vector3f(Float.parseFloat(parts[1]), Float
.parseFloat(parts[2]), Float.parseFloat(parts[3]));
normalList.add(norm);
}
private void addTextoList(String[] parts) {
float u = Float.parseFloat(parts[1]);
float v = 0;
//float w = 0; (3d coordinate possible)
if (parts.length > 2)
v = Float.parseFloat(parts[2]);
textureList.add(new Vector2f(u,v));
}
private void addVertextoList(String[] parts) {
vertexList.add(new Vector3f(Float.parseFloat(parts[1]), Float
.parseFloat(parts[2]), Float.parseFloat(parts[3])));
}
private class MaterialGrouping {
public MaterialGrouping() {
m = renderer.createMaterialState();
m.setAmbient(new ColorRGBA(.2f, .2f, .2f, 1));
m.setDiffuse(new ColorRGBA(.8f, .8f, .8f, 1));
m.setSpecular(ColorRGBA.white.clone());
m.setEnabled(true);
}
public void createBlendState() {
if (as != null)
return;
as = renderer.createBlendState();
as.setBlendEnabled(true);
as.setSourceFunction(BlendState.SourceFunction.SourceAlpha);
as.setDestinationFunction(BlendState.DestinationFunction.OneMinusSourceAlpha);
as.setTestEnabled(true);
as.setTestFunction(BlendState.TestFunction.GreaterThan);
as.setEnabled(true);
}
MaterialState m;
TextureState ts;
BlendState as;
}
/**
* Stores the indexes of a vertex/texture/normal triplet set that is to be
* indexed by the TriMesh.
*/
private class IndexSet {
int vIndex, nIndex, tIndex;
int index;
public IndexSet() {
}
public IndexSet(String parts) {
parseStringArray(parts);
}
public void parseStringArray(String parts) {
String[] triplet = parts.split("/");
vIndex = Integer.parseInt(triplet[0]);
if (vIndex < 0) {
vIndex += vertexList.size();
} else {
vIndex--; // obj starts at 1 not 0
}
if (triplet.length < 2 || triplet[1] == null
|| triplet[1].equals("")) {
tIndex = -1;
} else {
tIndex = Integer.parseInt(triplet[1]);
if (tIndex < 0) {
tIndex += textureList.size();
} else {
tIndex--; // obj starts at 1 not 0
}
}
if (triplet.length != 3 || triplet[2] == null
|| triplet[2].equals("")) {
nIndex = -1;
} else {
nIndex = Integer.parseInt(triplet[2]);
if (nIndex < 0) {
nIndex += normalList.size();
} else {
nIndex--; // obj starts at 1 not 0
}
}
}
@Override
public boolean equals(Object obj) {
if (!(obj instanceof IndexSet)) return false;
IndexSet other = (IndexSet)obj;
if (other.nIndex != this.nIndex) return false;
if (other.tIndex != this.tIndex) return false;
if (other.vIndex != this.vIndex) return false;
return true;
}
@Override
public int hashCode() {
int hash = 37;
hash += 37 * hash + vIndex;
hash += 37 * hash + nIndex;
hash += 37 * hash + tIndex;
return hash;
}
}
/**
* An array of information that will become a renderable trimesh. Each
* material has it's own trimesh.
*/
private class ArraySet {
private String objName = null;
private LinkedHashSet<IndexSet> sets = new LinkedHashSet<IndexSet>();
private HashMap<IndexSet, Integer> index = new HashMap<IndexSet, Integer>();
private ArrayList<Integer> indexes = new ArrayList<Integer>();
public int findSet(IndexSet v) {
if (sets.contains(v)) {
return index.get(v);
}
sets.add(v);
index.put(v, sets.size()-1);
return sets.size()-1;
}
}
/**
* @return true if the loader will generate missing face normals (default is true)
*/
public boolean isGenerateMissingNormals() {
return generateMissingNormals;
}
/**
* Set whether to generate missing face normals.
*
* @param generateMissingNormals
* the generateMissingNormals to set
*/
public void setGenerateMissingNormals(boolean generateMissingNormals) {
this.generateMissingNormals = generateMissingNormals;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -