AParser.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426
  1. package min3d.parser;
  2. import java.io.IOException;
  3. import java.io.InputStream;
  4. import java.util.ArrayList;
  5. import java.util.Collections;
  6. import java.util.Comparator;
  7. import java.util.HashMap;
  8. import app.ara.utils.files.FileManager;
  9. import min3d.Min3d;
  10. import min3d.Shared;
  11. import min3d.Utils;
  12. import min3d.animation.AnimationObject3d;
  13. import min3d.core.Object3dContainer;
  14. import min3d.vos.Color4;
  15. import min3d.vos.Number3d;
  16. import min3d.vos.Uv;
  17. import android.content.Context;
  18. import android.content.res.Resources;
  19. import android.graphics.Bitmap;
  20. import android.graphics.Bitmap.Config;
  21. import android.util.Log;
  22. /**
  23. * Abstract parser class with basic parsing functionality.
  24. *
  25. * @author dennis.ippel
  26. *
  27. */
  28. public abstract class AParser implements IParser {
  29. protected Resources resources;
  30. protected String resourceID;
  31. protected String packageID;
  32. protected String currentMaterialKey;
  33. protected ArrayList<ParseObjectData> parseObjects;
  34. protected ParseObjectData co;
  35. protected boolean firstObject;
  36. protected TextureAtlas textureAtlas;
  37. protected ArrayList<Number3d> vertices;
  38. protected ArrayList<Uv> texCoords;
  39. protected ArrayList<Number3d> normals;
  40. protected boolean generateMipMap;
  41. protected HashMap<String, Material> materialMap;
  42. protected InputStream mStream=null;
  43. protected String mPath;
  44. protected Context mContext=null;
  45. public AParser(Context a)
  46. {
  47. mContext=a;
  48. vertices = new ArrayList<Number3d>();
  49. texCoords = new ArrayList<Uv>();
  50. normals = new ArrayList<Number3d>();
  51. parseObjects = new ArrayList<ParseObjectData>();
  52. textureAtlas = new TextureAtlas();
  53. firstObject = true;
  54. materialMap = new HashMap<String, Material>();
  55. }
  56. public AParser(Context a, Resources resources, String resourceID, Boolean generateMipMap)
  57. {
  58. this(a);
  59. mContext=a;
  60. this.resources = resources;
  61. this.resourceID = resourceID;
  62. if (resourceID.indexOf(":") > -1)
  63. this.packageID = resourceID.split(":")[0];
  64. this.generateMipMap = generateMipMap;
  65. }
  66. public AParser(Context a, String path, Boolean generateMipMap)
  67. {
  68. this(a);
  69. mContext=a;
  70. mPath=path;
  71. try {
  72. mStream= FileManager.openFile(a,path);
  73. } catch (Exception e) {
  74. e.printStackTrace();
  75. }
  76. int d = mPath.lastIndexOf('/');
  77. if(d>=0) packageID=mPath.substring(0,d);
  78. else packageID="";
  79. this.generateMipMap = generateMipMap;
  80. }
  81. protected void cleanup()
  82. {
  83. parseObjects.clear();
  84. textureAtlas.cleanup();
  85. vertices.clear();
  86. texCoords.clear();
  87. normals.clear();
  88. }
  89. /**
  90. * Override this in the concrete parser
  91. */
  92. public Object3dContainer getParsedObject() {
  93. return null;
  94. }
  95. /**
  96. * Override this in the concrete parser if applicable
  97. */
  98. public AnimationObject3d getParsedAnimationObject() {
  99. return null;
  100. }
  101. protected String readString(InputStream stream) throws IOException {
  102. String result = new String();
  103. byte inByte;
  104. while ((inByte = (byte) stream.read()) != 0)
  105. result += (char) inByte;
  106. return result;
  107. }
  108. protected int readInt(InputStream stream) throws IOException {
  109. return stream.read() | (stream.read() << 8) | (stream.read() << 16)
  110. | (stream.read() << 24);
  111. }
  112. protected int readShort(InputStream stream) throws IOException {
  113. return (stream.read() | (stream.read() << 8));
  114. }
  115. protected float readFloat(InputStream stream) throws IOException {
  116. return Float.intBitsToFloat(readInt(stream));
  117. }
  118. /**
  119. * Override this in the concrete parser
  120. */
  121. public void parse() {
  122. }
  123. /**
  124. * Contains texture information. UV offsets and scaling is stored here.
  125. * This is used with texture atlases.
  126. *
  127. * @author dennis.ippel
  128. *
  129. */
  130. protected class BitmapAsset
  131. {
  132. /**
  133. * The texture bitmap
  134. */
  135. public Bitmap bitmap;
  136. /**
  137. * The texture identifier
  138. */
  139. public String key;
  140. /**
  141. * Resource ID
  142. */
  143. public String resourceID;
  144. /**
  145. * U-coordinate offset
  146. */
  147. public float uOffset;
  148. /**
  149. * V-coordinate offset
  150. */
  151. public float vOffset;
  152. /**
  153. * U-coordinate scaling value
  154. */
  155. public float uScale;
  156. /**
  157. * V-coordinate scaling value
  158. */
  159. public float vScale;
  160. public boolean useForAtlasDimensions;
  161. /**
  162. * Creates a new BitmapAsset object
  163. * @param
  164. * @param key
  165. */
  166. public BitmapAsset(String key, String resourceID)
  167. {
  168. this.key = key;
  169. this.resourceID = resourceID;
  170. useForAtlasDimensions = false;
  171. }
  172. }
  173. /**
  174. * When a model contains per-face textures a texture atlas is created. This
  175. * combines multiple textures into one and re-calculates the UV coordinates.
  176. *
  177. * @author dennis.ippel
  178. *
  179. */
  180. protected class TextureAtlas {
  181. /**
  182. * The texture bitmaps that should be combined into one.
  183. */
  184. private ArrayList<BitmapAsset> bitmaps;
  185. /**
  186. * The texture atlas bitmap
  187. */
  188. private Bitmap atlas;
  189. /**
  190. * Creates a new texture atlas instance.
  191. */
  192. public TextureAtlas() {
  193. bitmaps = new ArrayList<BitmapAsset>();
  194. }
  195. private String atlasId;
  196. /**
  197. * Adds a bitmap to the atlas
  198. *
  199. * @param
  200. */
  201. public void addBitmapAsset(BitmapAsset ba) {
  202. BitmapAsset existingBA = getBitmapAssetByResourceID(ba.resourceID);
  203. if(existingBA == null)
  204. {
  205. Bitmap b=null;
  206. if(mStream==null) {
  207. int bmResourceID = resources.getIdentifier(ba.resourceID, null, null);
  208. if (bmResourceID == 0) {
  209. return;
  210. }
  211. b = Utils.makeBitmapFromResourceId(bmResourceID);
  212. }else
  213. {
  214. try {
  215. b=FileManager.openImage(mContext, ba.resourceID);
  216. } catch (Exception e) {
  217. e.printStackTrace();
  218. }
  219. }
  220. ba.useForAtlasDimensions = true;
  221. ba.bitmap = b;
  222. }
  223. else
  224. {
  225. ba.bitmap = existingBA.bitmap;
  226. }
  227. bitmaps.add(ba);
  228. }
  229. public BitmapAsset getBitmapAssetByResourceID(String resourceID)
  230. {
  231. int numBitmaps = bitmaps.size();
  232. for(int i=0; i<numBitmaps; i++)
  233. {
  234. if(bitmaps.get(i).resourceID.equals(resourceID))
  235. return bitmaps.get(i);
  236. }
  237. return null;
  238. }
  239. /**
  240. * Generates a new texture atlas
  241. */
  242. public void generate() {
  243. Collections.sort(bitmaps, new BitmapHeightComparer());
  244. if(bitmaps.size() == 0) return;
  245. BitmapAsset largestBitmap = bitmaps.get(0);
  246. int totalWidth = 0;
  247. int numBitmaps = bitmaps.size();
  248. int uOffset = 0;
  249. int vOffset = 0;
  250. for (int i = 0; i < numBitmaps; i++) {
  251. if(bitmaps.get(i).useForAtlasDimensions)
  252. totalWidth += bitmaps.get(i).bitmap.getWidth();
  253. }
  254. atlas = Bitmap.createBitmap(totalWidth, largestBitmap.bitmap
  255. .getHeight(), Config.ARGB_8888);
  256. for (int i = 0; i < numBitmaps; i++) {
  257. BitmapAsset ba = bitmaps.get(i);
  258. BitmapAsset existingBA = getBitmapAssetByResourceID(ba.resourceID);
  259. if(ba.useForAtlasDimensions)
  260. {
  261. Bitmap b = ba.bitmap;
  262. int w = b.getWidth();
  263. int h = b.getHeight();
  264. int[] pixels = new int[w * h];
  265. b.getPixels(pixels, 0, w, 0, 0, w, h);
  266. atlas.setPixels(pixels, 0, w, uOffset, vOffset, w, h);
  267. ba.uOffset = (float) uOffset / totalWidth;
  268. ba.vOffset = 0;
  269. ba.uScale = (float) w / (float) totalWidth;
  270. ba.vScale = (float) h / (float) largestBitmap.bitmap.getHeight();
  271. uOffset += w;
  272. b.recycle();
  273. }
  274. else
  275. {
  276. ba.uOffset = existingBA.uOffset;
  277. ba.vOffset = existingBA.vOffset;
  278. ba.uScale = existingBA.uScale;
  279. ba.vScale = existingBA.vScale;
  280. }
  281. }
  282. /*
  283. FileOutputStream fos;
  284. try {
  285. fos = new FileOutputStream("/data/screenshot.png");
  286. atlas.compress(Bitmap.CompressFormat.PNG, 100, fos);
  287. fos.flush();
  288. fos.close();
  289. } catch (FileNotFoundException e) {
  290. // TODO Auto-generated catch block
  291. e.printStackTrace();
  292. } catch (IOException e) {
  293. // TODO Auto-generated catch block
  294. e.printStackTrace();
  295. }
  296. */
  297. setId(Shared.textureManager().getNewAtlasId());
  298. }
  299. /**
  300. * Returns the generated texture atlas bitmap
  301. *
  302. * @return
  303. */
  304. public Bitmap getBitmap() {
  305. return atlas;
  306. }
  307. /**
  308. * Indicates whether bitmaps have been added to the atlas.
  309. *
  310. * @return
  311. */
  312. public boolean hasBitmaps() {
  313. return bitmaps.size() > 0;
  314. }
  315. /**
  316. * Compares the height of two BitmapAsset objects.
  317. *
  318. * @author dennis.ippel
  319. *
  320. */
  321. private class BitmapHeightComparer implements Comparator<BitmapAsset> {
  322. public int compare(BitmapAsset b1, BitmapAsset b2) {
  323. int height1 = b1.bitmap.getHeight();
  324. int height2 = b2.bitmap.getHeight();
  325. if (height1 < height2) {
  326. return 1;
  327. } else if (height1 == height2) {
  328. return 0;
  329. } else {
  330. return -1;
  331. }
  332. }
  333. }
  334. /**
  335. * Returns a bitmap asset with a specified name.
  336. *
  337. * @param materialKey
  338. * @return
  339. */
  340. public BitmapAsset getBitmapAssetByName(String materialKey) {
  341. int numBitmaps = bitmaps.size();
  342. for (int i = 0; i < numBitmaps; i++) {
  343. if (bitmaps.get(i).key.equals(materialKey))
  344. return bitmaps.get(i);
  345. }
  346. return null;
  347. }
  348. public void cleanup()
  349. {
  350. int numBitmaps = bitmaps.size();
  351. for (int i = 0; i < numBitmaps; i++) {
  352. bitmaps.get(i).bitmap.recycle();
  353. }
  354. if(atlas != null) atlas.recycle();
  355. bitmaps.clear();
  356. vertices.clear();
  357. texCoords.clear();
  358. normals.clear();
  359. }
  360. public void setId(String newAtlasId) {
  361. atlasId = newAtlasId;
  362. }
  363. public String getId() {
  364. return atlasId;
  365. }
  366. }
  367. protected class Material {
  368. public String name;
  369. public String diffuseTextureMap;
  370. public Color4 diffuseColor;
  371. public Material(String name) {
  372. this.name = name;
  373. }
  374. }
  375. }