equivalent-exchange-3/ee3_common/net/minecraftforge/client/model/obj/WavefrontObject.java
2013-03-15 15:18:01 -04:00

210 lines
5.3 KiB
Java

package net.minecraftforge.client.model.obj;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Hashtable;
import net.minecraftforge.client.model.obj.parser.LineParser;
import net.minecraftforge.client.model.obj.parser.ObjLineParserFactory;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class WavefrontObject {
private ArrayList<Vertex> vertices = new ArrayList<Vertex>();
private ArrayList<Vertex> vertexNormals = new ArrayList<Vertex>();
private ArrayList<TextureCoordinate> textureCoordinates = new ArrayList<TextureCoordinate>();
private ArrayList<Group> groups = new ArrayList<Group>();
private Hashtable<String, Group> groupsDirectAccess = new Hashtable<String, Group>();
public String fileName;
private ObjLineParserFactory parserFactory;
private Group currentGroup;
public double radius = 0;
public float xScale;
public float yScale;
public float zScale;
public Vertex translate;
public Vertex rotate;
public WavefrontObject(String fileName) {
this(fileName, 1f, 1f, 1f, new Vertex(), new Vertex());
}
public WavefrontObject(String fileName, float xScale, float yScale, float zScale) {
this(fileName, xScale, yScale, zScale, new Vertex(), new Vertex());
}
public WavefrontObject(String fileName, float scale) {
this(fileName, scale, scale, scale, new Vertex(), new Vertex());
}
public WavefrontObject(String fileName, float scale, Vertex translation, Vertex rotation) {
this(fileName, scale, scale, scale, translation, rotation);
}
public WavefrontObject(String fileName, float xScale, float yScale, float zScale, Vertex translation, Vertex rotation) {
try {
this.fileName = fileName;
translate = translation;
rotate = rotation;
this.xScale = xScale;
this.yScale = yScale;
this.zScale = zScale;
parse(fileName);
}
catch (Exception e) {
System.out.println("Error, could not load obj:" + fileName);
}
}
public void parse(String fileName) {
BufferedReader reader = null;
InputStream inputStream = null;
parserFactory = new ObjLineParserFactory(this);
try {
inputStream = this.getClass().getResource(fileName).openStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
parseLine(currentLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
finally {
try {
reader.close();
inputStream.close();
}
catch (IOException e) {
e.printStackTrace();
}
}
}
private void parseLine(String currentLine) {
if (currentLine.length() > 0) {
LineParser parser = parserFactory.getLineParser(currentLine);
parser.parse();
parser.incoporateResults(this);
}
}
public void setTextureCoordinates(ArrayList<TextureCoordinate> textureCoordinates) {
this.textureCoordinates = textureCoordinates;
}
public ArrayList<TextureCoordinate> getTextureCoordinates() {
return textureCoordinates;
}
public void setVertices(ArrayList<Vertex> vertices) {
this.vertices = vertices;
}
public ArrayList<Vertex> getVertices() {
return vertices;
}
public void setVertexNormals(ArrayList<Vertex> vertexNormals) {
this.vertexNormals = vertexNormals;
}
public ArrayList<Vertex> getVertexNormals() {
return vertexNormals;
}
public ArrayList<Group> getGroups() {
return groups;
}
public Hashtable<String, Group> getGroupsDirectAccess() {
return groupsDirectAccess;
}
public Group getCurrentGroup() {
return currentGroup;
}
public void setCurrentGroup(Group currentGroup) {
this.currentGroup = currentGroup;
}
public String getBoudariesText() {
float minX = 0;
float maxX = 0;
float minY = 0;
float maxY = 0;
float minZ = 0;
float maxZ = 0;
Vertex currentVertex = null;
for (int i = 0; i < getVertices().size(); i++) {
currentVertex = getVertices().get(i);
if (currentVertex.x > maxX) {
maxX = currentVertex.x;
}
if (currentVertex.x < minX) {
minX = currentVertex.x;
}
if (currentVertex.y > maxY) {
maxY = currentVertex.y;
}
if (currentVertex.y < minY) {
minY = currentVertex.y;
}
if (currentVertex.z > maxZ) {
maxZ = currentVertex.z;
}
if (currentVertex.z < minZ) {
minZ = currentVertex.z;
}
}
return "maxX=" + maxX + " minX=" + minX + " maxY=" + maxY + " minY=" + minY + " maxZ=" + maxZ + " minZ=" + minZ;
}
public void printBoudariesText() {
System.out.println(getBoudariesText());
}
}