Skip to content

Instantly share code, notes, and snippets.

@Duta
Last active December 15, 2015 17:59
Show Gist options
  • Save Duta/5300560 to your computer and use it in GitHub Desktop.
Save Duta/5300560 to your computer and use it in GitHub Desktop.
// The vertex/face lists.
// In your code these are called
// "vectors" and "walls"
List<Vector> vertices = new ArrayList<Vector>();
List<Wall> faces = new ArrayList<Wall>();
// Now to parse the file
public void parseObjFile(String fileName) {
try {
FileReader fr = new FileReader(fileName);
BufferedReader br = new BufferedReader(fr);
String line;
while((line = br.readLine()) != null) {
// Turn all whitespace into single spaces
line = line.replaceAll("\\s+", " ");
// Ensure there isn't any leading/trailing whitespace
line = line.trim();
// Split the line into pieces
// (using a space as the separator)
String[] parts = line.split(" ");
// If there isn't anything on this line, skip it
if(parts.length == 0) {
continue;
}
if("v".equalsIgnoreCase(parts[0])) {
// If it starts with "v", it's a vertex
// Check that there is the right amount of pieces
if(parts.length < 4) {
continue;
}
// Attempt to get the x/y/z values
double x = 0.0, y = 0.0, z = 0.0;
try {
x = Double.parseDouble(parts[1]);
y = Double.parseDouble(parts[2]);
z = Double.parseDouble(parts[3]);
} catch(NumberFormatException ex) {
// If we got an error, print a
// message and skip this line.
System.err.println("Error while parsing \"" + line + "\" in the file \"" + fileName + "\".");
System.err.println("The file may not have parsed correctly.");
continue;
}
// Add this to the list of vertices
double[] location = {x, y, z};
Vector vertex = new Vector(location);
vertices.add(vertex);
} else if("f".equalsIgnoreCase(parts[0])) {
// If it starts with "f", it's a face
// Ensure it has at least 3 indexes
int len = parts.length - 1;
if(len < 4) {
continue;
}
// Attempt to parse the indexes
int[] points = new int[len - 1];
try {
for(int i = 1; i < len; i++) {
points[i-1] = Integer.parseInt(parts[i]);
}
} catch(NumberFormatException ex) {
// If we got an error, print a
// message and skip this line.
System.err.println("Error while parsing \"" + line + "\" in the file \"" + fileName + "\".");
System.err.println("The file may not have parsed correctly.");
continue;
}
// Add this to the list of faces
Wall face = new Wall(points);
faces.add(face);
}
}
br.close();
} catch(IOException ex) {
System.err.println("Error while parsing the file \"" + fileName + "\".");
System.err.println("Error message: " + ex.getMessage());
}
}
// You'll also need to update your draw() method.
// Before, you had this:
// <BEFORE>
for(int i=0;i<walls.size();i++)
{
int[] points = walls.get(i).points;
int[] x = {
300+(int)(200*(vectors.get(points[0]).x+distX)/(distZ+vectors.get(points[0]).z)),
300+(int)(200*(vectors.get(points[1]).x+distX)/(distZ+vectors.get(points[1]).z)),
300+(int)(200*(vectors.get(points[2]).x+distX)/(distZ+vectors.get(points[2]).z)),
300+(int)(200*(vectors.get(points[3]).x+distX)/(distZ+vectors.get(points[3]).z))
};
int[] y = {
200+(int)(200*(vectors.get(points[0]).y+distY)/(distZ+vectors.get(points[0]).z)),
200+(int)(200*(vectors.get(points[1]).y+distY)/(distZ+vectors.get(points[1]).z)),
200+(int)(200*(vectors.get(points[2]).y+distY)/(distZ+vectors.get(points[2]).z)),
200+(int)(200*(vectors.get(points[3]).y+distY)/(distZ+vectors.get(points[3]).z))
};
g.drawPolygon(x,y,4);
}
// </BEFORE>
// Now, put this (otherwise you'll get problems):
// <NOW>
for(Wall wall : walls)
{
int[] points = wall.points;
int len = points.length;
int[] x = new int[len];
int[] y = new int[len];
for(int i = 0; i < len; i++) {
x[i] = 300+(int)(200*(vectors.get(points[i]).x+distX)/(distZ+vectors.get(points[i]).z));
y[i] = 200+(int)(200*(vectors.get(points[i]).y+distY)/(distZ+vectors.get(points[i]).z));
}
g.drawPolygon(x,y,len);
}
// </NOW>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment