Advertisement
Guest User

Untitled

a guest
Feb 10th, 2015
265
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.51 KB | None | 0 0
  1. private static Vector3f parseVertex(String line) {
  2. String[] xyz = line.split(" ");
  3. float x = Float.valueOf(xyz[1]);
  4. float y = Float.valueOf(xyz[2]);
  5. float z = Float.valueOf(xyz[3]);
  6. return new Vector3f(x, y, z);
  7. }
  8.  
  9. private static Vector3f parseNormal(String line) {
  10. String[] xyz = line.split(" ");
  11. float x = Float.valueOf(xyz[1]);
  12. float y = Float.valueOf(xyz[2]);
  13. float z = Float.valueOf(xyz[3]);
  14. return new Vector3f(x, y, z);
  15. }
  16.  
  17. private static Vector2f parseTextureCoordinate(String line) {
  18. String[] xy = line.split(" ");
  19. float x = Float.valueOf(xy[1]);
  20. float y = Float.valueOf(xy[2]);
  21. //float z = Float.valueOf(xy[3]);
  22. return new Vector2f(x, y);
  23. }
  24.  
  25. private static Model.Face parseFace(boolean hasNormals, String line) {
  26. String[] faceIndices = line.split(" ");
  27. int[] vertexIndicesArray = {Integer.parseInt(faceIndices[1].split("/")[0]),
  28. Integer.parseInt(faceIndices[2].split("/")[0]), Integer.parseInt(faceIndices[3].split("/")[0])};
  29. if (hasNormals) {
  30. int[] normalIndicesArray = new int[3];
  31. normalIndicesArray[0] = Integer.parseInt(faceIndices[1].split("/")[2]);
  32. normalIndicesArray[1] = Integer.parseInt(faceIndices[2].split("/")[2]);
  33. normalIndicesArray[2] = Integer.parseInt(faceIndices[3].split("/")[2]);
  34. return new Model.Face(vertexIndicesArray, normalIndicesArray);
  35. } else {
  36. return new Model.Face((vertexIndicesArray));
  37. }
  38. }
  39.  
  40. public static Model loadModel(File f) throws IOException {
  41. BufferedReader reader = new BufferedReader(new FileReader(f));
  42. Model m = new Model();
  43. String line;
  44. while ((line = reader.readLine()) != null) {
  45. String prefix = line.split(" ")[0];
  46. if (prefix.equals("#")) {
  47. continue;
  48. } else if (prefix.equals("v")) {
  49. m.getVertices().add(parseVertex(line));
  50. } else if (prefix.equals("vn")) {
  51. m.getNormals().add(parseNormal(line));
  52. } else if (prefix.equals("f")) {
  53. m.getFaces().add(parseFace(m.hasNormals(), line));
  54. }else if (prefix.equals("f")) {
  55. m.getFaces().add(parseFace(m.hasNormals(), line));
  56. } else {
  57. continue;
  58. }
  59. }
  60. reader.close();
  61. return m;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement