AParser.java 9.6 KB

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