1 /*
2 Open Asset Import Library (assimp)
3 ----------------------------------------------------------------------
4 
5 Copyright (c) 2006-2019, assimp team
6 
7 
8 All rights reserved.
9 
10 Redistribution and use of this software in source and binary forms,
11 with or without modification, are permitted provided that the
12 following conditions are met:
13 
14 * Redistributions of source code must retain the above
15 copyright notice, this list of conditions and the
16 following disclaimer.
17 
18 * Redistributions in binary form must reproduce the above
19 copyright notice, this list of conditions and the
20 following disclaimer in the documentation and/or other
21 materials provided with the distribution.
22 
23 * Neither the name of the assimp team, nor the names of its
24 contributors may be used to endorse or promote products
25 derived from this software without specific prior
26 written permission of the assimp team.
27 
28 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
29 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
30 LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
31 A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
32 OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
35 DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
36 THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 
40 ----------------------------------------------------------------------
41 */
42 #include <utility>
43 #include "MMDPmxParser.h"
44 #include <assimp/StringUtils.h>
45 #ifdef ASSIMP_USE_HUNTER
46 #  include <utf8/utf8.h>
47 #else
48 #  include "../contrib/utf8cpp/source/utf8.h"
49 #endif
50 #include <assimp/Exceptional.h>
51 
52 namespace pmx
53 {
ReadIndex(std::istream * stream,int size)54 	int ReadIndex(std::istream *stream, int size)
55 	{
56 		switch (size)
57 		{
58 		case 1:
59 			uint8_t tmp8;
60 			stream->read((char*) &tmp8, sizeof(uint8_t));
61 			if (255 == tmp8)
62 			{
63 				return -1;
64 			}
65 			else {
66 				return (int) tmp8;
67 			}
68 		case 2:
69 			uint16_t tmp16;
70 			stream->read((char*) &tmp16, sizeof(uint16_t));
71 			if (65535 == tmp16)
72 			{
73 				return -1;
74 			}
75 			else {
76 				return (int) tmp16;
77 			}
78 		case 4:
79 			int tmp32;
80 			stream->read((char*) &tmp32, sizeof(int));
81 			return tmp32;
82 		default:
83 			return -1;
84 		}
85 	}
86 
ReadString(std::istream * stream,uint8_t encoding)87 	std::string ReadString(std::istream *stream, uint8_t encoding)
88 	{
89 		int size;
90 		stream->read((char*) &size, sizeof(int));
91 		std::vector<char> buffer;
92 		if (size == 0)
93 		{
94 			return std::string("");
95 		}
96 		buffer.reserve(size);
97 		stream->read((char*) buffer.data(), size);
98 		if (encoding == 0)
99 		{
100 			// UTF16 to UTF8
101 			const uint16_t* sourceStart = (uint16_t*)buffer.data();
102 			const unsigned int targetSize = size * 3; // enough to encode
103 			char *targetStart = new char[targetSize];
104             std::memset(targetStart, 0, targetSize * sizeof(char));
105 
106             utf8::utf16to8( sourceStart, sourceStart + size/2, targetStart );
107 
108 			std::string result(targetStart);
109             delete [] targetStart;
110 			return result;
111 		}
112 		else
113 		{
114 			// the name is already UTF8
115 			return std::string((const char*)buffer.data(), size);
116 		}
117 	}
118 
Read(std::istream * stream)119 	void PmxSetting::Read(std::istream *stream)
120 	{
121 		uint8_t count;
122 		stream->read((char*) &count, sizeof(uint8_t));
123 		if (count < 8)
124 		{
125 			throw DeadlyImportError("MMD: invalid size");
126 		}
127 		stream->read((char*) &encoding, sizeof(uint8_t));
128 		stream->read((char*) &uv, sizeof(uint8_t));
129 		stream->read((char*) &vertex_index_size, sizeof(uint8_t));
130 		stream->read((char*) &texture_index_size, sizeof(uint8_t));
131 		stream->read((char*) &material_index_size, sizeof(uint8_t));
132 		stream->read((char*) &bone_index_size, sizeof(uint8_t));
133 		stream->read((char*) &morph_index_size, sizeof(uint8_t));
134 		stream->read((char*) &rigidbody_index_size, sizeof(uint8_t));
135 		uint8_t temp;
136 		for (int i = 8; i < count; i++)
137 		{
138 			stream->read((char*)&temp, sizeof(uint8_t));
139 		}
140 	}
141 
Read(std::istream * stream,PmxSetting * setting)142 	void PmxVertexSkinningBDEF1::Read(std::istream *stream, PmxSetting *setting)
143 	{
144 		this->bone_index = ReadIndex(stream, setting->bone_index_size);
145 	}
146 
Read(std::istream * stream,PmxSetting * setting)147 	void PmxVertexSkinningBDEF2::Read(std::istream *stream, PmxSetting *setting)
148 	{
149 		this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
150 		this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
151 		stream->read((char*) &this->bone_weight, sizeof(float));
152 	}
153 
Read(std::istream * stream,PmxSetting * setting)154 	void PmxVertexSkinningBDEF4::Read(std::istream *stream, PmxSetting *setting)
155 	{
156 		this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
157 		this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
158 		this->bone_index3 = ReadIndex(stream, setting->bone_index_size);
159 		this->bone_index4 = ReadIndex(stream, setting->bone_index_size);
160 		stream->read((char*) &this->bone_weight1, sizeof(float));
161 		stream->read((char*) &this->bone_weight2, sizeof(float));
162 		stream->read((char*) &this->bone_weight3, sizeof(float));
163 		stream->read((char*) &this->bone_weight4, sizeof(float));
164 	}
165 
Read(std::istream * stream,PmxSetting * setting)166 	void PmxVertexSkinningSDEF::Read(std::istream *stream, PmxSetting *setting)
167 	{
168 		this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
169 		this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
170 		stream->read((char*) &this->bone_weight, sizeof(float));
171 		stream->read((char*) this->sdef_c, sizeof(float) * 3);
172 		stream->read((char*) this->sdef_r0, sizeof(float) * 3);
173 		stream->read((char*) this->sdef_r1, sizeof(float) * 3);
174 	}
175 
Read(std::istream * stream,PmxSetting * setting)176 	void PmxVertexSkinningQDEF::Read(std::istream *stream, PmxSetting *setting)
177 	{
178 		this->bone_index1 = ReadIndex(stream, setting->bone_index_size);
179 		this->bone_index2 = ReadIndex(stream, setting->bone_index_size);
180 		this->bone_index3 = ReadIndex(stream, setting->bone_index_size);
181 		this->bone_index4 = ReadIndex(stream, setting->bone_index_size);
182 		stream->read((char*) &this->bone_weight1, sizeof(float));
183 		stream->read((char*) &this->bone_weight2, sizeof(float));
184 		stream->read((char*) &this->bone_weight3, sizeof(float));
185 		stream->read((char*) &this->bone_weight4, sizeof(float));
186 	}
187 
Read(std::istream * stream,PmxSetting * setting)188 	void PmxVertex::Read(std::istream *stream, PmxSetting *setting)
189 	{
190 		stream->read((char*) this->position, sizeof(float) * 3);
191 		stream->read((char*) this->normal, sizeof(float) * 3);
192 		stream->read((char*) this->uv, sizeof(float) * 2);
193 		for (int i = 0; i < setting->uv; ++i)
194 		{
195 			stream->read((char*) this->uva[i], sizeof(float) * 4);
196 		}
197 		stream->read((char*) &this->skinning_type, sizeof(PmxVertexSkinningType));
198 		switch (this->skinning_type)
199 		{
200 		case PmxVertexSkinningType::BDEF1:
201 			this->skinning = mmd::make_unique<PmxVertexSkinningBDEF1>();
202 			break;
203 		case PmxVertexSkinningType::BDEF2:
204 			this->skinning = mmd::make_unique<PmxVertexSkinningBDEF2>();
205 			break;
206 		case PmxVertexSkinningType::BDEF4:
207 			this->skinning = mmd::make_unique<PmxVertexSkinningBDEF4>();
208 			break;
209 		case PmxVertexSkinningType::SDEF:
210 			this->skinning = mmd::make_unique<PmxVertexSkinningSDEF>();
211 			break;
212 		case PmxVertexSkinningType::QDEF:
213 			this->skinning = mmd::make_unique<PmxVertexSkinningQDEF>();
214 			break;
215 		default:
216 			throw "invalid skinning type";
217 		}
218 		this->skinning->Read(stream, setting);
219 		stream->read((char*) &this->edge, sizeof(float));
220 	}
221 
Read(std::istream * stream,PmxSetting * setting)222 	void PmxMaterial::Read(std::istream *stream, PmxSetting *setting)
223 	{
224 		this->material_name = ReadString(stream, setting->encoding);
225 		this->material_english_name = ReadString(stream, setting->encoding);
226 		stream->read((char*) this->diffuse, sizeof(float) * 4);
227 		stream->read((char*) this->specular, sizeof(float) * 3);
228 		stream->read((char*) &this->specularlity, sizeof(float));
229 		stream->read((char*) this->ambient, sizeof(float) * 3);
230 		stream->read((char*) &this->flag, sizeof(uint8_t));
231 		stream->read((char*) this->edge_color, sizeof(float) * 4);
232 		stream->read((char*) &this->edge_size, sizeof(float));
233 		this->diffuse_texture_index = ReadIndex(stream, setting->texture_index_size);
234 		this->sphere_texture_index = ReadIndex(stream, setting->texture_index_size);
235 		stream->read((char*) &this->sphere_op_mode, sizeof(uint8_t));
236 		stream->read((char*) &this->common_toon_flag, sizeof(uint8_t));
237 		if (this->common_toon_flag)
238 		{
239 			stream->read((char*) &this->toon_texture_index, sizeof(uint8_t));
240 		}
241 		else {
242 			this->toon_texture_index = ReadIndex(stream, setting->texture_index_size);
243 		}
244 		this->memo = ReadString(stream, setting->encoding);
245 		stream->read((char*) &this->index_count, sizeof(int));
246 	}
247 
Read(std::istream * stream,PmxSetting * setting)248 	void PmxIkLink::Read(std::istream *stream, PmxSetting *setting)
249 	{
250 		this->link_target = ReadIndex(stream, setting->bone_index_size);
251 		stream->read((char*) &this->angle_lock, sizeof(uint8_t));
252 		if (angle_lock == 1)
253 		{
254 			stream->read((char*) this->max_radian, sizeof(float) * 3);
255 			stream->read((char*) this->min_radian, sizeof(float) * 3);
256 		}
257 	}
258 
Read(std::istream * stream,PmxSetting * setting)259 	void PmxBone::Read(std::istream *stream, PmxSetting *setting)
260 	{
261 		this->bone_name = ReadString(stream, setting->encoding);
262 		this->bone_english_name = ReadString(stream, setting->encoding);
263 		stream->read((char*) this->position, sizeof(float) * 3);
264 		this->parent_index = ReadIndex(stream, setting->bone_index_size);
265 		stream->read((char*) &this->level, sizeof(int));
266 		stream->read((char*) &this->bone_flag, sizeof(uint16_t));
267 		if (this->bone_flag & 0x0001) {
268 			this->target_index = ReadIndex(stream, setting->bone_index_size);
269 		}
270 		else {
271 			stream->read((char*)this->offset, sizeof(float) * 3);
272 		}
273 		if (this->bone_flag & (0x0100 | 0x0200)) {
274 			this->grant_parent_index = ReadIndex(stream, setting->bone_index_size);
275 			stream->read((char*) &this->grant_weight, sizeof(float));
276 		}
277 		if (this->bone_flag & 0x0400) {
278 			stream->read((char*)this->lock_axis_orientation, sizeof(float) * 3);
279 		}
280 		if (this->bone_flag & 0x0800) {
281 			stream->read((char*)this->local_axis_x_orientation, sizeof(float) * 3);
282 			stream->read((char*)this->local_axis_y_orientation, sizeof(float) * 3);
283 		}
284 		if (this->bone_flag & 0x2000) {
285 			stream->read((char*) &this->key, sizeof(int));
286 		}
287 		if (this->bone_flag & 0x0020) {
288 			this->ik_target_bone_index = ReadIndex(stream, setting->bone_index_size);
289 			stream->read((char*) &ik_loop, sizeof(int));
290 			stream->read((char*) &ik_loop_angle_limit, sizeof(float));
291 			stream->read((char*) &ik_link_count, sizeof(int));
292 			this->ik_links = mmd::make_unique<PmxIkLink []>(ik_link_count);
293 			for (int i = 0; i < ik_link_count; i++) {
294 				ik_links[i].Read(stream, setting);
295 			}
296 		}
297 	}
298 
Read(std::istream * stream,PmxSetting * setting)299 	void PmxMorphVertexOffset::Read(std::istream *stream, PmxSetting *setting)
300 	{
301 		this->vertex_index = ReadIndex(stream, setting->vertex_index_size);
302 		stream->read((char*)this->position_offset, sizeof(float) * 3);
303 	}
304 
Read(std::istream * stream,PmxSetting * setting)305 	void PmxMorphUVOffset::Read(std::istream *stream, PmxSetting *setting)
306 	{
307 		this->vertex_index = ReadIndex(stream, setting->vertex_index_size);
308 		stream->read((char*)this->uv_offset, sizeof(float) * 4);
309 	}
310 
Read(std::istream * stream,PmxSetting * setting)311 	void PmxMorphBoneOffset::Read(std::istream *stream, PmxSetting *setting)
312 	{
313 		this->bone_index = ReadIndex(stream, setting->bone_index_size);
314 		stream->read((char*)this->translation, sizeof(float) * 3);
315 		stream->read((char*)this->rotation, sizeof(float) * 4);
316 	}
317 
Read(std::istream * stream,PmxSetting * setting)318 	void PmxMorphMaterialOffset::Read(std::istream *stream, PmxSetting *setting)
319 	{
320 		this->material_index = ReadIndex(stream, setting->material_index_size);
321 		stream->read((char*) &this->offset_operation, sizeof(uint8_t));
322 		stream->read((char*)this->diffuse, sizeof(float) * 4);
323 		stream->read((char*)this->specular, sizeof(float) * 3);
324 		stream->read((char*) &this->specularity, sizeof(float));
325 		stream->read((char*)this->ambient, sizeof(float) * 3);
326 		stream->read((char*)this->edge_color, sizeof(float) * 4);
327 		stream->read((char*) &this->edge_size, sizeof(float));
328 		stream->read((char*)this->texture_argb, sizeof(float) * 4);
329 		stream->read((char*)this->sphere_texture_argb, sizeof(float) * 4);
330 		stream->read((char*)this->toon_texture_argb, sizeof(float) * 4);
331 	}
332 
Read(std::istream * stream,PmxSetting * setting)333 	void PmxMorphGroupOffset::Read(std::istream *stream, PmxSetting *setting)
334 	{
335 		this->morph_index = ReadIndex(stream, setting->morph_index_size);
336 		stream->read((char*) &this->morph_weight, sizeof(float));
337 	}
338 
Read(std::istream * stream,PmxSetting * setting)339 	void PmxMorphFlipOffset::Read(std::istream *stream, PmxSetting *setting)
340 	{
341 		this->morph_index = ReadIndex(stream, setting->morph_index_size);
342 		stream->read((char*) &this->morph_value, sizeof(float));
343 	}
344 
Read(std::istream * stream,PmxSetting * setting)345 	void PmxMorphImplusOffset::Read(std::istream *stream, PmxSetting *setting)
346 	{
347 		this->rigid_body_index = ReadIndex(stream, setting->rigidbody_index_size);
348 		stream->read((char*) &this->is_local, sizeof(uint8_t));
349 		stream->read((char*)this->velocity, sizeof(float) * 3);
350 		stream->read((char*)this->angular_torque, sizeof(float) * 3);
351 	}
352 
Read(std::istream * stream,PmxSetting * setting)353 	void PmxMorph::Read(std::istream *stream, PmxSetting *setting)
354 	{
355 		this->morph_name = ReadString(stream, setting->encoding);
356 		this->morph_english_name = ReadString(stream, setting->encoding);
357 		stream->read((char*) &category, sizeof(MorphCategory));
358 		stream->read((char*) &morph_type, sizeof(MorphType));
359 		stream->read((char*) &this->offset_count, sizeof(int));
360 		switch (this->morph_type)
361 		{
362 		case MorphType::Group:
363 			group_offsets = mmd::make_unique<PmxMorphGroupOffset []>(this->offset_count);
364 			for (int i = 0; i < offset_count; i++)
365 			{
366 				group_offsets[i].Read(stream, setting);
367 			}
368 			break;
369 		case MorphType::Vertex:
370 			vertex_offsets = mmd::make_unique<PmxMorphVertexOffset []>(this->offset_count);
371 			for (int i = 0; i < offset_count; i++)
372 			{
373 				vertex_offsets[i].Read(stream, setting);
374 			}
375 			break;
376 		case MorphType::Bone:
377 			bone_offsets = mmd::make_unique<PmxMorphBoneOffset []>(this->offset_count);
378 			for (int i = 0; i < offset_count; i++)
379 			{
380 				bone_offsets[i].Read(stream, setting);
381 			}
382 			break;
383 		case MorphType::Matrial:
384 			material_offsets = mmd::make_unique<PmxMorphMaterialOffset []>(this->offset_count);
385 			for (int i = 0; i < offset_count; i++)
386 			{
387 				material_offsets[i].Read(stream, setting);
388 			}
389 			break;
390 		case MorphType::UV:
391 		case MorphType::AdditionalUV1:
392 		case MorphType::AdditionalUV2:
393 		case MorphType::AdditionalUV3:
394 		case MorphType::AdditionalUV4:
395 			uv_offsets = mmd::make_unique<PmxMorphUVOffset []>(this->offset_count);
396 			for (int i = 0; i < offset_count; i++)
397 			{
398 				uv_offsets[i].Read(stream, setting);
399 			}
400 			break;
401 		default:
402             throw DeadlyImportError("MMD: unknown morth type");
403 		}
404 	}
405 
Read(std::istream * stream,PmxSetting * setting)406 	void PmxFrameElement::Read(std::istream *stream, PmxSetting *setting)
407 	{
408 		stream->read((char*) &this->element_target, sizeof(uint8_t));
409 		if (this->element_target == 0x00)
410 		{
411 			this->index = ReadIndex(stream, setting->bone_index_size);
412 		}
413 		else {
414 			this->index = ReadIndex(stream, setting->morph_index_size);
415 		}
416 	}
417 
Read(std::istream * stream,PmxSetting * setting)418 	void PmxFrame::Read(std::istream *stream, PmxSetting *setting)
419 	{
420 		this->frame_name = ReadString(stream, setting->encoding);
421 		this->frame_english_name = ReadString(stream, setting->encoding);
422 		stream->read((char*) &this->frame_flag, sizeof(uint8_t));
423 		stream->read((char*) &this->element_count, sizeof(int));
424 		this->elements = mmd::make_unique<PmxFrameElement []>(this->element_count);
425 		for (int i = 0; i < this->element_count; i++)
426 		{
427 			this->elements[i].Read(stream, setting);
428 		}
429 	}
430 
Read(std::istream * stream,PmxSetting * setting)431 	void PmxRigidBody::Read(std::istream *stream, PmxSetting *setting)
432 	{
433 		this->girid_body_name = ReadString(stream, setting->encoding);
434 		this->girid_body_english_name = ReadString(stream, setting->encoding);
435 		this->target_bone = ReadIndex(stream, setting->bone_index_size);
436 		stream->read((char*) &this->group, sizeof(uint8_t));
437 		stream->read((char*) &this->mask, sizeof(uint16_t));
438 		stream->read((char*) &this->shape, sizeof(uint8_t));
439 		stream->read((char*) this->size, sizeof(float) * 3);
440 		stream->read((char*) this->position, sizeof(float) * 3);
441 		stream->read((char*) this->orientation, sizeof(float) * 3);
442 		stream->read((char*) &this->mass, sizeof(float));
443 		stream->read((char*) &this->move_attenuation, sizeof(float));
444 		stream->read((char*) &this->rotation_attenuation, sizeof(float));
445 		stream->read((char*) &this->repulsion, sizeof(float));
446 		stream->read((char*) &this->friction, sizeof(float));
447 		stream->read((char*) &this->physics_calc_type, sizeof(uint8_t));
448 	}
449 
Read(std::istream * stream,PmxSetting * setting)450 	void PmxJointParam::Read(std::istream *stream, PmxSetting *setting)
451 	{
452 		this->rigid_body1 = ReadIndex(stream, setting->rigidbody_index_size);
453 		this->rigid_body2 = ReadIndex(stream, setting->rigidbody_index_size);
454 		stream->read((char*) this->position, sizeof(float) * 3);
455 		stream->read((char*) this->orientaiton, sizeof(float) * 3);
456 		stream->read((char*) this->move_limitation_min, sizeof(float) * 3);
457 		stream->read((char*) this->move_limitation_max, sizeof(float) * 3);
458 		stream->read((char*) this->rotation_limitation_min, sizeof(float) * 3);
459 		stream->read((char*) this->rotation_limitation_max, sizeof(float) * 3);
460 		stream->read((char*) this->spring_move_coefficient, sizeof(float) * 3);
461 		stream->read((char*) this->spring_rotation_coefficient, sizeof(float) * 3);
462 	}
463 
Read(std::istream * stream,PmxSetting * setting)464 	void PmxJoint::Read(std::istream *stream, PmxSetting *setting)
465 	{
466 		this->joint_name = ReadString(stream, setting->encoding);
467 		this->joint_english_name = ReadString(stream, setting->encoding);
468 		stream->read((char*) &this->joint_type, sizeof(uint8_t));
469 		this->param.Read(stream, setting);
470 	}
471 
Read(std::istream * stream,PmxSetting * setting)472 	void PmxAncherRigidBody::Read(std::istream *stream, PmxSetting *setting)
473 	{
474 		this->related_rigid_body = ReadIndex(stream, setting->rigidbody_index_size);
475 		this->related_vertex = ReadIndex(stream, setting->vertex_index_size);
476 		stream->read((char*) &this->is_near, sizeof(uint8_t));
477 	}
478 
Read(std::istream *,PmxSetting *)479     void PmxSoftBody::Read(std::istream * /*stream*/, PmxSetting * /*setting*/)
480 	{
481 		std::cerr << "Not Implemented Exception" << std::endl;
482         throw DeadlyImportError("MMD: Not Implemented Exception");
483     }
484 
Init()485 	void PmxModel::Init()
486 	{
487 		this->version = 0.0f;
488 		this->model_name.clear();
489 		this->model_english_name.clear();
490 		this->model_comment.clear();
491 		this->model_english_comment.clear();
492 		this->vertex_count = 0;
493 		this->vertices = nullptr;
494 		this->index_count = 0;
495 		this->indices = nullptr;
496 		this->texture_count = 0;
497 		this->textures = nullptr;
498 		this->material_count = 0;
499 		this->materials = nullptr;
500 		this->bone_count = 0;
501 		this->bones = nullptr;
502 		this->morph_count = 0;
503 		this->morphs = nullptr;
504 		this->frame_count = 0;
505 		this->frames = nullptr;
506 		this->rigid_body_count = 0;
507 		this->rigid_bodies = nullptr;
508 		this->joint_count = 0;
509 		this->joints = nullptr;
510 		this->soft_body_count = 0;
511 		this->soft_bodies = nullptr;
512 	}
513 
Read(std::istream * stream)514 	void PmxModel::Read(std::istream *stream)
515 	{
516 		char magic[4];
517 		stream->read((char*) magic, sizeof(char) * 4);
518 		if (magic[0] != 0x50 || magic[1] != 0x4d || magic[2] != 0x58 || magic[3] != 0x20)
519 		{
520 			std::cerr << "invalid magic number." << std::endl;
521       throw DeadlyImportError("MMD: invalid magic number.");
522     }
523 		stream->read((char*) &version, sizeof(float));
524 		if (version != 2.0f && version != 2.1f)
525 		{
526 			std::cerr << "this is not ver2.0 or ver2.1 but " << version << "." << std::endl;
527             throw DeadlyImportError("MMD: this is not ver2.0 or ver2.1 but " + to_string(version));
528     }
529 		this->setting.Read(stream);
530 
531 		this->model_name = ReadString(stream, setting.encoding);
532 		this->model_english_name = ReadString(stream, setting.encoding);
533 		this->model_comment = ReadString(stream, setting.encoding);
534 		this->model_english_comment = ReadString(stream, setting.encoding);
535 
536 		// read vertices
537 		stream->read((char*) &vertex_count, sizeof(int));
538 		this->vertices = mmd::make_unique<PmxVertex []>(vertex_count);
539 		for (int i = 0; i < vertex_count; i++)
540 		{
541 			vertices[i].Read(stream, &setting);
542 		}
543 
544 		// read indices
545 		stream->read((char*) &index_count, sizeof(int));
546 		this->indices = mmd::make_unique<int []>(index_count);
547 		for (int i = 0; i < index_count; i++)
548 		{
549 			this->indices[i] = ReadIndex(stream, setting.vertex_index_size);
550 		}
551 
552 		// read texture names
553 		stream->read((char*) &texture_count, sizeof(int));
554 		this->textures = mmd::make_unique<std::string []>(texture_count);
555 		for (int i = 0; i < texture_count; i++)
556 		{
557 			this->textures[i] = ReadString(stream, setting.encoding);
558 		}
559 
560 		// read materials
561 		stream->read((char*) &material_count, sizeof(int));
562 		this->materials = mmd::make_unique<PmxMaterial []>(material_count);
563 		for (int i = 0; i < material_count; i++)
564 		{
565 			this->materials[i].Read(stream, &setting);
566 		}
567 
568 		// read bones
569 		stream->read((char*) &this->bone_count, sizeof(int));
570 		this->bones = mmd::make_unique<PmxBone []>(this->bone_count);
571 		for (int i = 0; i < this->bone_count; i++)
572 		{
573 			this->bones[i].Read(stream, &setting);
574 		}
575 
576 		// read morphs
577 		stream->read((char*) &this->morph_count, sizeof(int));
578 		this->morphs = mmd::make_unique<PmxMorph []>(this->morph_count);
579 		for (int i = 0; i < this->morph_count; i++)
580 		{
581 			this->morphs[i].Read(stream, &setting);
582 		}
583 
584 		// read display frames
585 		stream->read((char*) &this->frame_count, sizeof(int));
586 		this->frames = mmd::make_unique<PmxFrame []>(this->frame_count);
587 		for (int i = 0; i < this->frame_count; i++)
588 		{
589 			this->frames[i].Read(stream, &setting);
590 		}
591 
592 		// read rigid bodies
593 		stream->read((char*) &this->rigid_body_count, sizeof(int));
594 		this->rigid_bodies = mmd::make_unique<PmxRigidBody []>(this->rigid_body_count);
595 		for (int i = 0; i < this->rigid_body_count; i++)
596 		{
597 			this->rigid_bodies[i].Read(stream, &setting);
598 		}
599 
600 		// read joints
601 		stream->read((char*) &this->joint_count, sizeof(int));
602 		this->joints = mmd::make_unique<PmxJoint []>(this->joint_count);
603 		for (int i = 0; i < this->joint_count; i++)
604 		{
605 			this->joints[i].Read(stream, &setting);
606 		}
607 	}
608 }
609