Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.91 KB | None | 0 0
  1. package com.nea.nehe.lesson08;
  2.  
  3. import android.content.Context;
  4. import android.content.res.Resources;
  5. import android.util.Log;
  6.  
  7. import java.io.BufferedReader;
  8. import java.io.InputStream;
  9. import java.io.InputStreamReader;
  10. import java.util.ArrayList;
  11. import java.util.Vector;
  12.  
  13. public class Mesh
  14. {
  15. float[] vertices;
  16. float[] normals;
  17. float[] uv;
  18. short[] facesVerts;
  19. int[] facesUV;
  20. int[] facesNormals;
  21.  
  22. public Mesh(Context paramContext, int paramInt)
  23. {
  24. Resources localResources = paramContext.getResources();
  25. InputStream localInputStream = localResources.openRawResource(paramInt);
  26. try {
  27. loadObj(localInputStream);
  28. } catch (Exception e) {
  29. e.printStackTrace();
  30. }
  31. }
  32. public void loadObj (InputStream in)
  33. {
  34. String line = "";
  35. try
  36. {
  37. BufferedReader reader = new BufferedReader(new InputStreamReader(in));
  38. StringBuffer b = new StringBuffer();
  39. String l = reader.readLine();
  40. while (l != null)
  41. {
  42. b.append(l);
  43. b.append("\n");
  44. l = reader.readLine();
  45. }
  46. line = b.toString();
  47. reader.close();
  48. }
  49. catch (Exception ex)
  50. { }
  51. loadObjFromString(line);
  52. }
  53.  
  54. public void loadObjFromString (String obj)
  55. {
  56. String[] lines = obj.split("\n");
  57. vertices = new float[lines.length * 3];
  58. normals = new float[lines.length * 3];
  59. uv = new float[lines.length * 3];
  60.  
  61. int numVertices = 0;
  62. int numNormals = 0;
  63. int numUV = 0;
  64. int numFaces = 0;
  65.  
  66. facesVerts = new short[lines.length * 3];
  67. facesNormals = new int[lines.length * 3];
  68. facesUV = new int[lines.length * 3];
  69. int vertexIndex = 0;
  70. int normalIndex = 0;
  71. int uvIndex = 0;
  72. int faceIndex = 0;
  73.  
  74. for (int i = 0; i < lines.length; i++)
  75. {
  76. String line = lines[i];
  77. if (line.startsWith("v "))
  78. {
  79. String[] tokens = line.split("[ ]+");
  80. vertices[vertexIndex] = Float.parseFloat(tokens[1]);
  81. vertices[vertexIndex + 1] = Float.parseFloat(tokens[2]);
  82. vertices[vertexIndex + 2] = Float.parseFloat(tokens[3]);
  83. vertexIndex += 3;
  84. numVertices++;
  85. continue;
  86. }
  87.  
  88. if (line.startsWith("vn "))
  89. {
  90. String[] tokens = line.split("[ ]+");
  91. normals[normalIndex] = Float.parseFloat(tokens[1]);
  92. normals[normalIndex + 1] = Float.parseFloat(tokens[2]);
  93. normals[normalIndex + 2] = Float.parseFloat(tokens[3]);
  94. normalIndex += 3;
  95. numNormals++;
  96. continue;
  97. }
  98.  
  99. if (line.startsWith("vt"))
  100. {
  101. String[] tokens = line.split("[ ]+");
  102. uv[uvIndex] = Float.parseFloat(tokens[1]);
  103. uv[uvIndex + 1] = Float.parseFloat(tokens[2]);
  104. uvIndex += 2;
  105. numUV++;
  106. continue;
  107. }
  108.  
  109. if (line.startsWith("f "))
  110. {
  111. String[] tokens = line.split("[ ]+");
  112.  
  113. String[] parts = tokens[1].split("/");
  114. facesVerts[faceIndex] = (short)getIndex(parts[0], numVertices);
  115. if (parts.length > 2){facesNormals[faceIndex] = getIndex(parts[2], numNormals);}
  116. if (parts.length > 1){facesUV[faceIndex] = getIndex(parts[1], numUV);}
  117. faceIndex++;
  118.  
  119. parts = tokens[2].split("/");
  120. facesVerts[faceIndex] = (short)getIndex(parts[0], numVertices);
  121. if (parts.length > 2) facesNormals[faceIndex] = getIndex(parts[2], numNormals);
  122. if (parts.length > 1) facesUV[faceIndex] = getIndex(parts[1], numUV);
  123. faceIndex++;
  124.  
  125. parts = tokens[3].split("/");
  126. facesVerts[faceIndex] = (short)getIndex(parts[0], numVertices);
  127. if (parts.length > 2) facesNormals[faceIndex] = getIndex(parts[2], numNormals);
  128. if (parts.length > 1) facesUV[faceIndex] = getIndex(parts[1], numUV);
  129. faceIndex++;
  130. numFaces++;
  131. continue;
  132. }
  133. }
  134.  
  135. float[] verts = new float[(numFaces * 3) * (3 + (numNormals > 0 ? 3 : 0) + (numUV > 0 ? 2 : 0))];
  136.  
  137. for (int i = 0, vi = 0; i < numFaces * 3; i++)
  138. {
  139. int vertexIdx = facesVerts[i] * 3;
  140. verts[vi++] = vertices[vertexIdx];
  141. verts[vi++] = vertices[vertexIdx + 1];
  142. verts[vi++] = vertices[vertexIdx + 2];
  143.  
  144. if (numNormals > 0)
  145. {
  146. int normalIdx = facesNormals[i] * 3;
  147. verts[vi++] = normals[normalIdx];
  148. verts[vi++] = normals[normalIdx + 1];
  149. verts[vi++] = normals[normalIdx + 2];
  150. }
  151. if (numUV > 0)
  152. {
  153. int uvIdx = facesUV[i] * 2;
  154. verts[vi++] = uv[uvIdx];
  155. verts[vi++] = uv[uvIdx + 1];
  156. }
  157. }
  158. }
  159.  
  160. /* private static int[] convertToFixedPoint (float[] fverts)
  161. {
  162. int[] fpverts = new int[fverts.length];
  163. for (int i = 0; i < fverts.length; i++)
  164. fpverts[i] = (int)(fverts[i] * 65536);
  165. ;
  166. return fpverts;
  167. }*/
  168.  
  169. private static int getIndex (String index, int size)
  170. {
  171. if (index == null || index.length() == 0) return 0;
  172. int idx = Integer.parseInt(index);
  173. if (idx < 0)
  174. return size + idx;
  175. else
  176. return idx - 1;
  177. }
  178.  
  179. public float[] getNorms()
  180. {
  181. return normals;
  182. }
  183.  
  184. public float[] getTexs()
  185. {
  186. return uv;
  187. }
  188.  
  189. public float[] getVerts()
  190. {
  191. return vertices;
  192. }
  193.  
  194. public short[] getIndices()
  195. {
  196. return facesVerts;
  197. }
  198. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement