Unnecessary classes, and cleaning up formatting a bit in prep for the PR to Forge

This commit is contained in:
pahimar 2013-04-02 21:17:40 -04:00
parent fedf87c304
commit 25d863982f
7 changed files with 220 additions and 242 deletions

View file

@ -1,7 +0,0 @@
package net.minecraftforge.client.model;
import net.minecraft.client.model.ModelBase;
public class ModelCustom extends ModelBase {
}

View file

@ -1,8 +0,0 @@
package net.minecraftforge.client.model;
import net.minecraftforge.client.model.obj.WavefrontObject;
public class ModelWavefrontObj extends ModelCustom {
private WavefrontObject wavefrontObject;
}

View file

@ -6,70 +6,79 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class Face {
public class Face
{
public Vertex[] vertices;
public Vertex[] vertexNormals;
public Vertex faceNormal;
public TextureCoordinate[] textureCoordinates;
public void addFaceForRender(Tessellator tessellator) {
public void addFaceForRender(Tessellator tessellator)
{
addFaceForRender(tessellator, 0.0005F);
}
public void addFaceForRender(Tessellator tessellator, float textureOffset) {
if (faceNormal == null) {
public void addFaceForRender(Tessellator tessellator, float textureOffset)
{
if (faceNormal == null)
{
faceNormal = this.calculateFaceNormal();
}
tessellator.setNormal(faceNormal.x, faceNormal.y, faceNormal.z);
float averageU = 0F;
float averageV = 0F;
if (textureCoordinates.length != 0) {
for (int i = 0; i < textureCoordinates.length; ++i) {
if (textureCoordinates.length != 0)
{
for (int i = 0; i < textureCoordinates.length; ++i)
{
averageU += textureCoordinates[i].u;
averageV += textureCoordinates[i].v;
}
averageU = averageU / textureCoordinates.length;
averageV = averageV / textureCoordinates.length;
}
float offsetU, offsetV;
for (int i = 0; i < vertices.length; ++i) {
for (int i = 0; i < vertices.length; ++i)
{
if (textureCoordinates.length != 0) {
if (textureCoordinates.length != 0)
{
offsetU = textureOffset;
offsetV = textureOffset;
if (textureCoordinates[i].u > averageU) {
if (textureCoordinates[i].u > averageU)
{
offsetU = -offsetU;
}
if (textureCoordinates[i].v > averageV) {
if (textureCoordinates[i].v > averageV)
{
offsetV = -offsetV;
}
tessellator.addVertexWithUV(vertices[i].x, vertices[i].y, vertices[i].z, textureCoordinates[i].u + offsetU, textureCoordinates[i].v + offsetV);
}
else {
else
{
tessellator.addVertex(vertices[i].x, vertices[i].y, vertices[i].z);
}
}
}
public Vertex calculateFaceNormal() {
public Vertex calculateFaceNormal()
{
Vec3 v1 = Vec3.createVectorHelper(vertices[1].x - vertices[0].x, vertices[1].y - vertices[0].y, vertices[1].z - vertices[0].z);
Vec3 v2 = Vec3.createVectorHelper(vertices[2].x - vertices[0].x, vertices[2].y - vertices[0].y, vertices[2].z - vertices[0].z);
Vec3 normalVector = null;
normalVector = v1.crossProduct(v2).normalize();
return new Vertex((float)normalVector.xCoord, (float)normalVector.yCoord, (float)normalVector.zCoord);
return new Vertex((float) normalVector.xCoord, (float) normalVector.yCoord, (float) normalVector.zCoord);
}
}

View file

@ -10,49 +10,46 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class GroupObject {
public class GroupObject
{
public String name;
public ArrayList<Face> faces = new ArrayList<Face>();
public int glDrawingMode;
public GroupObject() {
public GroupObject()
{
this("");
}
public GroupObject(String name) {
public GroupObject(String name)
{
this(name, GL11.GL_TRIANGLES);
}
public GroupObject(String name, int glDrawingMode) {
public GroupObject(String name, int glDrawingMode)
{
this.name = name;
this.glDrawingMode = glDrawingMode;
}
public void render() {
if (faces.size() > 0) {
public void render()
{
if (faces.size() > 0)
{
Tessellator tessellator = Tessellator.instance;
tessellator.startDrawing(glDrawingMode);
for (Face face : faces) {
face.addFaceForRender(tessellator);
}
render(tessellator);
tessellator.draw();
}
}
public void render(Tessellator tessellator) {
if (faces.size() > 0) {
for (Face face : faces) {
public void render(Tessellator tessellator)
{
if (faces.size() > 0)
{
for (Face face : faces)
{
face.addFaceForRender(tessellator);
}
}

View file

@ -4,36 +4,18 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class TextureCoordinate {
/**
*
*/
public class TextureCoordinate
{
public float u, v, w;
/**
*
*/
public TextureCoordinate() {
}
/**
* @param u
* @param v
*/
public TextureCoordinate(float u, float v) {
public TextureCoordinate(float u, float v)
{
this(u, v, 0F);
}
/**
* @param u
* @param v
* @param w
*/
public TextureCoordinate(float u, float v, float w) {
public TextureCoordinate(float u, float v, float w)
{
this.u = u;
this.v = v;
this.w = w;

View file

@ -4,21 +4,18 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class Vertex {
public class Vertex
{
public float x, y, z;
public Vertex() {
}
public Vertex(float x, float y) {
public Vertex(float x, float y)
{
this(x, y, 0F);
}
public Vertex(float x, float y, float z) {
public Vertex(float x, float y, float z)
{
this.x = x;
this.y = y;
this.z = z;

View file

@ -15,304 +15,312 @@ import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class WavefrontObject {
public class WavefrontObject
{
private static final String REGEX_VERTEX = "(v( (\\-){0,1}\\d+\\.\\d+){3,4} *\\n)|(v( (\\-){0,1}\\d+\\.\\d+){3,4} *$)";
private static final String REGEX_VERTEX_NORMAL = "(vn( (\\-){0,1}\\d+\\.\\d+){3,4} *\\n)|(vn( (\\-){0,1}\\d+\\.\\d+){3,4} *$)";
private static final String REGEX_TEXTURE_COORDINATE = "(vt( (\\-){0,1}\\d+\\.\\d+){3,4} *\\n)|(vt( (\\-){0,1}\\d+\\.\\d+){3,4} *$)";
private static final String REGEX_FACE_VERTEX_TEXTURECOORD_VERTEXNORMAL = "(f( \\d+/\\d+/\\d+){3,4} *\\n)|(f( \\d+/\\d+/\\d+){3,4} *$)";
private static final String REGEX_FACE_VERTEX_TEXTURECOORD = "(f( \\d+/\\d+){3,4} *\\n)|(f( \\d+/\\d+){3,4} *$)";
private static final String REGEX_FACE_VERTEX_VERTEXNORMAL = "(f( \\d+//\\d+){3,4} *\\n)|(f( \\d+//\\d+){3,4} *$)";
private static final String REGEX_FACE_VERTEX = "(f( \\d+){3,4} *\\n)|(f( \\d+){3,4} *$)";
private static final String REGEX_GROUP_OBJECT = "([go]( [\\w\\d]+){1} *\\n)|([go]( [\\w\\d]+){1} *$)";
public ArrayList<Vertex> vertices = new ArrayList<Vertex>();
public ArrayList<Vertex> vertexNormals = new ArrayList<Vertex>();
public ArrayList<TextureCoordinate> textureCoordinates = new ArrayList<TextureCoordinate>();
public ArrayList<GroupObject> groupObjects = new ArrayList<GroupObject>();
private GroupObject currentGroupObject;
public WavefrontObject(String fileName) {
public WavefrontObject(String fileName)
{
parseObjModel(fileName);
}
public WavefrontObject(URL fileURL) {
public WavefrontObject(URL fileURL)
{
parseObjModel(fileURL);
}
private void parseObjModel(String fileName) {
private void parseObjModel(String fileName)
{
parseObjModel(this.getClass().getResource(fileName));
}
private void parseObjModel(URL fileURL) {
private void parseObjModel(URL fileURL)
{
BufferedReader reader = null;
InputStream inputStream = null;
try {
try
{
inputStream = fileURL.openStream();
reader = new BufferedReader(new InputStreamReader(inputStream));
String currentLine = null;
while ((currentLine = reader.readLine()) != null) {
while ((currentLine = reader.readLine()) != null)
{
currentLine = currentLine.replaceAll("\\s+", " ").trim();
if (currentLine.startsWith("#") || currentLine.length() == 0) {
if (currentLine.startsWith("#") || currentLine.length() == 0)
{
continue;
}
else if (currentLine.startsWith("v ")) {
else if (currentLine.startsWith("v "))
{
Vertex vertex = parseVertex(currentLine);
if (vertex != null) {
if (vertex != null)
{
vertices.add(vertex);
}
}
else if (currentLine.startsWith("vn ")) {
else if (currentLine.startsWith("vn "))
{
Vertex vertex = parseVertexNormal(currentLine);
if (vertex != null) {
if (vertex != null)
{
vertexNormals.add(vertex);
}
}
else if (currentLine.startsWith("vt ")) {
else if (currentLine.startsWith("vt "))
{
TextureCoordinate textureCoordinate = parseTextureCoordinate(currentLine);
if (textureCoordinate != null) {
if (textureCoordinate != null)
{
textureCoordinates.add(textureCoordinate);
}
}
else if (currentLine.startsWith("f ")) {
if (currentGroupObject == null) {
else if (currentLine.startsWith("f "))
{
if (currentGroupObject == null)
{
currentGroupObject = new GroupObject("Default");
}
Face face = parseFace(currentLine);
if (face != null) {
if (face != null)
{
currentGroupObject.faces.add(face);
}
}
else if (currentLine.startsWith("g ") | currentLine.startsWith("o ")) {
else if (currentLine.startsWith("g ") | currentLine.startsWith("o "))
{
GroupObject group = parseGroupObject(currentLine);
if (currentGroupObject != null) {
if (currentGroupObject != null)
{
groupObjects.add(currentGroupObject);
}
currentGroupObject = group;
}
}
groupObjects.add(currentGroupObject);
}
catch (IOException e) {
catch (IOException e)
{
e.printStackTrace();
}
finally {
try {
finally
{
try
{
reader.close();
inputStream.close();
}
catch (IOException e) {
catch (IOException e)
{
e.printStackTrace();
}
}
}
public void renderAll() {
public void renderAll()
{
Tessellator tessellator = Tessellator.instance;
if (currentGroupObject != null) {
tessellator.startDrawing(currentGroupObject.glDrawingMode);
if (currentGroupObject != null)
{
tessellator.startDrawing(currentGroupObject.glDrawingMode);
}
else {
else
{
tessellator.startDrawing(GL11.GL_TRIANGLES);
}
for (GroupObject groupObject : groupObjects) {
for (GroupObject groupObject : groupObjects)
{
groupObject.render(tessellator);
}
tessellator.draw();
}
public void renderOnly(String ... groupNames) {
for (GroupObject groupObject : groupObjects) {
for (String groupName : groupNames) {
if (groupName.equalsIgnoreCase(groupObject.name)) {
public void renderOnly(String... groupNames)
{
for (GroupObject groupObject : groupObjects)
{
for (String groupName : groupNames)
{
if (groupName.equalsIgnoreCase(groupObject.name))
{
groupObject.render();
}
}
}
}
public void renderAllExcept(String ... excludedGroupNames) {
for (GroupObject groupObject : groupObjects) {
for (String excludedGroupName : excludedGroupNames) {
if (!excludedGroupName.equalsIgnoreCase(groupObject.name)) {
public void renderAllExcept(String... excludedGroupNames)
{
for (GroupObject groupObject : groupObjects)
{
for (String excludedGroupName : excludedGroupNames)
{
if (!excludedGroupName.equalsIgnoreCase(groupObject.name))
{
groupObject.render();
}
}
}
}
private Vertex parseVertex(String line) {
private Vertex parseVertex(String line)
{
Vertex vertex = null;
line = line.substring(line.indexOf(" ") + 1);
String[] tokens = line.split(" ");
try {
if (tokens.length == 3) {
try
{
if (tokens.length == 3)
return new Vertex(Float.parseFloat(tokens[0]), Float.parseFloat(tokens[1]), Float.parseFloat(tokens[2]));
}
}
catch (NumberFormatException e) {
catch (NumberFormatException e)
{
e.printStackTrace();
}
return vertex;
}
private Vertex parseVertexNormal(String line) {
private Vertex parseVertexNormal(String line)
{
return parseVertex(line);
}
private TextureCoordinate parseTextureCoordinate(String line) {
private TextureCoordinate parseTextureCoordinate(String line)
{
TextureCoordinate textureCoordinate = null;
line = line.substring(line.indexOf(" ") + 1);
String[] tokens = line.split(" ");
try {
if (tokens.length == 2) {
try
{
if (tokens.length == 2)
return new TextureCoordinate(Float.parseFloat(tokens[0]), 1 - Float.parseFloat(tokens[1]));
}
else if (tokens.length == 3) {
else if (tokens.length == 3)
return new TextureCoordinate(Float.parseFloat(tokens[0]), 1 - Float.parseFloat(tokens[1]), Float.parseFloat(tokens[2]));
}
}
catch (NumberFormatException e) {
catch (NumberFormatException e)
{
e.printStackTrace();
}
return textureCoordinate;
}
private Face parseFace(String line) {
private Face parseFace(String line)
{
Face face = null;
face = new Face();
String trimmedLine = line.substring(line.indexOf(" ") + 1);
String[] tokens = trimmedLine.split(" ");
String[] subTokens = null;
if (tokens.length == 3) {
if (tokens.length == 3)
{
currentGroupObject.glDrawingMode = GL11.GL_TRIANGLES;
}
else if (tokens.length == 4) {
else if (tokens.length == 4)
{
currentGroupObject.glDrawingMode = GL11.GL_QUADS;
}
face.vertices = new Vertex[tokens.length];
face.textureCoordinates = new TextureCoordinate[tokens.length];
face.vertexNormals = new Vertex[tokens.length];
// f v1/vt1/vn1 v2/vt2/vn2 v3/vt3/vn3 ...
if (line.matches(REGEX_FACE_VERTEX_TEXTURECOORD_VERTEXNORMAL)) {
for (int i = 0; i < tokens.length; ++i) {
if (line.matches(REGEX_FACE_VERTEX_TEXTURECOORD_VERTEXNORMAL))
{
for (int i = 0; i < tokens.length; ++i)
{
subTokens = tokens[i].split("/");
face.vertices[i] = vertices.get(Integer.parseInt(subTokens[0]) - 1);
face.textureCoordinates[i] = textureCoordinates.get(Integer.parseInt(subTokens[1]) - 1);
face.vertexNormals[i] = vertexNormals.get(Integer.parseInt(subTokens[2]) - 1);
}
face.faceNormal = face.calculateFaceNormal();
}
// f v1/vt1 v2/vt2 v3/vt3 ...
else if (line.matches(REGEX_FACE_VERTEX_TEXTURECOORD)) {
for (int i = 0; i < tokens.length; ++i) {
else if (line.matches(REGEX_FACE_VERTEX_TEXTURECOORD))
{
for (int i = 0; i < tokens.length; ++i)
{
subTokens = tokens[i].split("/");
face.vertices[i] = vertices.get(Integer.parseInt(subTokens[0]) - 1);
face.textureCoordinates[i] = textureCoordinates.get(Integer.parseInt(subTokens[1]) - 1);
}
face.faceNormal = face.calculateFaceNormal();
}
// f v1//vn1 v2//vn2 v3//vn3 ...
else if (line.matches(REGEX_FACE_VERTEX_VERTEXNORMAL)) {
for (int i = 0; i < tokens.length; ++i) {
else if (line.matches(REGEX_FACE_VERTEX_VERTEXNORMAL))
{
for (int i = 0; i < tokens.length; ++i)
{
subTokens = tokens[i].split("//");
face.vertices[i] = vertices.get(Integer.parseInt(subTokens[0]) - 1);
face.vertexNormals[i] = vertexNormals.get(Integer.parseInt(subTokens[1]) - 1);
}
face.faceNormal = face.calculateFaceNormal();
}
// f v1 v2 v3 ...
else if (line.matches(REGEX_FACE_VERTEX)) {
for (int i = 0; i < tokens.length; ++i) {
else if (line.matches(REGEX_FACE_VERTEX))
{
for (int i = 0; i < tokens.length; ++i)
{
face.vertices[i] = vertices.get(Integer.parseInt(tokens[i]) - 1);
}
face.faceNormal = face.calculateFaceNormal();
}
else {
else
throw new IllegalArgumentException();
}
return face;
}
private GroupObject parseGroupObject(String line) {
private GroupObject parseGroupObject(String line)
{
GroupObject group = null;
String trimmedLine = line.substring(line.indexOf(" ") + 1);
if (trimmedLine.length() > 0) {
if (trimmedLine.length() > 0)
{
group = new GroupObject(trimmedLine);
}
return group;
}
private static boolean isValidVertexLine(String line) {
return line.matches(REGEX_VERTEX);
}
private static boolean isValidVertexNormalLine(String line) {
return line.matches(REGEX_VERTEX_NORMAL);
}
private static boolean isValidTextureCoordinateLine(String line) {
return line.matches(REGEX_TEXTURE_COORDINATE);
}
private static boolean isValidFaceLine(String line) {
return (line.matches(REGEX_FACE_VERTEX_TEXTURECOORD_VERTEXNORMAL) ||
line.matches(REGEX_FACE_VERTEX_TEXTURECOORD) ||
line.matches(REGEX_FACE_VERTEX_VERTEXNORMAL) ||
line.matches(REGEX_FACE_VERTEX));
}
private static boolean isValidGroupObjectLine(String line) {
return line.matches(REGEX_GROUP_OBJECT);
}
}