Guest User

Untitled

a guest
Jul 16th, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.06 KB | None | 0 0
  1. private int mBytesPerFloat = 4;
  2. private int numFaces;
  3. private FloatBuffer normals, textureCoordinates, positions;
  4.  
  5. ObjLoader objLoader = new ObjLoader(mContext, "engagement_ring.obj");
  6.  
  7. numFaces = objLoader.numFaces;
  8.  
  9. // Initialize the buffers.
  10. positions = ByteBuffer.allocateDirect(objLoader.positions.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
  11. positions.put(objLoader.positions).position(0);
  12.  
  13. normals = ByteBuffer.allocateDirect(objLoader.normals.length * mBytesPerFloat).order(ByteOrder.nativeOrder()).asFloatBuffer();
  14. normals.put(objLoader.normals).position(0);
  15.  
  16. textureCoordinates = ByteBuffer.allocateDirect(objLoader.textureCoordinates.length * mBytesPerFloat)
  17. .order(ByteOrder.nativeOrder()).asFloatBuffer();
  18. textureCoordinates.put(objLoader.textureCoordinates).position(0);
  19.  
  20. public final class ObjLoader {
  21.  
  22. public final int numFaces;
  23.  
  24. public final float[] normals;
  25. public final float[] textureCoordinates;
  26. public final float[] positions;
  27.  
  28. public ObjLoader(Context context, String file) {
  29.  
  30. Vector<Float> vertices = new Vector<>();
  31. Vector<Float> normals = new Vector<>();
  32. Vector<Float> textures = new Vector<>();
  33. Vector<String> faces = new Vector<>();
  34.  
  35. BufferedReader reader = null;
  36. try {
  37. InputStreamReader in = new InputStreamReader(context.getAssets().open(file));
  38. reader = new BufferedReader(in);
  39.  
  40. // read file until EOF
  41. String line;
  42. while ((line = reader.readLine()) != null) {
  43. String[] parts = line.split(" ");
  44. switch (parts[0]) {
  45. case "v":
  46. // vertices
  47. vertices.add(Float.valueOf(parts[1]));
  48. vertices.add(Float.valueOf(parts[2]));
  49. vertices.add(Float.valueOf(parts[3]));
  50. break;
  51. case "vt":
  52. // textures
  53. textures.add(Float.valueOf(parts[1]));
  54. textures.add(Float.valueOf(parts[2]));
  55. break;
  56. case "vn":
  57. // normals
  58. normals.add(Float.valueOf(parts[1]));
  59. normals.add(Float.valueOf(parts[2]));
  60. normals.add(Float.valueOf(parts[3]));
  61. break;
  62. case "f":
  63. // faces: vertex/texture/normal
  64. faces.add(parts[1]);
  65. faces.add(parts[2]);
  66. faces.add(parts[3]);
  67. break;
  68. }
  69. }
  70. } catch (IOException e) {
  71. // cannot load or read file
  72. } finally {
  73. if (reader != null) {
  74. try {
  75. reader.close();
  76. } catch (IOException e) {
  77. //log the exception
  78. }
  79. }
  80. }
  81.  
  82. numFaces = faces.size();
  83. this.normals = new float[numFaces * 3];
  84. textureCoordinates = new float[numFaces * 2];
  85. positions = new float[numFaces * 3];
  86. int positionIndex = 0;
  87. int normalIndex = 0;
  88. int textureIndex = 0;
  89. for (String face : faces) {
  90. String[] parts = face.split("/");
  91.  
  92. int index = 3 * (Short.valueOf(parts[0]) - 1);
  93. positions[positionIndex++] = vertices.get(index++);
  94. positions[positionIndex++] = vertices.get(index++);
  95. positions[positionIndex++] = vertices.get(index);
  96.  
  97. index = 2 * (Short.valueOf(parts[1]) - 1);
  98. textureCoordinates[normalIndex++] = textures.get(index++);
  99. // NOTE: Bitmap gets y-inverted
  100. textureCoordinates[normalIndex++] = 1 - textures.get(index);
  101.  
  102. index = 3 * (Short.valueOf(parts[2]) - 1);
  103. this.normals[textureIndex++] = normals.get(index++);
  104. this.normals[textureIndex++] = normals.get(index++);
  105. this.normals[textureIndex++] = normals.get(index);
  106. }
  107. }
  108. }
Add Comment
Please, Sign In to add comment