OSDN Git Service

Add GeometryOptimizer
[mikumikustudio/MikuMikuStudio.git] / src / projectkyoto / mmd / file / PMDModel.java
1 /*
2  * Copyright (c) 2011 Kazuhiko Kobayashi All rights reserved.
3  * <p/>
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * 
7  * * Redistributions of source code must retain the above copyright notice,
8  * this list of conditions and the following disclaimer.
9  * <p/>
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * <p/>
14  * * Neither the name of 'MMDLoaderJME' nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  * <p/>
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  */
30 package projectkyoto.mmd.file;
31
32 import java.io.BufferedInputStream;
33 import java.io.File;
34 import java.io.FileOutputStream;
35 import java.io.IOException;
36 import java.io.InputStream;
37 import java.io.RandomAccessFile;
38 import java.io.Serializable;
39 import java.net.URL;
40 import java.nio.ByteBuffer;
41 import java.nio.ByteOrder;
42 import java.nio.channels.FileChannel.MapMode;
43 import projectkyoto.mmd.file.util2.BufferUtil;
44
45 /**
46  *
47  * @author Kazuhiko Kobayashi
48  */
49 public class PMDModel implements Serializable{
50     // PMD Header
51
52     private String id; // char[3] "Pmd"
53     private float version; // float 0x00 0x00 0x80 0x3F == 1.00;
54     private String modelName; // char[20] モデル名
55     private String comment; // char[256] コメント
56     // 頂点リスト
57     private int vertCount; // 頂点数
58 //    private PMDVertex[] vertexList;
59     private ByteBuffer vertexBuffer;
60     private int faceVertCount;
61     private int faceVertIndex[];
62     private int materialCount;
63     private PMDMaterial[] material;
64     private PMDBoneList boneList;
65     private PMDIKList ikList;
66     private int skinCount;
67     private PMDSkinData skinData[];
68     private PMDSkinDispList skinDispList;
69     private PMDBoneDispNameList boneDispNameList;
70     private PMDBoneDispList boneDispList;
71     private PMDHeaderEnglish headerEnglish;
72     private PMDToonTextureList toonTextureList;
73     private PMDRigidBodyList rigidBodyList;
74     private PMDJointList jointList;
75
76     public PMDModel() {
77     }
78
79     public PMDModel(URL url) throws IOException {
80         readFromFile(url.openStream());
81     }
82     public PMDModel(InputStream is) throws IOException {
83         readFromFile(is);
84     }
85     public void readFromFile(InputStream is) throws IOException {
86         DataInputStreamLittleEndian dis = null;
87         try {
88 //            is = new DataInputStreamLittleEndian(new BufferedInputStream(
89 //                    new FileInputStream(
90 //                    file)));
91             dis = new DataInputStreamLittleEndian(new BufferedInputStream(is));
92             readFromStream(dis);
93             dis.close();
94 //            this.url = url;
95         } finally {
96             if (dis != null) {
97                 dis.close();
98                 dis = null;
99             }
100         }
101 //        System.out.println(toString());
102     }
103
104     public void readFromStream(DataInputStreamLittleEndian is) throws
105             IOException {
106         id = is.readString(3);
107         if (!"Pmd".equals(id)) {
108             throw new InvalidPMDFileException("Invalid ID:" + id);
109         }
110         version = is.readFloat();
111         modelName = is.readString(20);
112         comment = is.readString(256);
113         vertCount = is.readInt();
114 //        vertexList = new PMDVertex[vertCount];
115 //        vertexBuffer = ByteBuffer.allocateDirect(PMDVertex.size() * vertCount);
116         vertexBuffer = BufferUtil.createByteBuffer(PMDVertex.size() * vertCount);
117         vertexBuffer.order(ByteOrder.nativeOrder());
118         PMDVertex tmpVertex = new PMDVertex();
119         for (int i = 0; i < vertCount; i++) {
120             tmpVertex.readFromStream(is);
121             tmpVertex.writeToBuffer(vertexBuffer);
122             
123         }
124         faceVertCount = is.readInt();
125         faceVertIndex = new int[faceVertCount];
126         for (int i = 0; i < faceVertCount; i++) {
127             faceVertIndex[i] = is.readUnsignedShort();
128         }
129         // 逆にする。
130         for (int i = 0; i < faceVertCount; i += 3) {
131             int tmp = faceVertIndex[i];
132             faceVertIndex[i] = faceVertIndex[i + 1];
133             faceVertIndex[i + 1] = tmp;
134         }
135         materialCount = is.readInt();
136         material = new PMDMaterial[materialCount];
137         for (int i = 0; i < materialCount; i++) {
138             material[i] = new PMDMaterial(is);
139         }
140         boneList = new PMDBoneList(is);
141         ikList = new PMDIKList(is);
142         skinCount = is.readShort();
143         skinData = new PMDSkinData[skinCount];
144         for (int i = 0; i < skinCount; i++) {
145             skinData[i] = new PMDSkinData(is);
146         }
147         skinDispList = new PMDSkinDispList(is);
148         boneDispNameList = new PMDBoneDispNameList(is);
149         boneDispList = new PMDBoneDispList(is);
150         headerEnglish = new PMDHeaderEnglish(this, is);
151         toonTextureList = new PMDToonTextureList(is);
152         rigidBodyList = new PMDRigidBodyList(is);
153         jointList = new PMDJointList(is);
154 //        toonTextureList = new PMDToonTextureList();
155 //        rigidBodyList = new PMDRigidBodyList();
156 //        jointList = new PMDJointList();
157     }
158     public PMDVertex getVertex(int i) {
159         return getVertex(i, new PMDVertex());
160     }
161     public PMDVertex getVertex(int i, PMDVertex in) {
162         vertexBuffer.position(PMDVertex.size() * i);
163         in.readFromBuffer(vertexBuffer);
164         return in;
165     }
166     @Override
167     public String toString() {
168         StringBuffer sb = new StringBuffer();
169         sb.append("{id = " + id
170                 + " version = " + version
171                 + " modelName = " + modelName
172                 + " comment = " + comment);
173         sb.append(" vertexCount = " + vertCount);
174         PMDVertex tmpVertex = new PMDVertex();
175         vertexBuffer.position(0);
176         for (int i=0;i<vertCount;i++) {
177             tmpVertex.readFromBuffer(vertexBuffer);
178             sb.append(tmpVertex.toString());
179         }
180         sb.append(" faceVertCount = " + faceVertCount);
181         sb.append(" faceVertIndex = {");
182         for (int i : faceVertIndex) {
183             sb.append(i).append(" ");
184         }
185         sb.append("}");
186         sb.append(" materialCount = ").append(materialCount);
187         sb.append(" PMDMaterial = {");
188         for (PMDMaterial m : material) {
189             sb.append(m).append(" ");
190         }
191         sb.append("}\n");
192         sb.append("boneList = ");
193         sb.append(boneList.toString());
194         sb.append("\nikList = " + ikList);
195         sb.append("\nskinData = {");
196
197         for (int i = 0; i < skinCount; i++) {
198             sb.append(skinData[i]);
199             sb.append("\n");
200         }
201         sb.append("}\n}\n");
202
203         sb.append(rigidBodyList.toString());
204         return sb.toString();
205     }
206
207     public String getComment() {
208         return comment;
209     }
210
211     public void setComment(String comment) {
212         this.comment = comment;
213     }
214
215     public int getFaceVertCount() {
216         return faceVertCount;
217     }
218
219     public void setFaceVertCount(int faceVertCount) {
220         this.faceVertCount = faceVertCount;
221     }
222
223     public int[] getFaceVertIndex() {
224         return faceVertIndex;
225     }
226
227     public void setFaceVertIndex(int[] faceVertIndex) {
228         this.faceVertIndex = faceVertIndex;
229     }
230
231     public String getId() {
232         return id;
233     }
234
235     public void setId(String id) {
236         this.id = id;
237     }
238
239     public PMDMaterial[] getMaterial() {
240         return material;
241     }
242
243     public void setMaterial(PMDMaterial[] material) {
244         this.material = material;
245     }
246
247     public int getMaterialCount() {
248         return materialCount;
249     }
250
251     public void setMaterialCount(int materialCount) {
252         this.materialCount = materialCount;
253     }
254
255     public String getModelName() {
256         return modelName;
257     }
258
259     public void setModelName(String modelName) {
260         this.modelName = modelName;
261     }
262
263     public float getVersion() {
264         return version;
265     }
266
267     public void setVersion(float version) {
268         this.version = version;
269     }
270
271     public int getVertCount() {
272         return vertCount;
273     }
274
275     public void setVertCount(int vertCount) {
276         this.vertCount = vertCount;
277     }
278
279     public ByteBuffer getVertexBuffer() {
280         return vertexBuffer;
281     }
282
283     public void setVertexBuffer(ByteBuffer vertexBuffer) {
284         this.vertexBuffer = vertexBuffer;
285     }
286
287
288     public PMDBoneList getBoneList() {
289         return boneList;
290     }
291
292     public void setBoneList(PMDBoneList boneList) {
293         this.boneList = boneList;
294     }
295
296     public int getSkinCount() {
297         return skinCount;
298     }
299
300     public void setSkinCount(int skinCount) {
301         this.skinCount = skinCount;
302     }
303
304     public PMDSkinData[] getSkinData() {
305         return skinData;
306     }
307
308     public void setSkinData(PMDSkinData[] skinData) {
309         this.skinData = skinData;
310     }
311
312     public PMDIKList getIkList() {
313         return ikList;
314     }
315
316     public void setIkList(PMDIKList ikList) {
317         this.ikList = ikList;
318     }
319
320     public PMDBoneDispList getBoneDispList() {
321         return boneDispList;
322     }
323
324     public void setBoneDispList(PMDBoneDispList boneDispList) {
325         this.boneDispList = boneDispList;
326     }
327
328     public PMDBoneDispNameList getBoneDispNameList() {
329         return boneDispNameList;
330     }
331
332     public void setBoneDispNameList(PMDBoneDispNameList boneDispNameList) {
333         this.boneDispNameList = boneDispNameList;
334     }
335
336     public PMDHeaderEnglish getHeaderEnglish() {
337         return headerEnglish;
338     }
339
340     public void setHeaderEnglish(PMDHeaderEnglish headerEnglish) {
341         this.headerEnglish = headerEnglish;
342     }
343
344     public PMDSkinDispList getSkinDispList() {
345         return skinDispList;
346     }
347
348     public void setSkinDispList(PMDSkinDispList skinDispList) {
349         this.skinDispList = skinDispList;
350     }
351
352     public PMDToonTextureList getToonTextureList() {
353         return toonTextureList;
354     }
355
356     public void setToonTextureList(PMDToonTextureList toonTextureList) {
357         this.toonTextureList = toonTextureList;
358     }
359
360     public PMDJointList getJointList() {
361         return jointList;
362     }
363
364     public void setJointList(PMDJointList jointList) {
365         this.jointList = jointList;
366     }
367
368     public PMDRigidBodyList getRigidBodyList() {
369         return rigidBodyList;
370     }
371
372     public void setRigidBodyList(PMDRigidBodyList rigidBodyList) {
373         this.rigidBodyList = rigidBodyList;
374     }
375 }