Cleanup accidentally removed some necessary code (because it wasn't used yet), added back the regex checks to verify that lines are valid.

This commit is contained in:
pahimar 2013-04-02 21:42:24 -04:00
parent 25d863982f
commit dfed0727ad
3 changed files with 164 additions and 89 deletions

View file

@ -3,9 +3,6 @@ package net.minecraftforge.client.model.obj;
import java.util.ArrayList;
import net.minecraft.client.renderer.Tessellator;
import org.lwjgl.opengl.GL11;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
@ -24,7 +21,7 @@ public class GroupObject
public GroupObject(String name)
{
this(name, GL11.GL_TRIANGLES);
this(name, -1);
}
public GroupObject(String name, int glDrawingMode)

View file

@ -6,7 +6,7 @@ import cpw.mods.fml.relauncher.SideOnly;
@SideOnly(Side.CLIENT)
public class TextureCoordinate
{
public float u, v, w;
public TextureCoordinate(float u, float v)

View file

@ -18,10 +18,15 @@ import cpw.mods.fml.relauncher.SideOnly;
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>();
@ -56,7 +61,6 @@ public class WavefrontObject
String currentLine = null;
while ((currentLine = reader.readLine()) != null)
{
currentLine = currentLine.replaceAll("\\s+", " ").trim();
if (currentLine.startsWith("#") || currentLine.length() == 0)
@ -188,17 +192,20 @@ public class WavefrontObject
{
Vertex vertex = null;
line = line.substring(line.indexOf(" ") + 1);
String[] tokens = line.split(" ");
if (isValidVertexLine(line))
{
line = line.substring(line.indexOf(" ") + 1);
String[] tokens = line.split(" ");
try
{
if (tokens.length == 3)
return new Vertex(Float.parseFloat(tokens[0]), Float.parseFloat(tokens[1]), Float.parseFloat(tokens[2]));
}
catch (NumberFormatException e)
{
e.printStackTrace();
try
{
if (tokens.length == 3)
return new Vertex(Float.parseFloat(tokens[0]), Float.parseFloat(tokens[1]), Float.parseFloat(tokens[2]));
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
}
return vertex;
@ -206,26 +213,47 @@ public class WavefrontObject
private Vertex parseVertexNormal(String line)
{
return parseVertex(line);
Vertex vertexNormal = null;
if (isValidVertexNormalLine(line))
{
line = line.substring(line.indexOf(" ") + 1);
String[] tokens = line.split(" ");
try
{
if (tokens.length == 3)
return new Vertex(Float.parseFloat(tokens[0]), Float.parseFloat(tokens[1]), Float.parseFloat(tokens[2]));
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
}
return vertexNormal;
}
private TextureCoordinate parseTextureCoordinate(String line)
{
TextureCoordinate textureCoordinate = null;
line = line.substring(line.indexOf(" ") + 1);
String[] tokens = line.split(" ");
if (isValidTextureCoordinateLine(line))
{
line = line.substring(line.indexOf(" ") + 1);
String[] tokens = line.split(" ");
try
{
if (tokens.length == 2)
return new TextureCoordinate(Float.parseFloat(tokens[0]), 1 - Float.parseFloat(tokens[1]));
else if (tokens.length == 3)
return new TextureCoordinate(Float.parseFloat(tokens[0]), 1 - Float.parseFloat(tokens[1]), Float.parseFloat(tokens[2]));
}
catch (NumberFormatException e)
{
e.printStackTrace();
try
{
if (tokens.length == 2)
return new TextureCoordinate(Float.parseFloat(tokens[0]), 1 - Float.parseFloat(tokens[1]));
else if (tokens.length == 3)
return new TextureCoordinate(Float.parseFloat(tokens[0]), 1 - Float.parseFloat(tokens[1]), Float.parseFloat(tokens[2]));
}
catch (NumberFormatException e)
{
e.printStackTrace();
}
}
return textureCoordinate;
@ -235,77 +263,94 @@ public class WavefrontObject
{
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 (isValidFaceLine(line))
{
currentGroupObject.glDrawingMode = GL11.GL_TRIANGLES;
}
else if (tokens.length == 4)
{
currentGroupObject.glDrawingMode = GL11.GL_QUADS;
}
face = new Face();
face.vertices = new Vertex[tokens.length];
face.textureCoordinates = new TextureCoordinate[tokens.length];
face.vertexNormals = new Vertex[tokens.length];
String trimmedLine = line.substring(line.indexOf(" ") + 1);
String[] tokens = trimmedLine.split(" ");
String[] subTokens = null;
// 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 (tokens.length == 3)
{
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);
if (currentGroupObject.glDrawingMode == -1)
{
currentGroupObject.glDrawingMode = GL11.GL_TRIANGLES;
}
else if (currentGroupObject.glDrawingMode != GL11.GL_TRIANGLES)
{
// @TODO throw new exception (of some sort)
}
}
else if (tokens.length == 4)
{
if (currentGroupObject.glDrawingMode == -1)
{
currentGroupObject.glDrawingMode = GL11.GL_QUADS;
}
else if (currentGroupObject.glDrawingMode != GL11.GL_QUADS)
{
// @TODO throw new exception (of some sort)
}
}
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)
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))
{
subTokens = tokens[i].split("/");
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.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();
}
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)
// f v1/vt1 v2/vt2 v3/vt3 ...
else if (line.matches(REGEX_FACE_VERTEX_TEXTURECOORD))
{
subTokens = tokens[i].split("//");
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.vertices[i] = vertices.get(Integer.parseInt(subTokens[0]) - 1);
face.textureCoordinates[i] = textureCoordinates.get(Integer.parseInt(subTokens[1]) - 1);
}
face.faceNormal = face.calculateFaceNormal();
}
face.faceNormal = face.calculateFaceNormal();
}
// f v1 v2 v3 ...
else if (line.matches(REGEX_FACE_VERTEX))
{
for (int i = 0; i < tokens.length; ++i)
// f v1//vn1 v2//vn2 v3//vn3 ...
else if (line.matches(REGEX_FACE_VERTEX_VERTEXNORMAL))
{
face.vertices[i] = vertices.get(Integer.parseInt(tokens[i]) - 1);
}
for (int i = 0; i < tokens.length; ++i)
{
subTokens = tokens[i].split("//");
face.faceNormal = face.calculateFaceNormal();
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)
{
face.vertices[i] = vertices.get(Integer.parseInt(tokens[i]) - 1);
}
face.faceNormal = face.calculateFaceNormal();
}
else
throw new IllegalArgumentException();
}
else
throw new IllegalArgumentException();
return face;
}
@ -314,13 +359,46 @@ public class WavefrontObject
{
GroupObject group = null;
String trimmedLine = line.substring(line.indexOf(" ") + 1);
if (trimmedLine.length() > 0)
if (isValidGroupObjectLine(line))
{
group = new GroupObject(trimmedLine);
String trimmedLine = line.substring(line.indexOf(" ") + 1);
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);
}
}