-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpmx_model.h
353 lines (322 loc) · 5.6 KB
/
pmx_model.h
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
#ifndef __PMX_MODEL_H
#define __PMX_MODEL_H
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#define MAX(x, y) ((x) > (y) ? (x) : (y))
#define MIN(x, y) ((x) < (y) ? (x) : (y))
#ifdef NDEBUG
#define TRACE(...) fprintf(stderr, __VA_ARGS__)
#else
#define TRACE(...)
#endif
#define MAX_TEXT_LEN 64
#define ERROR_MSG_LEN 128
#define TOON_TEX 0
#define TOON_BUILTIN 1
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
typedef struct __attribute__((packed))
{
char sig[4];
float ver;
uint8_t data_count;
uint8_t text_enc;
uint8_t uv_count;
uint8_t vert_idx_size;
uint8_t tex_idx_size;
uint8_t mat_idx_size;
uint8_t bone_idx_size;
uint8_t morph_idx_size;
uint8_t rb_idx_size;
} PMXHeader;
typedef struct
{
uint32_t len;
char text[MAX_TEXT_LEN];
} PMXText;
typedef struct
{
PMXText name_jp;
PMXText name_en;
PMXText comm_jp;
PMXText comm_en;
} PMXInfo;
typedef enum
{
BDEF1 = 0,
BDEF2 = 1,
BDEF4 = 2,
SDEF = 3
} PMXWeightType;
typedef struct
{
float pos[3];
float normal[3];
float uv[2];
uint8_t weight_type;
union {
struct {
uint32_t idx0;
} bdef1;
struct {
uint32_t idx0;
uint32_t idx1;
float w0;
} bdef2;
struct {
uint32_t idx[4];
uint32_t w[4];
} bdef4;
struct {
uint32_t idx0;
uint32_t idx1;
float w0;
float c[3];
float r0[3];
float r1[3];
} sdef;
} weight;
float edge_scale;
} PMXVert;
typedef struct
{
uint32_t indices[3];
} PMXFace;
typedef struct
{
PMXText name;
} PMXTex;
typedef struct
{
PMXText name_jp;
PMXText name_en;
float diffuse[4];
float specular[3];
float power;
float ambient[3];
uint8_t draw_mode;
float edge[4];
float edge_size;
uint32_t tex_idx;
uint32_t env_idx;
uint8_t env_mode;
uint8_t toon_mode;
uint32_t toon_idx;
PMXText memo;
uint32_t face_count;
} PMXMat;
typedef struct
{
uint32_t idx;
uint8_t has_limit;
struct {
float lower[3];
float upper[3];
} limit;
} PMXIKLink;
#define BONE_FLAG_CONNECTED (1U << 0)
#define BONE_FLAG_ROTATABLE (1U << 1)
#define BONE_FLAG_MOVEABLE (1U << 2)
#define BONE_FLAG_DISPLAY (1U << 3)
#define BONE_FLAG_OPERATABLE (1U << 4)
#define BONE_FLAG_IK (1U << 5)
#define BONE_FLAG_LINK_DEFORM (1U << 7)
#define BONE_FLAG_LINK_ROTATION (1U << 8)
#define BONE_FLAG_LINK_MOVE (1U << 9)
#define BONE_FLAG_FIXED_AXIS (1U << 10)
#define BONE_FLAG_LOCAL_AXIS (1U << 11)
#define BONE_FLAG_PHYSICAL_TRANSFORM (1U << 12)
#define BONE_FLAG_EXT_PARENT_TRANSFORM (1U << 13)
typedef struct
{
PMXText name_jp;
PMXText name_en;
float pos[3];
uint32_t parent;
uint32_t transform_layer;
uint16_t flags;
union {
uint32_t target;
float offset[3];
} tip;
struct {
uint32_t idx;
float rate;
} link;
float fixed_axis[3];
struct {
float x[3];
float z[3];
} local_axis;
uint32_t ext_parent_key;
struct {
uint32_t idx;
uint32_t loop;
float limit_angle;
uint32_t link_count;
PMXIKLink *links;
} ik;
} PMXBone;
typedef enum
{
MORPH_TYPE_GROUP = 0,
MORPH_TYPE_VERTEX,
MORPH_TYPE_BONE,
MORPH_TYPE_UV,
MORPH_TYPE_ADD_UV_1,
MORPH_TYPE_ADD_UV_2,
MORPH_TYPE_ADD_UV_3,
MORPH_TYPE_ADD_UV_4,
MORPH_TYPE_MATERIAL,
MORPH_TYPE_FLIP,
MORPH_TYPE_IMPULSE
} PMXMorphType;
typedef union {
struct {
uint32_t idx;
float rate;
} group_flip;
struct {
uint32_t idx;
float offset[3];
} vertex;
struct {
uint32_t idx;
float move[3];
float rotation[4];
} bone;
struct {
uint32_t idx;
float offset[4];
} uv;
struct {
uint32_t idx;
uint8_t method;
float diffuse[4];
float specular[3];
float power;
float ambient[3];
float edge[4];
float edge_size;
float tex_tint[4];
float env_tint[4];
float toon_tint[4];
} material;
struct {
uint32_t idx;
uint8_t local;
float velocity[3];
float torque[3];
} impulse;
} PMXMorphOffset;
typedef struct
{
PMXText name_jp;
PMXText name_en;
uint8_t panel;
uint8_t type;
uint32_t offset_count;
PMXMorphOffset *offsets;
} PMXMorph;
typedef enum
{
FRAME_ELEM_TYPE_BONE = 0,
FRAME_ELEM_TYPE_MORPH
} PMXFrameElemType;
typedef struct
{
uint8_t type;
uint32_t idx;
} PMXFrameElement;
typedef struct
{
PMXText name_jp;
PMXText name_en;
uint8_t special;
uint32_t elem_count;
PMXFrameElement *elems;
} PMXFrame;
typedef enum
{
RIGIDBODY_SHAPE_SPHERE = 0,
RIGIDBODY_SHAPE_BOX,
RIGIDBODY_SHAPE_CAPSULE
} PMXRigidBodyShape;
typedef enum
{
RIGIDBODY_TYPE_STATIC = 0,
RIGIDBODY_TYPE_DYNAMIC,
RIGIDBODY_TYPE_COMBINE
} PMXRigidBodyType;
typedef struct
{
PMXText name_jp;
PMXText name_en;
uint32_t bone_idx;
uint8_t group;
uint16_t no_collide_group;
uint8_t shape;
float shape_size[3];
float pos[3];
float rot[3];
float mass;
float move_decay;
float rot_decay;
float elastic;
float friction;
uint8_t type;
} PMXRigidBody;
typedef struct
{
PMXText name_jp;
PMXText name_en;
uint8_t type;
uint32_t idx1;
uint32_t idx2;
float pos[3];
float rot[3];
struct {
float lower[3];
float upper[3];
} pos_limit;
struct {
float lower[3];
float upper[3];
} rot_limit;
float spring_pos[3];
float spring_rot[3];
} PMXJoint;
typedef struct
{
PMXHeader header;
PMXInfo info;
uint32_t vertex_count;
PMXVert *vertices;
uint32_t face_count;
PMXFace *faces;
uint32_t texture_count;
PMXTex *textures;
uint32_t material_count;
PMXMat *materials;
uint32_t bone_count;
PMXBone *bones;
uint32_t morph_count;
PMXMorph *morphs;
uint32_t frame_count;
PMXFrame *frames;
uint32_t rigidbody_count;
PMXRigidBody *rigidbodies;
uint32_t joint_count;
PMXJoint *joints;
} PMXModel;
int pmx_parse(const char *src, PMXModel *dst);
void pmx_free(PMXModel *model);
const char *pmx_get_error_msg(void);
#ifdef __cplusplus
}
#endif // __cplusplus
#endif // __PMX_MODEL_H