Subversion Repository Public Repository

Nextrek

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
package min3d.parser;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;

import min3d.Min3d;
import min3d.Shared;
import min3d.Utils;
import min3d.animation.AnimationObject3d;
import min3d.core.Object3dContainer;
import min3d.vos.Color4;
import min3d.vos.Number3d;
import min3d.vos.Uv;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.util.Log;

/**
 * Abstract parser class with basic parsing functionality.
 * 
 * @author dennis.ippel
 *
 */
public abstract class AParser implements IParser {
	protected Resources resources;
	protected String resourceID;
	protected String packageID;
	protected String currentMaterialKey;
	protected ArrayList<ParseObjectData> parseObjects;
	protected ParseObjectData co;
	protected boolean firstObject;
	protected TextureAtlas textureAtlas;
	protected ArrayList<Number3d> vertices;
	protected ArrayList<Uv> texCoords;
	protected ArrayList<Number3d> normals;
	protected boolean generateMipMap;
	protected HashMap<String, Material> materialMap;
	
	public AParser()
	{
		vertices = new ArrayList<Number3d>();
		texCoords = new ArrayList<Uv>();
		normals = new ArrayList<Number3d>();
		parseObjects = new ArrayList<ParseObjectData>();
		textureAtlas = new TextureAtlas();
		firstObject = true;
		materialMap = new HashMap<String, Material>();
	}
	
	public AParser(Resources resources, String resourceID, Boolean generateMipMap)
	{
		this();
		this.resources = resources;
		this.resourceID = resourceID;
		if (resourceID.indexOf(":") > -1)
			this.packageID = resourceID.split(":")[0];
		this.generateMipMap = generateMipMap;
	}
	
	protected void cleanup()
	{
		parseObjects.clear();
		textureAtlas.cleanup();
		vertices.clear();
		texCoords.clear();
		normals.clear();
	}
	
	/**
	 * Override this in the concrete parser
	 */
	public Object3dContainer getParsedObject() {
		return null;
	}
	
	/**
	 * Override this in the concrete parser if applicable 
	 */
	public AnimationObject3d getParsedAnimationObject() {
		return null;
	}

	protected String readString(InputStream stream) throws IOException {
		String result = new String();
		byte inByte;
		while ((inByte = (byte) stream.read()) != 0)
			result += (char) inByte;
		return result;
	}

	protected int readInt(InputStream stream) throws IOException {
		return stream.read() | (stream.read() << 8) | (stream.read() << 16)
				| (stream.read() << 24);
	}

	protected int readShort(InputStream stream) throws IOException {
		return (stream.read() | (stream.read() << 8));
	}

	protected float readFloat(InputStream stream) throws IOException {
		return Float.intBitsToFloat(readInt(stream));
	}

	/**
	 * Override this in the concrete parser
	 */
	public void parse() {
	}
	

	/**
	 * Contains texture information. UV offsets and scaling is stored here.
	 * This is used with texture atlases.
	 * 
	 * @author dennis.ippel
	 *
	 */
	protected class BitmapAsset
	{
		/**
		 * The texture bitmap
		 */
		public Bitmap bitmap;
		/**
		 * The texture identifier
		 */
		public String key;
		/**
		 * Resource ID
		 */
		public String resourceID;
		/**
		 * U-coordinate offset
		 */
		public float uOffset;
		/**
		 * V-coordinate offset
		 */
		public float vOffset;
		/**
		 * U-coordinate scaling value
		 */
		public float uScale;
		/**
		 * V-coordinate scaling value
		 */
		public float vScale;
		public boolean useForAtlasDimensions;
		
		/**
		 * Creates a new BitmapAsset object
		 * @param bitmap
		 * @param key
		 */
		public BitmapAsset(String key, String resourceID)
		{
			this.key = key;
			this.resourceID = resourceID;
			useForAtlasDimensions = false;
		}
	}
	
	/**
	 * When a model contains per-face textures a texture atlas is created. This
	 * combines multiple textures into one and re-calculates the UV coordinates.
	 * 
	 * @author dennis.ippel
	 * 
	 */
	protected class TextureAtlas {
		/**
		 * The texture bitmaps that should be combined into one.
		 */
		private ArrayList<BitmapAsset> bitmaps;
		/**
		 * The texture atlas bitmap
		 */
		private Bitmap atlas;

		/**
		 * Creates a new texture atlas instance.
		 */
		public TextureAtlas() {
			bitmaps = new ArrayList<BitmapAsset>();
		}
		private String atlasId;

		/**
		 * Adds a bitmap to the atlas
		 * 
		 * @param bitmap
		 */
		public void addBitmapAsset(BitmapAsset ba) {
			BitmapAsset existingBA = getBitmapAssetByResourceID(ba.resourceID);

			if(existingBA == null)
			{
				int bmResourceID = resources.getIdentifier(ba.resourceID, null, null);
				if(bmResourceID == 0)
				{
					Log.d(Min3d.TAG, "Texture not found: " + ba.resourceID);
					return;
				}

				Log.d(Min3d.TAG, "Adding texture " + ba.resourceID);
				
				Bitmap b = Utils.makeBitmapFromResourceId(bmResourceID);
				ba.useForAtlasDimensions = true;
				ba.bitmap = b;
			}
			else
			{
				ba.bitmap = existingBA.bitmap;
			}

			bitmaps.add(ba);
		}
		
		public BitmapAsset getBitmapAssetByResourceID(String resourceID)
		{
			int numBitmaps = bitmaps.size();
			
			for(int i=0; i<numBitmaps; i++)
			{
				if(bitmaps.get(i).resourceID.equals(resourceID))
					return bitmaps.get(i);
			}
			
			return null;
		}

		/**
		 * Generates a new texture atlas
		 */
		public void generate() {
			Collections.sort(bitmaps, new BitmapHeightComparer());

			if(bitmaps.size() == 0) return;
			
			BitmapAsset largestBitmap = bitmaps.get(0);
			int totalWidth = 0;
			int numBitmaps = bitmaps.size();
			int uOffset = 0;
			int vOffset = 0;

			for (int i = 0; i < numBitmaps; i++) {
				if(bitmaps.get(i).useForAtlasDimensions)
					totalWidth += bitmaps.get(i).bitmap.getWidth();
			}

			atlas = Bitmap.createBitmap(totalWidth, largestBitmap.bitmap
					.getHeight(), Config.ARGB_8888);

			for (int i = 0; i < numBitmaps; i++) {
				BitmapAsset ba = bitmaps.get(i);
				BitmapAsset existingBA = getBitmapAssetByResourceID(ba.resourceID);				
				
				if(ba.useForAtlasDimensions)
				{
					Bitmap b = ba.bitmap;
					int w = b.getWidth();
					int h = b.getHeight();
					int[] pixels = new int[w * h];
					
					b.getPixels(pixels, 0, w, 0, 0, w, h);
					atlas.setPixels(pixels, 0, w, uOffset, vOffset, w, h);
					
					ba.uOffset = (float) uOffset / totalWidth;
					ba.vOffset = 0;
					ba.uScale = (float) w / (float) totalWidth;
					ba.vScale = (float) h / (float) largestBitmap.bitmap.getHeight();
					
					uOffset += w;
					b.recycle();
				}
				else
				{
					ba.uOffset = existingBA.uOffset;
					ba.vOffset = existingBA.vOffset;
					ba.uScale = existingBA.uScale;
					ba.vScale = existingBA.vScale;
				}
			}
			/*
			FileOutputStream fos;
			try {
				fos = new FileOutputStream("/data/screenshot.png");
				atlas.compress(Bitmap.CompressFormat.PNG, 100, fos);
				fos.flush();
				fos.close();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			*/
			setId(Shared.textureManager().getNewAtlasId());
		}

		/**
		 * Returns the generated texture atlas bitmap
		 * 
		 * @return
		 */
		public Bitmap getBitmap() {
			return atlas;
		}

		/**
		 * Indicates whether bitmaps have been added to the atlas.
		 * 
		 * @return
		 */
		public boolean hasBitmaps() {
			return bitmaps.size() > 0;
		}

		/**
		 * Compares the height of two BitmapAsset objects.
		 * 
		 * @author dennis.ippel
		 * 
		 */
		private class BitmapHeightComparer implements Comparator<BitmapAsset> {
			public int compare(BitmapAsset b1, BitmapAsset b2) {
				int height1 = b1.bitmap.getHeight();
				int height2 = b2.bitmap.getHeight();

				if (height1 < height2) {
					return 1;
				} else if (height1 == height2) {
					return 0;
				} else {
					return -1;
				}
			}
		}
		
		/**
		 * Returns a bitmap asset with a specified name.
		 * 
		 * @param materialKey
		 * @return
		 */
		public BitmapAsset getBitmapAssetByName(String materialKey) {
			int numBitmaps = bitmaps.size();

			for (int i = 0; i < numBitmaps; i++) {
				if (bitmaps.get(i).key.equals(materialKey))
					return bitmaps.get(i);
			}

			return null;
		}
		
		public void cleanup()
		{
			int numBitmaps = bitmaps.size();

			for (int i = 0; i < numBitmaps; i++) {
				bitmaps.get(i).bitmap.recycle();
			}
			
			if(atlas != null) atlas.recycle();
			bitmaps.clear();
			vertices.clear();
			texCoords.clear();
			normals.clear();
		}

		public void setId(String newAtlasId) {
			atlasId = newAtlasId;			
		}

		public String getId() {
			return atlasId;
		}
	}
	
	protected class Material {
		public String name;
		public String diffuseTextureMap;
		public Color4 diffuseColor;

		public Material(String name) {
			this.name = name;
		}
	}
}

Commits for Nextrek/Android/LibrerieNextrek/src/min3d/parser/AParser.java

Diff revisions: vs.
Revision Author Commited Message
4 FMMortaroli picture FMMortaroli Fri 19 Apr, 2013 16:54:38 +0000