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
package min3d.parser;

import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import min3d.Min3d;
import min3d.Shared;
import min3d.animation.AnimationObject3d;
import min3d.animation.KeyFrame;
import min3d.vos.Number3d;
import min3d.vos.Uv;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.Log;

public class MD2Parser extends AParser implements IParser {
	private MD2Header header;
	private String currentTextureName;
	private KeyFrame[] frames;

	public MD2Parser(Resources resources, String resourceID, boolean generateMipMap) {
		super(resources, resourceID, generateMipMap);
	}

	@Override
	public AnimationObject3d getParsedAnimationObject() {
		Log.d(Min3d.TAG, "Start object creation");
		Bitmap texture = null;
		AnimationObject3d animObj;

		if (textureAtlas.hasBitmaps()) {
			textureAtlas.generate();
			texture = textureAtlas.getBitmap();
			Shared.textureManager().addTextureId(texture, textureAtlas.getId(), generateMipMap);
		}

		Log.d(Min3d.TAG, "Creating object " + co.name);
		animObj = co.getParsedObject(textureAtlas, materialMap, frames);

		if (textureAtlas.hasBitmaps()) {
			if (texture != null)
				texture.recycle();
		}
		Log.d(Min3d.TAG, "Object creation finished");

		super.cleanup();

		return animObj;
	}

	@Override
	public void parse() {
		InputStream fileIn = resources.openRawResource(resources.getIdentifier(
				resourceID, null, null));
		BufferedInputStream stream = new BufferedInputStream(fileIn);

		co = new ParseObjectData();
		header = new MD2Header();

		Log.d(Min3d.TAG, "Start parsing MD2 file");
		try {
			header.parse(stream);
			frames = new KeyFrame[header.numFrames];
			byte[] bytes = new byte[header.offsetEnd - 68];
			stream.read(bytes);
			getMaterials(stream, bytes);
			getTexCoords(stream, bytes);
			getFrames(stream, bytes);
			getTriangles(stream, bytes);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void getMaterials(BufferedInputStream stream, byte[] bytes)
			throws IOException {
		ByteArrayInputStream ba = new ByteArrayInputStream(bytes,
				header.offsetSkins - 68, bytes.length - header.offsetSkins);
		LittleEndianDataInputStream is = new LittleEndianDataInputStream(ba);
		try
		{
    		for (int i = 0; i < header.numSkins; i++) {
    			String skinPath = is.readString(64);
    			StringBuffer texture = new StringBuffer(packageID);
    			texture.append(":drawable/");
    
    			skinPath = skinPath.substring(skinPath.lastIndexOf("/") + 1,
    					skinPath.length());
    			StringBuffer textureName = new StringBuffer(skinPath.toLowerCase());
    			int dotIndex = textureName.lastIndexOf(".");
    			if (dotIndex > -1)
    				texture.append(textureName.substring(0, dotIndex));
    			else
    				texture.append(textureName);
    
    			currentTextureName = texture.toString();
    			textureAtlas.addBitmapAsset(new BitmapAsset(currentTextureName,
    					currentTextureName));
    		}
		} finally {
		    is.close();
		}
	}

	private void getTexCoords(BufferedInputStream stream, byte[] bytes)
			throws IOException {
		ByteArrayInputStream ba = new ByteArrayInputStream(bytes,
				header.offsetTexCoord - 68, bytes.length
						- header.offsetTexCoord);
		LittleEndianDataInputStream is = new LittleEndianDataInputStream(ba);
		try {
    		for (int i = 0; i < header.numTexCoord; i++) {
    			co.texCoords.add(new Uv((float)is.readShort() / (float)header.skinWidth, (float)is.readShort() / (float)header.skinHeight));
    		}
		} finally {
		    is.close();
		}
	}

	private void getFrames(BufferedInputStream stream, byte[] bytes)
			throws IOException {
		ByteArrayInputStream ba = new ByteArrayInputStream(bytes,
				header.offsetFrames - 68, bytes.length - header.offsetFrames);
		LittleEndianDataInputStream is = new LittleEndianDataInputStream(ba);
		//ArrayList<Number3d> firstFrameVerts = new ArrayList<Number3d>();
		try {
    		for (int i = 0; i < header.numFrames; i++) {
    			float scaleX = is.readFloat();
    			float scaleY = is.readFloat();
    			float scaleZ = is.readFloat();
    			float translateX = is.readFloat();
    			float translateY = is.readFloat();
    			float translateZ = is.readFloat();
    			String name = is.readString(16);
    			
    			if(name.indexOf("_") > 0)
    				name = name.subSequence(0, name.lastIndexOf("_")).toString();
    			else
    				name = name.substring(0, 6).replaceAll("[0-9]{1,2}$", "");
    			
    			Log.d(Min3d.TAG, "frame name: " + name);
    			float vertices[] = new float[header.numVerts * 3];
    			int index = 0;
    
    			for (int j = 0; j < header.numVerts; j++) {
    				vertices[index++] = scaleX * is.readUnsignedByte() + translateX;
    				vertices[index++] = scaleY * is.readUnsignedByte() + translateY;
    				vertices[index++] = scaleZ * is.readUnsignedByte() + translateZ;
    				
    				//int normalIndex = is.readUnsignedByte();
    				if (i == 0)
    					co.vertices.add(new Number3d(vertices[index - 3],
    							vertices[index - 2], vertices[index - 1]));
    			}
    
    			frames[i] = new KeyFrame(name, vertices);
    		}
		} finally {
		    is.close();
		}
	}

	private void getTriangles(BufferedInputStream stream, byte[] bytes)
			throws IOException {
		ByteArrayInputStream ba = new ByteArrayInputStream(bytes,
				header.offsetTriangles - 68, bytes.length
						- header.offsetTriangles);
		LittleEndianDataInputStream is = new LittleEndianDataInputStream(ba);
		try
		{
    		int[] indices = new int[header.numTriangles*3];
    		int index = 0;
    
    		for (int i = 0; i < header.numTriangles; i++) {
    			int[] vertexIDs = new int[3];
    			int[] uvIDS = new int[3];
    
    			indices[index+2] = vertexIDs[2] = is.readUnsignedShort();
    			indices[index+1] = vertexIDs[1] = is.readUnsignedShort();
    			indices[index] = vertexIDs[0] = is.readUnsignedShort();
    			index += 3;
    			uvIDS[2] = is.readUnsignedShort();
    			uvIDS[1] = is.readUnsignedShort();
    			uvIDS[0] = is.readUnsignedShort();
    
    			ParseObjectFace f = new ParseObjectFace();
    			f.v = vertexIDs;
    			f.uv = uvIDS;
    			f.hasn = f.hasuv = true;
    			f.faceLength = 3;
    			f.materialKey = currentTextureName;
    			co.numFaces++;
    			co.faces.add(f);
    			co.calculateFaceNormal(f);
    		}
    		
    		for(int j=0; j<header.numFrames; j++)
    		{
    			frames[j].setIndices(indices);
    		}
		} finally {
		    is.close();
		}
	}

	private class MD2Header {
		public int id;
		public int version;
		public int skinWidth;
		public int skinHeight;
		//public int frameSize;
		public int numSkins;
		public int numVerts;
		public int numTexCoord;
		public int numTriangles;
		//public int numGLCommands;
		public int numFrames;
		public int offsetSkins;
		public int offsetTexCoord;
		public int offsetTriangles;
		public int offsetFrames;
		//public int offsetGLCommands;
		public int offsetEnd;

		public void parse(InputStream stream) throws Exception {
			id = readInt(stream);
			version = readInt(stream);

			if (id != 844121161 || version != 8)
				throw new Exception("This is not a valid MD2 file.");

			skinWidth = readInt(stream);
			skinHeight = readInt(stream);
			/*frameSize = */readInt(stream);

			numSkins = readInt(stream);
			numVerts = readInt(stream);
			numTexCoord = readInt(stream);
			numTriangles = readInt(stream);
			/* numGLCommands = */readInt(stream);
			numFrames = readInt(stream);

			offsetSkins = readInt(stream);
			offsetTexCoord = readInt(stream);
			offsetTriangles = readInt(stream);
			offsetFrames = readInt(stream);
			/* offsetGLCommands = */readInt(stream);
			offsetEnd = readInt(stream);
		}
	}
}

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

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