Commit baba25d1 authored by Human Gamer's avatar Human Gamer

Can now import OBJ files as DIF files, collision currently does not work for these however

parent b35c1ba0
......@@ -46,6 +46,7 @@ import com.matt.difinspector.structures.TriFan;
import com.matt.difinspector.structures.TriangleF;
import com.matt.difinspector.structures.Zone;
import com.matt.difinspector.util.Util;
import com.matt.jmatt.lists.ErrorList;
public class Interior
{
......@@ -123,11 +124,13 @@ public class Interior
private int fileVersion;
private List<Short> normalIndices;
private Point3F[] planeNormals;
private Point3F[] normalsLegacy;
private ErrorList errors;
// private PlaneIndex[] planeIndices;
public Interior()
{
this.errors = new ErrorList();
}
public boolean read(ReverseDataInputStream dis) throws IOException
......@@ -444,10 +447,10 @@ public class Interior
// normalsAlt = true;
/* numNormalsAlt = */ dis.readByte();
}
this.normals = new Point3F[numNormals];
this.normalsLegacy = new Point3F[numNormals];
for (int i = 0; i < numNormals; i++)
{
this.normals[i] = dis.readPoint3F();
this.normalsLegacy[i] = dis.readPoint3F();
}
// NormalIndices
......@@ -1003,12 +1006,24 @@ public class Interior
public void prepareData()
{
if (this.boundingBox == null)
this.boundingBox = new Box3F(new Point3F(), new Point3F());
if (this.boundingSphere == null)
this.boundingSphere = new SphereF(new Point3F(), 0);
if (this.texGenEQs == null)
this.texGenEQs = new TexGenList();
this.texGenEQs.clear();
if (this.materialList == null)
this.materialList = new MaterialList();
this.materialList.getMaterials().clear();
this.materialList.addMaterial("NULL");
this.materialList.addMaterial("ORIGIN");
this.materialList.addMaterial("TRIGGER");
this.materialList.addMaterial("EMITTER");
if (this.surfaces == null)
this.surfaces = new Surface[0];
for (int i = 0; i < this.surfaces.length; i++)
{
String texture = this.surfaces[i].getTexture().toLowerCase();
......@@ -1024,6 +1039,8 @@ public class Interior
this.surfaces[i].setTexGenIndex(this.texGenEQs.indexOf(texGen));
}
if (this.convexHulls == null)
this.convexHulls = new ConvexHull[0];
for (int i = 0; i < this.convexHulls.length; i++)
{
ConvexHull hull = this.convexHulls[i];
......@@ -1554,6 +1571,12 @@ public class Interior
targetVersion = 0;
tgeInterior = true;
}
if (!this.verify(targetVersion))
{
return false;
}
dos.writeInt(targetVersion);
dos.writeInt(this.detailLevel);
......@@ -1812,12 +1835,12 @@ public class Interior
// Normals
if (targetVersion == 4 || targetVersion == 5)
{
if (this.normals == null)
this.normals = new Point3F[0];
dos.writeInt(this.normals.length);
for (int i = 0; i < this.normals.length; i++)
if (this.normalsLegacy == null)
this.normalsLegacy = new Point3F[0];
dos.writeInt(this.normalsLegacy.length);
for (int i = 0; i < this.normalsLegacy.length; i++)
{
dos.writePoint3F(this.normals[i]);
dos.writePoint3F(this.normalsLegacy[i]);
}
// Quick Fix
......@@ -2489,6 +2512,8 @@ public class Interior
bw.write(TAB + "hasAlarmState = " + (this.hasAlarmState ? "true" : "false") + "\n");
bw.write(TAB + "numLightStateEntries = " + this.numLightStateEntries + "\n");
if (this.planes == null)
this.planes = new PlaneF[0];
bw.write(TAB + "numPlanes = " + this.planes.length + "\n");
bw.write(TAB + "{\n");
for (int i = 0; i < this.planes.length; i++)
......@@ -2497,6 +2522,8 @@ public class Interior
}
bw.write(TAB + "}\n\n");
if (this.points == null)
this.points = new ItrPaddedPoint[0];
bw.write(TAB + "numPoints = " + this.points.length + "\n");
bw.write(TAB + "{\n");
for (int i = 0; i < this.points.length; i++)
......@@ -2507,6 +2534,8 @@ public class Interior
bw.write(TAB + "pointVisibilities = " + Util.getArrayString(this.pointVisibility) + "\n\n");
if (this.texGenEQs == null)
this.texGenEQs = new TexGenList();
bw.write(TAB + "numTexGenEQs = " + this.texGenEQs.size());
bw.write(TAB + "{\n");
for (int i = 0; i < this.texGenEQs.size(); i++)
......@@ -2536,6 +2565,8 @@ public class Interior
bw.write(TAB + "}\n\n");
// Material List
if (this.materialList == null)
this.materialList = new MaterialList();
bw.write(TAB + "new MaterialList() {\n");
this.materialList.dumpInfo(bw);
bw.write(TAB + "}\n\n");
......@@ -2610,15 +2641,28 @@ public class Interior
bw.write(TAB + "}\n\n");
// Normals
if (this.normals == null)
this.normals = new Point3F[0];
bw.write(TAB + "numNormals = " + this.normals.length);
bw.write(TAB + "{\n");
for (int i = 0; i < this.normals.length; i++)
if (this.fileVersion == 4 || this.fileVersion == 5)
{
bw.write(TAB + TAB + this.normals[i] + "\n");
if (this.normalsLegacy == null)
this.normalsLegacy = new Point3F[0];
bw.write(TAB + "numNormalsV4 = " + this.normalsLegacy.length);
bw.write(TAB + "{\n");
for (int i = 0; i < this.normalsLegacy.length; i++)
{
bw.write(TAB + TAB + this.normalsLegacy[i] + "\n");
}
bw.write(TAB + "}\n\n");
} else {
if (this.normals == null)
this.normals = new Point3F[0];
bw.write(TAB + "numNormals = " + this.normals.length);
bw.write(TAB + "{\n");
for (int i = 0; i < this.normals.length; i++)
{
bw.write(TAB + TAB + this.normals[i] + "\n");
}
bw.write(TAB + "}\n\n");
}
bw.write(TAB + "}\n\n");
// NormalLMapIndices
bw.write(TAB + "normalLMapIndices = " + Util.getArrayString(this.normalLMapIndices) + "\n\n");
......@@ -2693,7 +2737,8 @@ public class Interior
bw.write(TAB + "nameBuffer = " + Util.getArrayString(this.nameBuffer) + "\n\n");
// Sub Objects
this.subObjects = new InteriorSubObject[0];
if (this.subObjects == null)
this.subObjects = new InteriorSubObject[0];
bw.write(TAB + "numSubObjects = " + this.subObjects.length);
bw.write(TAB + "{\n");
for (int i = 0; i < this.subObjects.length; i++)
......@@ -3507,7 +3552,7 @@ public class Interior
//for (int i = 0; i < this.convexHulls.length; i++)
//{
List<OBJIndex> faces = model.getIndices();
int surfaceStart = surfaces.size();
//int surfaceStart = surfaces.size();
short surfaceCount = 0;
for (int j = 0; j < faces.size(); j++)
{
......@@ -3518,7 +3563,7 @@ public class Interior
//Point3I vIndex = face.getVertexIndex();
//Point3I tIndex = face.getTextureIndex();
Point3F point = model.getPositions().get(face.getVertexIndex());
//Point3F point = model.getPositions().get(face.getVertexIndex());
//int ix = vIndex.getX();
//int iy = vIndex.getY();
......@@ -3598,6 +3643,20 @@ public class Interior
return true;
}
public boolean verify(int version)
{
this.errors.clear();
if (version == 4 || version == 5)
{
if (!Util.hasElements(this.normalsLegacy))
{
this.errors.add("No Legacy Normals!");
return false;
}
}
return true;
}
public int getDetailLevel()
{
return detailLevel;
......@@ -3668,6 +3727,15 @@ public class Interior
this.planes = planes;
}
public void setPlanes(List<PlaneF> planes)
{
this.planes = new PlaneF[planes.size()];
for (int i = 0; i < planes.size(); i++)
{
this.planes[i] = planes.get(i);
}
}
public ItrPaddedPoint[] getPoints()
{
return points;
......@@ -3798,6 +3866,14 @@ public class Interior
this.surfaces = surfaces;
}
public void setSurfaces(List<Surface> surfaces)
{
this.surfaces = new Surface[surfaces.size()];
for (int i = 0; i < surfaces.size(); i++)
this.surfaces[i] = surfaces.get(i);
}
public TexGenPlanes[] getLmTexGenEQs()
{
return lmTexGenEQs;
......@@ -4108,6 +4184,16 @@ public class Interior
this.normals = normals;
}
public Point3F[] getNormalsLegacy()
{
return normalsLegacy;
}
public void setNormalsLegacy(Point3F[] normalsLegacy)
{
this.normalsLegacy = normalsLegacy;
}
public TexMatrix[] getTexMatrices()
{
return texMatrices;
......@@ -4322,4 +4408,9 @@ public class Interior
{
this.planeNormals = planeNormals;
}
public ErrorList getErrors()
{
return errors;
}
}
\ No newline at end of file
......@@ -16,7 +16,12 @@ import org.xml.sax.SAXException;
import com.dddviewr.collada.Collada;
import com.matt.difinspector.io.ReverseDataInputStream;
import com.matt.difinspector.io.ReverseDataOutputStream;
import com.matt.difinspector.mesh.InteriorBuilder;
import com.matt.difinspector.models.OBJModel;
import com.matt.difinspector.models.OBJModel.OBJIndex;
import com.matt.difinspector.structures.Point2F;
import com.matt.difinspector.structures.Point3F;
import com.matt.jmatt.lists.ErrorList;
public class InteriorResource
{
......@@ -29,6 +34,7 @@ public class InteriorResource
private List<ForceField> forceFields;
private List<AISpecialNode> aiSpecialNodes;
private List<ItrGameEntity> gameEntities;
private ErrorList errors;
public InteriorResource()
{
......@@ -39,6 +45,7 @@ public class InteriorResource
this.forceFields = new ArrayList<ForceField>();
this.aiSpecialNodes = new ArrayList<AISpecialNode>();
this.gameEntities = new ArrayList<ItrGameEntity>();
this.errors = new ErrorList();
}
public boolean read(File file)
......@@ -202,13 +209,19 @@ public class InteriorResource
dos.writeInt(this.detailLevels.size());
for (int i = 0; i < this.detailLevels.size(); i++)
{
this.detailLevels.get(i).write(dos, targetVersion);
if (!this.detailLevels.get(i).write(dos, targetVersion))
{
return false;
}
}
dos.writeInt(this.subObjects.size());
for (int i = 0; i < this.subObjects.size(); i++)
{
this.subObjects.get(i).write(dos, targetVersion);
if (!this.subObjects.get(i).write(dos, targetVersion))
{
return false;
}
}
dos.writeInt(0);
......@@ -485,10 +498,27 @@ public class InteriorResource
try
{
System.out.println("Importing OBJ File");
OBJModel model = new OBJModel(file);
Interior interior = new Interior();
if (interior.processOBJ(model))
System.out.println("Converting To Interior");
InteriorBuilder ib = new InteriorBuilder("tile_advanced");
for (OBJIndex index : model.getIndices())
{
Point3F vertex = model.getPositions().get(index.getVertexIndex());
Point2F texCoord = model.getTexCoords().get(index.getTexCoordIndex());
String texture = model.getTextures().get(index.getTextureIndex());
ib.addVertex(vertex, texCoord, texture);
}
System.out.println("Building Interior");
Interior interior = ib.build();
System.out.println("Adding Detail Level 0");
//if (interior.processOBJ(model))
if (interior != null)
{
this.detailLevels.add(interior);
} else
......@@ -543,5 +573,22 @@ public class InteriorResource
{
return this.hasVehicleCollisions;
}
public ErrorList getErrors()
{
ErrorList errorList = new ErrorList();
errorList.append(this.errors);
for (Interior interior : this.detailLevels)
{
errorList.append(interior.getErrors());
}
if (errorList.getCount() == 0)
{
errorList.add();
}
return errorList;
}
}
......@@ -89,6 +89,9 @@ public class PlaneEditor extends JFrame
public void updateDisplay()
{
if (this.interior.getDetailLevels().get(0).getPlanes() == null)
return;
int planeIndex = this.planeId;
PlaneF plane = this.interior.getDetailLevels().get(0).getPlanes()[planeIndex];
......
......@@ -259,11 +259,16 @@ public class RenderSettings extends JFrame
InteriorResource res = DifInspector.getInstance().getInterior();
Interior interior = res.getDetailLevels().get(0);
if (this.hulls.isSelected())
{
if (interior.getConvexHulls() == null)
return;
this.indexField.setMax(interior.getConvexHulls().length - 1);
} else if (this.surfaces.isSelected())
{
if (interior.getSurfaces() == null)
return;
this.indexField.setMax(interior.getSurfaces().length - 1);
}
}
......
......@@ -155,13 +155,29 @@ public class Selector extends JFrame
Interior interior = res.getDetailLevels().get(0);
if (this.mode == EnumSelectionMode.SURFACES)
{
if (interior.getSurfaces() == null)
return;
this.indexField.setMax(interior.getSurfaces().length - 1);
}
else if (this.mode == EnumSelectionMode.POINTS)
{
if (interior.getPoints() == null)
return;
this.indexField.setMax(interior.getPoints().length - 1);
}
else if (this.mode == EnumSelectionMode.HULLS)
{
if (interior.getConvexHulls() == null)
return;
this.indexField.setMax(interior.getConvexHulls().length - 1);
}
else if (this.mode == EnumSelectionMode.PLANES)
{
if (interior.getPlanes() == null)
return;
this.indexField.setMax(interior.getPlanes().length - 1);
}
}
public void refresh()
......
......@@ -161,6 +161,8 @@ public class SurfaceEditor extends JFrame
public void updateDisplay()
{
if (this.interior.getDetailLevels().get(0).getSurfaces() == null)
return;
/*
* String text = this.surfaceSelection.getSelectedItem().toString();
*
......
package com.matt.difinspector.mesh;
import com.matt.difinspector.structures.Point2F;
import com.matt.difinspector.structures.Point3F;
public class Mesh
{
protected Point3F[] vertices;
protected int[] indices;
protected Point2F[] textureCoords;
protected int[] texCoordIndices;
protected String[] textures;
protected int[] textureIndices;
public Mesh()
{
this.vertices = new Point3F[0];
this.indices = new int[0];
this.textureCoords = new Point2F[0];
this.texCoordIndices = new int[0];
this.textures = new String[0];
this.textureIndices = new int[0];
}
public Point3F[] getVertices()
{
return this.vertices;
}
public int[] getIndices()
{
return this.indices;
}
public void setVertices(Point3F[] vertices, int[] indices)
{
this.vertices = vertices;
this.indices = indices;
}
public Point2F[] getTextureCoords()
{
return this.textureCoords;
}
public int[] getTexCoordIndices()
{
return this.texCoordIndices;
}
public void setTextureCoords(Point2F[] textureCoords, int[] texCoordIndices)
{
this.textureCoords = textureCoords;
this.texCoordIndices = texCoordIndices;
}
public String[] getTextures()
{
return this.textures;
}
public int[] getTextureIndices()
{
return this.textureIndices;
}
public void setTextures(String[] textures, int[] textureIndices)
{
this.textures = textures;
this.textureIndices = textureIndices;
}
}
package com.matt.difinspector.mesh;
import com.matt.difinspector.structures.Point2F;
import com.matt.difinspector.structures.Point3F;
import com.matt.difinspector.util.Util;
import com.matt.jmatt.lists.OptimizedList;
public class MeshBuilder
{
protected OptimizedList<Point3F> positions;
protected OptimizedList<Point2F> textureCoordinates;
protected OptimizedList<String> textures;
protected String defaultTexture;
public MeshBuilder(String defaultTexture)
{
this.positions = new OptimizedList<Point3F>();
this.textureCoordinates = new OptimizedList<Point2F>();
this.textures = new OptimizedList<String>();
this.defaultTexture = defaultTexture;
}
public void addVertex(Point3F vertex, Point2F uv, String texture)
{
this.positions.add(vertex);
this.textureCoordinates.add(uv);
this.textures.add(texture);
}
public void addVertex(Point3F vertex, Point2F uv)
{
String lastTexture = textures.lastObject();
if (lastTexture == null)
lastTexture = this.defaultTexture;
this.addVertex(vertex, uv, lastTexture);
}
public void addVertex(Point3F vertex)
{
Point2F lastUV = this.textureCoordinates.lastObject();
if (lastUV == null)
lastUV = new Point2F();
this.addVertex(vertex, lastUV);
}
public Mesh build()
{
Point3F[] vertices = new Point3F[this.positions.sizeObjects()];
Integer[] indices = new Integer[this.positions.sizeIndices()];
this.positions.getObjects().toArray(vertices);
this.positions.getIndices().toArray(indices);
Point2F[] textureCoords = new Point2F[this.textureCoordinates.sizeObjects()];
Integer[] textureCoordIndices = new Integer[this.textureCoordinates.sizeIndices()];
this.textureCoordinates.getObjects().toArray(textureCoords);
this.textureCoordinates.getIndices().toArray(textureCoordIndices);
String[] textures = new String[this.textures.sizeObjects()];
Integer[] textureIndices = new Integer[this.textures.sizeIndices()];
this.textures.getObjects().toArray(textures);
this.textures.getIndices().toArray(textureIndices);
Mesh mesh = new Mesh();
mesh.setVertices(vertices, Util.getIntArray(indices));
mesh.setTextureCoords(textureCoords, Util.getIntArray(textureCoordIndices));
mesh.setTextures(textures, Util.getIntArray(textureIndices));
return mesh;
}
}
......@@ -16,6 +16,7 @@ public class OBJModel
protected List<Point3F> positions;
protected List<Point2F> texCoords;
protected List<Point3F> normals;
protected List<String> textures;
protected List<OBJIndex> indices;
protected boolean hasTexCoords;
protected boolean hasNormals;
......@@ -27,9 +28,12 @@ public class OBJModel
this.texCoords = new ArrayList<Point2F>();
this.normals = new ArrayList<Point3F>();
this.indices = new ArrayList<OBJIndex>();
this.textures = new ArrayList<String>();
this.hasTexCoords = false;
this.hasNormals = false;
String currentTexture = "";
BufferedReader meshReader = new BufferedReader(new FileReader(file));
String line;
......@@ -60,23 +64,34 @@ public class OBJModel
this.normals.add(new Point3F(Float.valueOf(tokens[1]), Float.valueOf(tokens[2]), Float.valueOf(tokens[3])));
} else if (tokens[0].equals("f"))
{
/*if (tokens.length > 4)
{
System.out.println("WARNING: Face Needs To Be Triangulated!");
}*/
for (int i = 0; i < tokens.length - 3; i++)
{
this.indices.add(parseOBJIndex(tokens[1]));
this.indices.add(parseOBJIndex(tokens[2 + i]));
this.indices.add(parseOBJIndex(tokens[3 + i]));
this.indices.add(parseOBJIndex(tokens[1], currentTexture));
this.indices.add(parseOBJIndex(tokens[2 + i], currentTexture));
this.indices.add(parseOBJIndex(tokens[3 + i], currentTexture));
}
} else if (tokens[0].equals("usemtl"))
{
currentTexture = tokens[1];
if (!this.textures.contains(currentTexture))
this.textures.add(currentTexture);
}
}
meshReader.close();
}
private OBJIndex parseOBJIndex(String token)
private OBJIndex parseOBJIndex(String token, String texture)
{
String[] values = token.split("/");
OBJIndex result = new OBJIndex();
result.setTextureIndex(this.textures.indexOf(texture));
result.setVertexIndex(Integer.parseInt(values[0]) - 1);
if (values.length > 1)
......@@ -102,6 +117,7 @@ public class OBJModel
protected int vertexIndex;
protected int texCoordIndex;
protected int normalIndex;
protected int textureIndex;
public OBJIndex()
{
......@@ -137,6 +153,16 @@ public class OBJModel
{
this.normalIndex = normalIndex;
}
public int getTextureIndex()
{
return textureIndex;
}
public void setTextureIndex(int textureIndex)
{
this.textureIndex = textureIndex;
}
}
public List<Point3F> getPositions()
......@@ -198,6 +224,16 @@ public class OBJModel
{
this.hasNormals = hasNormals;
}
public List<String> getTextures()
{
return textures;