Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static Vector3f parseVertex(String line) {
- String[] xyz = line.split(" ");
- float x = Float.valueOf(xyz[1]);
- float y = Float.valueOf(xyz[2]);
- float z = Float.valueOf(xyz[3]);
- return new Vector3f(x, y, z);
- }
- private static Vector3f parseNormal(String line) {
- String[] xyz = line.split(" ");
- float x = Float.valueOf(xyz[1]);
- float y = Float.valueOf(xyz[2]);
- float z = Float.valueOf(xyz[3]);
- return new Vector3f(x, y, z);
- }
- private static Vector2f parseTextureCoordinate(String line) {
- String[] xy = line.split(" ");
- float x = Float.valueOf(xy[1]);
- float y = Float.valueOf(xy[2]);
- //float z = Float.valueOf(xy[3]);
- return new Vector2f(x, y);
- }
- private static Model.Face parseFace(boolean hasNormals, String line) {
- String[] faceIndices = line.split(" ");
- int[] vertexIndicesArray = {Integer.parseInt(faceIndices[1].split("/")[0]),
- Integer.parseInt(faceIndices[2].split("/")[0]), Integer.parseInt(faceIndices[3].split("/")[0])};
- if (hasNormals) {
- int[] normalIndicesArray = new int[3];
- normalIndicesArray[0] = Integer.parseInt(faceIndices[1].split("/")[2]);
- normalIndicesArray[1] = Integer.parseInt(faceIndices[2].split("/")[2]);
- normalIndicesArray[2] = Integer.parseInt(faceIndices[3].split("/")[2]);
- return new Model.Face(vertexIndicesArray, normalIndicesArray);
- } else {
- return new Model.Face((vertexIndicesArray));
- }
- }
- public static Model loadModel(File f) throws IOException {
- BufferedReader reader = new BufferedReader(new FileReader(f));
- Model m = new Model();
- String line;
- while ((line = reader.readLine()) != null) {
- String prefix = line.split(" ")[0];
- if (prefix.equals("#")) {
- continue;
- } else if (prefix.equals("v")) {
- m.getVertices().add(parseVertex(line));
- } else if (prefix.equals("vn")) {
- m.getNormals().add(parseNormal(line));
- } else if (prefix.equals("f")) {
- m.getFaces().add(parseFace(m.hasNormals(), line));
- }else if (prefix.equals("f")) {
- m.getFaces().add(parseFace(m.hasNormals(), line));
- } else {
- continue;
- }
- }
- reader.close();
- return m;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement