Guest User

Untitled

a guest
Dec 13th, 2015
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.99 KB | None | 0 0
  1. public class OBJLoader {
  2.  
  3. public static org.bearengine.objects.Mesh loadMesh(String fileName) throws Exception {
  4. List<String> lines = Files.readAllLines(Paths.get(OBJLoader.class.getResource(fileName).toURI()));
  5.  
  6. List<Vector3f> vertices = new ArrayList<>();
  7. List<Vector2f> textures = new ArrayList<>();
  8. List<Vector3f> normals = new ArrayList<>();
  9. List<Face> faces = new ArrayList<>();
  10.  
  11. for (String line : lines) {
  12. String[] tokens = line.split("\\s+");
  13. switch (tokens[0]) {
  14. case "v":
  15. // Geometric vertex
  16. Vector3f vec3f = new Vector3f(
  17. Float.parseFloat(tokens[1]),
  18. Float.parseFloat(tokens[2]),
  19. Float.parseFloat(tokens[3]));
  20. vertices.add(vec3f);
  21. break;
  22. case "vt":
  23. // Texture coordinate
  24. Vector2f vec2f = new Vector2f(
  25. Float.parseFloat(tokens[1]),
  26. Float.parseFloat(tokens[2]));
  27. textures.add(vec2f);
  28. break;
  29. case "vn":
  30. // Vertex normal
  31. Vector3f vec3fNorm = new Vector3f(
  32. Float.parseFloat(tokens[1]),
  33. Float.parseFloat(tokens[2]),
  34. Float.parseFloat(tokens[3]));
  35. normals.add(vec3fNorm);
  36. break;
  37. case "f":
  38. Face face = new Face(tokens[1], tokens[2], tokens[3]);
  39. faces.add(face);
  40. break;
  41. default:
  42. // Ignore other lines
  43. break;
  44. }
  45. }
  46. return reorderLists(vertices, textures, normals, faces);
  47. }
  48.  
  49. private static org.bearengine.objects.Mesh reorderLists(List<Vector3f> posList, List<Vector2f> textCoordList, List<Vector3f> normList, List<Face> facesList) {
  50. List<Integer> indices = new ArrayList<Integer>();
  51. // Create position array in the order it has been declared
  52. float[] posArr = new float[posList.size() * 3];
  53. int i = 0;
  54. for (Vector3f pos : posList) {
  55. posArr[i * 3] = pos.x;
  56. posArr[i * 3 + 1] = pos.y;
  57. posArr[i * 3 + 2] = pos.z;
  58. i++;
  59. }
  60. float[] textCoordArr = new float[posList.size() * 2];
  61. float[] normArr = new float[posList.size() * 3];
  62.  
  63. for (Face face : facesList) {
  64. IdxGroup[] faceVertexIndices = face.getFaceVertexIndices();
  65. for (IdxGroup indValue : faceVertexIndices) {
  66. processFaceVertex(indValue, textCoordList, normList,
  67. indices, textCoordArr, normArr);
  68. }
  69. }
  70. int[] indicesArr = new int[indices.size()];
  71. indicesArr = indices.stream().mapToInt((Integer v) -> v).toArray();
  72.  
  73. org.bearengine.objects.Mesh mesh = new org.bearengine.objects.Mesh();
  74. mesh.setVertices(posList.toArray(new Vector3f[posList.size()]));
  75. mesh.setTextureCoords(textCoordList.toArray(new Vector2f[textCoordList.size()]));
  76. mesh.setNormals(normList.toArray(new Vector3f[normList.size()]));
  77. mesh.setIndices(indicesArr);
  78. return mesh;
  79. }
  80.  
  81. private static void processFaceVertex(IdxGroup indices, List<Vector2f> textCoordList,
  82. List<Vector3f> normList, List<Integer> indicesList,
  83. float[] texCoordArr, float[] normArr) {
  84.  
  85. // Set index for vertex coordinates
  86. int posIndex = indices.idxPos;
  87. indicesList.add(posIndex);
  88.  
  89. // Reorder texture coordinates
  90. if (indices.idxTextCoord >= 0) {
  91. Vector2f textCoord = textCoordList.get(indices.idxTextCoord);
  92. texCoordArr[posIndex * 2] = textCoord.x;
  93. texCoordArr[posIndex * 2 + 1] = 1 - textCoord.y;
  94. }
  95. if (indices.idxVecNormal >= 0) {
  96. // Reorder vectornormals
  97. Vector3f vecNorm = normList.get(indices.idxVecNormal);
  98. normArr[posIndex * 3] = vecNorm.x;
  99. normArr[posIndex * 3 + 1] = vecNorm.y;
  100. normArr[posIndex * 3 + 2] = vecNorm.z;
  101. }
  102. }
  103.  
  104. protected static class Face {
  105.  
  106. /**
  107. * List of idxGroup groups for a face triangle (3 vertices per face).
  108. */
  109. private IdxGroup[] idxGroups = new IdxGroup[3];
  110.  
  111. public Face(String v1, String v2, String v3) {
  112. idxGroups = new IdxGroup[3];
  113. // Parse the lines
  114. idxGroups[0] = parseLine(v1);
  115. idxGroups[1] = parseLine(v2);
  116. idxGroups[2] = parseLine(v3);
  117. }
  118.  
  119. private IdxGroup parseLine(String line) {
  120. IdxGroup idxGroup = new IdxGroup();
  121.  
  122. String[] lineTokens = line.split("/");
  123. int length = lineTokens.length;
  124. idxGroup.idxPos = Integer.parseInt(lineTokens[0]) - 1;
  125. if (length > 1) {
  126. // It can be empty if the obj does not define text coords
  127. String textCoord = lineTokens[1];
  128. idxGroup.idxTextCoord = textCoord.length() > 0 ? Integer.parseInt(textCoord) - 1 : IdxGroup.NO_VALUE;
  129. if (length > 2) {
  130. idxGroup.idxVecNormal = Integer.parseInt(lineTokens[2]) - 1;
  131. }
  132. }
  133.  
  134. return idxGroup;
  135. }
  136.  
  137. public IdxGroup[] getFaceVertexIndices() {
  138. return idxGroups;
  139. }
  140. }
  141.  
  142. protected static class IdxGroup {
  143.  
  144. public static final int NO_VALUE = -1;
  145.  
  146. public int idxPos;
  147.  
  148. public int idxTextCoord;
  149.  
  150. public int idxVecNormal;
  151.  
  152. public IdxGroup() {
  153. idxPos = NO_VALUE;
  154. idxTextCoord = NO_VALUE;
  155. idxVecNormal = NO_VALUE;
  156. }
  157. }
  158.  
  159. }
Advertisement
Add Comment
Please, Sign In to add comment