2019-10-29 17:42:10 +00:00
|
|
|
/*
|
|
|
|
* m3d.h
|
|
|
|
*
|
|
|
|
* Copyright (C) 2019 bzt (bztsrc@gitlab)
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person
|
|
|
|
* obtaining a copy of this software and associated documentation
|
|
|
|
* files (the "Software"), to deal in the Software without
|
|
|
|
* restriction, including without limitation the rights to use, copy,
|
|
|
|
* modify, merge, publish, distribute, sublicense, and/or sell copies
|
|
|
|
* of the Software, and to permit persons to whom the Software is
|
|
|
|
* furnished to do so, subject to the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
|
|
|
* HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
|
|
|
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
|
|
|
* DEALINGS IN THE SOFTWARE.
|
|
|
|
*
|
|
|
|
* @brief ANSI C89 / C++11 single header importer / exporter SDK for the Model 3D (.M3D) format
|
|
|
|
* https://gitlab.com/bztsrc/model3d
|
|
|
|
*
|
|
|
|
* PNG decompressor included from (with minor modifications to make it C89 valid):
|
|
|
|
* stb_image - v2.13 - public domain image loader - http://nothings.org/stb_image.h
|
|
|
|
*
|
|
|
|
* @version: 1.0.0
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _M3D_H_
|
|
|
|
#define _M3D_H_
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#ifdef __cplusplus
|
2019-10-29 17:42:10 +00:00
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
/*** configuration ***/
|
|
|
|
#ifndef M3D_MALLOC
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_MALLOC(sz) malloc(sz)
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
#ifndef M3D_REALLOC
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_REALLOC(p, nsz) realloc(p, nsz)
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
#ifndef M3D_FREE
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_FREE(p) free(p)
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
#ifndef M3D_LOG
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_LOG(x)
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
#ifndef M3D_APIVERSION
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_APIVERSION 0x0100
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifndef M3D_DOUBLE
|
|
|
|
typedef float M3D_FLOAT;
|
2019-11-18 02:04:52 +00:00
|
|
|
#ifndef M3D_EPSILON
|
|
|
|
/* carefully choosen for IEEE 754 don't change */
|
|
|
|
#define M3D_EPSILON ((M3D_FLOAT)1e-7)
|
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
#else
|
|
|
|
typedef double M3D_FLOAT;
|
2019-11-18 02:04:52 +00:00
|
|
|
#ifndef M3D_EPSILON
|
|
|
|
#define M3D_EPSILON ((M3D_FLOAT)1e-14)
|
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
#if !defined(M3D_SMALLINDEX)
|
|
|
|
typedef uint32_t M3D_INDEX;
|
2019-12-10 05:30:41 +00:00
|
|
|
#define M3D_UNDEF 0xffffffff
|
2019-10-29 17:42:10 +00:00
|
|
|
#define M3D_INDEXMAX 0xfffffffe
|
|
|
|
#else
|
|
|
|
typedef uint16_t M3D_INDEX;
|
2019-12-10 05:30:41 +00:00
|
|
|
#define M3D_UNDEF 0xffff
|
2019-10-29 17:42:10 +00:00
|
|
|
#define M3D_INDEXMAX 0xfffe
|
|
|
|
#endif
|
2019-12-10 05:30:41 +00:00
|
|
|
#define M3D_NOTDEFINED 0xffffffff
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifndef M3D_NUMBONE
|
|
|
|
#define M3D_NUMBONE 4
|
|
|
|
#endif
|
|
|
|
#ifndef M3D_BONEMAXLEVEL
|
|
|
|
#define M3D_BONEMAXLEVEL 8
|
|
|
|
#endif
|
2020-07-20 15:04:11 +00:00
|
|
|
#if !defined(_MSC_VER) || defined(__clang__)
|
2020-07-07 15:35:03 +00:00
|
|
|
#ifndef _inline
|
2019-10-29 23:55:54 +00:00
|
|
|
#define _inline __inline__
|
2020-07-07 15:35:03 +00:00
|
|
|
#endif
|
2019-10-29 23:55:54 +00:00
|
|
|
#define _pack __attribute__((packed))
|
2019-12-13 08:16:41 +00:00
|
|
|
#define _unused __attribute__((unused))
|
2019-10-29 23:55:54 +00:00
|
|
|
#else
|
|
|
|
#define _inline
|
|
|
|
#define _pack
|
2020-03-08 20:24:01 +00:00
|
|
|
#define _unused __pragma(warning(suppress : 4100))
|
2019-10-29 23:55:54 +00:00
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
#ifndef __cplusplus
|
2019-10-30 02:04:33 +00:00
|
|
|
#define _register register
|
2019-10-30 02:06:24 +00:00
|
|
|
#else
|
2019-10-30 02:04:33 +00:00
|
|
|
#define _register
|
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-07-20 15:04:11 +00:00
|
|
|
#if _MSC_VER > 1920 && !defined(__clang__)
|
2020-03-15 10:47:05 +00:00
|
|
|
# pragma warning(push)
|
2020-03-18 20:32:17 +00:00
|
|
|
# pragma warning(disable : 4100 4127 4189 4505 4244 4403 4701 4703)
|
2020-03-18 18:31:30 +00:00
|
|
|
# if (_MSC_VER > 1800 )
|
2020-03-18 20:32:17 +00:00
|
|
|
# pragma warning(disable : 5573 5744)
|
2020-03-18 18:31:30 +00:00
|
|
|
# endif
|
2020-03-15 10:47:05 +00:00
|
|
|
#endif // _WIN32
|
2020-03-01 12:15:45 +00:00
|
|
|
|
2019-10-29 17:42:10 +00:00
|
|
|
/*** File format structures ***/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* M3D file format structure
|
|
|
|
* 3DMO m3dchunk_t file header chunk, may followed by compressed data
|
|
|
|
* HEAD m3dhdr_t model header chunk
|
|
|
|
* n x m3dchunk_t more chunks follow
|
2019-11-18 02:04:52 +00:00
|
|
|
* PRVW preview chunk (optional)
|
2019-10-29 17:42:10 +00:00
|
|
|
* CMAP color map chunk (optional)
|
|
|
|
* TMAP texture map chunk (optional)
|
|
|
|
* VRTS vertex data chunk (optional if it's a material library)
|
|
|
|
* BONE bind-pose skeleton, bone hierarchy chunk (optional)
|
|
|
|
* n x m3db_t contains propably more, but at least one bone
|
2019-11-18 02:04:52 +00:00
|
|
|
* n x m3ds_t skin group records
|
2019-10-29 17:42:10 +00:00
|
|
|
* MTRL* material chunk(s), can be more (optional)
|
|
|
|
* n x m3dp_t each material contains propapbly more, but at least one property
|
|
|
|
* the properties are configurable with a static array, see m3d_propertytypes
|
|
|
|
* n x m3dchunk_t at least one, but maybe more face chunks
|
|
|
|
* PROC* procedural face, or
|
2019-11-18 02:04:52 +00:00
|
|
|
* MESH* triangle mesh (vertex index list) or
|
|
|
|
* SHPE* mathematical shapes like parameterized surfaces
|
|
|
|
* LBLS* annotation label chunks, can be more (optional)
|
2019-10-29 17:42:10 +00:00
|
|
|
* ACTN* action chunk(s), animation-pose skeletons, can be more (optional)
|
|
|
|
* n x m3dfr_t each action contains probably more, but at least one frame
|
|
|
|
* n x m3dtr_t each frame contains probably more, but at least one transformation
|
|
|
|
* ASET* inlined asset chunk(s), can be more (optional)
|
|
|
|
* OMD3 end chunk
|
2019-11-18 02:04:52 +00:00
|
|
|
*
|
|
|
|
* Typical chunks for a game engine: 3DMO, HEAD, CMAP, TMAP, VRTS, BONE, MTRL, MESH, ACTN, OMD3
|
|
|
|
* Typical chunks for CAD software: 3DMO, HEAD, PRVW, CMAP, TMAP, VRTS, MTRL, SHPE, LBLS, OMD3
|
2019-10-29 17:42:10 +00:00
|
|
|
*/
|
2019-11-18 02:04:52 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack(push)
|
|
|
|
#pragma pack(1)
|
|
|
|
#endif
|
|
|
|
|
2019-10-29 17:42:10 +00:00
|
|
|
typedef struct {
|
|
|
|
char magic[4];
|
|
|
|
uint32_t length;
|
|
|
|
float scale; /* deliberately not M3D_FLOAT */
|
|
|
|
uint32_t types;
|
2019-10-29 23:55:54 +00:00
|
|
|
} _pack m3dhdr_t;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
char magic[4];
|
|
|
|
uint32_t length;
|
2019-10-29 23:55:54 +00:00
|
|
|
} _pack m3dchunk_t;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2019-11-18 02:04:52 +00:00
|
|
|
#ifdef _MSC_VER
|
|
|
|
#pragma pack(pop)
|
|
|
|
#endif
|
|
|
|
|
2019-10-29 17:42:10 +00:00
|
|
|
/*** in-memory model structure ***/
|
|
|
|
|
|
|
|
/* textmap entry */
|
|
|
|
typedef struct {
|
|
|
|
M3D_FLOAT u;
|
|
|
|
M3D_FLOAT v;
|
|
|
|
} m3dti_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_textureindex_t m3dti_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* texture */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
char *name; /* texture name */
|
|
|
|
uint8_t *d; /* pixels data */
|
|
|
|
uint16_t w; /* width */
|
|
|
|
uint16_t h; /* height */
|
|
|
|
uint8_t f; /* format, 1 = grayscale, 2 = grayscale+alpha, 3 = rgb, 4 = rgba */
|
2019-11-18 02:04:52 +00:00
|
|
|
} m3dtx_t;
|
|
|
|
#define m3d_texturedata_t m3dtx_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
M3D_INDEX vertexid;
|
|
|
|
M3D_FLOAT weight;
|
|
|
|
} m3dw_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_weight_t m3dw_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* bone entry */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_INDEX parent; /* parent bone index */
|
|
|
|
char *name; /* name for this bone */
|
|
|
|
M3D_INDEX pos; /* vertex index position */
|
|
|
|
M3D_INDEX ori; /* vertex index orientation (quaternion) */
|
|
|
|
M3D_INDEX numweight; /* number of controlled vertices */
|
|
|
|
m3dw_t *weight; /* weights for those vertices */
|
|
|
|
M3D_FLOAT mat4[16]; /* transformation matrix */
|
2019-10-29 17:42:10 +00:00
|
|
|
} m3db_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_bone_t m3db_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* skin: bone per vertex entry */
|
|
|
|
typedef struct {
|
|
|
|
M3D_INDEX boneid[M3D_NUMBONE];
|
|
|
|
M3D_FLOAT weight[M3D_NUMBONE];
|
|
|
|
} m3ds_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_skin_t m3ds_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* vertex entry */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FLOAT x; /* 3D coordinates and weight */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FLOAT y;
|
|
|
|
M3D_FLOAT z;
|
|
|
|
M3D_FLOAT w;
|
2020-03-08 20:24:01 +00:00
|
|
|
uint32_t color; /* default vertex color */
|
|
|
|
M3D_INDEX skinid; /* skin index */
|
2019-11-18 02:04:52 +00:00
|
|
|
#ifdef M3D_VERTEXTYPE
|
|
|
|
uint8_t type;
|
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
} m3dv_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_vertex_t m3dv_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* material property formats */
|
|
|
|
enum {
|
|
|
|
m3dpf_color,
|
|
|
|
m3dpf_uint8,
|
|
|
|
m3dpf_uint16,
|
|
|
|
m3dpf_uint32,
|
|
|
|
m3dpf_float,
|
|
|
|
m3dpf_map
|
|
|
|
};
|
|
|
|
typedef struct {
|
|
|
|
uint8_t format;
|
|
|
|
uint8_t id;
|
|
|
|
#ifdef M3D_ASCII
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_PROPERTYDEF(f, i, n) \
|
|
|
|
{ (f), (i), (char *)(n) }
|
2019-10-29 17:42:10 +00:00
|
|
|
char *key;
|
|
|
|
#else
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_PROPERTYDEF(f, i, n) \
|
|
|
|
{ (f), (i) }
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
} m3dpd_t;
|
|
|
|
|
2019-10-29 23:55:54 +00:00
|
|
|
/* material property types */
|
2019-10-29 17:42:10 +00:00
|
|
|
/* You shouldn't change the first 8 display and first 4 physical property. Assign the rest as you like. */
|
|
|
|
enum {
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dp_Kd = 0, /* scalar display properties */
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dp_Ka,
|
|
|
|
m3dp_Ks,
|
|
|
|
m3dp_Ns,
|
|
|
|
m3dp_Ke,
|
|
|
|
m3dp_Tf,
|
|
|
|
m3dp_Km,
|
|
|
|
m3dp_d,
|
|
|
|
m3dp_il,
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dp_Pr = 64, /* scalar physical properties */
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dp_Pm,
|
|
|
|
m3dp_Ps,
|
|
|
|
m3dp_Ni,
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dp_Nt,
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dp_map_Kd = 128, /* textured display map properties */
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dp_map_Ka,
|
|
|
|
m3dp_map_Ks,
|
|
|
|
m3dp_map_Ns,
|
|
|
|
m3dp_map_Ke,
|
|
|
|
m3dp_map_Tf,
|
|
|
|
m3dp_map_Km, /* bump map */
|
|
|
|
m3dp_map_D,
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dp_map_N, /* normal map */
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dp_map_Pr = 192, /* textured physical map properties */
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dp_map_Pm,
|
|
|
|
m3dp_map_Ps,
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dp_map_Ni,
|
|
|
|
m3dp_map_Nt
|
2019-10-29 17:42:10 +00:00
|
|
|
};
|
2020-03-08 20:24:01 +00:00
|
|
|
enum { /* aliases */
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dp_bump = m3dp_map_Km,
|
2019-12-15 11:25:34 +00:00
|
|
|
m3dp_map_il = m3dp_map_N,
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dp_refl = m3dp_map_Pm
|
|
|
|
};
|
|
|
|
|
|
|
|
/* material property */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
uint8_t type; /* property type, see "m3dp_*" enumeration */
|
2019-10-29 17:42:10 +00:00
|
|
|
union {
|
2020-03-08 20:24:01 +00:00
|
|
|
uint32_t color; /* if value is a color, m3dpf_color */
|
|
|
|
uint32_t num; /* if value is a number, m3dpf_uint8, m3pf_uint16, m3dpf_uint32 */
|
|
|
|
float fnum; /* if value is a floating point number, m3dpf_float */
|
|
|
|
M3D_INDEX textureid; /* if value is a texture, m3dpf_map */
|
2019-10-29 17:42:10 +00:00
|
|
|
} value;
|
|
|
|
} m3dp_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_property_t m3dp_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* material entry */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
char *name; /* name of the material */
|
|
|
|
uint8_t numprop; /* number of properties */
|
|
|
|
m3dp_t *prop; /* properties array */
|
2019-10-29 17:42:10 +00:00
|
|
|
} m3dm_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_material_t m3dm_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* face entry */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_INDEX materialid; /* material index */
|
|
|
|
M3D_INDEX vertex[3]; /* 3D points of the triangle in CCW order */
|
|
|
|
M3D_INDEX normal[3]; /* normal vectors */
|
|
|
|
M3D_INDEX texcoord[3]; /* UV coordinates */
|
2019-10-29 17:42:10 +00:00
|
|
|
} m3df_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_face_t m3df_t
|
|
|
|
|
|
|
|
/* shape command types. must match the row in m3d_commandtypes */
|
|
|
|
enum {
|
|
|
|
/* special commands */
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dc_use = 0, /* use material */
|
|
|
|
m3dc_inc, /* include another shape */
|
|
|
|
m3dc_mesh, /* include part of polygon mesh */
|
2019-11-18 02:04:52 +00:00
|
|
|
/* approximations */
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dc_div, /* subdivision by constant resolution for both u, v */
|
|
|
|
m3dc_sub, /* subdivision by constant, different for u and v */
|
|
|
|
m3dc_len, /* spacial subdivision by maxlength */
|
|
|
|
m3dc_dist, /* subdivision by maxdistance and maxangle */
|
2019-11-18 02:04:52 +00:00
|
|
|
/* modifiers */
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dc_degu, /* degree for both u, v */
|
|
|
|
m3dc_deg, /* separate degree for u and v */
|
|
|
|
m3dc_rangeu, /* range for u */
|
|
|
|
m3dc_range, /* range for u and v */
|
|
|
|
m3dc_paru, /* u parameters (knots) */
|
|
|
|
m3dc_parv, /* v parameters */
|
|
|
|
m3dc_trim, /* outer trimming curve */
|
|
|
|
m3dc_hole, /* inner trimming curve */
|
|
|
|
m3dc_scrv, /* spacial curve */
|
|
|
|
m3dc_sp, /* special points */
|
2019-11-18 02:04:52 +00:00
|
|
|
/* helper curves */
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dc_bez1, /* Bezier 1D */
|
|
|
|
m3dc_bsp1, /* B-spline 1D */
|
|
|
|
m3dc_bez2, /* bezier 2D */
|
|
|
|
m3dc_bsp2, /* B-spline 2D */
|
2019-11-18 02:04:52 +00:00
|
|
|
/* surfaces */
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dc_bezun, /* Bezier 3D with control, UV, normal */
|
|
|
|
m3dc_bezu, /* with control and UV */
|
|
|
|
m3dc_bezn, /* with control and normal */
|
|
|
|
m3dc_bez, /* control points only */
|
|
|
|
m3dc_nurbsun, /* B-spline 3D */
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dc_nurbsu,
|
|
|
|
m3dc_nurbsn,
|
|
|
|
m3dc_nurbs,
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dc_conn, /* connect surfaces */
|
2019-11-18 02:04:52 +00:00
|
|
|
/* geometrical */
|
|
|
|
m3dc_line,
|
|
|
|
m3dc_polygon,
|
|
|
|
m3dc_circle,
|
|
|
|
m3dc_cylinder,
|
|
|
|
m3dc_shpere,
|
|
|
|
m3dc_torus,
|
2019-11-18 14:22:15 +00:00
|
|
|
m3dc_cone,
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dc_cube
|
|
|
|
};
|
|
|
|
|
|
|
|
/* shape command argument types */
|
|
|
|
enum {
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dcp_mi_t = 1, /* material index */
|
|
|
|
m3dcp_hi_t, /* shape index */
|
|
|
|
m3dcp_fi_t, /* face index */
|
|
|
|
m3dcp_ti_t, /* texture map index */
|
|
|
|
m3dcp_vi_t, /* vertex index */
|
|
|
|
m3dcp_qi_t, /* vertex index for quaternions */
|
|
|
|
m3dcp_vc_t, /* coordinate or radius, float scalar */
|
|
|
|
m3dcp_i1_t, /* int8 scalar */
|
|
|
|
m3dcp_i2_t, /* int16 scalar */
|
|
|
|
m3dcp_i4_t, /* int32 scalar */
|
|
|
|
m3dcp_va_t /* variadic arguments */
|
2019-11-18 02:04:52 +00:00
|
|
|
};
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_CMDMAXARG 8 /* if you increase this, add more arguments to the macro below */
|
2019-11-18 02:04:52 +00:00
|
|
|
typedef struct {
|
|
|
|
#ifdef M3D_ASCII
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_CMDDEF(t, n, p, a, b, c, d, e, f, g, h) \
|
|
|
|
{ \
|
|
|
|
(char *)(n), (p), { (a), (b), (c), (d), (e), (f), (g), (h) } \
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
char *key;
|
|
|
|
#else
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_CMDDEF(t, n, p, a, b, c, d, e, f, g, h) \
|
|
|
|
{ \
|
|
|
|
(p), { (a), (b), (c), (d), (e), (f), (g), (h) } \
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
#endif
|
|
|
|
uint8_t p;
|
|
|
|
uint8_t a[M3D_CMDMAXARG];
|
|
|
|
} m3dcd_t;
|
|
|
|
|
|
|
|
/* shape command */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
uint16_t type; /* shape type */
|
|
|
|
uint32_t *arg; /* arguments array */
|
2019-11-18 02:04:52 +00:00
|
|
|
} m3dc_t;
|
|
|
|
#define m3d_shapecommand_t m3dc_t
|
|
|
|
|
|
|
|
/* shape entry */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
char *name; /* name of the mathematical shape */
|
|
|
|
M3D_INDEX group; /* group this shape belongs to or -1 */
|
|
|
|
uint32_t numcmd; /* number of commands */
|
|
|
|
m3dc_t *cmd; /* commands array */
|
2019-11-18 02:04:52 +00:00
|
|
|
} m3dh_t;
|
|
|
|
#define m3d_shape_t m3dh_t
|
|
|
|
|
|
|
|
/* label entry */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
char *name; /* name of the annotation layer or NULL */
|
|
|
|
char *lang; /* language code or NULL */
|
|
|
|
char *text; /* the label text */
|
|
|
|
uint32_t color; /* color */
|
|
|
|
M3D_INDEX vertexid; /* the vertex the label refers to */
|
2019-11-18 02:04:52 +00:00
|
|
|
} m3dl_t;
|
|
|
|
#define m3d_label_t m3dl_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2019-10-29 23:55:54 +00:00
|
|
|
/* frame transformations / working copy skeleton entry */
|
2019-10-29 17:42:10 +00:00
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_INDEX boneid; /* selects a node in bone hierarchy */
|
|
|
|
M3D_INDEX pos; /* vertex index new position */
|
|
|
|
M3D_INDEX ori; /* vertex index new orientation (quaternion) */
|
2019-10-29 17:42:10 +00:00
|
|
|
} m3dtr_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_transform_t m3dtr_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* animation frame entry */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
uint32_t msec; /* frame's position on the timeline, timestamp */
|
|
|
|
M3D_INDEX numtransform; /* number of transformations in this frame */
|
|
|
|
m3dtr_t *transform; /* transformations */
|
2019-10-29 17:42:10 +00:00
|
|
|
} m3dfr_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_frame_t m3dfr_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* model action entry */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
char *name; /* name of the action */
|
|
|
|
uint32_t durationmsec; /* duration in millisec (1/1000 sec) */
|
|
|
|
M3D_INDEX numframe; /* number of frames in this animation */
|
|
|
|
m3dfr_t *frame; /* frames array */
|
2019-10-29 17:42:10 +00:00
|
|
|
} m3da_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_action_t m3da_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* inlined asset */
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
char *name; /* asset name (same pointer as in texture[].name) */
|
|
|
|
uint8_t *data; /* compressed asset data */
|
|
|
|
uint32_t length; /* compressed data length */
|
2019-10-29 23:55:54 +00:00
|
|
|
} m3di_t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#define m3d_inlinedasset_t m3di_t
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/*** in-memory model structure ***/
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_FLG_FREERAW (1 << 0)
|
|
|
|
#define M3D_FLG_FREESTR (1 << 1)
|
|
|
|
#define M3D_FLG_MTLLIB (1 << 2)
|
|
|
|
#define M3D_FLG_GENNORM (1 << 3)
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
typedef struct {
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dhdr_t *raw; /* pointer to raw data */
|
|
|
|
char flags; /* internal flags */
|
|
|
|
signed char errcode; /* returned error code */
|
|
|
|
char vc_s, vi_s, si_s, ci_s, ti_s, bi_s, nb_s, sk_s, fc_s, hi_s, fi_s; /* decoded sizes for types */
|
|
|
|
char *name; /* name of the model, like "Utah teapot" */
|
|
|
|
char *license; /* usage condition or license, like "MIT", "LGPL" or "BSD-3clause" */
|
|
|
|
char *author; /* nickname, email, homepage or github URL etc. */
|
|
|
|
char *desc; /* comments, descriptions. May contain '\n' newline character */
|
|
|
|
M3D_FLOAT scale; /* the model's bounding cube's size in SI meters */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX numcmap;
|
2020-03-08 20:24:01 +00:00
|
|
|
uint32_t *cmap; /* color map */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX numtmap;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dti_t *tmap; /* texture map indices */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX numtexture;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dtx_t *texture; /* uncompressed textures */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX numbone;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3db_t *bone; /* bone hierarchy */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX numvertex;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dv_t *vertex; /* vertex data */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX numskin;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3ds_t *skin; /* skin data */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX nummaterial;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dm_t *material; /* material list */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX numface;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3df_t *face; /* model face, polygon (triangle) mesh */
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_INDEX numshape;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dh_t *shape; /* model face, shape commands */
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_INDEX numlabel;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dl_t *label; /* annotation labels */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX numaction;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3da_t *action; /* action animations */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_INDEX numinlined;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3di_t *inlined; /* inlined assets */
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_INDEX numextra;
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dchunk_t **extra; /* unknown chunks, application / engine specific data probably */
|
|
|
|
m3di_t preview; /* preview chunk */
|
2019-10-29 17:42:10 +00:00
|
|
|
} m3d_t;
|
|
|
|
|
|
|
|
/*** export parameters ***/
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_EXP_INT8 0
|
|
|
|
#define M3D_EXP_INT16 1
|
|
|
|
#define M3D_EXP_FLOAT 2
|
|
|
|
#define M3D_EXP_DOUBLE 3
|
|
|
|
|
|
|
|
#define M3D_EXP_NOCMAP (1 << 0)
|
|
|
|
#define M3D_EXP_NOMATERIAL (1 << 1)
|
|
|
|
#define M3D_EXP_NOFACE (1 << 2)
|
|
|
|
#define M3D_EXP_NONORMAL (1 << 3)
|
|
|
|
#define M3D_EXP_NOTXTCRD (1 << 4)
|
|
|
|
#define M3D_EXP_FLIPTXTCRD (1 << 5)
|
|
|
|
#define M3D_EXP_NORECALC (1 << 6)
|
|
|
|
#define M3D_EXP_IDOSUCK (1 << 7)
|
|
|
|
#define M3D_EXP_NOBONE (1 << 8)
|
|
|
|
#define M3D_EXP_NOACTION (1 << 9)
|
|
|
|
#define M3D_EXP_INLINE (1 << 10)
|
|
|
|
#define M3D_EXP_EXTRA (1 << 11)
|
|
|
|
#define M3D_EXP_NOZLIB (1 << 14)
|
|
|
|
#define M3D_EXP_ASCII (1 << 15)
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/*** error codes ***/
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_SUCCESS 0
|
|
|
|
#define M3D_ERR_ALLOC -1
|
|
|
|
#define M3D_ERR_BADFILE -2
|
|
|
|
#define M3D_ERR_UNIMPL -65
|
|
|
|
#define M3D_ERR_UNKPROP -66
|
|
|
|
#define M3D_ERR_UNKMESH -67
|
|
|
|
#define M3D_ERR_UNKIMG -68
|
|
|
|
#define M3D_ERR_UNKFRAME -69
|
|
|
|
#define M3D_ERR_UNKCMD -70
|
|
|
|
#define M3D_ERR_TRUNC -71
|
|
|
|
#define M3D_ERR_CMAP -72
|
|
|
|
#define M3D_ERR_TMAP -73
|
|
|
|
#define M3D_ERR_VRTS -74
|
|
|
|
#define M3D_ERR_BONE -75
|
|
|
|
#define M3D_ERR_MTRL -76
|
|
|
|
#define M3D_ERR_SHPE -77
|
|
|
|
|
|
|
|
#define M3D_ERR_ISFATAL(x) ((x) < 0 && (x) > -65)
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* callbacks */
|
2020-03-08 20:24:01 +00:00
|
|
|
typedef unsigned char *(*m3dread_t)(char *filename, unsigned int *size); /* read file contents into buffer */
|
|
|
|
typedef void (*m3dfree_t)(void *buffer); /* free file contents buffer */
|
|
|
|
typedef int (*m3dtxsc_t)(const char *name, const void *script, uint32_t len, m3dtx_t *output); /* interpret texture script */
|
|
|
|
typedef int (*m3dprsc_t)(const char *name, const void *script, uint32_t len, m3d_t *model); /* interpret surface script */
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif /* ifndef M3D_APIVERSION */
|
|
|
|
|
|
|
|
/*** C prototypes ***/
|
|
|
|
/* import / export */
|
|
|
|
m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d_t *mtllib);
|
|
|
|
unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size);
|
|
|
|
void m3d_free(m3d_t *model);
|
|
|
|
/* generate animation pose skeleton */
|
|
|
|
m3dtr_t *m3d_frame(m3d_t *model, M3D_INDEX actionid, M3D_INDEX frameid, m3dtr_t *skeleton);
|
|
|
|
m3db_t *m3d_pose(m3d_t *model, M3D_INDEX actionid, uint32_t msec);
|
|
|
|
|
|
|
|
/* private prototypes used by both importer and exporter */
|
|
|
|
char *_m3d_safestr(char *in, int morelines);
|
|
|
|
|
|
|
|
/*** C implementation ***/
|
|
|
|
#ifdef M3D_IMPLEMENTATION
|
|
|
|
#if !defined(M3D_NOIMPORTER) || defined(M3D_EXPORTER)
|
2019-10-29 23:55:54 +00:00
|
|
|
/* material property definitions */
|
2019-10-30 00:14:20 +00:00
|
|
|
static m3dpd_t m3d_propertytypes[] = {
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_PROPERTYDEF(m3dpf_color, m3dp_Kd, "Kd"), /* diffuse color */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_color, m3dp_Ka, "Ka"), /* ambient color */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_color, m3dp_Ks, "Ks"), /* specular color */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_float, m3dp_Ns, "Ns"), /* specular exponent */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_color, m3dp_Ke, "Ke"), /* emissive (emitting light of this color) */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_color, m3dp_Tf, "Tf"), /* transmission color */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_float, m3dp_Km, "Km"), /* bump strength */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_float, m3dp_d, "d"), /* dissolve (transparency) */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_uint8, m3dp_il, "il"), /* illumination model (informational, ignored by PBR-shaders) */
|
|
|
|
|
|
|
|
M3D_PROPERTYDEF(m3dpf_float, m3dp_Pr, "Pr"), /* roughness */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_float, m3dp_Pm, "Pm"), /* metallic, also reflection */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_float, m3dp_Ps, "Ps"), /* sheen */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_float, m3dp_Ni, "Ni"), /* index of refraction (optical density) */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_float, m3dp_Nt, "Nt"), /* thickness of face in millimeter, for printing */
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* aliases, note that "map_*" aliases are handled automatically */
|
|
|
|
M3D_PROPERTYDEF(m3dpf_map, m3dp_map_Km, "bump"),
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_PROPERTYDEF(m3dpf_map, m3dp_map_N, "map_N"), /* as normal map has no scalar version, it's counterpart is 'il' */
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_PROPERTYDEF(m3dpf_map, m3dp_map_Pm, "refl")
|
|
|
|
};
|
2019-11-18 02:04:52 +00:00
|
|
|
/* shape command definitions. if more commands start with the same string, the longer must come first */
|
|
|
|
static m3dcd_t m3d_commandtypes[] = {
|
|
|
|
/* technical */
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_CMDDEF(m3dc_use, "use", 1, m3dcp_mi_t, 0, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_inc, "inc", 3, m3dcp_hi_t, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vi_t, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_mesh, "mesh", 1, m3dcp_fi_t, m3dcp_fi_t, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vi_t, 0, 0, 0),
|
2019-11-18 02:04:52 +00:00
|
|
|
/* approximations */
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_CMDDEF(m3dc_div, "div", 1, m3dcp_vc_t, 0, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_sub, "sub", 2, m3dcp_vc_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_len, "len", 1, m3dcp_vc_t, 0, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_dist, "dist", 2, m3dcp_vc_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
|
2019-11-18 02:04:52 +00:00
|
|
|
/* modifiers */
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_CMDDEF(m3dc_degu, "degu", 1, m3dcp_i1_t, 0, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_deg, "deg", 2, m3dcp_i1_t, m3dcp_i1_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_rangeu, "rangeu", 1, m3dcp_ti_t, 0, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_range, "range", 2, m3dcp_ti_t, m3dcp_ti_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_paru, "paru", 2, m3dcp_va_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_parv, "parv", 2, m3dcp_va_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_trim, "trim", 3, m3dcp_va_t, m3dcp_ti_t, m3dcp_i2_t, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_hole, "hole", 3, m3dcp_va_t, m3dcp_ti_t, m3dcp_i2_t, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_scrv, "scrv", 3, m3dcp_va_t, m3dcp_ti_t, m3dcp_i2_t, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_sp, "sp", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
|
2019-11-18 02:04:52 +00:00
|
|
|
/* helper curves */
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_CMDDEF(m3dc_bez1, "bez1", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_bsp1, "bsp1", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_bez2, "bez2", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_bsp2, "bsp2", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
|
2019-11-18 02:04:52 +00:00
|
|
|
/* surfaces */
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_CMDDEF(m3dc_bezun, "bezun", 4, m3dcp_va_t, m3dcp_vi_t, m3dcp_ti_t, m3dcp_vi_t, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_bezu, "bezu", 3, m3dcp_va_t, m3dcp_vi_t, m3dcp_ti_t, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_bezn, "bezn", 3, m3dcp_va_t, m3dcp_vi_t, m3dcp_vi_t, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_bez, "bez", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_CMDDEF(m3dc_nurbsun, "nurbsun", 4, m3dcp_va_t, m3dcp_vi_t, m3dcp_ti_t, m3dcp_vi_t, 0, 0, 0, 0),
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_CMDDEF(m3dc_nurbsu, "nurbsu", 3, m3dcp_va_t, m3dcp_vi_t, m3dcp_ti_t, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_nurbsn, "nurbsn", 3, m3dcp_va_t, m3dcp_vi_t, m3dcp_vi_t, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_nurbs, "nurbs", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_conn, "conn", 6, m3dcp_i2_t, m3dcp_ti_t, m3dcp_i2_t, m3dcp_i2_t, m3dcp_ti_t, m3dcp_i2_t, 0, 0),
|
2019-11-18 02:04:52 +00:00
|
|
|
/* geometrical */
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_CMDDEF(m3dc_line, "line", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_CMDDEF(m3dc_polygon, "polygon", 2, m3dcp_va_t, m3dcp_vi_t, 0, 0, 0, 0, 0, 0),
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_CMDDEF(m3dc_circle, "circle", 3, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vc_t, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_cylinder, "cylinder", 6, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vc_t, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vc_t, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_shpere, "shpere", 2, m3dcp_vi_t, m3dcp_vc_t, 0, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_torus, "torus", 4, m3dcp_vi_t, m3dcp_qi_t, m3dcp_vc_t, m3dcp_vc_t, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_cone, "cone", 3, m3dcp_vi_t, m3dcp_vi_t, m3dcp_vi_t, 0, 0, 0, 0, 0),
|
|
|
|
M3D_CMDDEF(m3dc_cube, "cube", 3, m3dcp_vi_t, m3dcp_vi_t, m3dcp_vi_t, 0, 0, 0, 0, 0)
|
2019-11-18 02:04:52 +00:00
|
|
|
};
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if !defined(M3D_NOIMPORTER) && !defined(STBI_INCLUDE_STB_IMAGE_H)
|
|
|
|
/* PNG decompressor from
|
|
|
|
|
|
|
|
stb_image - v2.23 - public domain image loader - http://nothings.org/stb_image.h
|
|
|
|
*/
|
2019-11-18 17:31:53 +00:00
|
|
|
static const char *_m3dstbi__g_failure_reason;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
enum {
|
|
|
|
STBI_default = 0,
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
STBI_grey = 1,
|
|
|
|
STBI_grey_alpha = 2,
|
|
|
|
STBI_rgb = 3,
|
|
|
|
STBI_rgb_alpha = 4
|
2019-10-29 17:42:10 +00:00
|
|
|
};
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
enum {
|
|
|
|
STBI__SCAN_load = 0,
|
|
|
|
STBI__SCAN_type,
|
|
|
|
STBI__SCAN_header
|
2019-10-29 17:42:10 +00:00
|
|
|
};
|
|
|
|
|
2019-11-18 17:31:53 +00:00
|
|
|
typedef unsigned short _m3dstbi_us;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2019-11-18 17:31:53 +00:00
|
|
|
typedef uint16_t _m3dstbi__uint16;
|
2020-03-08 20:24:01 +00:00
|
|
|
typedef int16_t _m3dstbi__int16;
|
2019-11-18 17:31:53 +00:00
|
|
|
typedef uint32_t _m3dstbi__uint32;
|
2020-03-08 20:24:01 +00:00
|
|
|
typedef int32_t _m3dstbi__int32;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
_m3dstbi__uint32 img_x, img_y;
|
|
|
|
int img_n, img_out_n;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
void *io_user_data;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
int read_from_callbacks;
|
|
|
|
int buflen;
|
|
|
|
unsigned char buffer_start[128];
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
unsigned char *img_buffer, *img_buffer_end;
|
|
|
|
unsigned char *img_buffer_original, *img_buffer_original_end;
|
2019-11-18 17:31:53 +00:00
|
|
|
} _m3dstbi__context;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
int bits_per_channel;
|
|
|
|
int num_channels;
|
|
|
|
int channel_order;
|
2019-11-18 17:31:53 +00:00
|
|
|
} _m3dstbi__result_info;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
#define STBI_ASSERT(v)
|
2020-03-08 20:24:01 +00:00
|
|
|
#define STBI_NOTUSED(v) (void)sizeof(v)
|
|
|
|
#define STBI__BYTECAST(x) ((unsigned char)((x)&255))
|
|
|
|
#define STBI_MALLOC(sz) M3D_MALLOC(sz)
|
|
|
|
#define STBI_REALLOC(p, newsz) M3D_REALLOC(p, newsz)
|
|
|
|
#define STBI_FREE(p) M3D_FREE(p)
|
|
|
|
#define STBI_REALLOC_SIZED(p, oldsz, newsz) STBI_REALLOC(p, newsz)
|
|
|
|
|
|
|
|
_inline static unsigned char _m3dstbi__get8(_m3dstbi__context *s) {
|
|
|
|
if (s->img_buffer < s->img_buffer_end)
|
|
|
|
return *s->img_buffer++;
|
|
|
|
return 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static int _m3dstbi__at_eof(_m3dstbi__context *s) {
|
|
|
|
return s->img_buffer >= s->img_buffer_end;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static void _m3dstbi__skip(_m3dstbi__context *s, int n) {
|
|
|
|
if (n < 0) {
|
|
|
|
s->img_buffer = s->img_buffer_end;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
s->img_buffer += n;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__getn(_m3dstbi__context *s, unsigned char *buffer, int n) {
|
|
|
|
if (s->img_buffer + n <= s->img_buffer_end) {
|
|
|
|
memcpy(buffer, s->img_buffer, n);
|
|
|
|
s->img_buffer += n;
|
|
|
|
return 1;
|
|
|
|
} else
|
|
|
|
return 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__get16be(_m3dstbi__context *s) {
|
|
|
|
int z = _m3dstbi__get8(s);
|
|
|
|
return (z << 8) + _m3dstbi__get8(s);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static _m3dstbi__uint32 _m3dstbi__get32be(_m3dstbi__context *s) {
|
|
|
|
_m3dstbi__uint32 z = _m3dstbi__get16be(s);
|
|
|
|
return (z << 16) + _m3dstbi__get16be(s);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#define _m3dstbi__err(x, y) _m3dstbi__errstr(y)
|
|
|
|
static int _m3dstbi__errstr(const char *str) {
|
|
|
|
_m3dstbi__g_failure_reason = str;
|
|
|
|
return 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static void *_m3dstbi__malloc(size_t size) {
|
2019-10-29 17:42:10 +00:00
|
|
|
return STBI_MALLOC(size);
|
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__addsizes_valid(int a, int b) {
|
|
|
|
if (b < 0) return 0;
|
|
|
|
return a <= 2147483647 - b;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__mul2sizes_valid(int a, int b) {
|
|
|
|
if (a < 0 || b < 0) return 0;
|
|
|
|
if (b == 0) return 1;
|
|
|
|
return a <= 2147483647 / b;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__mad2sizes_valid(int a, int b, int add) {
|
|
|
|
return _m3dstbi__mul2sizes_valid(a, b) && _m3dstbi__addsizes_valid(a * b, add);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__mad3sizes_valid(int a, int b, int c, int add) {
|
|
|
|
return _m3dstbi__mul2sizes_valid(a, b) && _m3dstbi__mul2sizes_valid(a * b, c) &&
|
|
|
|
_m3dstbi__addsizes_valid(a * b * c, add);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static void *_m3dstbi__malloc_mad2(int a, int b, int add) {
|
|
|
|
if (!_m3dstbi__mad2sizes_valid(a, b, add)) return NULL;
|
|
|
|
return _m3dstbi__malloc(a * b + add);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static void *_m3dstbi__malloc_mad3(int a, int b, int c, int add) {
|
|
|
|
if (!_m3dstbi__mad3sizes_valid(a, b, c, add)) return NULL;
|
|
|
|
return _m3dstbi__malloc(a * b * c + add);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static unsigned char _m3dstbi__compute_y(int r, int g, int b) {
|
|
|
|
return (unsigned char)(((r * 77) + (g * 150) + (29 * b)) >> 8);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static unsigned char *_m3dstbi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y) {
|
|
|
|
int i, j;
|
|
|
|
unsigned char *good;
|
|
|
|
|
|
|
|
if (req_comp == img_n) return data;
|
|
|
|
STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
|
|
|
|
|
|
|
|
good = (unsigned char *)_m3dstbi__malloc_mad3(req_comp, x, y, 0);
|
|
|
|
if (good == NULL) {
|
|
|
|
STBI_FREE(data);
|
|
|
|
_m3dstbi__err("outofmem", "Out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < (int)y; ++j) {
|
|
|
|
unsigned char *src = data + j * x * img_n;
|
|
|
|
unsigned char *dest = good + j * x * req_comp;
|
|
|
|
|
|
|
|
#define STBI__COMBO(a, b) ((a)*8 + (b))
|
|
|
|
#define STBI__CASE(a, b) \
|
|
|
|
case STBI__COMBO(a, b): \
|
|
|
|
for (i = x - 1; i >= 0; --i, src += a, dest += b)
|
|
|
|
switch (STBI__COMBO(img_n, req_comp)) {
|
|
|
|
STBI__CASE(1, 2) { dest[0] = src[0], dest[1] = 255; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(1, 3) { dest[0] = dest[1] = dest[2] = src[0]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(1, 4) { dest[0] = dest[1] = dest[2] = src[0], dest[3] = 255; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(2, 1) { dest[0] = src[0]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(2, 3) { dest[0] = dest[1] = dest[2] = src[0]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(2, 4) { dest[0] = dest[1] = dest[2] = src[0], dest[3] = src[1]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(3, 4) { dest[0] = src[0], dest[1] = src[1], dest[2] = src[2], dest[3] = 255; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(3, 1) { dest[0] = _m3dstbi__compute_y(src[0], src[1], src[2]); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(3, 2) { dest[0] = _m3dstbi__compute_y(src[0], src[1], src[2]), dest[1] = 255; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(4, 1) { dest[0] = _m3dstbi__compute_y(src[0], src[1], src[2]); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(4, 2) { dest[0] = _m3dstbi__compute_y(src[0], src[1], src[2]), dest[1] = src[3]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(4, 3) { dest[0] = src[0], dest[1] = src[1], dest[2] = src[2]; }
|
|
|
|
break;
|
|
|
|
default: STBI_ASSERT(0);
|
|
|
|
}
|
|
|
|
#undef STBI__CASE
|
|
|
|
}
|
|
|
|
|
|
|
|
STBI_FREE(data);
|
|
|
|
return good;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static _m3dstbi__uint16 _m3dstbi__compute_y_16(int r, int g, int b) {
|
|
|
|
return (_m3dstbi__uint16)(((r * 77) + (g * 150) + (29 * b)) >> 8);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static _m3dstbi__uint16 *_m3dstbi__convert_format16(_m3dstbi__uint16 *data, int img_n, int req_comp, unsigned int x, unsigned int y) {
|
|
|
|
int i, j;
|
|
|
|
_m3dstbi__uint16 *good;
|
|
|
|
|
|
|
|
if (req_comp == img_n) return data;
|
|
|
|
STBI_ASSERT(req_comp >= 1 && req_comp <= 4);
|
|
|
|
|
|
|
|
good = (_m3dstbi__uint16 *)_m3dstbi__malloc(req_comp * x * y * 2);
|
|
|
|
if (good == NULL) {
|
|
|
|
STBI_FREE(data);
|
|
|
|
_m3dstbi__err("outofmem", "Out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < (int)y; ++j) {
|
|
|
|
_m3dstbi__uint16 *src = data + j * x * img_n;
|
|
|
|
_m3dstbi__uint16 *dest = good + j * x * req_comp;
|
|
|
|
|
|
|
|
#define STBI__COMBO(a, b) ((a)*8 + (b))
|
|
|
|
#define STBI__CASE(a, b) \
|
|
|
|
case STBI__COMBO(a, b): \
|
|
|
|
for (i = x - 1; i >= 0; --i, src += a, dest += b)
|
|
|
|
switch (STBI__COMBO(img_n, req_comp)) {
|
|
|
|
STBI__CASE(1, 2) { dest[0] = src[0], dest[1] = 0xffff; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(1, 3) { dest[0] = dest[1] = dest[2] = src[0]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(1, 4) { dest[0] = dest[1] = dest[2] = src[0], dest[3] = 0xffff; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(2, 1) { dest[0] = src[0]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(2, 3) { dest[0] = dest[1] = dest[2] = src[0]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(2, 4) { dest[0] = dest[1] = dest[2] = src[0], dest[3] = src[1]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(3, 4) { dest[0] = src[0], dest[1] = src[1], dest[2] = src[2], dest[3] = 0xffff; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(3, 1) { dest[0] = _m3dstbi__compute_y_16(src[0], src[1], src[2]); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(3, 2) { dest[0] = _m3dstbi__compute_y_16(src[0], src[1], src[2]), dest[1] = 0xffff; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(4, 1) { dest[0] = _m3dstbi__compute_y_16(src[0], src[1], src[2]); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(4, 2) { dest[0] = _m3dstbi__compute_y_16(src[0], src[1], src[2]), dest[1] = src[3]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(4, 3) { dest[0] = src[0], dest[1] = src[1], dest[2] = src[2]; }
|
|
|
|
break;
|
|
|
|
default: STBI_ASSERT(0);
|
|
|
|
}
|
|
|
|
#undef STBI__CASE
|
|
|
|
}
|
|
|
|
|
|
|
|
STBI_FREE(data);
|
|
|
|
return good;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#define STBI__ZFAST_BITS 9
|
|
|
|
#define STBI__ZFAST_MASK ((1 << STBI__ZFAST_BITS) - 1)
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
_m3dstbi__uint16 fast[1 << STBI__ZFAST_BITS];
|
|
|
|
_m3dstbi__uint16 firstcode[16];
|
|
|
|
int maxcode[17];
|
|
|
|
_m3dstbi__uint16 firstsymbol[16];
|
|
|
|
unsigned char size[288];
|
|
|
|
_m3dstbi__uint16 value[288];
|
2019-11-18 17:31:53 +00:00
|
|
|
} _m3dstbi__zhuffman;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static int _m3dstbi__bitreverse16(int n) {
|
|
|
|
n = ((n & 0xAAAA) >> 1) | ((n & 0x5555) << 1);
|
|
|
|
n = ((n & 0xCCCC) >> 2) | ((n & 0x3333) << 2);
|
|
|
|
n = ((n & 0xF0F0) >> 4) | ((n & 0x0F0F) << 4);
|
|
|
|
n = ((n & 0xFF00) >> 8) | ((n & 0x00FF) << 8);
|
|
|
|
return n;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static int _m3dstbi__bit_reverse(int v, int bits) {
|
|
|
|
STBI_ASSERT(bits <= 16);
|
|
|
|
return _m3dstbi__bitreverse16(v) >> (16 - bits);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__zbuild_huffman(_m3dstbi__zhuffman *z, unsigned char *sizelist, int num) {
|
|
|
|
int i, k = 0;
|
|
|
|
int code, next_code[16], sizes[17];
|
|
|
|
|
|
|
|
memset(sizes, 0, sizeof(sizes));
|
|
|
|
memset(z->fast, 0, sizeof(z->fast));
|
|
|
|
for (i = 0; i < num; ++i)
|
|
|
|
++sizes[sizelist[i]];
|
|
|
|
sizes[0] = 0;
|
|
|
|
for (i = 1; i < 16; ++i)
|
|
|
|
if (sizes[i] > (1 << i))
|
|
|
|
return _m3dstbi__err("bad sizes", "Corrupt PNG");
|
|
|
|
code = 0;
|
|
|
|
for (i = 1; i < 16; ++i) {
|
|
|
|
next_code[i] = code;
|
|
|
|
z->firstcode[i] = (_m3dstbi__uint16)code;
|
|
|
|
z->firstsymbol[i] = (_m3dstbi__uint16)k;
|
|
|
|
code = (code + sizes[i]);
|
|
|
|
if (sizes[i])
|
|
|
|
if (code - 1 >= (1 << i)) return _m3dstbi__err("bad codelengths", "Corrupt PNG");
|
|
|
|
z->maxcode[i] = code << (16 - i);
|
|
|
|
code <<= 1;
|
|
|
|
k += sizes[i];
|
|
|
|
}
|
|
|
|
z->maxcode[16] = 0x10000;
|
|
|
|
for (i = 0; i < num; ++i) {
|
|
|
|
int s = sizelist[i];
|
|
|
|
if (s) {
|
|
|
|
int c = next_code[s] - z->firstcode[s] + z->firstsymbol[s];
|
|
|
|
_m3dstbi__uint16 fastv = (_m3dstbi__uint16)((s << 9) | i);
|
|
|
|
z->size[c] = (unsigned char)s;
|
|
|
|
z->value[c] = (_m3dstbi__uint16)i;
|
|
|
|
if (s <= STBI__ZFAST_BITS) {
|
|
|
|
int j = _m3dstbi__bit_reverse(next_code[s], s);
|
|
|
|
while (j < (1 << STBI__ZFAST_BITS)) {
|
|
|
|
z->fast[j] = fastv;
|
|
|
|
j += (1 << s);
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
++next_code[s];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
unsigned char *zbuffer, *zbuffer_end;
|
|
|
|
int num_bits;
|
|
|
|
_m3dstbi__uint32 code_buffer;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
char *zout;
|
|
|
|
char *zout_start;
|
|
|
|
char *zout_end;
|
|
|
|
int z_expandable;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_m3dstbi__zhuffman z_length, z_distance;
|
2019-11-18 17:31:53 +00:00
|
|
|
} _m3dstbi__zbuf;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static unsigned char _m3dstbi__zget8(_m3dstbi__zbuf *z) {
|
|
|
|
if (z->zbuffer >= z->zbuffer_end) return 0;
|
|
|
|
return *z->zbuffer++;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static void _m3dstbi__fill_bits(_m3dstbi__zbuf *z) {
|
|
|
|
do {
|
|
|
|
STBI_ASSERT(z->code_buffer < (1U << z->num_bits));
|
|
|
|
z->code_buffer |= (unsigned int)_m3dstbi__zget8(z) << z->num_bits;
|
|
|
|
z->num_bits += 8;
|
|
|
|
} while (z->num_bits <= 24);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static unsigned int _m3dstbi__zreceive(_m3dstbi__zbuf *z, int n) {
|
|
|
|
unsigned int k;
|
|
|
|
if (z->num_bits < n) _m3dstbi__fill_bits(z);
|
|
|
|
k = z->code_buffer & ((1 << n) - 1);
|
|
|
|
z->code_buffer >>= n;
|
|
|
|
z->num_bits -= n;
|
|
|
|
return k;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__zhuffman_decode_slowpath(_m3dstbi__zbuf *a, _m3dstbi__zhuffman *z) {
|
|
|
|
int b, s, k;
|
|
|
|
k = _m3dstbi__bit_reverse(a->code_buffer, 16);
|
|
|
|
for (s = STBI__ZFAST_BITS + 1;; ++s)
|
|
|
|
if (k < z->maxcode[s])
|
|
|
|
break;
|
|
|
|
if (s == 16) return -1;
|
|
|
|
b = (k >> (16 - s)) - z->firstcode[s] + z->firstsymbol[s];
|
|
|
|
STBI_ASSERT(z->size[b] == s);
|
|
|
|
a->code_buffer >>= s;
|
|
|
|
a->num_bits -= s;
|
|
|
|
return z->value[b];
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static int _m3dstbi__zhuffman_decode(_m3dstbi__zbuf *a, _m3dstbi__zhuffman *z) {
|
|
|
|
int b, s;
|
|
|
|
if (a->num_bits < 16) _m3dstbi__fill_bits(a);
|
|
|
|
b = z->fast[a->code_buffer & STBI__ZFAST_MASK];
|
|
|
|
if (b) {
|
|
|
|
s = b >> 9;
|
|
|
|
a->code_buffer >>= s;
|
|
|
|
a->num_bits -= s;
|
|
|
|
return b & 511;
|
|
|
|
}
|
|
|
|
return _m3dstbi__zhuffman_decode_slowpath(a, z);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__zexpand(_m3dstbi__zbuf *z, char *zout, int n) {
|
|
|
|
char *q;
|
|
|
|
int cur, limit, old_limit;
|
|
|
|
z->zout = zout;
|
|
|
|
if (!z->z_expandable) return _m3dstbi__err("output buffer limit", "Corrupt PNG");
|
|
|
|
cur = (int)(z->zout - z->zout_start);
|
|
|
|
limit = old_limit = (int)(z->zout_end - z->zout_start);
|
|
|
|
while (cur + n > limit)
|
|
|
|
limit *= 2;
|
|
|
|
q = (char *)STBI_REALLOC_SIZED(z->zout_start, old_limit, limit);
|
|
|
|
STBI_NOTUSED(old_limit);
|
|
|
|
if (q == NULL) return _m3dstbi__err("outofmem", "Out of memory");
|
|
|
|
z->zout_start = q;
|
|
|
|
z->zout = q + cur;
|
|
|
|
z->zout_end = q + limit;
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 17:31:53 +00:00
|
|
|
static int _m3dstbi__zlength_base[31] = {
|
2020-03-08 20:24:01 +00:00
|
|
|
3, 4, 5, 6, 7, 8, 9, 10, 11, 13,
|
|
|
|
15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
|
|
|
|
67, 83, 99, 115, 131, 163, 195, 227, 258, 0, 0
|
|
|
|
};
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__zlength_extra[31] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0, 0, 0 };
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__zdist_base[32] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193,
|
|
|
|
257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 0, 0 };
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__zdist_extra[32] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__parse_huffman_block(_m3dstbi__zbuf *a) {
|
|
|
|
char *zout = a->zout;
|
|
|
|
for (;;) {
|
|
|
|
int z = _m3dstbi__zhuffman_decode(a, &a->z_length);
|
|
|
|
if (z < 256) {
|
|
|
|
if (z < 0) return _m3dstbi__err("bad huffman code", "Corrupt PNG");
|
|
|
|
if (zout >= a->zout_end) {
|
|
|
|
if (!_m3dstbi__zexpand(a, zout, 1)) return 0;
|
|
|
|
zout = a->zout;
|
|
|
|
}
|
|
|
|
*zout++ = (char)z;
|
|
|
|
} else {
|
|
|
|
unsigned char *p;
|
|
|
|
int len, dist;
|
|
|
|
if (z == 256) {
|
|
|
|
a->zout = zout;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
z -= 257;
|
|
|
|
len = _m3dstbi__zlength_base[z];
|
|
|
|
if (_m3dstbi__zlength_extra[z]) len += _m3dstbi__zreceive(a, _m3dstbi__zlength_extra[z]);
|
|
|
|
z = _m3dstbi__zhuffman_decode(a, &a->z_distance);
|
|
|
|
if (z < 0) return _m3dstbi__err("bad huffman code", "Corrupt PNG");
|
|
|
|
dist = _m3dstbi__zdist_base[z];
|
|
|
|
if (_m3dstbi__zdist_extra[z]) dist += _m3dstbi__zreceive(a, _m3dstbi__zdist_extra[z]);
|
|
|
|
if (zout - a->zout_start < dist) return _m3dstbi__err("bad dist", "Corrupt PNG");
|
|
|
|
if (zout + len > a->zout_end) {
|
|
|
|
if (!_m3dstbi__zexpand(a, zout, len)) return 0;
|
|
|
|
zout = a->zout;
|
|
|
|
}
|
|
|
|
p = (unsigned char *)(zout - dist);
|
|
|
|
if (dist == 1) {
|
|
|
|
unsigned char v = *p;
|
|
|
|
if (len) {
|
|
|
|
do
|
|
|
|
*zout++ = v;
|
|
|
|
while (--len);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (len) {
|
|
|
|
do
|
|
|
|
*zout++ = *p++;
|
|
|
|
while (--len);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__compute_huffman_codes(_m3dstbi__zbuf *a) {
|
|
|
|
static unsigned char length_dezigzag[19] = { 16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15 };
|
|
|
|
_m3dstbi__zhuffman z_codelength;
|
|
|
|
unsigned char lencodes[286 + 32 + 137];
|
|
|
|
unsigned char codelength_sizes[19];
|
|
|
|
int i, n;
|
|
|
|
|
|
|
|
int hlit = _m3dstbi__zreceive(a, 5) + 257;
|
|
|
|
int hdist = _m3dstbi__zreceive(a, 5) + 1;
|
|
|
|
int hclen = _m3dstbi__zreceive(a, 4) + 4;
|
|
|
|
int ntot = hlit + hdist;
|
|
|
|
|
|
|
|
memset(codelength_sizes, 0, sizeof(codelength_sizes));
|
|
|
|
for (i = 0; i < hclen; ++i) {
|
|
|
|
int s = _m3dstbi__zreceive(a, 3);
|
|
|
|
codelength_sizes[length_dezigzag[i]] = (unsigned char)s;
|
|
|
|
}
|
|
|
|
if (!_m3dstbi__zbuild_huffman(&z_codelength, codelength_sizes, 19)) return 0;
|
|
|
|
|
|
|
|
n = 0;
|
|
|
|
while (n < ntot) {
|
|
|
|
int c = _m3dstbi__zhuffman_decode(a, &z_codelength);
|
|
|
|
if (c < 0 || c >= 19) return _m3dstbi__err("bad codelengths", "Corrupt PNG");
|
|
|
|
if (c < 16)
|
|
|
|
lencodes[n++] = (unsigned char)c;
|
|
|
|
else {
|
|
|
|
unsigned char fill = 0;
|
|
|
|
if (c == 16) {
|
|
|
|
c = _m3dstbi__zreceive(a, 2) + 3;
|
|
|
|
if (n == 0) return _m3dstbi__err("bad codelengths", "Corrupt PNG");
|
|
|
|
fill = lencodes[n - 1];
|
|
|
|
} else if (c == 17)
|
|
|
|
c = _m3dstbi__zreceive(a, 3) + 3;
|
|
|
|
else {
|
|
|
|
STBI_ASSERT(c == 18);
|
|
|
|
c = _m3dstbi__zreceive(a, 7) + 11;
|
|
|
|
}
|
|
|
|
if (ntot - n < c) return _m3dstbi__err("bad codelengths", "Corrupt PNG");
|
|
|
|
memset(lencodes + n, fill, c);
|
|
|
|
n += c;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (n != ntot) return _m3dstbi__err("bad codelengths", "Corrupt PNG");
|
|
|
|
if (!_m3dstbi__zbuild_huffman(&a->z_length, lencodes, hlit)) return 0;
|
|
|
|
if (!_m3dstbi__zbuild_huffman(&a->z_distance, lencodes + hlit, hdist)) return 0;
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static int _m3dstbi__parse_uncompressed_block(_m3dstbi__zbuf *a) {
|
|
|
|
unsigned char header[4];
|
|
|
|
int len, nlen, k;
|
|
|
|
if (a->num_bits & 7)
|
|
|
|
_m3dstbi__zreceive(a, a->num_bits & 7);
|
|
|
|
k = 0;
|
|
|
|
while (a->num_bits > 0) {
|
|
|
|
header[k++] = (unsigned char)(a->code_buffer & 255);
|
|
|
|
a->code_buffer >>= 8;
|
|
|
|
a->num_bits -= 8;
|
|
|
|
}
|
|
|
|
STBI_ASSERT(a->num_bits == 0);
|
|
|
|
while (k < 4)
|
|
|
|
header[k++] = _m3dstbi__zget8(a);
|
|
|
|
len = header[1] * 256 + header[0];
|
|
|
|
nlen = header[3] * 256 + header[2];
|
|
|
|
if (nlen != (len ^ 0xffff)) return _m3dstbi__err("zlib corrupt", "Corrupt PNG");
|
|
|
|
if (a->zbuffer + len > a->zbuffer_end) return _m3dstbi__err("read past buffer", "Corrupt PNG");
|
|
|
|
if (a->zout + len > a->zout_end)
|
|
|
|
if (!_m3dstbi__zexpand(a, a->zout, len)) return 0;
|
|
|
|
memcpy(a->zout, a->zbuffer, len);
|
|
|
|
a->zbuffer += len;
|
|
|
|
a->zout += len;
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__parse_zlib_header(_m3dstbi__zbuf *a) {
|
|
|
|
int cmf = _m3dstbi__zget8(a);
|
|
|
|
int cm = cmf & 15;
|
|
|
|
/* int cinfo = cmf >> 4; */
|
|
|
|
int flg = _m3dstbi__zget8(a);
|
|
|
|
if ((cmf * 256 + flg) % 31 != 0) return _m3dstbi__err("bad zlib header", "Corrupt PNG");
|
|
|
|
if (flg & 32) return _m3dstbi__err("no preset dict", "Corrupt PNG");
|
|
|
|
if (cm != 8) return _m3dstbi__err("bad compression", "Corrupt PNG");
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 17:31:53 +00:00
|
|
|
static unsigned char _m3dstbi__zdefault_length[288], _m3dstbi__zdefault_distance[32];
|
2020-03-08 20:24:01 +00:00
|
|
|
static void _m3dstbi__init_zdefaults(void) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i <= 143; ++i)
|
|
|
|
_m3dstbi__zdefault_length[i] = 8;
|
|
|
|
for (; i <= 255; ++i)
|
|
|
|
_m3dstbi__zdefault_length[i] = 9;
|
|
|
|
for (; i <= 279; ++i)
|
|
|
|
_m3dstbi__zdefault_length[i] = 7;
|
|
|
|
for (; i <= 287; ++i)
|
|
|
|
_m3dstbi__zdefault_length[i] = 8;
|
|
|
|
|
|
|
|
for (i = 0; i <= 31; ++i)
|
|
|
|
_m3dstbi__zdefault_distance[i] = 5;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__parse_zlib(_m3dstbi__zbuf *a, int parse_header) {
|
|
|
|
int final, type;
|
|
|
|
if (parse_header)
|
|
|
|
if (!_m3dstbi__parse_zlib_header(a)) return 0;
|
|
|
|
a->num_bits = 0;
|
|
|
|
a->code_buffer = 0;
|
|
|
|
do {
|
|
|
|
final = _m3dstbi__zreceive(a, 1);
|
|
|
|
type = _m3dstbi__zreceive(a, 2);
|
|
|
|
if (type == 0) {
|
|
|
|
if (!_m3dstbi__parse_uncompressed_block(a)) return 0;
|
|
|
|
} else if (type == 3) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
if (type == 1) {
|
|
|
|
if (!_m3dstbi__zbuild_huffman(&a->z_length, _m3dstbi__zdefault_length, 288)) return 0;
|
|
|
|
if (!_m3dstbi__zbuild_huffman(&a->z_distance, _m3dstbi__zdefault_distance, 32)) return 0;
|
|
|
|
} else {
|
|
|
|
if (!_m3dstbi__compute_huffman_codes(a)) return 0;
|
|
|
|
}
|
|
|
|
if (!_m3dstbi__parse_huffman_block(a)) return 0;
|
|
|
|
}
|
|
|
|
} while (!final);
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__do_zlib(_m3dstbi__zbuf *a, char *obuf, int olen, int exp, int parse_header) {
|
|
|
|
a->zout_start = obuf;
|
|
|
|
a->zout = obuf;
|
|
|
|
a->zout_end = obuf + olen;
|
|
|
|
a->z_expandable = exp;
|
|
|
|
_m3dstbi__init_zdefaults();
|
|
|
|
return _m3dstbi__parse_zlib(a, parse_header);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
char *_m3dstbi_zlib_decode_malloc_guesssize_headerflag(const char *buffer, int len, int initial_size, int *outlen, int parse_header) {
|
|
|
|
_m3dstbi__zbuf a;
|
|
|
|
char *p = (char *)_m3dstbi__malloc(initial_size);
|
|
|
|
if (p == NULL) return NULL;
|
|
|
|
a.zbuffer = (unsigned char *)buffer;
|
|
|
|
a.zbuffer_end = (unsigned char *)buffer + len;
|
|
|
|
if (_m3dstbi__do_zlib(&a, p, initial_size, 1, parse_header)) {
|
|
|
|
if (outlen) *outlen = (int)(a.zout - a.zout_start);
|
|
|
|
return a.zout_start;
|
|
|
|
} else {
|
|
|
|
STBI_FREE(a.zout_start);
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
_m3dstbi__uint32 length;
|
|
|
|
_m3dstbi__uint32 type;
|
2019-11-18 17:31:53 +00:00
|
|
|
} _m3dstbi__pngchunk;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static _m3dstbi__pngchunk _m3dstbi__get_chunk_header(_m3dstbi__context *s) {
|
|
|
|
_m3dstbi__pngchunk c;
|
|
|
|
c.length = _m3dstbi__get32be(s);
|
|
|
|
c.type = _m3dstbi__get32be(s);
|
|
|
|
return c;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static int _m3dstbi__check_png_header(_m3dstbi__context *s) {
|
|
|
|
static unsigned char png_sig[8] = { 137, 80, 78, 71, 13, 10, 26, 10 };
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < 8; ++i)
|
|
|
|
if (_m3dstbi__get8(s) != png_sig[i]) return _m3dstbi__err("bad png sig", "Not a PNG");
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef struct
|
|
|
|
{
|
2020-03-08 20:24:01 +00:00
|
|
|
_m3dstbi__context *s;
|
|
|
|
unsigned char *idata, *expanded, *out;
|
|
|
|
int depth;
|
2019-11-18 17:31:53 +00:00
|
|
|
} _m3dstbi__png;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
enum {
|
2020-03-08 20:24:01 +00:00
|
|
|
STBI__F_none = 0,
|
|
|
|
STBI__F_sub = 1,
|
|
|
|
STBI__F_up = 2,
|
|
|
|
STBI__F_avg = 3,
|
|
|
|
STBI__F_paeth = 4,
|
|
|
|
STBI__F_avg_first,
|
|
|
|
STBI__F_paeth_first
|
2019-10-29 17:42:10 +00:00
|
|
|
};
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static unsigned char first_row_filter[5] = {
|
|
|
|
STBI__F_none,
|
|
|
|
STBI__F_sub,
|
|
|
|
STBI__F_none,
|
|
|
|
STBI__F_avg_first,
|
|
|
|
STBI__F_paeth_first
|
2019-10-29 17:42:10 +00:00
|
|
|
};
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__paeth(int a, int b, int c) {
|
|
|
|
int p = a + b - c;
|
|
|
|
int pa = abs(p - a);
|
|
|
|
int pb = abs(p - b);
|
|
|
|
int pc = abs(p - c);
|
|
|
|
if (pa <= pb && pa <= pc) return a;
|
|
|
|
if (pb <= pc) return b;
|
|
|
|
return c;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static unsigned char _m3dstbi__depth_scale_table[9] = { 0, 0xff, 0x55, 0, 0x11, 0, 0, 0, 0x01 };
|
|
|
|
|
|
|
|
static int _m3dstbi__create_png_image_raw(_m3dstbi__png *a, unsigned char *raw, _m3dstbi__uint32 raw_len, int out_n, _m3dstbi__uint32 x, _m3dstbi__uint32 y, int depth, int color) {
|
|
|
|
int bytes = (depth == 16 ? 2 : 1);
|
|
|
|
_m3dstbi__context *s = a->s;
|
|
|
|
_m3dstbi__uint32 i, j, stride = x * out_n * bytes;
|
|
|
|
_m3dstbi__uint32 img_len, img_width_bytes;
|
|
|
|
int k;
|
|
|
|
int img_n = s->img_n;
|
|
|
|
|
|
|
|
int output_bytes = out_n * bytes;
|
|
|
|
int filter_bytes = img_n * bytes;
|
|
|
|
int width = x;
|
|
|
|
|
|
|
|
STBI_ASSERT(out_n == s->img_n || out_n == s->img_n + 1);
|
|
|
|
a->out = (unsigned char *)_m3dstbi__malloc_mad3(x, y, output_bytes, 0);
|
|
|
|
if (!a->out) return _m3dstbi__err("outofmem", "Out of memory");
|
|
|
|
|
|
|
|
if (!_m3dstbi__mad3sizes_valid(img_n, x, depth, 7)) return _m3dstbi__err("too large", "Corrupt PNG");
|
|
|
|
img_width_bytes = (((img_n * x * depth) + 7) >> 3);
|
|
|
|
img_len = (img_width_bytes + 1) * y;
|
|
|
|
if (s->img_x == x && s->img_y == y) {
|
|
|
|
if (raw_len != img_len) return _m3dstbi__err("not enough pixels", "Corrupt PNG");
|
|
|
|
} else {
|
|
|
|
if (raw_len < img_len) return _m3dstbi__err("not enough pixels", "Corrupt PNG");
|
|
|
|
}
|
|
|
|
|
|
|
|
for (j = 0; j < y; ++j) {
|
|
|
|
unsigned char *cur = a->out + stride * j;
|
|
|
|
unsigned char *prior = cur - stride;
|
|
|
|
int filter = *raw++;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (filter > 4)
|
|
|
|
return _m3dstbi__err("invalid filter", "Corrupt PNG");
|
|
|
|
|
|
|
|
if (depth < 8) {
|
|
|
|
STBI_ASSERT(img_width_bytes <= x);
|
|
|
|
cur += x * out_n - img_width_bytes;
|
|
|
|
filter_bytes = 1;
|
|
|
|
width = img_width_bytes;
|
|
|
|
}
|
|
|
|
prior = cur - stride;
|
|
|
|
|
|
|
|
if (j == 0) filter = first_row_filter[filter];
|
|
|
|
|
|
|
|
for (k = 0; k < filter_bytes; ++k) {
|
|
|
|
switch (filter) {
|
|
|
|
case STBI__F_none: cur[k] = raw[k]; break;
|
|
|
|
case STBI__F_sub: cur[k] = raw[k]; break;
|
|
|
|
case STBI__F_up: cur[k] = STBI__BYTECAST(raw[k] + prior[k]); break;
|
|
|
|
case STBI__F_avg: cur[k] = STBI__BYTECAST(raw[k] + (prior[k] >> 1)); break;
|
|
|
|
case STBI__F_paeth: cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(0, prior[k], 0)); break;
|
|
|
|
case STBI__F_avg_first: cur[k] = raw[k]; break;
|
|
|
|
case STBI__F_paeth_first: cur[k] = raw[k]; break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (depth == 8) {
|
|
|
|
if (img_n != out_n)
|
|
|
|
cur[img_n] = 255;
|
|
|
|
raw += img_n;
|
|
|
|
cur += out_n;
|
|
|
|
prior += out_n;
|
|
|
|
} else if (depth == 16) {
|
|
|
|
if (img_n != out_n) {
|
|
|
|
cur[filter_bytes] = 255;
|
|
|
|
cur[filter_bytes + 1] = 255;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
raw += filter_bytes;
|
|
|
|
cur += output_bytes;
|
|
|
|
prior += output_bytes;
|
|
|
|
} else {
|
|
|
|
raw += 1;
|
|
|
|
cur += 1;
|
|
|
|
prior += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depth < 8 || img_n == out_n) {
|
|
|
|
int nk = (width - 1) * filter_bytes;
|
|
|
|
#define STBI__CASE(f) \
|
|
|
|
case f: \
|
|
|
|
for (k = 0; k < nk; ++k)
|
|
|
|
switch (filter) {
|
|
|
|
case STBI__F_none:
|
|
|
|
memcpy(cur, raw, nk);
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k - filter_bytes]); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k - filter_bytes]) >> 1)); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(cur[k - filter_bytes], prior[k], prior[k - filter_bytes])); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k - filter_bytes] >> 1)); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(cur[k - filter_bytes], 0, 0)); }
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
#undef STBI__CASE
|
|
|
|
raw += nk;
|
|
|
|
} else {
|
|
|
|
STBI_ASSERT(img_n + 1 == out_n);
|
|
|
|
#define STBI__CASE(f) \
|
|
|
|
case f: \
|
|
|
|
for (i = x - 1; i >= 1; --i, cur[filter_bytes] = 255, raw += filter_bytes, cur += output_bytes, prior += output_bytes) \
|
|
|
|
for (k = 0; k < filter_bytes; ++k)
|
|
|
|
switch (filter) {
|
|
|
|
STBI__CASE(STBI__F_none) { cur[k] = raw[k]; }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_sub) { cur[k] = STBI__BYTECAST(raw[k] + cur[k - output_bytes]); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_up) { cur[k] = STBI__BYTECAST(raw[k] + prior[k]); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_avg) { cur[k] = STBI__BYTECAST(raw[k] + ((prior[k] + cur[k - output_bytes]) >> 1)); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_paeth) { cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(cur[k - output_bytes], prior[k], prior[k - output_bytes])); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_avg_first) { cur[k] = STBI__BYTECAST(raw[k] + (cur[k - output_bytes] >> 1)); }
|
|
|
|
break;
|
|
|
|
STBI__CASE(STBI__F_paeth_first) { cur[k] = STBI__BYTECAST(raw[k] + _m3dstbi__paeth(cur[k - output_bytes], 0, 0)); }
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
#undef STBI__CASE
|
|
|
|
|
|
|
|
if (depth == 16) {
|
|
|
|
cur = a->out + stride * j;
|
|
|
|
for (i = 0; i < x; ++i, cur += output_bytes) {
|
|
|
|
cur[filter_bytes + 1] = 255;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (depth < 8) {
|
|
|
|
for (j = 0; j < y; ++j) {
|
|
|
|
unsigned char *cur = a->out + stride * j;
|
|
|
|
unsigned char *in = a->out + stride * j + x * out_n - img_width_bytes;
|
|
|
|
unsigned char scale = (color == 0) ? _m3dstbi__depth_scale_table[depth] : 1;
|
|
|
|
|
|
|
|
if (depth == 4) {
|
|
|
|
for (k = x * img_n; k >= 2; k -= 2, ++in) {
|
|
|
|
*cur++ = scale * ((*in >> 4));
|
|
|
|
*cur++ = scale * ((*in) & 0x0f);
|
|
|
|
}
|
|
|
|
if (k > 0) *cur++ = scale * ((*in >> 4));
|
|
|
|
} else if (depth == 2) {
|
|
|
|
for (k = x * img_n; k >= 4; k -= 4, ++in) {
|
|
|
|
*cur++ = scale * ((*in >> 6));
|
|
|
|
*cur++ = scale * ((*in >> 4) & 0x03);
|
|
|
|
*cur++ = scale * ((*in >> 2) & 0x03);
|
|
|
|
*cur++ = scale * ((*in) & 0x03);
|
|
|
|
}
|
|
|
|
if (k > 0) *cur++ = scale * ((*in >> 6));
|
|
|
|
if (k > 1) *cur++ = scale * ((*in >> 4) & 0x03);
|
|
|
|
if (k > 2) *cur++ = scale * ((*in >> 2) & 0x03);
|
|
|
|
} else if (depth == 1) {
|
|
|
|
for (k = x * img_n; k >= 8; k -= 8, ++in) {
|
|
|
|
*cur++ = scale * ((*in >> 7));
|
|
|
|
*cur++ = scale * ((*in >> 6) & 0x01);
|
|
|
|
*cur++ = scale * ((*in >> 5) & 0x01);
|
|
|
|
*cur++ = scale * ((*in >> 4) & 0x01);
|
|
|
|
*cur++ = scale * ((*in >> 3) & 0x01);
|
|
|
|
*cur++ = scale * ((*in >> 2) & 0x01);
|
|
|
|
*cur++ = scale * ((*in >> 1) & 0x01);
|
|
|
|
*cur++ = scale * ((*in) & 0x01);
|
|
|
|
}
|
|
|
|
if (k > 0) *cur++ = scale * ((*in >> 7));
|
|
|
|
if (k > 1) *cur++ = scale * ((*in >> 6) & 0x01);
|
|
|
|
if (k > 2) *cur++ = scale * ((*in >> 5) & 0x01);
|
|
|
|
if (k > 3) *cur++ = scale * ((*in >> 4) & 0x01);
|
|
|
|
if (k > 4) *cur++ = scale * ((*in >> 3) & 0x01);
|
|
|
|
if (k > 5) *cur++ = scale * ((*in >> 2) & 0x01);
|
|
|
|
if (k > 6) *cur++ = scale * ((*in >> 1) & 0x01);
|
|
|
|
}
|
|
|
|
if (img_n != out_n) {
|
|
|
|
int q;
|
|
|
|
cur = a->out + stride * j;
|
|
|
|
if (img_n == 1) {
|
|
|
|
for (q = x - 1; q >= 0; --q) {
|
|
|
|
cur[q * 2 + 1] = 255;
|
|
|
|
cur[q * 2 + 0] = cur[q];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
STBI_ASSERT(img_n == 3);
|
|
|
|
for (q = x - 1; q >= 0; --q) {
|
|
|
|
cur[q * 4 + 3] = 255;
|
|
|
|
cur[q * 4 + 2] = cur[q * 3 + 2];
|
|
|
|
cur[q * 4 + 1] = cur[q * 3 + 1];
|
|
|
|
cur[q * 4 + 0] = cur[q * 3 + 0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (depth == 16) {
|
|
|
|
unsigned char *cur = a->out;
|
|
|
|
_m3dstbi__uint16 *cur16 = (_m3dstbi__uint16 *)cur;
|
|
|
|
|
|
|
|
for (i = 0; i < x * y * out_n; ++i, cur16++, cur += 2) {
|
|
|
|
*cur16 = (cur[0] << 8) | cur[1];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__create_png_image(_m3dstbi__png *a, unsigned char *image_data, _m3dstbi__uint32 image_data_len, int out_n, int depth, int color, int interlaced) {
|
|
|
|
int bytes = (depth == 16 ? 2 : 1);
|
|
|
|
int out_bytes = out_n * bytes;
|
|
|
|
unsigned char *final;
|
|
|
|
int p;
|
|
|
|
if (!interlaced)
|
|
|
|
return _m3dstbi__create_png_image_raw(a, image_data, image_data_len, out_n, a->s->img_x, a->s->img_y, depth, color);
|
|
|
|
|
|
|
|
final = (unsigned char *)_m3dstbi__malloc_mad3(a->s->img_x, a->s->img_y, out_bytes, 0);
|
|
|
|
for (p = 0; p < 7; ++p) {
|
|
|
|
int xorig[] = { 0, 4, 0, 2, 0, 1, 0 };
|
|
|
|
int yorig[] = { 0, 0, 4, 0, 2, 0, 1 };
|
|
|
|
int xspc[] = { 8, 8, 4, 4, 2, 2, 1 };
|
|
|
|
int yspc[] = { 8, 8, 8, 4, 4, 2, 2 };
|
|
|
|
int i, j, x, y;
|
|
|
|
x = (a->s->img_x - xorig[p] + xspc[p] - 1) / xspc[p];
|
|
|
|
y = (a->s->img_y - yorig[p] + yspc[p] - 1) / yspc[p];
|
|
|
|
if (x && y) {
|
|
|
|
_m3dstbi__uint32 img_len = ((((a->s->img_n * x * depth) + 7) >> 3) + 1) * y;
|
|
|
|
if (!_m3dstbi__create_png_image_raw(a, image_data, image_data_len, out_n, x, y, depth, color)) {
|
|
|
|
STBI_FREE(final);
|
|
|
|
return 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < y; ++j) {
|
|
|
|
for (i = 0; i < x; ++i) {
|
|
|
|
int out_y = j * yspc[p] + yorig[p];
|
|
|
|
int out_x = i * xspc[p] + xorig[p];
|
|
|
|
memcpy(final + out_y * a->s->img_x * out_bytes + out_x * out_bytes,
|
|
|
|
a->out + (j * x + i) * out_bytes, out_bytes);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STBI_FREE(a->out);
|
|
|
|
image_data += img_len;
|
|
|
|
image_data_len -= img_len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a->out = final;
|
|
|
|
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-04-28 05:39:13 +00:00
|
|
|
static int _m3dstbi__compute_transparency(_m3dstbi__png *z, unsigned char* tc, int out_n) {
|
2020-03-08 20:24:01 +00:00
|
|
|
_m3dstbi__context *s = z->s;
|
|
|
|
_m3dstbi__uint32 i, pixel_count = s->img_x * s->img_y;
|
|
|
|
unsigned char *p = z->out;
|
|
|
|
|
|
|
|
STBI_ASSERT(out_n == 2 || out_n == 4);
|
|
|
|
|
|
|
|
if (out_n == 2) {
|
|
|
|
for (i = 0; i < pixel_count; ++i) {
|
|
|
|
p[1] = (p[0] == tc[0] ? 0 : 255);
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < pixel_count; ++i) {
|
|
|
|
if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
|
|
|
|
p[3] = 0;
|
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__compute_transparency16(_m3dstbi__png *z, _m3dstbi__uint16 tc[3], int out_n) {
|
|
|
|
_m3dstbi__context *s = z->s;
|
|
|
|
_m3dstbi__uint32 i, pixel_count = s->img_x * s->img_y;
|
|
|
|
_m3dstbi__uint16 *p = (_m3dstbi__uint16 *)z->out;
|
|
|
|
|
|
|
|
STBI_ASSERT(out_n == 2 || out_n == 4);
|
|
|
|
|
|
|
|
if (out_n == 2) {
|
|
|
|
for (i = 0; i < pixel_count; ++i) {
|
|
|
|
p[1] = (p[0] == tc[0] ? 0 : 65535);
|
|
|
|
p += 2;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < pixel_count; ++i) {
|
|
|
|
if (p[0] == tc[0] && p[1] == tc[1] && p[2] == tc[2])
|
|
|
|
p[3] = 0;
|
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__expand_png_palette(_m3dstbi__png *a, unsigned char *palette, int len, int pal_img_n) {
|
|
|
|
_m3dstbi__uint32 i, pixel_count = a->s->img_x * a->s->img_y;
|
|
|
|
unsigned char *p, *temp_out, *orig = a->out;
|
|
|
|
|
|
|
|
p = (unsigned char *)_m3dstbi__malloc_mad2(pixel_count, pal_img_n, 0);
|
|
|
|
if (p == NULL) return _m3dstbi__err("outofmem", "Out of memory");
|
|
|
|
|
|
|
|
temp_out = p;
|
|
|
|
|
|
|
|
if (pal_img_n == 3) {
|
|
|
|
for (i = 0; i < pixel_count; ++i) {
|
|
|
|
int n = orig[i] * 4;
|
|
|
|
p[0] = palette[n];
|
|
|
|
p[1] = palette[n + 1];
|
|
|
|
p[2] = palette[n + 2];
|
|
|
|
p += 3;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (i = 0; i < pixel_count; ++i) {
|
|
|
|
int n = orig[i] * 4;
|
|
|
|
p[0] = palette[n];
|
|
|
|
p[1] = palette[n + 1];
|
|
|
|
p[2] = palette[n + 2];
|
|
|
|
p[3] = palette[n + 3];
|
|
|
|
p += 4;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
STBI_FREE(a->out);
|
|
|
|
a->out = temp_out;
|
|
|
|
|
|
|
|
STBI_NOTUSED(len);
|
|
|
|
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#define STBI__PNG_TYPE(a, b, c, d) (((unsigned)(a) << 24) + ((unsigned)(b) << 16) + ((unsigned)(c) << 8) + (unsigned)(d))
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbi__parse_png_file(_m3dstbi__png *z, int scan, int req_comp) {
|
|
|
|
unsigned char palette[1024], pal_img_n = 0;
|
2020-04-28 05:39:13 +00:00
|
|
|
unsigned char has_trans = 0, tc[3] = {};
|
2020-03-08 20:24:01 +00:00
|
|
|
_m3dstbi__uint16 tc16[3];
|
|
|
|
_m3dstbi__uint32 ioff = 0, idata_limit = 0, i, pal_len = 0;
|
|
|
|
int first = 1, k, interlace = 0, color = 0;
|
|
|
|
_m3dstbi__context *s = z->s;
|
|
|
|
|
|
|
|
z->expanded = NULL;
|
|
|
|
z->idata = NULL;
|
|
|
|
z->out = NULL;
|
|
|
|
|
|
|
|
if (!_m3dstbi__check_png_header(s)) return 0;
|
|
|
|
|
|
|
|
if (scan == STBI__SCAN_type) return 1;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
_m3dstbi__pngchunk c = _m3dstbi__get_chunk_header(s);
|
|
|
|
switch (c.type) {
|
|
|
|
case STBI__PNG_TYPE('C', 'g', 'B', 'I'):
|
|
|
|
_m3dstbi__skip(s, c.length);
|
|
|
|
break;
|
|
|
|
case STBI__PNG_TYPE('I', 'H', 'D', 'R'): {
|
|
|
|
int comp, filter;
|
|
|
|
if (!first) return _m3dstbi__err("multiple IHDR", "Corrupt PNG");
|
|
|
|
first = 0;
|
|
|
|
if (c.length != 13) return _m3dstbi__err("bad IHDR len", "Corrupt PNG");
|
|
|
|
s->img_x = _m3dstbi__get32be(s);
|
|
|
|
if (s->img_x > (1 << 24)) return _m3dstbi__err("too large", "Very large image (corrupt?)");
|
|
|
|
s->img_y = _m3dstbi__get32be(s);
|
|
|
|
if (s->img_y > (1 << 24)) return _m3dstbi__err("too large", "Very large image (corrupt?)");
|
|
|
|
z->depth = _m3dstbi__get8(s);
|
|
|
|
if (z->depth != 1 && z->depth != 2 && z->depth != 4 && z->depth != 8 && z->depth != 16) return _m3dstbi__err("1/2/4/8/16-bit only", "PNG not supported: 1/2/4/8/16-bit only");
|
|
|
|
color = _m3dstbi__get8(s);
|
|
|
|
if (color > 6) return _m3dstbi__err("bad ctype", "Corrupt PNG");
|
|
|
|
if (color == 3 && z->depth == 16) return _m3dstbi__err("bad ctype", "Corrupt PNG");
|
|
|
|
if (color == 3)
|
|
|
|
pal_img_n = 3;
|
|
|
|
else if (color & 1)
|
|
|
|
return _m3dstbi__err("bad ctype", "Corrupt PNG");
|
|
|
|
comp = _m3dstbi__get8(s);
|
|
|
|
if (comp) return _m3dstbi__err("bad comp method", "Corrupt PNG");
|
|
|
|
filter = _m3dstbi__get8(s);
|
|
|
|
if (filter) return _m3dstbi__err("bad filter method", "Corrupt PNG");
|
|
|
|
interlace = _m3dstbi__get8(s);
|
|
|
|
if (interlace > 1) return _m3dstbi__err("bad interlace method", "Corrupt PNG");
|
|
|
|
if (!s->img_x || !s->img_y) return _m3dstbi__err("0-pixel image", "Corrupt PNG");
|
|
|
|
if (!pal_img_n) {
|
|
|
|
s->img_n = (color & 2 ? 3 : 1) + (color & 4 ? 1 : 0);
|
|
|
|
if ((1 << 30) / s->img_x / s->img_n < s->img_y) return _m3dstbi__err("too large", "Image too large to decode");
|
|
|
|
if (scan == STBI__SCAN_header) return 1;
|
|
|
|
} else {
|
|
|
|
s->img_n = 1;
|
|
|
|
if ((1 << 30) / s->img_x / 4 < s->img_y) return _m3dstbi__err("too large", "Corrupt PNG");
|
|
|
|
}
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
|
|
|
|
case STBI__PNG_TYPE('P', 'L', 'T', 'E'): {
|
|
|
|
if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG");
|
|
|
|
if (c.length > 256 * 3) return _m3dstbi__err("invalid PLTE", "Corrupt PNG");
|
|
|
|
pal_len = c.length / 3;
|
|
|
|
if (pal_len * 3 != c.length) return _m3dstbi__err("invalid PLTE", "Corrupt PNG");
|
|
|
|
for (i = 0; i < pal_len; ++i) {
|
|
|
|
palette[i * 4 + 0] = _m3dstbi__get8(s);
|
|
|
|
palette[i * 4 + 1] = _m3dstbi__get8(s);
|
|
|
|
palette[i * 4 + 2] = _m3dstbi__get8(s);
|
|
|
|
palette[i * 4 + 3] = 255;
|
|
|
|
}
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
|
|
|
|
case STBI__PNG_TYPE('t', 'R', 'N', 'S'): {
|
|
|
|
if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG");
|
|
|
|
if (z->idata) return _m3dstbi__err("tRNS after IDAT", "Corrupt PNG");
|
|
|
|
if (pal_img_n) {
|
|
|
|
if (scan == STBI__SCAN_header) {
|
|
|
|
s->img_n = 4;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (pal_len == 0) return _m3dstbi__err("tRNS before PLTE", "Corrupt PNG");
|
|
|
|
if (c.length > pal_len) return _m3dstbi__err("bad tRNS len", "Corrupt PNG");
|
|
|
|
pal_img_n = 4;
|
|
|
|
for (i = 0; i < c.length; ++i)
|
|
|
|
palette[i * 4 + 3] = _m3dstbi__get8(s);
|
|
|
|
} else {
|
|
|
|
if (!(s->img_n & 1)) return _m3dstbi__err("tRNS with alpha", "Corrupt PNG");
|
|
|
|
if (c.length != (_m3dstbi__uint32)s->img_n * 2) return _m3dstbi__err("bad tRNS len", "Corrupt PNG");
|
|
|
|
has_trans = 1;
|
|
|
|
if (z->depth == 16) {
|
|
|
|
for (k = 0; k < s->img_n; ++k)
|
|
|
|
tc16[k] = (_m3dstbi__uint16)_m3dstbi__get16be(s);
|
|
|
|
} else {
|
|
|
|
for (k = 0; k < s->img_n; ++k)
|
|
|
|
tc[k] = (unsigned char)(_m3dstbi__get16be(s) & 255) * _m3dstbi__depth_scale_table[z->depth];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
|
|
|
|
case STBI__PNG_TYPE('I', 'D', 'A', 'T'): {
|
|
|
|
if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG");
|
|
|
|
if (pal_img_n && !pal_len) return _m3dstbi__err("no PLTE", "Corrupt PNG");
|
|
|
|
if (scan == STBI__SCAN_header) {
|
|
|
|
s->img_n = pal_img_n;
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if ((int)(ioff + c.length) < (int)ioff) return 0;
|
|
|
|
if (ioff + c.length > idata_limit) {
|
|
|
|
_m3dstbi__uint32 idata_limit_old = idata_limit;
|
|
|
|
unsigned char *p;
|
|
|
|
if (idata_limit == 0) idata_limit = c.length > 4096 ? c.length : 4096;
|
|
|
|
while (ioff + c.length > idata_limit)
|
|
|
|
idata_limit *= 2;
|
|
|
|
STBI_NOTUSED(idata_limit_old);
|
|
|
|
p = (unsigned char *)STBI_REALLOC_SIZED(z->idata, idata_limit_old, idata_limit);
|
|
|
|
if (p == NULL) return _m3dstbi__err("outofmem", "Out of memory");
|
|
|
|
z->idata = p;
|
|
|
|
}
|
|
|
|
if (!_m3dstbi__getn(s, z->idata + ioff, c.length)) return _m3dstbi__err("outofdata", "Corrupt PNG");
|
|
|
|
ioff += c.length;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
|
|
|
|
case STBI__PNG_TYPE('I', 'E', 'N', 'D'): {
|
|
|
|
_m3dstbi__uint32 raw_len, bpl;
|
|
|
|
if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG");
|
|
|
|
if (scan != STBI__SCAN_load) return 1;
|
|
|
|
if (z->idata == NULL) return _m3dstbi__err("no IDAT", "Corrupt PNG");
|
|
|
|
bpl = (s->img_x * z->depth + 7) / 8;
|
|
|
|
raw_len = bpl * s->img_y * s->img_n /* pixels */ + s->img_y /* filter mode per row */;
|
|
|
|
z->expanded = (unsigned char *)_m3dstbi_zlib_decode_malloc_guesssize_headerflag((char *)z->idata, ioff, raw_len, (int *)&raw_len, 1);
|
|
|
|
if (z->expanded == NULL) return 0;
|
|
|
|
STBI_FREE(z->idata);
|
|
|
|
z->idata = NULL;
|
|
|
|
if ((req_comp == s->img_n + 1 && req_comp != 3 && !pal_img_n) || has_trans)
|
|
|
|
s->img_out_n = s->img_n + 1;
|
|
|
|
else
|
|
|
|
s->img_out_n = s->img_n;
|
|
|
|
if (!_m3dstbi__create_png_image(z, z->expanded, raw_len, s->img_out_n, z->depth, color, interlace)) return 0;
|
|
|
|
if (has_trans) {
|
|
|
|
if (z->depth == 16) {
|
|
|
|
if (!_m3dstbi__compute_transparency16(z, tc16, s->img_out_n)) return 0;
|
|
|
|
} else {
|
|
|
|
if (!_m3dstbi__compute_transparency(z, tc, s->img_out_n)) return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (pal_img_n) {
|
|
|
|
s->img_n = pal_img_n;
|
|
|
|
s->img_out_n = pal_img_n;
|
|
|
|
if (req_comp >= 3) s->img_out_n = req_comp;
|
|
|
|
if (!_m3dstbi__expand_png_palette(z, palette, pal_len, s->img_out_n))
|
|
|
|
return 0;
|
|
|
|
} else if (has_trans) {
|
|
|
|
++s->img_n;
|
|
|
|
}
|
|
|
|
STBI_FREE(z->expanded);
|
|
|
|
z->expanded = NULL;
|
|
|
|
return 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
if (first) return _m3dstbi__err("first not IHDR", "Corrupt PNG");
|
|
|
|
if ((c.type & (1 << 29)) == 0) {
|
|
|
|
return _m3dstbi__err("invalid_chunk", "PNG not supported: unknown PNG chunk type");
|
|
|
|
}
|
|
|
|
_m3dstbi__skip(s, c.length);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
_m3dstbi__get32be(s);
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static void *_m3dstbi__do_png(_m3dstbi__png *p, int *x, int *y, int *n, int req_comp, _m3dstbi__result_info *ri) {
|
|
|
|
void *result = NULL;
|
|
|
|
if (req_comp < 0 || req_comp > 4) {
|
|
|
|
_m3dstbi__err("bad req_comp", "Internal error");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (_m3dstbi__parse_png_file(p, STBI__SCAN_load, req_comp)) {
|
|
|
|
if (p->depth < 8)
|
|
|
|
ri->bits_per_channel = 8;
|
|
|
|
else
|
|
|
|
ri->bits_per_channel = p->depth;
|
|
|
|
result = p->out;
|
|
|
|
p->out = NULL;
|
|
|
|
if (req_comp && req_comp != p->s->img_out_n) {
|
|
|
|
if (ri->bits_per_channel == 8)
|
|
|
|
result = _m3dstbi__convert_format((unsigned char *)result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
|
|
|
|
else
|
|
|
|
result = _m3dstbi__convert_format16((_m3dstbi__uint16 *)result, p->s->img_out_n, req_comp, p->s->img_x, p->s->img_y);
|
|
|
|
p->s->img_out_n = req_comp;
|
|
|
|
if (result == NULL) return result;
|
|
|
|
}
|
|
|
|
*x = p->s->img_x;
|
|
|
|
*y = p->s->img_y;
|
|
|
|
if (n) *n = p->s->img_n;
|
|
|
|
}
|
|
|
|
STBI_FREE(p->out);
|
|
|
|
p->out = NULL;
|
|
|
|
STBI_FREE(p->expanded);
|
|
|
|
p->expanded = NULL;
|
|
|
|
STBI_FREE(p->idata);
|
|
|
|
p->idata = NULL;
|
|
|
|
|
|
|
|
return result;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static void *_m3dstbi__png_load(_m3dstbi__context *s, int *x, int *y, int *comp, int req_comp, _m3dstbi__result_info *ri) {
|
|
|
|
_m3dstbi__png p;
|
|
|
|
p.s = s;
|
|
|
|
return _m3dstbi__do_png(&p, x, y, comp, req_comp, ri);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 17:31:53 +00:00
|
|
|
#define stbi__context _m3dstbi__context
|
|
|
|
#define stbi__result_info _m3dstbi__result_info
|
|
|
|
#define stbi__png_load _m3dstbi__png_load
|
|
|
|
#define stbi_zlib_decode_malloc_guesssize_headerflag _m3dstbi_zlib_decode_malloc_guesssize_headerflag
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(M3D_EXPORTER) && !defined(INCLUDE_STB_IMAGE_WRITE_H)
|
|
|
|
/* zlib_compressor from
|
|
|
|
|
|
|
|
stb_image_write - v1.13 - public domain - http://nothings.org/stb/stb_image_write.h
|
|
|
|
*/
|
2019-11-18 17:31:53 +00:00
|
|
|
typedef unsigned char _m3dstbiw__uc;
|
|
|
|
typedef unsigned short _m3dstbiw__us;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2019-11-18 17:31:53 +00:00
|
|
|
typedef uint16_t _m3dstbiw__uint16;
|
2020-03-08 20:24:01 +00:00
|
|
|
typedef int16_t _m3dstbiw__int16;
|
2019-11-18 17:31:53 +00:00
|
|
|
typedef uint32_t _m3dstbiw__uint32;
|
2020-03-08 20:24:01 +00:00
|
|
|
typedef int32_t _m3dstbiw__int32;
|
|
|
|
|
|
|
|
#define STBIW_MALLOC(s) M3D_MALLOC(s)
|
|
|
|
#define STBIW_REALLOC(p, ns) M3D_REALLOC(p, ns)
|
|
|
|
#define STBIW_REALLOC_SIZED(p, oldsz, newsz) STBIW_REALLOC(p, newsz)
|
|
|
|
#define STBIW_FREE M3D_FREE
|
|
|
|
#define STBIW_MEMMOVE memmove
|
|
|
|
#define STBIW_UCHAR (uint8_t)
|
2019-10-29 17:42:10 +00:00
|
|
|
#define STBIW_ASSERT(x)
|
2020-03-08 20:24:01 +00:00
|
|
|
#define _m3dstbiw___sbraw(a) ((int *)(a)-2)
|
|
|
|
#define _m3dstbiw___sbm(a) _m3dstbiw___sbraw(a)[0]
|
|
|
|
#define _m3dstbiw___sbn(a) _m3dstbiw___sbraw(a)[1]
|
|
|
|
|
|
|
|
#define _m3dstbiw___sbneedgrow(a, n) ((a) == 0 || _m3dstbiw___sbn(a) + n >= _m3dstbiw___sbm(a))
|
|
|
|
#define _m3dstbiw___sbmaybegrow(a, n) (_m3dstbiw___sbneedgrow(a, (n)) ? _m3dstbiw___sbgrow(a, n) : 0)
|
|
|
|
#define _m3dstbiw___sbgrow(a, n) _m3dstbiw___sbgrowf((void **)&(a), (n), sizeof(*(a)))
|
|
|
|
|
|
|
|
#define _m3dstbiw___sbpush(a, v) (_m3dstbiw___sbmaybegrow(a, 1), (a)[_m3dstbiw___sbn(a)++] = (v))
|
|
|
|
#define _m3dstbiw___sbcount(a) ((a) ? _m3dstbiw___sbn(a) : 0)
|
|
|
|
#define _m3dstbiw___sbfree(a) ((a) ? STBIW_FREE(_m3dstbiw___sbraw(a)), 0 : 0)
|
|
|
|
|
|
|
|
static void *_m3dstbiw___sbgrowf(void **arr, int increment, int itemsize) {
|
|
|
|
int m = *arr ? 2 * _m3dstbiw___sbm(*arr) + increment : increment + 1;
|
|
|
|
void *p = STBIW_REALLOC_SIZED(*arr ? _m3dstbiw___sbraw(*arr) : 0, *arr ? (_m3dstbiw___sbm(*arr) * itemsize + sizeof(int) * 2) : 0, itemsize * m + sizeof(int) * 2);
|
|
|
|
STBIW_ASSERT(p);
|
|
|
|
if (p) {
|
|
|
|
if (!*arr) ((int *)p)[1] = 0;
|
|
|
|
*arr = (void *)((int *)p + 2);
|
|
|
|
_m3dstbiw___sbm(*arr) = m;
|
|
|
|
}
|
|
|
|
return *arr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static unsigned char *_m3dstbiw___zlib_flushf(unsigned char *data, unsigned int *bitbuffer, int *bitcount) {
|
|
|
|
while (*bitcount >= 8) {
|
|
|
|
_m3dstbiw___sbpush(data, STBIW_UCHAR(*bitbuffer));
|
|
|
|
*bitbuffer >>= 8;
|
|
|
|
*bitcount -= 8;
|
|
|
|
}
|
|
|
|
return data;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3dstbiw___zlib_bitrev(int code, int codebits) {
|
|
|
|
int res = 0;
|
|
|
|
while (codebits--) {
|
|
|
|
res = (res << 1) | (code & 1);
|
|
|
|
code >>= 1;
|
|
|
|
}
|
|
|
|
return res;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static unsigned int _m3dstbiw___zlib_countm(unsigned char *a, unsigned char *b, int limit) {
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < limit && i < 258; ++i)
|
|
|
|
if (a[i] != b[i]) break;
|
|
|
|
return i;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
static unsigned int _m3dstbiw___zhash(unsigned char *data) {
|
|
|
|
_m3dstbiw__uint32 hash = data[0] + (data[1] << 8) + (data[2] << 16);
|
|
|
|
hash ^= hash << 3;
|
|
|
|
hash += hash >> 5;
|
|
|
|
hash ^= hash << 4;
|
|
|
|
hash += hash >> 17;
|
|
|
|
hash ^= hash << 25;
|
|
|
|
hash += hash >> 6;
|
|
|
|
return hash;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
2019-11-18 17:31:53 +00:00
|
|
|
#define _m3dstbiw___zlib_flush() (out = _m3dstbiw___zlib_flushf(out, &bitbuf, &bitcount))
|
2020-03-08 20:24:01 +00:00
|
|
|
#define _m3dstbiw___zlib_add(code, codebits) \
|
|
|
|
(bitbuf |= (code) << bitcount, bitcount += (codebits), _m3dstbiw___zlib_flush())
|
|
|
|
#define _m3dstbiw___zlib_huffa(b, c) _m3dstbiw___zlib_add(_m3dstbiw___zlib_bitrev(b, c), c)
|
|
|
|
#define _m3dstbiw___zlib_huff1(n) _m3dstbiw___zlib_huffa(0x30 + (n), 8)
|
|
|
|
#define _m3dstbiw___zlib_huff2(n) _m3dstbiw___zlib_huffa(0x190 + (n)-144, 9)
|
|
|
|
#define _m3dstbiw___zlib_huff3(n) _m3dstbiw___zlib_huffa(0 + (n)-256, 7)
|
|
|
|
#define _m3dstbiw___zlib_huff4(n) _m3dstbiw___zlib_huffa(0xc0 + (n)-280, 8)
|
|
|
|
#define _m3dstbiw___zlib_huff(n) ((n) <= 143 ? _m3dstbiw___zlib_huff1(n) : (n) <= 255 ? _m3dstbiw___zlib_huff2(n) : (n) <= 279 ? _m3dstbiw___zlib_huff3(n) : _m3dstbiw___zlib_huff4(n))
|
2019-11-18 17:31:53 +00:00
|
|
|
#define _m3dstbiw___zlib_huffb(n) ((n) <= 143 ? _m3dstbiw___zlib_huff1(n) : _m3dstbiw___zlib_huff2(n))
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#define _m3dstbiw___ZHASH 16384
|
|
|
|
|
|
|
|
unsigned char *_m3dstbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality) {
|
|
|
|
static unsigned short lengthc[] = { 3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258, 259 };
|
|
|
|
static unsigned char lengtheb[] = { 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0 };
|
|
|
|
static unsigned short distc[] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577, 32768 };
|
|
|
|
static unsigned char disteb[] = { 0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13 };
|
|
|
|
unsigned int bitbuf = 0;
|
|
|
|
int i, j, bitcount = 0;
|
|
|
|
unsigned char *out = NULL;
|
|
|
|
unsigned char ***hash_table = (unsigned char ***)STBIW_MALLOC(_m3dstbiw___ZHASH * sizeof(char **));
|
|
|
|
if (hash_table == NULL)
|
|
|
|
return NULL;
|
|
|
|
if (quality < 5) quality = 5;
|
|
|
|
|
|
|
|
_m3dstbiw___sbpush(out, 0x78);
|
|
|
|
_m3dstbiw___sbpush(out, 0x5e);
|
|
|
|
_m3dstbiw___zlib_add(1, 1);
|
|
|
|
_m3dstbiw___zlib_add(1, 2);
|
|
|
|
|
|
|
|
for (i = 0; i < _m3dstbiw___ZHASH; ++i)
|
|
|
|
hash_table[i] = NULL;
|
|
|
|
|
|
|
|
i = 0;
|
|
|
|
while (i < data_len - 3) {
|
|
|
|
int h = _m3dstbiw___zhash(data + i) & (_m3dstbiw___ZHASH - 1), best = 3;
|
|
|
|
unsigned char *bestloc = 0;
|
|
|
|
unsigned char **hlist = hash_table[h];
|
|
|
|
int n = _m3dstbiw___sbcount(hlist);
|
|
|
|
for (j = 0; j < n; ++j) {
|
|
|
|
if (hlist[j] - data > i - 32768) {
|
|
|
|
int d = _m3dstbiw___zlib_countm(hlist[j], data + i, data_len - i);
|
|
|
|
if (d >= best) best = d, bestloc = hlist[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (hash_table[h] && _m3dstbiw___sbn(hash_table[h]) == 2 * quality) {
|
|
|
|
STBIW_MEMMOVE(hash_table[h], hash_table[h] + quality, sizeof(hash_table[h][0]) * quality);
|
|
|
|
_m3dstbiw___sbn(hash_table[h]) = quality;
|
|
|
|
}
|
|
|
|
_m3dstbiw___sbpush(hash_table[h], data + i);
|
|
|
|
|
|
|
|
if (bestloc) {
|
|
|
|
h = _m3dstbiw___zhash(data + i + 1) & (_m3dstbiw___ZHASH - 1);
|
|
|
|
hlist = hash_table[h];
|
|
|
|
n = _m3dstbiw___sbcount(hlist);
|
|
|
|
for (j = 0; j < n; ++j) {
|
|
|
|
if (hlist[j] - data > i - 32767) {
|
|
|
|
int e = _m3dstbiw___zlib_countm(hlist[j], data + i + 1, data_len - i - 1);
|
|
|
|
if (e > best) {
|
|
|
|
bestloc = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bestloc) {
|
|
|
|
int d = (int)(data + i - bestloc);
|
|
|
|
STBIW_ASSERT(d <= 32767 && best <= 258);
|
|
|
|
for (j = 0; best > lengthc[j + 1] - 1; ++j)
|
|
|
|
;
|
|
|
|
_m3dstbiw___zlib_huff(j + 257);
|
|
|
|
if (lengtheb[j]) _m3dstbiw___zlib_add(best - lengthc[j], lengtheb[j]);
|
|
|
|
for (j = 0; d > distc[j + 1] - 1; ++j)
|
|
|
|
;
|
|
|
|
_m3dstbiw___zlib_add(_m3dstbiw___zlib_bitrev(j, 5), 5);
|
|
|
|
if (disteb[j]) _m3dstbiw___zlib_add(d - distc[j], disteb[j]);
|
|
|
|
i += best;
|
|
|
|
} else {
|
|
|
|
_m3dstbiw___zlib_huffb(data[i]);
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (; i < data_len; ++i)
|
|
|
|
_m3dstbiw___zlib_huffb(data[i]);
|
|
|
|
_m3dstbiw___zlib_huff(256);
|
|
|
|
while (bitcount)
|
|
|
|
_m3dstbiw___zlib_add(0, 1);
|
|
|
|
|
|
|
|
for (i = 0; i < _m3dstbiw___ZHASH; ++i)
|
|
|
|
(void)_m3dstbiw___sbfree(hash_table[i]);
|
|
|
|
STBIW_FREE(hash_table);
|
|
|
|
|
|
|
|
{
|
|
|
|
unsigned int s1 = 1, s2 = 0;
|
|
|
|
int blocklen = (int)(data_len % 5552);
|
|
|
|
j = 0;
|
|
|
|
while (j < data_len) {
|
|
|
|
for (i = 0; i < blocklen; ++i)
|
|
|
|
s1 += data[j + i], s2 += s1;
|
|
|
|
s1 %= 65521, s2 %= 65521;
|
|
|
|
j += blocklen;
|
|
|
|
blocklen = 5552;
|
|
|
|
}
|
|
|
|
_m3dstbiw___sbpush(out, STBIW_UCHAR(s2 >> 8));
|
|
|
|
_m3dstbiw___sbpush(out, STBIW_UCHAR(s2));
|
|
|
|
_m3dstbiw___sbpush(out, STBIW_UCHAR(s1 >> 8));
|
|
|
|
_m3dstbiw___sbpush(out, STBIW_UCHAR(s1));
|
|
|
|
}
|
|
|
|
*out_len = _m3dstbiw___sbn(out);
|
|
|
|
STBIW_MEMMOVE(_m3dstbiw___sbraw(out), out, *out_len);
|
|
|
|
return (unsigned char *)_m3dstbiw___sbraw(out);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 17:31:53 +00:00
|
|
|
#define stbi_zlib_compress _m3dstbi_zlib_compress
|
|
|
|
#else
|
2020-03-08 20:24:01 +00:00
|
|
|
unsigned char *_m3dstbi_zlib_compress(unsigned char *data, int data_len, int *out_len, int quality);
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_CHUNKMAGIC(m, a, b, c, d) ((m)[0] == (a) && (m)[1] == (b) && (m)[2] == (c) && (m)[3] == (d))
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
#ifdef M3D_ASCII
|
2020-03-08 20:24:01 +00:00
|
|
|
#include <locale.h> /* sprintf and strtod cares about number locale */
|
|
|
|
#include <stdio.h> /* get sprintf */
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
2019-11-25 04:03:10 +00:00
|
|
|
#ifdef M3D_PROFILING
|
|
|
|
#include <sys/time.h>
|
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
#if !defined(M3D_NOIMPORTER) && defined(M3D_ASCII)
|
|
|
|
/* helper functions for the ASCII parser */
|
|
|
|
static char *_m3d_findarg(char *s) {
|
2020-03-08 20:24:01 +00:00
|
|
|
while (s && *s && *s != ' ' && *s != '\t' && *s != '\r' && *s != '\n')
|
|
|
|
s++;
|
|
|
|
while (s && *s && (*s == ' ' || *s == '\t'))
|
|
|
|
s++;
|
2019-10-29 17:42:10 +00:00
|
|
|
return s;
|
|
|
|
}
|
|
|
|
static char *_m3d_findnl(char *s) {
|
2020-03-08 20:24:01 +00:00
|
|
|
while (s && *s && *s != '\r' && *s != '\n')
|
|
|
|
s++;
|
|
|
|
if (*s == '\r') s++;
|
|
|
|
if (*s == '\n') s++;
|
2019-10-29 17:42:10 +00:00
|
|
|
return s;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
static char *_m3d_gethex(char *s, uint32_t *ret) {
|
|
|
|
if (*s == '#') s++;
|
2019-10-29 17:42:10 +00:00
|
|
|
*ret = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (; *s; s++) {
|
|
|
|
if (*s >= '0' && *s <= '9') {
|
|
|
|
*ret <<= 4;
|
|
|
|
*ret += (uint32_t)(*s - '0');
|
|
|
|
} else if (*s >= 'a' && *s <= 'f') {
|
|
|
|
*ret <<= 4;
|
|
|
|
*ret += (uint32_t)(*s - 'a' + 10);
|
|
|
|
} else if (*s >= 'A' && *s <= 'F') {
|
|
|
|
*ret <<= 4;
|
|
|
|
*ret += (uint32_t)(*s - 'A' + 10);
|
|
|
|
} else
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
return _m3d_findarg(s);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
static char *_m3d_getint(char *s, uint32_t *ret) {
|
2019-10-29 17:42:10 +00:00
|
|
|
char *e = s;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!s || !*s || *s == '\r' || *s == '\n') return s;
|
|
|
|
for (; *e >= '0' && *e <= '9'; e++)
|
|
|
|
;
|
2019-10-29 17:42:10 +00:00
|
|
|
*ret = atoi(s);
|
|
|
|
return e;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
static char *_m3d_getfloat(char *s, M3D_FLOAT *ret) {
|
2019-10-29 17:42:10 +00:00
|
|
|
char *e = s;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!s || !*s || *s == '\r' || *s == '\n') return s;
|
|
|
|
for (; *e == '-' || *e == '+' || *e == '.' || (*e >= '0' && *e <= '9') || *e == 'e' || *e == 'E'; e++)
|
|
|
|
;
|
2019-10-29 17:42:10 +00:00
|
|
|
*ret = (M3D_FLOAT)strtod(s, NULL);
|
|
|
|
return _m3d_findarg(e);
|
|
|
|
}
|
|
|
|
#endif
|
2019-12-14 07:52:48 +00:00
|
|
|
#if !defined(M3D_NODUP) && (!defined(M3D_NOIMPORTER) || defined(M3D_ASCII) || defined(M3D_EXPORTER))
|
2019-10-29 17:42:10 +00:00
|
|
|
/* helper function to create safe strings */
|
2020-03-08 20:24:01 +00:00
|
|
|
char *_m3d_safestr(char *in, int morelines) {
|
2019-10-29 17:42:10 +00:00
|
|
|
char *out, *o, *i = in;
|
|
|
|
int l;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!in || !*in) {
|
|
|
|
out = (char *)M3D_MALLOC(1);
|
|
|
|
if (!out) return NULL;
|
|
|
|
out[0] = 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
for (o = in, l = 0; *o && ((morelines & 1) || (*o != '\r' && *o != '\n')) && l < 256; o++, l++)
|
|
|
|
;
|
|
|
|
out = o = (char *)M3D_MALLOC(l + 1);
|
|
|
|
if (!out) return NULL;
|
|
|
|
while (*i == ' ' || *i == '\t' || *i == '\r' || (morelines && *i == '\n'))
|
|
|
|
i++;
|
|
|
|
for (; *i && (morelines || (*i != '\r' && *i != '\n')); i++) {
|
|
|
|
if (*i == '\r') continue;
|
|
|
|
if (*i == '\n') {
|
|
|
|
if (morelines >= 3 && o > out && *(o - 1) == '\n') break;
|
|
|
|
if (i > in && *(i - 1) == '\n') continue;
|
|
|
|
if (morelines & 1) {
|
|
|
|
if (morelines == 1) *o++ = '\r';
|
2019-10-29 17:42:10 +00:00
|
|
|
*o++ = '\n';
|
|
|
|
} else
|
|
|
|
break;
|
2020-03-08 20:24:01 +00:00
|
|
|
} else if (*i == ' ' || *i == '\t') {
|
|
|
|
*o++ = morelines ? ' ' : '_';
|
2019-10-29 17:42:10 +00:00
|
|
|
} else
|
|
|
|
*o++ = !morelines && (*i == '/' || *i == '\\') ? '_' : *i;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (; o > out && (*(o - 1) == ' ' || *(o - 1) == '\t' || *(o - 1) == '\r' || *(o - 1) == '\n'); o--)
|
|
|
|
;
|
2019-10-29 17:42:10 +00:00
|
|
|
*o = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
out = (char *)M3D_REALLOC(out, (uintptr_t)o - (uintptr_t)out + 1);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#ifndef M3D_NOIMPORTER
|
|
|
|
/* helper function to load and decode/generate a texture */
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_INDEX _m3d_gettx(m3d_t *model, m3dread_t readfilecb, m3dfree_t freecb, char *fn) {
|
2019-11-18 17:31:53 +00:00
|
|
|
unsigned int i, len = 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
unsigned char *buff = NULL;
|
|
|
|
char *fn2;
|
2019-11-18 02:04:52 +00:00
|
|
|
#ifdef STBI__PNG_TYPE
|
2019-11-18 17:31:53 +00:00
|
|
|
unsigned int w, h;
|
2019-10-29 17:42:10 +00:00
|
|
|
stbi__context s;
|
|
|
|
stbi__result_info ri;
|
2019-11-18 02:04:52 +00:00
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2019-11-18 02:04:52 +00:00
|
|
|
/* do we have loaded this texture already? */
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numtexture; i++)
|
|
|
|
if (!strcmp(fn, model->texture[i].name)) return i;
|
2019-11-18 02:04:52 +00:00
|
|
|
/* see if it's inlined in the model */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->inlined) {
|
|
|
|
for (i = 0; i < model->numinlined; i++)
|
|
|
|
if (!strcmp(fn, model->inlined[i].name)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
buff = model->inlined[i].data;
|
|
|
|
len = model->inlined[i].length;
|
|
|
|
freecb = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* try to load from external source */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!buff && readfilecb) {
|
2019-12-10 05:30:41 +00:00
|
|
|
i = (unsigned int)strlen(fn);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (i < 5 || fn[i - 4] != '.') {
|
|
|
|
fn2 = (char *)M3D_MALLOC(i + 5);
|
|
|
|
if (!fn2) {
|
|
|
|
model->errcode = M3D_ERR_ALLOC;
|
|
|
|
return M3D_UNDEF;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
memcpy(fn2, fn, i);
|
2020-03-08 20:24:01 +00:00
|
|
|
memcpy(fn2 + i, ".png", 5);
|
2019-10-29 17:42:10 +00:00
|
|
|
buff = (*readfilecb)(fn2, &len);
|
|
|
|
M3D_FREE(fn2);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!buff) {
|
2019-10-29 17:42:10 +00:00
|
|
|
buff = (*readfilecb)(fn, &len);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!buff) return M3D_UNDEF;
|
2019-12-14 07:52:48 +00:00
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* add to textures array */
|
2019-10-29 17:42:10 +00:00
|
|
|
i = model->numtexture++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->texture = (m3dtx_t *)M3D_REALLOC(model->texture, model->numtexture * sizeof(m3dtx_t));
|
|
|
|
if (!model->texture) {
|
|
|
|
if (buff && freecb) (*freecb)(buff);
|
2019-11-18 02:04:52 +00:00
|
|
|
model->errcode = M3D_ERR_ALLOC;
|
2019-12-10 05:30:41 +00:00
|
|
|
return M3D_UNDEF;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
model->texture[i].name = fn;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->texture[i].w = model->texture[i].h = 0;
|
|
|
|
model->texture[i].d = NULL;
|
|
|
|
if (buff) {
|
|
|
|
if (buff[0] == 0x89 && buff[1] == 'P' && buff[2] == 'N' && buff[3] == 'G') {
|
2019-11-18 02:04:52 +00:00
|
|
|
#ifdef STBI__PNG_TYPE
|
2019-12-14 07:52:48 +00:00
|
|
|
s.read_from_callbacks = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
s.img_buffer = s.img_buffer_original = (unsigned char *)buff;
|
|
|
|
s.img_buffer_end = s.img_buffer_original_end = (unsigned char *)buff + len;
|
2019-12-14 07:52:48 +00:00
|
|
|
/* don't use model->texture[i].w directly, it's a uint16_t */
|
|
|
|
w = h = len = 0;
|
|
|
|
ri.bits_per_channel = 8;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->texture[i].d = (uint8_t *)stbi__png_load(&s, (int *)&w, (int *)&h, (int *)&len, 0, &ri);
|
|
|
|
model->texture[i].w = (uint16_t)w;
|
|
|
|
model->texture[i].h = (uint16_t)h;
|
2019-12-14 07:52:48 +00:00
|
|
|
model->texture[i].f = (uint8_t)len;
|
2019-11-18 02:04:52 +00:00
|
|
|
#endif
|
2019-12-14 07:52:48 +00:00
|
|
|
} else {
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifdef M3D_TX_INTERP
|
2020-03-08 20:24:01 +00:00
|
|
|
if ((model->errcode = M3D_TX_INTERP(fn, buff, len, &model->texture[i])) != M3D_SUCCESS) {
|
2019-12-14 07:52:48 +00:00
|
|
|
M3D_LOG("Unable to generate texture");
|
|
|
|
M3D_LOG(fn);
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
#else
|
2019-12-14 07:52:48 +00:00
|
|
|
M3D_LOG("Unimplemented interpreter");
|
|
|
|
M3D_LOG(fn);
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
2019-12-14 07:52:48 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (freecb) (*freecb)(buff);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!model->texture[i].d)
|
2019-10-29 17:42:10 +00:00
|
|
|
model->errcode = M3D_ERR_UNKIMG;
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* helper function to load and generate a procedural surface */
|
2020-03-08 20:24:01 +00:00
|
|
|
void _m3d_getpr(m3d_t *model, _unused m3dread_t readfilecb, _unused m3dfree_t freecb, _unused char *fn) {
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifdef M3D_PR_INTERP
|
|
|
|
unsigned int i, len = 0;
|
|
|
|
unsigned char *buff = readfilecb ? (*readfilecb)(fn, &len) : NULL;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!buff && model->inlined) {
|
|
|
|
for (i = 0; i < model->numinlined; i++)
|
|
|
|
if (!strcmp(fn, model->inlined[i].name)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
buff = model->inlined[i].data;
|
|
|
|
len = model->inlined[i].length;
|
|
|
|
freecb = NULL;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!buff || !len || (model->errcode = M3D_PR_INTERP(fn, buff, len, model)) != M3D_SUCCESS) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Unable to generate procedural surface");
|
|
|
|
M3D_LOG(fn);
|
|
|
|
model->errcode = M3D_ERR_UNKIMG;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (freecb && buff) (*freecb)(buff);
|
2019-10-29 17:42:10 +00:00
|
|
|
#else
|
2019-12-11 04:47:59 +00:00
|
|
|
(void)readfilecb;
|
|
|
|
(void)freecb;
|
|
|
|
(void)fn;
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Unimplemented interpreter");
|
|
|
|
M3D_LOG(fn);
|
|
|
|
model->errcode = M3D_ERR_UNIMPL;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* helpers to read indices from data stream */
|
2020-03-08 20:24:01 +00:00
|
|
|
#define M3D_GETSTR(x) \
|
|
|
|
do { \
|
|
|
|
offs = 0; \
|
|
|
|
data = _m3d_getidx(data, model->si_s, &offs); \
|
|
|
|
x = offs ? ((char *)model->raw + 16 + offs) : NULL; \
|
|
|
|
} while (0)
|
|
|
|
_inline static unsigned char *_m3d_getidx(unsigned char *data, char type, M3D_INDEX *idx) {
|
|
|
|
switch (type) {
|
|
|
|
case 1:
|
|
|
|
*idx = data[0] > 253 ? (int8_t)data[0] : data[0];
|
|
|
|
data++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
*idx = *((uint16_t *)data) > 65533 ? *((int16_t *)data) : *((uint16_t *)data);
|
|
|
|
data += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*idx = *((int32_t *)data);
|
|
|
|
data += 4;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef M3D_NOANIMATION
|
|
|
|
/* multiply 4 x 4 matrices. Do not use float *r[16] as argument, because some compilers misinterpret that as
|
|
|
|
* 16 pointers each pointing to a float, but we need a single pointer to 16 floats. */
|
2020-03-08 20:24:01 +00:00
|
|
|
void _m3d_mul(M3D_FLOAT *r, M3D_FLOAT *a, M3D_FLOAT *b) {
|
|
|
|
r[0] = b[0] * a[0] + b[4] * a[1] + b[8] * a[2] + b[12] * a[3];
|
|
|
|
r[1] = b[1] * a[0] + b[5] * a[1] + b[9] * a[2] + b[13] * a[3];
|
|
|
|
r[2] = b[2] * a[0] + b[6] * a[1] + b[10] * a[2] + b[14] * a[3];
|
|
|
|
r[3] = b[3] * a[0] + b[7] * a[1] + b[11] * a[2] + b[15] * a[3];
|
|
|
|
r[4] = b[0] * a[4] + b[4] * a[5] + b[8] * a[6] + b[12] * a[7];
|
|
|
|
r[5] = b[1] * a[4] + b[5] * a[5] + b[9] * a[6] + b[13] * a[7];
|
|
|
|
r[6] = b[2] * a[4] + b[6] * a[5] + b[10] * a[6] + b[14] * a[7];
|
|
|
|
r[7] = b[3] * a[4] + b[7] * a[5] + b[11] * a[6] + b[15] * a[7];
|
|
|
|
r[8] = b[0] * a[8] + b[4] * a[9] + b[8] * a[10] + b[12] * a[11];
|
|
|
|
r[9] = b[1] * a[8] + b[5] * a[9] + b[9] * a[10] + b[13] * a[11];
|
|
|
|
r[10] = b[2] * a[8] + b[6] * a[9] + b[10] * a[10] + b[14] * a[11];
|
|
|
|
r[11] = b[3] * a[8] + b[7] * a[9] + b[11] * a[10] + b[15] * a[11];
|
|
|
|
r[12] = b[0] * a[12] + b[4] * a[13] + b[8] * a[14] + b[12] * a[15];
|
|
|
|
r[13] = b[1] * a[12] + b[5] * a[13] + b[9] * a[14] + b[13] * a[15];
|
|
|
|
r[14] = b[2] * a[12] + b[6] * a[13] + b[10] * a[14] + b[14] * a[15];
|
|
|
|
r[15] = b[3] * a[12] + b[7] * a[13] + b[11] * a[14] + b[15] * a[15];
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
/* calculate 4 x 4 matrix inverse */
|
2020-03-08 20:24:01 +00:00
|
|
|
void _m3d_inv(M3D_FLOAT *m) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FLOAT r[16];
|
|
|
|
M3D_FLOAT det =
|
2020-03-08 20:24:01 +00:00
|
|
|
m[0] * m[5] * m[10] * m[15] - m[0] * m[5] * m[11] * m[14] + m[0] * m[6] * m[11] * m[13] - m[0] * m[6] * m[9] * m[15] + m[0] * m[7] * m[9] * m[14] - m[0] * m[7] * m[10] * m[13] - m[1] * m[6] * m[11] * m[12] + m[1] * m[6] * m[8] * m[15] - m[1] * m[7] * m[8] * m[14] + m[1] * m[7] * m[10] * m[12] - m[1] * m[4] * m[10] * m[15] + m[1] * m[4] * m[11] * m[14] + m[2] * m[7] * m[8] * m[13] - m[2] * m[7] * m[9] * m[12] + m[2] * m[4] * m[9] * m[15] - m[2] * m[4] * m[11] * m[13] + m[2] * m[5] * m[11] * m[12] - m[2] * m[5] * m[8] * m[15] - m[3] * m[4] * m[9] * m[14] + m[3] * m[4] * m[10] * m[13] - m[3] * m[5] * m[10] * m[12] + m[3] * m[5] * m[8] * m[14] - m[3] * m[6] * m[8] * m[13] + m[3] * m[6] * m[9] * m[12];
|
|
|
|
if (det == (M3D_FLOAT)0.0 || det == (M3D_FLOAT)-0.0)
|
|
|
|
det = (M3D_FLOAT)1.0;
|
|
|
|
else
|
|
|
|
det = (M3D_FLOAT)1.0 / det;
|
|
|
|
r[0] = det * (m[5] * (m[10] * m[15] - m[11] * m[14]) + m[6] * (m[11] * m[13] - m[9] * m[15]) + m[7] * (m[9] * m[14] - m[10] * m[13]));
|
|
|
|
r[1] = -det * (m[1] * (m[10] * m[15] - m[11] * m[14]) + m[2] * (m[11] * m[13] - m[9] * m[15]) + m[3] * (m[9] * m[14] - m[10] * m[13]));
|
|
|
|
r[2] = det * (m[1] * (m[6] * m[15] - m[7] * m[14]) + m[2] * (m[7] * m[13] - m[5] * m[15]) + m[3] * (m[5] * m[14] - m[6] * m[13]));
|
|
|
|
r[3] = -det * (m[1] * (m[6] * m[11] - m[7] * m[10]) + m[2] * (m[7] * m[9] - m[5] * m[11]) + m[3] * (m[5] * m[10] - m[6] * m[9]));
|
|
|
|
r[4] = -det * (m[4] * (m[10] * m[15] - m[11] * m[14]) + m[6] * (m[11] * m[12] - m[8] * m[15]) + m[7] * (m[8] * m[14] - m[10] * m[12]));
|
|
|
|
r[5] = det * (m[0] * (m[10] * m[15] - m[11] * m[14]) + m[2] * (m[11] * m[12] - m[8] * m[15]) + m[3] * (m[8] * m[14] - m[10] * m[12]));
|
|
|
|
r[6] = -det * (m[0] * (m[6] * m[15] - m[7] * m[14]) + m[2] * (m[7] * m[12] - m[4] * m[15]) + m[3] * (m[4] * m[14] - m[6] * m[12]));
|
|
|
|
r[7] = det * (m[0] * (m[6] * m[11] - m[7] * m[10]) + m[2] * (m[7] * m[8] - m[4] * m[11]) + m[3] * (m[4] * m[10] - m[6] * m[8]));
|
|
|
|
r[8] = det * (m[4] * (m[9] * m[15] - m[11] * m[13]) + m[5] * (m[11] * m[12] - m[8] * m[15]) + m[7] * (m[8] * m[13] - m[9] * m[12]));
|
|
|
|
r[9] = -det * (m[0] * (m[9] * m[15] - m[11] * m[13]) + m[1] * (m[11] * m[12] - m[8] * m[15]) + m[3] * (m[8] * m[13] - m[9] * m[12]));
|
|
|
|
r[10] = det * (m[0] * (m[5] * m[15] - m[7] * m[13]) + m[1] * (m[7] * m[12] - m[4] * m[15]) + m[3] * (m[4] * m[13] - m[5] * m[12]));
|
|
|
|
r[11] = -det * (m[0] * (m[5] * m[11] - m[7] * m[9]) + m[1] * (m[7] * m[8] - m[4] * m[11]) + m[3] * (m[4] * m[9] - m[5] * m[8]));
|
|
|
|
r[12] = -det * (m[4] * (m[9] * m[14] - m[10] * m[13]) + m[5] * (m[10] * m[12] - m[8] * m[14]) + m[6] * (m[8] * m[13] - m[9] * m[12]));
|
|
|
|
r[13] = det * (m[0] * (m[9] * m[14] - m[10] * m[13]) + m[1] * (m[10] * m[12] - m[8] * m[14]) + m[2] * (m[8] * m[13] - m[9] * m[12]));
|
|
|
|
r[14] = -det * (m[0] * (m[5] * m[14] - m[6] * m[13]) + m[1] * (m[6] * m[12] - m[4] * m[14]) + m[2] * (m[4] * m[13] - m[5] * m[12]));
|
|
|
|
r[15] = det * (m[0] * (m[5] * m[10] - m[6] * m[9]) + m[1] * (m[6] * m[8] - m[4] * m[10]) + m[2] * (m[4] * m[9] - m[5] * m[8]));
|
2019-10-29 17:42:10 +00:00
|
|
|
memcpy(m, &r, sizeof(r));
|
|
|
|
}
|
|
|
|
/* compose a coloumn major 4 x 4 matrix from vec3 position and vec4 orientation/rotation quaternion */
|
2020-03-08 20:24:01 +00:00
|
|
|
void _m3d_mat(M3D_FLOAT *r, m3dv_t *p, m3dv_t *q) {
|
|
|
|
if (q->x == (M3D_FLOAT)0.0 && q->y == (M3D_FLOAT)0.0 && q->z >= (M3D_FLOAT)0.7071065 && q->z <= (M3D_FLOAT)0.7071075 &&
|
|
|
|
q->w == (M3D_FLOAT)0.0) {
|
|
|
|
r[1] = r[2] = r[4] = r[6] = r[8] = r[9] = (M3D_FLOAT)0.0;
|
|
|
|
r[0] = r[5] = r[10] = (M3D_FLOAT)-1.0;
|
2019-10-29 17:42:10 +00:00
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
r[0] = 1 - 2 * (q->y * q->y + q->z * q->z);
|
|
|
|
if (r[0] > -M3D_EPSILON && r[0] < M3D_EPSILON) r[0] = (M3D_FLOAT)0.0;
|
|
|
|
r[1] = 2 * (q->x * q->y - q->z * q->w);
|
|
|
|
if (r[1] > -M3D_EPSILON && r[1] < M3D_EPSILON) r[1] = (M3D_FLOAT)0.0;
|
|
|
|
r[2] = 2 * (q->x * q->z + q->y * q->w);
|
|
|
|
if (r[2] > -M3D_EPSILON && r[2] < M3D_EPSILON) r[2] = (M3D_FLOAT)0.0;
|
|
|
|
r[4] = 2 * (q->x * q->y + q->z * q->w);
|
|
|
|
if (r[4] > -M3D_EPSILON && r[4] < M3D_EPSILON) r[4] = (M3D_FLOAT)0.0;
|
|
|
|
r[5] = 1 - 2 * (q->x * q->x + q->z * q->z);
|
|
|
|
if (r[5] > -M3D_EPSILON && r[5] < M3D_EPSILON) r[5] = (M3D_FLOAT)0.0;
|
|
|
|
r[6] = 2 * (q->y * q->z - q->x * q->w);
|
|
|
|
if (r[6] > -M3D_EPSILON && r[6] < M3D_EPSILON) r[6] = (M3D_FLOAT)0.0;
|
|
|
|
r[8] = 2 * (q->x * q->z - q->y * q->w);
|
|
|
|
if (r[8] > -M3D_EPSILON && r[8] < M3D_EPSILON) r[8] = (M3D_FLOAT)0.0;
|
|
|
|
r[9] = 2 * (q->y * q->z + q->x * q->w);
|
|
|
|
if (r[9] > -M3D_EPSILON && r[9] < M3D_EPSILON) r[9] = (M3D_FLOAT)0.0;
|
|
|
|
r[10] = 1 - 2 * (q->x * q->x + q->y * q->y);
|
|
|
|
if (r[10] > -M3D_EPSILON && r[10] < M3D_EPSILON) r[10] = (M3D_FLOAT)0.0;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
r[3] = p->x;
|
|
|
|
r[7] = p->y;
|
|
|
|
r[11] = p->z;
|
|
|
|
r[12] = 0;
|
|
|
|
r[13] = 0;
|
|
|
|
r[14] = 0;
|
|
|
|
r[15] = 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
2019-11-01 02:39:36 +00:00
|
|
|
#if !defined(M3D_NOANIMATION) || !defined(M3D_NONORMALS)
|
2019-12-11 04:47:59 +00:00
|
|
|
/* portable fast inverse square root calculation. returns 1/sqrt(x) */
|
2020-03-08 20:24:01 +00:00
|
|
|
static M3D_FLOAT _m3d_rsq(M3D_FLOAT x) {
|
2019-11-01 02:39:36 +00:00
|
|
|
#ifdef M3D_DOUBLE
|
2020-03-08 20:24:01 +00:00
|
|
|
return ((M3D_FLOAT)15.0 / (M3D_FLOAT)8.0) + ((M3D_FLOAT)-5.0 / (M3D_FLOAT)4.0) * x + ((M3D_FLOAT)3.0 / (M3D_FLOAT)8.0) * x * x;
|
2019-11-01 02:39:36 +00:00
|
|
|
#else
|
|
|
|
/* John Carmack's */
|
|
|
|
float x2 = x * 0.5f;
|
2020-03-08 20:24:01 +00:00
|
|
|
*((uint32_t *)&x) = (0x5f3759df - (*((uint32_t *)&x) >> 1));
|
2019-11-01 02:39:36 +00:00
|
|
|
return x * (1.5f - (x2 * x * x));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to decode a Model 3D into in-memory format
|
|
|
|
*/
|
2020-03-08 20:24:01 +00:00
|
|
|
m3d_t *m3d_load(unsigned char *data, m3dread_t readfilecb, m3dfree_t freecb, m3d_t *mtllib) {
|
2019-10-29 17:42:10 +00:00
|
|
|
unsigned char *end, *chunk, *buff, weights[8];
|
2019-11-18 02:04:52 +00:00
|
|
|
unsigned int i, j, k, l, n, am, len = 0, reclen, offs;
|
|
|
|
char *name, *lang;
|
|
|
|
float f;
|
2019-10-29 17:42:10 +00:00
|
|
|
m3d_t *model;
|
2019-11-01 02:39:36 +00:00
|
|
|
M3D_INDEX mi;
|
|
|
|
M3D_FLOAT w;
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dcd_t *cd;
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dtx_t *tx;
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dh_t *h;
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dm_t *m;
|
|
|
|
m3da_t *a;
|
|
|
|
m3di_t *t;
|
2019-11-18 02:04:52 +00:00
|
|
|
#ifndef M3D_NONORMALS
|
2019-11-25 04:03:10 +00:00
|
|
|
char neednorm = 0;
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dv_t *norm = NULL, *v0, *v1, *v2, va, vb;
|
|
|
|
#endif
|
|
|
|
#ifndef M3D_NOANIMATION
|
|
|
|
M3D_FLOAT r[16];
|
|
|
|
#endif
|
|
|
|
#if !defined(M3D_NOWEIGHTS) || !defined(M3D_NOANIMATION)
|
|
|
|
m3db_t *b;
|
|
|
|
#endif
|
|
|
|
#ifndef M3D_NOWEIGHTS
|
2019-10-29 17:42:10 +00:00
|
|
|
m3ds_t *sk;
|
2019-11-18 02:04:52 +00:00
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifdef M3D_ASCII
|
|
|
|
m3ds_t s;
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_INDEX bi[M3D_BONEMAXLEVEL + 1], level;
|
2019-10-29 17:42:10 +00:00
|
|
|
const char *ol;
|
2019-11-18 02:04:52 +00:00
|
|
|
char *ptr, *pe, *fn;
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
2019-11-25 04:03:10 +00:00
|
|
|
#ifdef M3D_PROFILING
|
|
|
|
struct timeval tv0, tv1, tvd;
|
|
|
|
gettimeofday(&tv0, NULL);
|
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!data || (!M3D_CHUNKMAGIC(data, '3', 'D', 'M', 'O')
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifdef M3D_ASCII
|
2020-03-08 20:24:01 +00:00
|
|
|
&& !M3D_CHUNKMAGIC(data, '3', 'd', 'm', 'o')
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
))
|
|
|
|
return NULL;
|
|
|
|
model = (m3d_t *)M3D_MALLOC(sizeof(m3d_t));
|
|
|
|
if (!model) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Out of memory");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memset(model, 0, sizeof(m3d_t));
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (mtllib) {
|
2019-10-29 17:42:10 +00:00
|
|
|
model->nummaterial = mtllib->nummaterial;
|
|
|
|
model->material = mtllib->material;
|
|
|
|
model->numtexture = mtllib->numtexture;
|
|
|
|
model->texture = mtllib->texture;
|
|
|
|
model->flags |= M3D_FLG_MTLLIB;
|
|
|
|
}
|
|
|
|
#ifdef M3D_ASCII
|
|
|
|
/* ASCII variant? */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (M3D_CHUNKMAGIC(data, '3', 'd', 'm', 'o')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
model->errcode = M3D_ERR_BADFILE;
|
|
|
|
model->flags |= M3D_FLG_FREESTR;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->raw = (m3dhdr_t *)data;
|
|
|
|
ptr = (char *)data;
|
2019-10-29 17:42:10 +00:00
|
|
|
ol = setlocale(LC_NUMERIC, NULL);
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
/* parse header. Don't use sscanf, that's incredibly slow */
|
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_findnl(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
model->scale = (float)strtod(ptr, NULL);
|
|
|
|
ptr = pe;
|
|
|
|
if (model->scale <= (M3D_FLOAT)0.0) model->scale = (M3D_FLOAT)1.0;
|
|
|
|
model->name = _m3d_safestr(ptr, 2);
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
if (!*ptr) goto asciiend;
|
|
|
|
model->license = _m3d_safestr(ptr, 2);
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
if (!*ptr) goto asciiend;
|
|
|
|
model->author = _m3d_safestr(ptr, 2);
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
if (!*ptr) goto asciiend;
|
|
|
|
if (*ptr != '\r' && *ptr != '\n')
|
2019-11-18 02:04:52 +00:00
|
|
|
model->desc = _m3d_safestr(ptr, 3);
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr) {
|
|
|
|
while (*ptr && *ptr != '\n')
|
|
|
|
ptr++;
|
|
|
|
ptr++;
|
|
|
|
if (*ptr == '\r') ptr++;
|
|
|
|
if (*ptr == '\n') break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the main chunk reader loop */
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr) {
|
|
|
|
while (*ptr && (*ptr == '\r' || *ptr == '\n'))
|
|
|
|
ptr++;
|
|
|
|
if (!*ptr || (ptr[0] == 'E' && ptr[1] == 'n' && ptr[2] == 'd')) break;
|
2019-10-29 17:42:10 +00:00
|
|
|
/* make sure there's at least one data row */
|
2020-03-08 20:24:01 +00:00
|
|
|
pe = ptr;
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
/* Preview chunk */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!memcmp(pe, "Preview", 7)) {
|
|
|
|
if (readfilecb) {
|
2019-11-18 02:04:52 +00:00
|
|
|
pe = _m3d_safestr(ptr, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe || !*pe) goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->preview.data = (*readfilecb)(pe, &model->preview.length);
|
|
|
|
M3D_FREE(pe);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n')
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* texture map chunk */
|
|
|
|
if (!memcmp(pe, "Textmap", 7)) {
|
|
|
|
if (model->tmap) {
|
|
|
|
M3D_LOG("More texture map chunks, should be unique");
|
|
|
|
goto asciiend;
|
|
|
|
}
|
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
2019-10-29 17:42:10 +00:00
|
|
|
i = model->numtmap++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->tmap = (m3dti_t *)M3D_REALLOC(model->tmap, model->numtmap * sizeof(m3dti_t));
|
|
|
|
if (!model->tmap) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_getfloat(ptr, &model->tmap[i].u);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
_m3d_getfloat(ptr, &model->tmap[i].v);
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* vertex chunk */
|
|
|
|
if (!memcmp(pe, "Vertex", 6)) {
|
|
|
|
if (model->vertex) {
|
|
|
|
M3D_LOG("More vertex chunks, should be unique");
|
|
|
|
goto asciiend;
|
|
|
|
}
|
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
2019-10-29 17:42:10 +00:00
|
|
|
i = model->numvertex++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->vertex = (m3dv_t *)M3D_REALLOC(model->vertex, model->numvertex * sizeof(m3dv_t));
|
|
|
|
if (!model->vertex) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(&model->vertex[i], 0, sizeof(m3dv_t));
|
2019-12-10 05:30:41 +00:00
|
|
|
model->vertex[i].skinid = M3D_UNDEF;
|
2019-10-29 17:42:10 +00:00
|
|
|
model->vertex[i].color = 0;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->vertex[i].w = (M3D_FLOAT)1.0;
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_getfloat(ptr, &model->vertex[i].x);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_getfloat(ptr, &model->vertex[i].y);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_getfloat(ptr, &model->vertex[i].z);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_getfloat(ptr, &model->vertex[i].w);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr) goto asciiend;
|
|
|
|
if (*ptr == '#') {
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_gethex(ptr, &model->vertex[i].color);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr) goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
/* parse skin */
|
|
|
|
memset(&s, 0, sizeof(m3ds_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0, w = (M3D_FLOAT)0.0; j < M3D_NUMBONE && *ptr && *ptr != '\r' && *ptr != '\n'; j++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_getint(ptr, &k);
|
|
|
|
s.boneid[j] = (M3D_INDEX)k;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (*ptr == ':') {
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr++;
|
|
|
|
ptr = _m3d_getfloat(ptr, &s.weight[j]);
|
2019-11-18 02:04:52 +00:00
|
|
|
w += s.weight[j];
|
2020-03-08 20:24:01 +00:00
|
|
|
} else if (!j)
|
2019-10-29 17:42:10 +00:00
|
|
|
s.weight[j] = (M3D_FLOAT)1.0;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr) goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (s.boneid[0] != M3D_UNDEF && s.weight[0] > (M3D_FLOAT)0.0) {
|
|
|
|
if (w != (M3D_FLOAT)1.0 && w != (M3D_FLOAT)0.0)
|
|
|
|
for (j = 0; j < M3D_NUMBONE && s.weight[j] > (M3D_FLOAT)0.0; j++)
|
2019-11-18 02:04:52 +00:00
|
|
|
s.weight[j] /= w;
|
2019-12-10 05:30:41 +00:00
|
|
|
k = M3D_NOTDEFINED;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->skin) {
|
|
|
|
for (j = 0; j < model->numskin; j++)
|
|
|
|
if (!memcmp(&model->skin[j], &s, sizeof(m3ds_t))) {
|
|
|
|
k = j;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k == M3D_NOTDEFINED) {
|
2019-11-18 02:04:52 +00:00
|
|
|
k = model->numskin++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->skin = (m3ds_t *)M3D_REALLOC(model->skin, model->numskin * sizeof(m3ds_t));
|
2019-11-18 02:04:52 +00:00
|
|
|
memcpy(&model->skin[k], &s, sizeof(m3ds_t));
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
model->vertex[i].skinid = (M3D_INDEX)k;
|
|
|
|
}
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* Skeleton, bone hierarchy */
|
|
|
|
if (!memcmp(pe, "Bones", 5)) {
|
|
|
|
if (model->bone) {
|
|
|
|
M3D_LOG("More bones chunks, should be unique");
|
|
|
|
goto asciiend;
|
|
|
|
}
|
2019-12-10 05:30:41 +00:00
|
|
|
bi[0] = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
2019-10-29 17:42:10 +00:00
|
|
|
i = model->numbone++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->bone = (m3db_t *)M3D_REALLOC(model->bone, model->numbone * sizeof(m3db_t));
|
|
|
|
if (!model->bone) goto memerr;
|
|
|
|
for (level = 0; *ptr == '/'; ptr++, level++)
|
|
|
|
;
|
|
|
|
if (level > M3D_BONEMAXLEVEL || !*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
|
|
|
bi[level + 1] = i;
|
2019-10-29 17:42:10 +00:00
|
|
|
model->bone[i].numweight = 0;
|
|
|
|
model->bone[i].weight = NULL;
|
|
|
|
model->bone[i].parent = bi[level];
|
|
|
|
ptr = _m3d_getint(ptr, &k);
|
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
model->bone[i].pos = (M3D_INDEX)k;
|
|
|
|
ptr = _m3d_getint(ptr, &k);
|
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
model->bone[i].ori = (M3D_INDEX)k;
|
2019-12-10 05:30:41 +00:00
|
|
|
model->vertex[k].skinid = M3D_INDEXMAX;
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_safestr(ptr, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe || !*pe) goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
model->bone[i].name = pe;
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* material chunk */
|
|
|
|
if (!memcmp(pe, "Material", 8)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_findarg(pe);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*pe || *pe == '\r' || *pe == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_safestr(pe, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe || !*pe) goto asciiend;
|
|
|
|
for (i = 0; i < model->nummaterial; i++)
|
|
|
|
if (!strcmp(pe, model->material[i].name)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Multiple definitions for material");
|
|
|
|
M3D_LOG(pe);
|
|
|
|
M3D_FREE(pe);
|
|
|
|
pe = NULL;
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n')
|
|
|
|
ptr = _m3d_findnl(ptr);
|
2019-10-29 17:42:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe) continue;
|
2019-10-29 17:42:10 +00:00
|
|
|
i = model->nummaterial++;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->flags & M3D_FLG_MTLLIB) {
|
2019-10-29 17:42:10 +00:00
|
|
|
m = model->material;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->material = (m3dm_t *)M3D_MALLOC(model->nummaterial * sizeof(m3dm_t));
|
|
|
|
if (!model->material) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
memcpy(model->material, m, (model->nummaterial - 1) * sizeof(m3dm_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->texture) {
|
2019-10-29 17:42:10 +00:00
|
|
|
tx = model->texture;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->texture = (m3dtx_t *)M3D_MALLOC(model->numtexture * sizeof(m3dtx_t));
|
|
|
|
if (!model->texture) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
memcpy(model->texture, tx, model->numtexture * sizeof(m3dm_t));
|
|
|
|
}
|
|
|
|
model->flags &= ~M3D_FLG_MTLLIB;
|
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
model->material = (m3dm_t *)M3D_REALLOC(model->material, model->nummaterial * sizeof(m3dm_t));
|
|
|
|
if (!model->material) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
m = &model->material[i];
|
|
|
|
m->name = pe;
|
|
|
|
m->numprop = 0;
|
|
|
|
m->prop = NULL;
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
2019-10-29 17:42:10 +00:00
|
|
|
k = n = 256;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (*ptr == 'm' && *(ptr + 1) == 'a' && *(ptr + 2) == 'p' && *(ptr + 3) == '_') {
|
2019-10-29 17:42:10 +00:00
|
|
|
k = m3dpf_map;
|
|
|
|
ptr += 4;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); j++)
|
|
|
|
if (!memcmp(ptr, m3d_propertytypes[j].key, strlen(m3d_propertytypes[j].key))) {
|
2019-10-29 17:42:10 +00:00
|
|
|
n = m3d_propertytypes[j].id;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k != m3dpf_map) k = m3d_propertytypes[j].format;
|
2019-10-29 17:42:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (n != 256 && k != 256) {
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
j = m->numprop++;
|
2020-03-08 20:24:01 +00:00
|
|
|
m->prop = (m3dp_t *)M3D_REALLOC(m->prop, m->numprop * sizeof(m3dp_t));
|
|
|
|
if (!m->prop) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
m->prop[j].type = n + (k == m3dpf_map && n < 128 ? 128 : 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (k) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case m3dpf_color: ptr = _m3d_gethex(ptr, &m->prop[j].value.color); break;
|
|
|
|
case m3dpf_uint8:
|
|
|
|
case m3dpf_uint16:
|
|
|
|
case m3dpf_uint32: ptr = _m3d_getint(ptr, &m->prop[j].value.num); break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case m3dpf_float: ptr = _m3d_getfloat(ptr, &m->prop[j].value.fnum); break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case m3dpf_map:
|
|
|
|
pe = _m3d_safestr(ptr, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe || !*pe) goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
m->prop[j].value.textureid = _m3d_gettx(model, readfilecb, freecb, pe);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->errcode == M3D_ERR_ALLOC) {
|
|
|
|
M3D_FREE(pe);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-12-14 07:52:48 +00:00
|
|
|
/* this error code only returned if readfilecb was specified */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (m->prop[j].value.textureid == M3D_UNDEF) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Texture not found");
|
|
|
|
M3D_LOG(pe);
|
|
|
|
m->numprop--;
|
|
|
|
}
|
|
|
|
M3D_FREE(pe);
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
M3D_LOG("Unknown material property in");
|
|
|
|
M3D_LOG(m->name);
|
|
|
|
model->errcode = M3D_ERR_UNKPROP;
|
|
|
|
}
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!m->numprop) model->nummaterial--;
|
2019-10-29 17:42:10 +00:00
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* procedural */
|
|
|
|
if (!memcmp(pe, "Procedural", 10)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_safestr(ptr, 0);
|
|
|
|
_m3d_getpr(model, readfilecb, freecb, pe);
|
|
|
|
M3D_FREE(pe);
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n')
|
|
|
|
ptr = _m3d_findnl(ptr);
|
2019-10-29 17:42:10 +00:00
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* mesh */
|
|
|
|
if (!memcmp(pe, "Mesh", 4)) {
|
2019-12-10 05:30:41 +00:00
|
|
|
mi = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
|
|
|
if (*ptr == 'u') {
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr) goto asciiend;
|
2019-12-10 05:30:41 +00:00
|
|
|
mi = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (*ptr != '\r' && *ptr != '\n') {
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_safestr(ptr, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe || !*pe) goto asciiend;
|
|
|
|
for (j = 0; j < model->nummaterial; j++)
|
|
|
|
if (!strcmp(pe, model->material[j].name)) {
|
|
|
|
mi = (M3D_INDEX)j;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (mi == M3D_UNDEF && !(model->flags & M3D_FLG_MTLLIB)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
mi = model->nummaterial++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->material = (m3dm_t *)M3D_REALLOC(model->material, model->nummaterial * sizeof(m3dm_t));
|
|
|
|
if (!model->material) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->material[mi].name = pe;
|
|
|
|
model->material[mi].numprop = 1;
|
|
|
|
model->material[mi].prop = NULL;
|
|
|
|
} else
|
|
|
|
M3D_FREE(pe);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i = model->numface++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->face = (m3df_t *)M3D_REALLOC(model->face, model->numface * sizeof(m3df_t));
|
|
|
|
if (!model->face) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
memset(&model->face[i], 255, sizeof(m3df_t)); /* set all index to -1 by default */
|
|
|
|
model->face[i].materialid = mi;
|
|
|
|
/* hardcoded triangles. */
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < 3; j++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
/* vertex */
|
|
|
|
ptr = _m3d_getint(ptr, &k);
|
|
|
|
model->face[i].vertex[j] = (M3D_INDEX)k;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr) goto asciiend;
|
|
|
|
if (*ptr == '/') {
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr++;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (*ptr != '/') {
|
2019-10-29 17:42:10 +00:00
|
|
|
/* texcoord */
|
|
|
|
ptr = _m3d_getint(ptr, &k);
|
|
|
|
model->face[i].texcoord[j] = (M3D_INDEX)k;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr) goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (*ptr == '/') {
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr++;
|
|
|
|
/* normal */
|
|
|
|
ptr = _m3d_getint(ptr, &k);
|
|
|
|
model->face[i].normal[j] = (M3D_INDEX)k;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr) goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-25 04:03:10 +00:00
|
|
|
#ifndef M3D_NONORMALS
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->face[i].normal[j] == M3D_UNDEF) neednorm = 1;
|
2019-11-25 04:03:10 +00:00
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_findarg(ptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* mathematical shape */
|
|
|
|
if (!memcmp(pe, "Shape", 5)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
pe = _m3d_findarg(pe);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*pe || *pe == '\r' || *pe == '\n') goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
pe = _m3d_safestr(pe, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe || !*pe) goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
i = model->numshape++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->shape = (m3dh_t *)M3D_REALLOC(model->shape, model->numshape * sizeof(m3ds_t));
|
|
|
|
if (!model->shape) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
h = &model->shape[i];
|
|
|
|
h->name = pe;
|
2019-12-10 05:30:41 +00:00
|
|
|
h->group = M3D_UNDEF;
|
2019-11-18 02:04:52 +00:00
|
|
|
h->numcmd = 0;
|
|
|
|
h->cmd = NULL;
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
|
|
|
if (!memcmp(ptr, "group", 5)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr = _m3d_findarg(ptr);
|
|
|
|
ptr = _m3d_getint(ptr, &h->group);
|
|
|
|
ptr = _m3d_findnl(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (h->group != M3D_UNDEF && h->group >= model->numbone) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Unknown bone id as shape group in shape");
|
|
|
|
M3D_LOG(pe);
|
2019-12-10 05:30:41 +00:00
|
|
|
h->group = M3D_UNDEF;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->errcode = M3D_ERR_SHPE;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (cd = NULL, k = 0; k < (unsigned int)(sizeof(m3d_commandtypes) / sizeof(m3d_commandtypes[0])); k++) {
|
2019-12-10 05:30:41 +00:00
|
|
|
j = (unsigned int)strlen(m3d_commandtypes[k].key);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!memcmp(ptr, m3d_commandtypes[k].key, j) && (ptr[j] == ' ' || ptr[j] == '\r' || ptr[j] == '\n')) {
|
|
|
|
cd = &m3d_commandtypes[k];
|
|
|
|
break;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (cd) {
|
2019-11-18 02:04:52 +00:00
|
|
|
j = h->numcmd++;
|
2020-03-08 20:24:01 +00:00
|
|
|
h->cmd = (m3dc_t *)M3D_REALLOC(h->cmd, h->numcmd * sizeof(m3dc_t));
|
|
|
|
if (!h->cmd) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
h->cmd[j].type = k;
|
2020-03-08 20:24:01 +00:00
|
|
|
h->cmd[j].arg = (uint32_t *)M3D_MALLOC(cd->p * sizeof(uint32_t));
|
|
|
|
if (!h->cmd[j].arg) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(h->cmd[j].arg, 0, cd->p * sizeof(uint32_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (k = n = 0, l = cd->p; k < l; k++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr) goto asciiend;
|
|
|
|
if (*ptr == '[') {
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr = _m3d_findarg(ptr + 1);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr) goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (*ptr == ']' || *ptr == '\r' || *ptr == '\n') break;
|
|
|
|
switch (cd->a[((k - n) % (cd->p - n)) + n]) {
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_mi_t:
|
2019-12-10 05:30:41 +00:00
|
|
|
mi = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (*ptr != '\r' && *ptr != '\n') {
|
2019-11-18 02:04:52 +00:00
|
|
|
pe = _m3d_safestr(ptr, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe || !*pe) goto asciiend;
|
|
|
|
for (n = 0; n < model->nummaterial; n++)
|
|
|
|
if (!strcmp(pe, model->material[n].name)) {
|
|
|
|
mi = (M3D_INDEX)n;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (mi == M3D_UNDEF && !(model->flags & M3D_FLG_MTLLIB)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
mi = model->nummaterial++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->material = (m3dm_t *)M3D_REALLOC(model->material,
|
|
|
|
model->nummaterial * sizeof(m3dm_t));
|
|
|
|
if (!model->material) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->material[mi].name = pe;
|
|
|
|
model->material[mi].numprop = 1;
|
|
|
|
model->material[mi].prop = NULL;
|
|
|
|
} else
|
|
|
|
M3D_FREE(pe);
|
|
|
|
}
|
|
|
|
h->cmd[j].arg[k] = mi;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_vc_t:
|
|
|
|
_m3d_getfloat(ptr, &w);
|
2020-03-08 20:24:01 +00:00
|
|
|
h->cmd[j].arg[k] = *((uint32_t *)&w);
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_va_t:
|
|
|
|
ptr = _m3d_getint(ptr, &h->cmd[j].arg[k]);
|
2020-03-08 20:24:01 +00:00
|
|
|
n = k + 1;
|
|
|
|
l += (h->cmd[j].arg[k] - 1) * (cd->p - k - 1);
|
|
|
|
h->cmd[j].arg = (uint32_t *)M3D_REALLOC(h->cmd[j].arg, l * sizeof(uint32_t));
|
|
|
|
if (!h->cmd[j].arg) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(&h->cmd[j].arg[k + 1], 0, (l - k - 1) * sizeof(uint32_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_qi_t:
|
|
|
|
ptr = _m3d_getint(ptr, &h->cmd[j].arg[k]);
|
2019-12-10 05:30:41 +00:00
|
|
|
model->vertex[h->cmd[i].arg[k]].skinid = M3D_INDEXMAX;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
default:
|
|
|
|
ptr = _m3d_getint(ptr, &h->cmd[j].arg[k]);
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
M3D_LOG("Unknown shape command in");
|
|
|
|
M3D_LOG(h->name);
|
|
|
|
model->errcode = M3D_ERR_UNKCMD;
|
|
|
|
}
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!h->numcmd) model->numshape--;
|
2019-11-18 02:04:52 +00:00
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* annotation labels */
|
|
|
|
if (!memcmp(pe, "Labels", 6)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
pe = _m3d_findarg(pe);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*pe) goto asciiend;
|
|
|
|
if (*pe == '\r' || *pe == '\n')
|
|
|
|
pe = NULL;
|
|
|
|
else
|
|
|
|
pe = _m3d_safestr(pe, 0);
|
|
|
|
k = 0;
|
|
|
|
fn = NULL;
|
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
|
|
|
if (*ptr == 'c') {
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*pe || *pe == '\r' || *pe == '\n') goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr = _m3d_gethex(ptr, &k);
|
2020-03-08 20:24:01 +00:00
|
|
|
} else if (*ptr == 'l') {
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*pe || *pe == '\r' || *pe == '\n') goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
fn = _m3d_safestr(ptr, 2);
|
|
|
|
} else {
|
|
|
|
i = model->numlabel++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->label = (m3dl_t *)M3D_REALLOC(model->label, model->numlabel * sizeof(m3dl_t));
|
|
|
|
if (!model->label) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->label[i].name = pe;
|
|
|
|
model->label[i].lang = fn;
|
|
|
|
model->label[i].color = k;
|
|
|
|
ptr = _m3d_getint(ptr, &j);
|
|
|
|
model->label[i].vertexid = (M3D_INDEX)j;
|
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*pe || *pe == '\r' || *pe == '\n') goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->label[i].text = _m3d_safestr(ptr, 2);
|
|
|
|
}
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* action */
|
|
|
|
if (!memcmp(pe, "Action", 6)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_findarg(pe);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*pe || *pe == '\r' || *pe == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_getint(pe, &k);
|
|
|
|
pe = _m3d_findarg(pe);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*pe || *pe == '\r' || *pe == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_safestr(pe, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe || !*pe) goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
i = model->numaction++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->action = (m3da_t *)M3D_REALLOC(model->action, model->numaction * sizeof(m3da_t));
|
|
|
|
if (!model->action) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
a = &model->action[i];
|
|
|
|
a->name = pe;
|
|
|
|
a->durationmsec = k;
|
|
|
|
/* skip the first frame marker as there's always at least one frame */
|
|
|
|
a->numframe = 1;
|
2020-03-08 20:24:01 +00:00
|
|
|
a->frame = (m3dfr_t *)M3D_MALLOC(sizeof(m3dfr_t));
|
|
|
|
if (!a->frame) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
a->frame[0].msec = 0;
|
|
|
|
a->frame[0].numtransform = 0;
|
|
|
|
a->frame[0].transform = NULL;
|
|
|
|
i = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (*ptr == 'f')
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_findnl(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
|
|
|
if (*ptr == 'f') {
|
2019-10-29 17:42:10 +00:00
|
|
|
i = a->numframe++;
|
2020-03-08 20:24:01 +00:00
|
|
|
a->frame = (m3dfr_t *)M3D_REALLOC(a->frame, a->numframe * sizeof(m3dfr_t));
|
|
|
|
if (!a->frame) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_findarg(ptr);
|
|
|
|
ptr = _m3d_getint(ptr, &a->frame[i].msec);
|
|
|
|
a->frame[i].numtransform = 0;
|
|
|
|
a->frame[i].transform = NULL;
|
|
|
|
} else {
|
|
|
|
j = a->frame[i].numtransform++;
|
2020-03-08 20:24:01 +00:00
|
|
|
a->frame[i].transform = (m3dtr_t *)M3D_REALLOC(a->frame[i].transform,
|
|
|
|
a->frame[i].numtransform * sizeof(m3dtr_t));
|
|
|
|
if (!a->frame[i].transform) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_getint(ptr, &k);
|
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
a->frame[i].transform[j].boneid = (M3D_INDEX)k;
|
|
|
|
ptr = _m3d_getint(ptr, &k);
|
|
|
|
ptr = _m3d_findarg(ptr);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
a->frame[i].transform[j].pos = (M3D_INDEX)k;
|
|
|
|
ptr = _m3d_getint(ptr, &k);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*ptr || *ptr == '\r' || *ptr == '\n') goto asciiend;
|
2019-10-29 17:42:10 +00:00
|
|
|
a->frame[i].transform[j].ori = (M3D_INDEX)k;
|
2019-12-10 05:30:41 +00:00
|
|
|
model->vertex[k].skinid = M3D_INDEXMAX;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* inlined assets chunk */
|
|
|
|
if (!memcmp(pe, "Assets", 6)) {
|
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
|
|
|
if (readfilecb) {
|
2019-11-18 02:04:52 +00:00
|
|
|
pe = _m3d_safestr(ptr, 2);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!pe || !*pe) goto asciiend;
|
2019-11-18 02:04:52 +00:00
|
|
|
i = model->numinlined++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->inlined = (m3di_t *)M3D_REALLOC(model->inlined, model->numinlined * sizeof(m3di_t));
|
|
|
|
if (!model->inlined) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
t = &model->inlined[i];
|
|
|
|
model->inlined[i].data = (*readfilecb)(pe, &model->inlined[i].length);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->inlined[i].data) {
|
2019-11-18 02:04:52 +00:00
|
|
|
fn = strrchr(pe, '.');
|
2020-03-08 20:24:01 +00:00
|
|
|
if (fn && (fn[1] == 'p' || fn[1] == 'P') && (fn[2] == 'n' || fn[2] == 'N') &&
|
|
|
|
(fn[3] == 'g' || fn[3] == 'G')) *fn = 0;
|
2019-11-18 02:04:52 +00:00
|
|
|
fn = strrchr(pe, '/');
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!fn) fn = strrchr(pe, '\\');
|
|
|
|
if (!fn)
|
|
|
|
fn = pe;
|
|
|
|
else
|
|
|
|
fn++;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->inlined[i].name = _m3d_safestr(fn, 0);
|
|
|
|
} else
|
|
|
|
model->numinlined--;
|
|
|
|
M3D_FREE(pe);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
ptr = _m3d_findnl(ptr);
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* extra chunks */
|
|
|
|
if (!memcmp(pe, "Extra", 5)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
pe = _m3d_findarg(pe);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!*pe || *pe == '\r' || *pe == '\n') goto asciiend;
|
|
|
|
buff = (unsigned char *)_m3d_findnl(ptr);
|
2019-11-25 04:03:10 +00:00
|
|
|
k = ((uint32_t)((uintptr_t)buff - (uintptr_t)ptr) / 3) + 1;
|
2019-11-18 02:04:52 +00:00
|
|
|
i = model->numextra++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->extra = (m3dchunk_t **)M3D_REALLOC(model->extra, model->numextra * sizeof(m3dchunk_t *));
|
|
|
|
if (!model->extra) goto memerr;
|
|
|
|
model->extra[i] = (m3dchunk_t *)M3D_MALLOC(k + sizeof(m3dchunk_t));
|
|
|
|
if (!model->extra[i]) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memcpy(&model->extra[i]->magic, pe, 4);
|
|
|
|
model->extra[i]->length = sizeof(m3dchunk_t);
|
2020-03-08 20:24:01 +00:00
|
|
|
pe = (char *)model->extra[i] + sizeof(m3dchunk_t);
|
|
|
|
while (*ptr && *ptr != '\r' && *ptr != '\n') {
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr = _m3d_gethex(ptr, &k);
|
|
|
|
*pe++ = (uint8_t)k;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->extra[i]->length++;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
goto asciiend;
|
|
|
|
}
|
|
|
|
model->errcode = M3D_SUCCESS;
|
2020-03-08 20:24:01 +00:00
|
|
|
asciiend:
|
2019-10-29 17:42:10 +00:00
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto postprocess;
|
|
|
|
}
|
|
|
|
#endif
|
2019-11-25 04:03:10 +00:00
|
|
|
/* Binary variant */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!M3D_CHUNKMAGIC(data + 8, 'H', 'E', 'A', 'D')) {
|
|
|
|
buff = (unsigned char *)stbi_zlib_decode_malloc_guesssize_headerflag((const char *)data + 8, ((m3dchunk_t *)data)->length - 8,
|
|
|
|
4096, (int *)&len, 1);
|
|
|
|
if (!buff || !len || !M3D_CHUNKMAGIC(buff, 'H', 'E', 'A', 'D')) {
|
|
|
|
if (buff) M3D_FREE(buff);
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(model);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
buff = (unsigned char *)M3D_REALLOC(buff, len);
|
2019-10-29 17:42:10 +00:00
|
|
|
model->flags |= M3D_FLG_FREERAW; /* mark that we have to free the raw buffer */
|
|
|
|
data = buff;
|
2019-11-25 04:03:10 +00:00
|
|
|
#ifdef M3D_PROFILING
|
|
|
|
gettimeofday(&tv1, NULL);
|
|
|
|
tvd.tv_sec = tv1.tv_sec - tv0.tv_sec;
|
|
|
|
tvd.tv_usec = tv1.tv_usec - tv0.tv_usec;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (tvd.tv_usec < 0) {
|
|
|
|
tvd.tv_sec--;
|
|
|
|
tvd.tv_usec += 1000000L;
|
|
|
|
}
|
2019-11-25 04:03:10 +00:00
|
|
|
printf(" Deflate model %ld.%06ld sec\n", tvd.tv_sec, tvd.tv_usec);
|
|
|
|
memcpy(&tv0, &tv1, sizeof(struct timeval));
|
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
len = ((m3dhdr_t *)data)->length;
|
2019-10-29 17:42:10 +00:00
|
|
|
data += 8;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
model->raw = (m3dhdr_t *)data;
|
2019-10-29 17:42:10 +00:00
|
|
|
end = data + len;
|
|
|
|
|
|
|
|
/* parse header */
|
|
|
|
data += sizeof(m3dhdr_t);
|
|
|
|
M3D_LOG(data);
|
2020-03-08 20:24:01 +00:00
|
|
|
model->name = (char *)data;
|
|
|
|
for (; data < end && *data; data++) {
|
|
|
|
};
|
|
|
|
data++;
|
|
|
|
model->license = (char *)data;
|
|
|
|
for (; data < end && *data; data++) {
|
|
|
|
};
|
|
|
|
data++;
|
|
|
|
model->author = (char *)data;
|
|
|
|
for (; data < end && *data; data++) {
|
|
|
|
};
|
|
|
|
data++;
|
|
|
|
model->desc = (char *)data;
|
|
|
|
chunk = (unsigned char *)model->raw + model->raw->length;
|
2019-10-29 17:42:10 +00:00
|
|
|
model->scale = (M3D_FLOAT)model->raw->scale;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->scale <= (M3D_FLOAT)0.0) model->scale = (M3D_FLOAT)1.0;
|
|
|
|
model->vc_s = 1 << ((model->raw->types >> 0) & 3); /* vertex coordinate size */
|
|
|
|
model->vi_s = 1 << ((model->raw->types >> 2) & 3); /* vertex index size */
|
|
|
|
model->si_s = 1 << ((model->raw->types >> 4) & 3); /* string offset size */
|
|
|
|
model->ci_s = 1 << ((model->raw->types >> 6) & 3); /* color index size */
|
|
|
|
model->ti_s = 1 << ((model->raw->types >> 8) & 3); /* tmap index size */
|
|
|
|
model->bi_s = 1 << ((model->raw->types >> 10) & 3); /* bone index size */
|
|
|
|
model->nb_s = 1 << ((model->raw->types >> 12) & 3); /* number of bones per vertex */
|
|
|
|
model->sk_s = 1 << ((model->raw->types >> 14) & 3); /* skin index size */
|
|
|
|
model->fc_s = 1 << ((model->raw->types >> 16) & 3); /* frame counter size */
|
|
|
|
model->hi_s = 1 << ((model->raw->types >> 18) & 3); /* shape index size */
|
|
|
|
model->fi_s = 1 << ((model->raw->types >> 20) & 3); /* face index size */
|
|
|
|
if (model->ci_s == 8) model->ci_s = 0; /* optional indices */
|
|
|
|
if (model->ti_s == 8) model->ti_s = 0;
|
|
|
|
if (model->bi_s == 8) model->bi_s = 0;
|
|
|
|
if (model->sk_s == 8) model->sk_s = 0;
|
|
|
|
if (model->fc_s == 8) model->fc_s = 0;
|
|
|
|
if (model->hi_s == 8) model->hi_s = 0;
|
|
|
|
if (model->fi_s == 8) model->fi_s = 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/* variable limit checks */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (sizeof(M3D_FLOAT) == 4 && model->vc_s > 4) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Double precision coordinates not supported, truncating to float...");
|
|
|
|
model->errcode = M3D_ERR_TRUNC;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (sizeof(M3D_INDEX) == 2 && (model->vi_s > 2 || model->si_s > 2 || model->ci_s > 2 || model->ti_s > 2 ||
|
|
|
|
model->bi_s > 2 || model->sk_s > 2 || model->fc_s > 2 || model->hi_s > 2 || model->fi_s > 2)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("32 bit indices not supported, unable to load model");
|
|
|
|
M3D_FREE(model);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->vi_s > 4 || model->si_s > 4) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Invalid index size, unable to load model");
|
|
|
|
M3D_FREE(model);
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->nb_s > M3D_NUMBONE) {
|
2019-11-25 04:03:10 +00:00
|
|
|
M3D_LOG("Model has more bones per vertex than what importer was configured to support");
|
2019-10-29 17:42:10 +00:00
|
|
|
model->errcode = M3D_ERR_TRUNC;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look for inlined assets in advance, material and procedural chunks may need them */
|
|
|
|
buff = chunk;
|
2020-03-08 20:24:01 +00:00
|
|
|
while (buff < end && !M3D_CHUNKMAGIC(buff, 'O', 'M', 'D', '3')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
data = buff;
|
2020-03-08 20:24:01 +00:00
|
|
|
len = ((m3dchunk_t *)data)->length;
|
2019-12-13 05:38:49 +00:00
|
|
|
buff += len;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (len < sizeof(m3dchunk_t) || buff >= end) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Invalid chunk size");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= sizeof(m3dchunk_t) + model->si_s;
|
|
|
|
|
|
|
|
/* inlined assets */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (M3D_CHUNKMAGIC(data, 'A', 'S', 'E', 'T') && len > 0) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Inlined asset");
|
|
|
|
i = model->numinlined++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->inlined = (m3di_t *)M3D_REALLOC(model->inlined, model->numinlined * sizeof(m3di_t));
|
|
|
|
if (!model->inlined) {
|
|
|
|
memerr:
|
|
|
|
M3D_LOG("Out of memory");
|
2019-10-29 17:42:10 +00:00
|
|
|
model->errcode = M3D_ERR_ALLOC;
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
data += sizeof(m3dchunk_t);
|
|
|
|
t = &model->inlined[i];
|
|
|
|
M3D_GETSTR(t->name);
|
|
|
|
M3D_LOG(t->name);
|
2020-03-08 20:24:01 +00:00
|
|
|
t->data = (uint8_t *)data;
|
2019-10-29 17:42:10 +00:00
|
|
|
t->length = len;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* parse chunks */
|
2020-03-08 20:24:01 +00:00
|
|
|
while (chunk < end && !M3D_CHUNKMAGIC(chunk, 'O', 'M', 'D', '3')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
data = chunk;
|
2020-03-08 20:24:01 +00:00
|
|
|
len = ((m3dchunk_t *)chunk)->length;
|
2019-12-13 05:38:49 +00:00
|
|
|
chunk += len;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (len < sizeof(m3dchunk_t) || chunk >= end) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Invalid chunk size");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
len -= sizeof(m3dchunk_t);
|
|
|
|
|
2019-11-18 02:04:52 +00:00
|
|
|
/* preview chunk */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (M3D_CHUNKMAGIC(data, 'P', 'R', 'V', 'W') && len > 0) {
|
2019-11-18 02:04:52 +00:00
|
|
|
model->preview.length = len;
|
|
|
|
model->preview.data = data + sizeof(m3dchunk_t);
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* color map */
|
|
|
|
if (M3D_CHUNKMAGIC(data, 'C', 'M', 'A', 'P')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Color map");
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->cmap) {
|
|
|
|
M3D_LOG("More color map chunks, should be unique");
|
|
|
|
model->errcode = M3D_ERR_CMAP;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!model->ci_s) {
|
|
|
|
M3D_LOG("Color map chunk, shouldn't be any");
|
|
|
|
model->errcode = M3D_ERR_CMAP;
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
model->numcmap = len / sizeof(uint32_t);
|
2020-03-08 20:24:01 +00:00
|
|
|
model->cmap = (uint32_t *)(data + sizeof(m3dchunk_t));
|
2019-10-29 17:42:10 +00:00
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* texture map */
|
|
|
|
if (M3D_CHUNKMAGIC(data, 'T', 'M', 'A', 'P')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Texture map");
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->tmap) {
|
|
|
|
M3D_LOG("More texture map chunks, should be unique");
|
|
|
|
model->errcode = M3D_ERR_TMAP;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!model->ti_s) {
|
|
|
|
M3D_LOG("Texture map chunk, shouldn't be any");
|
|
|
|
model->errcode = M3D_ERR_TMAP;
|
|
|
|
continue;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
reclen = model->vc_s + model->vc_s;
|
|
|
|
model->numtmap = len / reclen;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->tmap = (m3dti_t *)M3D_MALLOC(model->numtmap * sizeof(m3dti_t));
|
|
|
|
if (!model->tmap) goto memerr;
|
|
|
|
for (i = 0, data += sizeof(m3dchunk_t); data < chunk; i++) {
|
|
|
|
switch (model->vc_s) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case 1:
|
2019-12-11 04:47:59 +00:00
|
|
|
model->tmap[i].u = (M3D_FLOAT)(data[0]) / (M3D_FLOAT)255.0;
|
|
|
|
model->tmap[i].v = (M3D_FLOAT)(data[1]) / (M3D_FLOAT)255.0;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 2:
|
2020-03-08 20:24:01 +00:00
|
|
|
model->tmap[i].u = (M3D_FLOAT)(*((int16_t *)(data + 0))) / (M3D_FLOAT)65535.0;
|
|
|
|
model->tmap[i].v = (M3D_FLOAT)(*((int16_t *)(data + 2))) / (M3D_FLOAT)65535.0;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 4:
|
2020-03-08 20:24:01 +00:00
|
|
|
model->tmap[i].u = (M3D_FLOAT)(*((float *)(data + 0)));
|
|
|
|
model->tmap[i].v = (M3D_FLOAT)(*((float *)(data + 4)));
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 8:
|
2020-03-08 20:24:01 +00:00
|
|
|
model->tmap[i].u = (M3D_FLOAT)(*((double *)(data + 0)));
|
|
|
|
model->tmap[i].v = (M3D_FLOAT)(*((double *)(data + 8)));
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
data += reclen;
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* vertex list */
|
|
|
|
if (M3D_CHUNKMAGIC(data, 'V', 'R', 'T', 'S')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Vertex list");
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->vertex) {
|
|
|
|
M3D_LOG("More vertex chunks, should be unique");
|
|
|
|
model->errcode = M3D_ERR_VRTS;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (model->ci_s && model->ci_s < 4 && !model->cmap) model->errcode = M3D_ERR_CMAP;
|
2019-10-29 17:42:10 +00:00
|
|
|
reclen = model->ci_s + model->sk_s + 4 * model->vc_s;
|
|
|
|
model->numvertex = len / reclen;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->vertex = (m3dv_t *)M3D_MALLOC(model->numvertex * sizeof(m3dv_t));
|
|
|
|
if (!model->vertex) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
memset(model->vertex, 0, model->numvertex * sizeof(m3dv_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0, data += sizeof(m3dchunk_t); data < chunk && i < model->numvertex; i++) {
|
|
|
|
switch (model->vc_s) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case 1:
|
2019-12-11 04:47:59 +00:00
|
|
|
model->vertex[i].x = (M3D_FLOAT)((int8_t)data[0]) / (M3D_FLOAT)127.0;
|
|
|
|
model->vertex[i].y = (M3D_FLOAT)((int8_t)data[1]) / (M3D_FLOAT)127.0;
|
|
|
|
model->vertex[i].z = (M3D_FLOAT)((int8_t)data[2]) / (M3D_FLOAT)127.0;
|
|
|
|
model->vertex[i].w = (M3D_FLOAT)((int8_t)data[3]) / (M3D_FLOAT)127.0;
|
2019-10-29 17:42:10 +00:00
|
|
|
data += 4;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 2:
|
2020-03-08 20:24:01 +00:00
|
|
|
model->vertex[i].x = (M3D_FLOAT)(*((int16_t *)(data + 0))) / (M3D_FLOAT)32767.0;
|
|
|
|
model->vertex[i].y = (M3D_FLOAT)(*((int16_t *)(data + 2))) / (M3D_FLOAT)32767.0;
|
|
|
|
model->vertex[i].z = (M3D_FLOAT)(*((int16_t *)(data + 4))) / (M3D_FLOAT)32767.0;
|
|
|
|
model->vertex[i].w = (M3D_FLOAT)(*((int16_t *)(data + 6))) / (M3D_FLOAT)32767.0;
|
2019-10-29 17:42:10 +00:00
|
|
|
data += 8;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 4:
|
2020-03-08 20:24:01 +00:00
|
|
|
model->vertex[i].x = (M3D_FLOAT)(*((float *)(data + 0)));
|
|
|
|
model->vertex[i].y = (M3D_FLOAT)(*((float *)(data + 4)));
|
|
|
|
model->vertex[i].z = (M3D_FLOAT)(*((float *)(data + 8)));
|
|
|
|
model->vertex[i].w = (M3D_FLOAT)(*((float *)(data + 12)));
|
2019-10-29 17:42:10 +00:00
|
|
|
data += 16;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 8:
|
2020-03-08 20:24:01 +00:00
|
|
|
model->vertex[i].x = (M3D_FLOAT)(*((double *)(data + 0)));
|
|
|
|
model->vertex[i].y = (M3D_FLOAT)(*((double *)(data + 8)));
|
|
|
|
model->vertex[i].z = (M3D_FLOAT)(*((double *)(data + 16)));
|
|
|
|
model->vertex[i].w = (M3D_FLOAT)(*((double *)(data + 24)));
|
2019-10-29 17:42:10 +00:00
|
|
|
data += 32;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (model->ci_s) {
|
|
|
|
case 1:
|
|
|
|
model->vertex[i].color = model->cmap ? model->cmap[data[0]] : 0;
|
|
|
|
data++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
model->vertex[i].color = model->cmap ? model->cmap[*((uint16_t *)data)] : 0;
|
|
|
|
data += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
model->vertex[i].color = *((uint32_t *)data);
|
|
|
|
data += 4;
|
|
|
|
break;
|
|
|
|
/* case 8: break; */
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-12-10 05:30:41 +00:00
|
|
|
model->vertex[i].skinid = M3D_UNDEF;
|
2019-10-29 17:42:10 +00:00
|
|
|
data = _m3d_getidx(data, model->sk_s, &model->vertex[i].skinid);
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* skeleton: bone hierarchy and skin */
|
|
|
|
if (M3D_CHUNKMAGIC(data, 'B', 'O', 'N', 'E')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Skeleton");
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->bone) {
|
|
|
|
M3D_LOG("More bone chunks, should be unique");
|
|
|
|
model->errcode = M3D_ERR_BONE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!model->bi_s) {
|
|
|
|
M3D_LOG("Bone chunk, shouldn't be any");
|
|
|
|
model->errcode = M3D_ERR_BONE;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!model->vertex) {
|
|
|
|
M3D_LOG("No vertex chunk before bones");
|
|
|
|
model->errcode = M3D_ERR_VRTS;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
data += sizeof(m3dchunk_t);
|
|
|
|
model->numbone = 0;
|
|
|
|
data = _m3d_getidx(data, model->bi_s, &model->numbone);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numbone) {
|
|
|
|
model->bone = (m3db_t *)M3D_MALLOC(model->numbone * sizeof(m3db_t));
|
|
|
|
if (!model->bone) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
model->numskin = 0;
|
|
|
|
data = _m3d_getidx(data, model->sk_s, &model->numskin);
|
|
|
|
/* read bone hierarchy */
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numbone; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
data = _m3d_getidx(data, model->bi_s, &model->bone[i].parent);
|
|
|
|
M3D_GETSTR(model->bone[i].name);
|
|
|
|
data = _m3d_getidx(data, model->vi_s, &model->bone[i].pos);
|
|
|
|
data = _m3d_getidx(data, model->vi_s, &model->bone[i].ori);
|
|
|
|
model->bone[i].numweight = 0;
|
|
|
|
model->bone[i].weight = NULL;
|
|
|
|
}
|
|
|
|
/* read skin definitions */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numskin) {
|
|
|
|
model->skin = (m3ds_t *)M3D_MALLOC(model->numskin * sizeof(m3ds_t));
|
|
|
|
if (!model->skin) goto memerr;
|
|
|
|
for (i = 0; data < chunk && i < model->numskin; i++) {
|
|
|
|
for (j = 0; j < M3D_NUMBONE; j++) {
|
2019-12-10 05:30:41 +00:00
|
|
|
model->skin[i].boneid[j] = M3D_UNDEF;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->skin[i].weight[j] = (M3D_FLOAT)0.0;
|
|
|
|
}
|
|
|
|
memset(&weights, 0, sizeof(weights));
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->nb_s == 1)
|
|
|
|
weights[0] = 255;
|
2019-11-18 02:04:52 +00:00
|
|
|
else {
|
|
|
|
memcpy(&weights, data, model->nb_s);
|
|
|
|
data += model->nb_s;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0, w = (M3D_FLOAT)0.0; j < (unsigned int)model->nb_s; j++) {
|
|
|
|
if (weights[j]) {
|
|
|
|
if (j >= M3D_NUMBONE)
|
2019-11-18 02:04:52 +00:00
|
|
|
data += model->bi_s;
|
|
|
|
else {
|
2019-12-11 04:47:59 +00:00
|
|
|
model->skin[i].weight[j] = (M3D_FLOAT)(weights[j]) / (M3D_FLOAT)255.0;
|
2019-11-18 02:04:52 +00:00
|
|
|
w += model->skin[i].weight[j];
|
|
|
|
data = _m3d_getidx(data, model->bi_s, &model->skin[i].boneid[j]);
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* this can occur if model has more bones than what the importer is configured to handle */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (w != (M3D_FLOAT)1.0 && w != (M3D_FLOAT)0.0) {
|
|
|
|
for (j = 0; j < M3D_NUMBONE; j++)
|
2019-11-18 02:04:52 +00:00
|
|
|
model->skin[i].weight[j] /= w;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* material */
|
|
|
|
if (M3D_CHUNKMAGIC(data, 'M', 'T', 'R', 'L')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
data += sizeof(m3dchunk_t);
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_GETSTR(name);
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Material");
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG(name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->ci_s < 4 && !model->numcmap) model->errcode = M3D_ERR_CMAP;
|
|
|
|
for (i = 0; i < model->nummaterial; i++)
|
|
|
|
if (!strcmp(name, model->material[i].name)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
model->errcode = M3D_ERR_MTRL;
|
|
|
|
M3D_LOG("Multiple definitions for material");
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG(name);
|
|
|
|
name = NULL;
|
2019-10-29 17:42:10 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (name) {
|
2019-10-29 17:42:10 +00:00
|
|
|
i = model->nummaterial++;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->flags & M3D_FLG_MTLLIB) {
|
2019-10-29 17:42:10 +00:00
|
|
|
m = model->material;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->material = (m3dm_t *)M3D_MALLOC(model->nummaterial * sizeof(m3dm_t));
|
|
|
|
if (!model->material) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
memcpy(model->material, m, (model->nummaterial - 1) * sizeof(m3dm_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->texture) {
|
2019-10-29 17:42:10 +00:00
|
|
|
tx = model->texture;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->texture = (m3dtx_t *)M3D_MALLOC(model->numtexture * sizeof(m3dtx_t));
|
|
|
|
if (!model->texture) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
memcpy(model->texture, tx, model->numtexture * sizeof(m3dm_t));
|
|
|
|
}
|
|
|
|
model->flags &= ~M3D_FLG_MTLLIB;
|
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
model->material = (m3dm_t *)M3D_REALLOC(model->material, model->nummaterial * sizeof(m3dm_t));
|
|
|
|
if (!model->material) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
m = &model->material[i];
|
|
|
|
m->numprop = 0;
|
2019-11-18 02:04:52 +00:00
|
|
|
m->name = name;
|
2020-03-08 20:24:01 +00:00
|
|
|
m->prop = (m3dp_t *)M3D_MALLOC((len / 2) * sizeof(m3dp_t));
|
|
|
|
if (!m->prop) goto memerr;
|
|
|
|
while (data < chunk) {
|
2019-10-29 17:42:10 +00:00
|
|
|
i = m->numprop++;
|
|
|
|
m->prop[i].type = *data++;
|
|
|
|
m->prop[i].value.num = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (m->prop[i].type >= 128)
|
2019-10-29 17:42:10 +00:00
|
|
|
k = m3dpf_map;
|
|
|
|
else {
|
2020-03-08 20:24:01 +00:00
|
|
|
for (k = 256, j = 0; j < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); j++)
|
|
|
|
if (m->prop[i].type == m3d_propertytypes[j].id) {
|
|
|
|
k = m3d_propertytypes[j].format;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (k) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case m3dpf_color:
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (model->ci_s) {
|
|
|
|
case 1:
|
|
|
|
m->prop[i].value.color = model->cmap ? model->cmap[data[0]] : 0;
|
|
|
|
data++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
m->prop[i].value.color = model->cmap ? model->cmap[*((uint16_t *)data)] : 0;
|
|
|
|
data += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
m->prop[i].value.color = *((uint32_t *)data);
|
|
|
|
data += 4;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
case m3dpf_uint8: m->prop[i].value.num = *data++; break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case m3dpf_uint16:
|
|
|
|
m->prop[i].value.num = *((uint16_t *)data);
|
|
|
|
data += 2;
|
|
|
|
break;
|
|
|
|
case m3dpf_uint32:
|
|
|
|
m->prop[i].value.num = *((uint32_t *)data);
|
|
|
|
data += 4;
|
|
|
|
break;
|
|
|
|
case m3dpf_float:
|
|
|
|
m->prop[i].value.fnum = *((float *)data);
|
|
|
|
data += 4;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
case m3dpf_map:
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_GETSTR(name);
|
|
|
|
m->prop[i].value.textureid = _m3d_gettx(model, readfilecb, freecb, name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->errcode == M3D_ERR_ALLOC) goto memerr;
|
2019-12-14 07:52:48 +00:00
|
|
|
/* this error code only returned if readfilecb was specified */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (m->prop[i].value.textureid == M3D_UNDEF) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Texture not found");
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG(m->name);
|
2019-10-29 17:42:10 +00:00
|
|
|
m->numprop--;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
M3D_LOG("Unknown material property in");
|
|
|
|
M3D_LOG(m->name);
|
|
|
|
model->errcode = M3D_ERR_UNKPROP;
|
|
|
|
data = chunk;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
m->prop = (m3dp_t *)M3D_REALLOC(m->prop, m->numprop * sizeof(m3dp_t));
|
|
|
|
if (!m->prop) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* face */
|
|
|
|
if (M3D_CHUNKMAGIC(data, 'P', 'R', 'O', 'C')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
/* procedural surface */
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_GETSTR(name);
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Procedural surface");
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG(name);
|
|
|
|
_m3d_getpr(model, readfilecb, freecb, name);
|
2020-03-08 20:24:01 +00:00
|
|
|
} else if (M3D_CHUNKMAGIC(data, 'M', 'E', 'S', 'H')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Mesh data");
|
|
|
|
/* mesh */
|
|
|
|
data += sizeof(m3dchunk_t);
|
2019-12-10 05:30:41 +00:00
|
|
|
mi = M3D_UNDEF;
|
2019-10-29 17:42:10 +00:00
|
|
|
am = model->numface;
|
2020-03-08 20:24:01 +00:00
|
|
|
while (data < chunk) {
|
2019-10-29 17:42:10 +00:00
|
|
|
k = *data++;
|
|
|
|
n = k >> 4;
|
|
|
|
k &= 15;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!n) {
|
2019-10-29 17:42:10 +00:00
|
|
|
/* use material */
|
2019-12-10 05:30:41 +00:00
|
|
|
mi = M3D_UNDEF;
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_GETSTR(name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (name) {
|
|
|
|
for (j = 0; j < model->nummaterial; j++)
|
|
|
|
if (!strcmp(name, model->material[j].name)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
mi = (M3D_INDEX)j;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (mi == M3D_UNDEF) model->errcode = M3D_ERR_MTRL;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (n != 3) {
|
|
|
|
M3D_LOG("Only triangle mesh supported for now");
|
|
|
|
model->errcode = M3D_ERR_UNKMESH;
|
|
|
|
return model;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
i = model->numface++;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numface > am) {
|
2019-10-29 17:42:10 +00:00
|
|
|
am = model->numface + 4095;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->face = (m3df_t *)M3D_REALLOC(model->face, am * sizeof(m3df_t));
|
|
|
|
if (!model->face) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
memset(&model->face[i], 255, sizeof(m3df_t)); /* set all index to -1 by default */
|
|
|
|
model->face[i].materialid = mi;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < n; j++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
/* vertex */
|
|
|
|
data = _m3d_getidx(data, model->vi_s, &model->face[i].vertex[j]);
|
|
|
|
/* texcoord */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k & 1)
|
2019-10-29 17:42:10 +00:00
|
|
|
data = _m3d_getidx(data, model->ti_s, &model->face[i].texcoord[j]);
|
|
|
|
/* normal */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k & 2)
|
2019-10-29 17:42:10 +00:00
|
|
|
data = _m3d_getidx(data, model->vi_s, &model->face[i].normal[j]);
|
2019-11-25 04:03:10 +00:00
|
|
|
#ifndef M3D_NONORMALS
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->face[i].normal[j] == M3D_UNDEF) neednorm = 1;
|
2019-11-25 04:03:10 +00:00
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
model->face = (m3df_t *)M3D_REALLOC(model->face, model->numface * sizeof(m3df_t));
|
|
|
|
} else if (M3D_CHUNKMAGIC(data, 'S', 'H', 'P', 'E')) {
|
2019-11-18 02:04:52 +00:00
|
|
|
/* mathematical shape */
|
|
|
|
data += sizeof(m3dchunk_t);
|
|
|
|
M3D_GETSTR(name);
|
|
|
|
M3D_LOG("Mathematical Shape");
|
|
|
|
M3D_LOG(name);
|
|
|
|
i = model->numshape++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->shape = (m3dh_t *)M3D_REALLOC(model->shape, model->numshape * sizeof(m3dh_t));
|
|
|
|
if (!model->shape) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
h = &model->shape[i];
|
|
|
|
h->numcmd = 0;
|
|
|
|
h->cmd = NULL;
|
|
|
|
h->name = name;
|
2019-12-10 05:30:41 +00:00
|
|
|
h->group = M3D_UNDEF;
|
2019-11-18 02:04:52 +00:00
|
|
|
data = _m3d_getidx(data, model->bi_s, &h->group);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (h->group != M3D_UNDEF && h->group >= model->numbone) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Unknown bone id as shape group in shape");
|
|
|
|
M3D_LOG(name);
|
2019-12-10 05:30:41 +00:00
|
|
|
h->group = M3D_UNDEF;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->errcode = M3D_ERR_SHPE;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
while (data < chunk) {
|
2019-11-18 02:04:52 +00:00
|
|
|
i = h->numcmd++;
|
2020-03-08 20:24:01 +00:00
|
|
|
h->cmd = (m3dc_t *)M3D_REALLOC(h->cmd, h->numcmd * sizeof(m3dc_t));
|
|
|
|
if (!h->cmd) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
h->cmd[i].type = *data++;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (h->cmd[i].type & 0x80) {
|
2019-11-18 02:04:52 +00:00
|
|
|
h->cmd[i].type &= 0x7F;
|
|
|
|
h->cmd[i].type |= (*data++ << 7);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (h->cmd[i].type >= (unsigned int)(sizeof(m3d_commandtypes) / sizeof(m3d_commandtypes[0]))) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Unknown shape command in");
|
|
|
|
M3D_LOG(h->name);
|
|
|
|
model->errcode = M3D_ERR_UNKCMD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
cd = &m3d_commandtypes[h->cmd[i].type];
|
2020-03-08 20:24:01 +00:00
|
|
|
h->cmd[i].arg = (uint32_t *)M3D_MALLOC(cd->p * sizeof(uint32_t));
|
|
|
|
if (!h->cmd[i].arg) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(h->cmd[i].arg, 0, cd->p * sizeof(uint32_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (k = n = 0, l = cd->p; k < l; k++)
|
|
|
|
switch (cd->a[((k - n) % (cd->p - n)) + n]) {
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_mi_t:
|
2019-12-10 05:30:41 +00:00
|
|
|
h->cmd[i].arg[k] = M3D_NOTDEFINED;
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_GETSTR(name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (name) {
|
|
|
|
for (n = 0; n < model->nummaterial; n++)
|
|
|
|
if (!strcmp(name, model->material[n].name)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
h->cmd[i].arg[k] = n;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (h->cmd[i].arg[k] == M3D_NOTDEFINED) model->errcode = M3D_ERR_MTRL;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_vc_t:
|
|
|
|
f = 0.0f;
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (model->vc_s) {
|
2019-11-18 02:04:52 +00:00
|
|
|
case 1: f = (float)((int8_t)data[0]) / 127; break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case 2: f = (float)(*((int16_t *)(data + 0))) / 32767; break;
|
|
|
|
case 4: f = (float)(*((float *)(data + 0))); break;
|
|
|
|
case 8: f = (float)(*((double *)(data + 0))); break;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
h->cmd[i].arg[k] = *((uint32_t *)&f);
|
2019-11-18 02:04:52 +00:00
|
|
|
data += model->vc_s;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_hi_t: data = _m3d_getidx(data, model->hi_s, &h->cmd[i].arg[k]); break;
|
|
|
|
case m3dcp_fi_t: data = _m3d_getidx(data, model->fi_s, &h->cmd[i].arg[k]); break;
|
|
|
|
case m3dcp_ti_t: data = _m3d_getidx(data, model->ti_s, &h->cmd[i].arg[k]); break;
|
|
|
|
case m3dcp_qi_t:
|
|
|
|
case m3dcp_vi_t: data = _m3d_getidx(data, model->vi_s, &h->cmd[i].arg[k]); break;
|
|
|
|
case m3dcp_i1_t: data = _m3d_getidx(data, 1, &h->cmd[i].arg[k]); break;
|
|
|
|
case m3dcp_i2_t: data = _m3d_getidx(data, 2, &h->cmd[i].arg[k]); break;
|
|
|
|
case m3dcp_i4_t: data = _m3d_getidx(data, 4, &h->cmd[i].arg[k]); break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case m3dcp_va_t:
|
|
|
|
data = _m3d_getidx(data, 4, &h->cmd[i].arg[k]);
|
|
|
|
n = k + 1;
|
|
|
|
l += (h->cmd[i].arg[k] - 1) * (cd->p - k - 1);
|
|
|
|
h->cmd[i].arg = (uint32_t *)M3D_REALLOC(h->cmd[i].arg, l * sizeof(uint32_t));
|
|
|
|
if (!h->cmd[i].arg) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(&h->cmd[i].arg[k + 1], 0, (l - k - 1) * sizeof(uint32_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* annotation label list */
|
|
|
|
if (M3D_CHUNKMAGIC(data, 'L', 'B', 'L', 'S')) {
|
2019-11-18 02:04:52 +00:00
|
|
|
data += sizeof(m3dchunk_t);
|
|
|
|
M3D_GETSTR(name);
|
|
|
|
M3D_GETSTR(lang);
|
|
|
|
M3D_LOG("Label list");
|
2020-03-08 20:24:01 +00:00
|
|
|
if (name) {
|
|
|
|
M3D_LOG(name);
|
|
|
|
}
|
|
|
|
if (lang) {
|
|
|
|
M3D_LOG(lang);
|
|
|
|
}
|
|
|
|
if (model->ci_s && model->ci_s < 4 && !model->cmap) model->errcode = M3D_ERR_CMAP;
|
2019-11-18 02:04:52 +00:00
|
|
|
k = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (model->ci_s) {
|
|
|
|
case 1:
|
|
|
|
k = model->cmap ? model->cmap[data[0]] : 0;
|
|
|
|
data++;
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
k = model->cmap ? model->cmap[*((uint16_t *)data)] : 0;
|
|
|
|
data += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
k = *((uint32_t *)data);
|
|
|
|
data += 4;
|
|
|
|
break;
|
|
|
|
/* case 8: break; */
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
reclen = model->vi_s + model->si_s;
|
2020-03-08 20:24:01 +00:00
|
|
|
i = model->numlabel;
|
|
|
|
model->numlabel += len / reclen;
|
|
|
|
model->label = (m3dl_t *)M3D_REALLOC(model->label, model->numlabel * sizeof(m3dl_t));
|
|
|
|
if (!model->label) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(&model->label[i], 0, (model->numlabel - i) * sizeof(m3dl_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (; data < chunk && i < model->numlabel; i++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
model->label[i].name = name;
|
|
|
|
model->label[i].lang = lang;
|
|
|
|
model->label[i].color = k;
|
|
|
|
data = _m3d_getidx(data, model->vi_s, &model->label[i].vertexid);
|
|
|
|
M3D_GETSTR(model->label[i].text);
|
|
|
|
}
|
|
|
|
} else
|
2020-03-08 20:24:01 +00:00
|
|
|
/* action */
|
|
|
|
if (M3D_CHUNKMAGIC(data, 'A', 'C', 'T', 'N')) {
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Action");
|
|
|
|
i = model->numaction++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->action = (m3da_t *)M3D_REALLOC(model->action, model->numaction * sizeof(m3da_t));
|
|
|
|
if (!model->action) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
a = &model->action[i];
|
|
|
|
data += sizeof(m3dchunk_t);
|
|
|
|
M3D_GETSTR(a->name);
|
|
|
|
M3D_LOG(a->name);
|
2020-03-08 20:24:01 +00:00
|
|
|
a->numframe = *((uint16_t *)data);
|
|
|
|
data += 2;
|
|
|
|
if (a->numframe < 1) {
|
2019-10-29 17:42:10 +00:00
|
|
|
model->numaction--;
|
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
a->durationmsec = *((uint32_t *)data);
|
|
|
|
data += 4;
|
|
|
|
a->frame = (m3dfr_t *)M3D_MALLOC(a->numframe * sizeof(m3dfr_t));
|
|
|
|
if (!a->frame) goto memerr;
|
|
|
|
for (i = 0; data < chunk && i < a->numframe; i++) {
|
|
|
|
a->frame[i].msec = *((uint32_t *)data);
|
|
|
|
data += 4;
|
|
|
|
a->frame[i].numtransform = 0;
|
|
|
|
a->frame[i].transform = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
data = _m3d_getidx(data, model->fc_s, &a->frame[i].numtransform);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (a->frame[i].numtransform > 0) {
|
|
|
|
a->frame[i].transform = (m3dtr_t *)M3D_MALLOC(a->frame[i].numtransform * sizeof(m3dtr_t));
|
|
|
|
for (j = 0; j < a->frame[i].numtransform; j++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
data = _m3d_getidx(data, model->bi_s, &a->frame[i].transform[j].boneid);
|
|
|
|
data = _m3d_getidx(data, model->vi_s, &a->frame[i].transform[j].pos);
|
|
|
|
data = _m3d_getidx(data, model->vi_s, &a->frame[i].transform[j].ori);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2019-11-18 02:04:52 +00:00
|
|
|
i = model->numextra++;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->extra = (m3dchunk_t **)M3D_REALLOC(model->extra, model->numextra * sizeof(m3dchunk_t *));
|
|
|
|
if (!model->extra) goto memerr;
|
|
|
|
model->extra[i] = (m3dchunk_t *)data;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* calculate normals, normalize skin weights, create bone/vertex cross-references and calculate transform matrices */
|
2019-11-01 02:39:36 +00:00
|
|
|
#ifdef M3D_ASCII
|
2019-10-29 17:42:10 +00:00
|
|
|
postprocess:
|
2019-11-01 02:39:36 +00:00
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Post-process");
|
2019-11-25 04:03:10 +00:00
|
|
|
#ifdef M3D_PROFILING
|
|
|
|
gettimeofday(&tv1, NULL);
|
|
|
|
tvd.tv_sec = tv1.tv_sec - tv0.tv_sec;
|
|
|
|
tvd.tv_usec = tv1.tv_usec - tv0.tv_usec;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (tvd.tv_usec < 0) {
|
|
|
|
tvd.tv_sec--;
|
|
|
|
tvd.tv_usec += 1000000L;
|
|
|
|
}
|
2019-11-25 04:03:10 +00:00
|
|
|
printf(" Parsing chunks %ld.%06ld sec\n", tvd.tv_sec, tvd.tv_usec);
|
|
|
|
#endif
|
2019-11-01 02:39:36 +00:00
|
|
|
#ifndef M3D_NONORMALS
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numface && model->face && neednorm) {
|
2019-10-29 17:42:10 +00:00
|
|
|
/* if they are missing, calculate triangle normals into a temporary buffer */
|
2020-03-08 20:24:01 +00:00
|
|
|
norm = (m3dv_t *)M3D_MALLOC(model->numface * sizeof(m3dv_t));
|
|
|
|
if (!norm) goto memerr;
|
|
|
|
for (i = 0, n = model->numvertex; i < model->numface; i++)
|
|
|
|
if (model->face[i].normal[0] == M3D_UNDEF) {
|
2019-11-18 02:04:52 +00:00
|
|
|
v0 = &model->vertex[model->face[i].vertex[0]];
|
|
|
|
v1 = &model->vertex[model->face[i].vertex[1]];
|
2019-10-29 17:42:10 +00:00
|
|
|
v2 = &model->vertex[model->face[i].vertex[2]];
|
2020-03-08 20:24:01 +00:00
|
|
|
va.x = v1->x - v0->x;
|
|
|
|
va.y = v1->y - v0->y;
|
|
|
|
va.z = v1->z - v0->z;
|
|
|
|
vb.x = v2->x - v0->x;
|
|
|
|
vb.y = v2->y - v0->y;
|
|
|
|
vb.z = v2->z - v0->z;
|
2019-11-18 02:04:52 +00:00
|
|
|
v0 = &norm[i];
|
|
|
|
v0->x = (va.y * vb.z) - (va.z * vb.y);
|
|
|
|
v0->y = (va.z * vb.x) - (va.x * vb.z);
|
|
|
|
v0->z = (va.x * vb.y) - (va.y * vb.x);
|
|
|
|
w = _m3d_rsq((v0->x * v0->x) + (v0->y * v0->y) + (v0->z * v0->z));
|
2020-03-08 20:24:01 +00:00
|
|
|
v0->x *= w;
|
|
|
|
v0->y *= w;
|
|
|
|
v0->z *= w;
|
2019-11-18 02:04:52 +00:00
|
|
|
model->face[i].normal[0] = model->face[i].vertex[0] + n;
|
|
|
|
model->face[i].normal[1] = model->face[i].vertex[1] + n;
|
|
|
|
model->face[i].normal[2] = model->face[i].vertex[2] + n;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* this is the fast way, we don't care if a normal is repeated in model->vertex */
|
2019-11-25 04:03:10 +00:00
|
|
|
M3D_LOG("Generating normals");
|
|
|
|
model->flags |= M3D_FLG_GENNORM;
|
|
|
|
model->numvertex <<= 1;
|
2020-03-08 20:24:01 +00:00
|
|
|
model->vertex = (m3dv_t *)M3D_REALLOC(model->vertex, model->numvertex * sizeof(m3dv_t));
|
|
|
|
if (!model->vertex) goto memerr;
|
2019-11-25 04:03:10 +00:00
|
|
|
memset(&model->vertex[n], 0, n * sizeof(m3dv_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numface; i++)
|
|
|
|
for (j = 0; j < 3; j++) {
|
2019-11-25 04:03:10 +00:00
|
|
|
v0 = &model->vertex[model->face[i].vertex[j] + n];
|
|
|
|
v0->x += norm[i].x;
|
|
|
|
v0->y += norm[i].y;
|
|
|
|
v0->z += norm[i].z;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-25 04:03:10 +00:00
|
|
|
/* for each vertex, take the average of the temporary normals and use that */
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0, v0 = &model->vertex[n]; i < n; i++, v0++) {
|
2019-11-25 04:03:10 +00:00
|
|
|
w = _m3d_rsq((v0->x * v0->x) + (v0->y * v0->y) + (v0->z * v0->z));
|
2020-03-08 20:24:01 +00:00
|
|
|
v0->x *= w;
|
|
|
|
v0->y *= w;
|
|
|
|
v0->z *= w;
|
2019-12-10 05:30:41 +00:00
|
|
|
v0->skinid = M3D_UNDEF;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-25 04:03:10 +00:00
|
|
|
M3D_FREE(norm);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-01 02:39:36 +00:00
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numbone && model->bone && model->numskin && model->skin && model->numvertex && model->vertex) {
|
2019-11-01 02:39:36 +00:00
|
|
|
#ifndef M3D_NOWEIGHTS
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Generating weight cross-reference");
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numvertex; i++) {
|
|
|
|
if (model->vertex[i].skinid < model->numskin) {
|
2019-10-29 17:42:10 +00:00
|
|
|
sk = &model->skin[model->vertex[i].skinid];
|
|
|
|
w = (M3D_FLOAT)0.0;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < M3D_NUMBONE && sk->boneid[j] != M3D_UNDEF && sk->weight[j] > (M3D_FLOAT)0.0; j++)
|
2019-10-29 17:42:10 +00:00
|
|
|
w += sk->weight[j];
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < M3D_NUMBONE && sk->boneid[j] != M3D_UNDEF && sk->weight[j] > (M3D_FLOAT)0.0; j++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
sk->weight[j] /= w;
|
|
|
|
b = &model->bone[sk->boneid[j]];
|
|
|
|
k = b->numweight++;
|
2020-03-08 20:24:01 +00:00
|
|
|
b->weight = (m3dw_t *)M3D_REALLOC(b->weight, b->numweight * sizeof(m3da_t));
|
|
|
|
if (!b->weight) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
b->weight[k].vertexid = i;
|
|
|
|
b->weight[k].weight = sk->weight[j];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-01 02:39:36 +00:00
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifndef M3D_NOANIMATION
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Calculating bone transformation matrices");
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numbone; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
b = &model->bone[i];
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->bone[i].parent == M3D_UNDEF) {
|
|
|
|
_m3d_mat((M3D_FLOAT *)&b->mat4, &model->vertex[b->pos], &model->vertex[b->ori]);
|
2019-10-29 17:42:10 +00:00
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
_m3d_mat((M3D_FLOAT *)&r, &model->vertex[b->pos], &model->vertex[b->ori]);
|
|
|
|
_m3d_mul((M3D_FLOAT *)&b->mat4, (M3D_FLOAT *)&model->bone[b->parent].mat4, (M3D_FLOAT *)&r);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numbone; i++)
|
|
|
|
_m3d_inv((M3D_FLOAT *)&model->bone[i].mat4);
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
2019-11-25 04:03:10 +00:00
|
|
|
#ifdef M3D_PROFILING
|
|
|
|
gettimeofday(&tv0, NULL);
|
|
|
|
tvd.tv_sec = tv0.tv_sec - tv1.tv_sec;
|
|
|
|
tvd.tv_usec = tv0.tv_usec - tv1.tv_usec;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (tvd.tv_usec < 0) {
|
|
|
|
tvd.tv_sec--;
|
|
|
|
tvd.tv_usec += 1000000L;
|
|
|
|
}
|
2019-11-25 04:03:10 +00:00
|
|
|
printf(" Post-process %ld.%06ld sec\n", tvd.tv_sec, tvd.tv_usec);
|
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
return model;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Calculates skeletons for animation frames, returns a working copy (should be freed after use)
|
|
|
|
*/
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dtr_t *m3d_frame(m3d_t *model, M3D_INDEX actionid, M3D_INDEX frameid, m3dtr_t *skeleton) {
|
2019-10-29 17:42:10 +00:00
|
|
|
unsigned int i;
|
|
|
|
M3D_INDEX s = frameid;
|
|
|
|
m3dfr_t *fr;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!model || !model->numbone || !model->bone || (actionid != M3D_UNDEF && (!model->action || actionid >= model->numaction || frameid >= model->action[actionid].numframe))) {
|
|
|
|
model->errcode = M3D_ERR_UNKFRAME;
|
|
|
|
return skeleton;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
model->errcode = M3D_SUCCESS;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!skeleton) {
|
|
|
|
skeleton = (m3dtr_t *)M3D_MALLOC(model->numbone * sizeof(m3dtr_t));
|
|
|
|
if (!skeleton) {
|
2019-10-29 17:42:10 +00:00
|
|
|
model->errcode = M3D_ERR_ALLOC;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
goto gen;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (actionid == M3D_UNDEF || !frameid) {
|
|
|
|
gen:
|
|
|
|
s = 0;
|
|
|
|
for (i = 0; i < model->numbone; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
skeleton[i].boneid = i;
|
|
|
|
skeleton[i].pos = model->bone[i].pos;
|
|
|
|
skeleton[i].ori = model->bone[i].ori;
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (actionid < model->numaction && (frameid || !model->action[actionid].frame[0].msec)) {
|
|
|
|
for (; s <= frameid; s++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
fr = &model->action[actionid].frame[s];
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < fr->numtransform; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
skeleton[fr->transform[i].boneid].pos = fr->transform[i].pos;
|
|
|
|
skeleton[fr->transform[i].boneid].ori = fr->transform[i].ori;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return skeleton;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef M3D_NOANIMATION
|
|
|
|
/**
|
|
|
|
* Returns interpolated animation-pose, a working copy (should be freed after use)
|
|
|
|
*/
|
2020-03-08 20:24:01 +00:00
|
|
|
m3db_t *m3d_pose(m3d_t *model, M3D_INDEX actionid, uint32_t msec) {
|
2019-10-29 17:42:10 +00:00
|
|
|
unsigned int i, j, l;
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_FLOAT r[16], t, c, d, s;
|
2019-10-29 17:42:10 +00:00
|
|
|
m3db_t *ret;
|
|
|
|
m3dv_t *v, *p, *f;
|
|
|
|
m3dtr_t *tmp;
|
|
|
|
m3dfr_t *fr;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!model || !model->numbone || !model->bone) {
|
2019-10-29 17:42:10 +00:00
|
|
|
model->errcode = M3D_ERR_UNKFRAME;
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
ret = (m3db_t *)M3D_MALLOC(model->numbone * sizeof(m3db_t));
|
|
|
|
if (!ret) {
|
2019-10-29 17:42:10 +00:00
|
|
|
model->errcode = M3D_ERR_ALLOC;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy(ret, model->bone, model->numbone * sizeof(m3db_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numbone; i++)
|
|
|
|
_m3d_inv((M3D_FLOAT *)&ret[i].mat4);
|
|
|
|
if (!model->action || actionid >= model->numaction) {
|
2019-10-29 17:42:10 +00:00
|
|
|
model->errcode = M3D_ERR_UNKFRAME;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
msec %= model->action[actionid].durationmsec;
|
|
|
|
model->errcode = M3D_SUCCESS;
|
|
|
|
fr = &model->action[actionid].frame[0];
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = l = 0; j < model->action[actionid].numframe && model->action[actionid].frame[j].msec <= msec; j++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
fr = &model->action[actionid].frame[j];
|
|
|
|
l = fr->msec;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < fr->numtransform; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
ret[fr->transform[i].boneid].pos = fr->transform[i].pos;
|
|
|
|
ret[fr->transform[i].boneid].ori = fr->transform[i].ori;
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (l != msec) {
|
|
|
|
model->vertex = (m3dv_t *)M3D_REALLOC(model->vertex, (model->numvertex + 2 * model->numbone) * sizeof(m3dv_t));
|
|
|
|
if (!model->vertex) {
|
2019-10-29 17:42:10 +00:00
|
|
|
free(ret);
|
|
|
|
model->errcode = M3D_ERR_ALLOC;
|
|
|
|
return NULL;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
tmp = (m3dtr_t *)M3D_MALLOC(model->numbone * sizeof(m3dtr_t));
|
|
|
|
if (tmp) {
|
|
|
|
for (i = 0; i < model->numbone; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
tmp[i].pos = ret[i].pos;
|
|
|
|
tmp[i].ori = ret[i].ori;
|
|
|
|
}
|
|
|
|
fr = &model->action[actionid].frame[j % model->action[actionid].numframe];
|
|
|
|
t = l >= fr->msec ? (M3D_FLOAT)1.0 : (M3D_FLOAT)(msec - l) / (M3D_FLOAT)(fr->msec - l);
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < fr->numtransform; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
tmp[fr->transform[i].boneid].pos = fr->transform[i].pos;
|
|
|
|
tmp[fr->transform[i].boneid].ori = fr->transform[i].ori;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0, j = model->numvertex; i < model->numbone; i++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
/* interpolation of position */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (ret[i].pos != tmp[i].pos) {
|
2019-10-29 17:42:10 +00:00
|
|
|
p = &model->vertex[ret[i].pos];
|
|
|
|
f = &model->vertex[tmp[i].pos];
|
|
|
|
v = &model->vertex[j];
|
|
|
|
v->x = p->x + t * (f->x - p->x);
|
|
|
|
v->y = p->y + t * (f->y - p->y);
|
|
|
|
v->z = p->z + t * (f->z - p->z);
|
|
|
|
ret[i].pos = j++;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* interpolation of orientation */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (ret[i].ori != tmp[i].ori) {
|
2019-10-29 17:42:10 +00:00
|
|
|
p = &model->vertex[ret[i].ori];
|
|
|
|
f = &model->vertex[tmp[i].ori];
|
|
|
|
v = &model->vertex[j];
|
2019-11-18 02:04:52 +00:00
|
|
|
d = p->w * f->w + p->x * f->x + p->y * f->y + p->z * f->z;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (d < 0) {
|
|
|
|
d = -d;
|
|
|
|
s = (M3D_FLOAT)-1.0;
|
|
|
|
} else
|
|
|
|
s = (M3D_FLOAT)1.0;
|
2019-11-18 02:04:52 +00:00
|
|
|
#if 0
|
|
|
|
/* don't use SLERP, requires two more variables, libm linkage and it is slow (but nice) */
|
|
|
|
a = (M3D_FLOAT)1.0 - t; b = t;
|
|
|
|
if(d < (M3D_FLOAT)0.999999) { c = acosf(d); b = 1 / sinf(c); a = sinf(a * c) * b; b *= sinf(t * c) * s; }
|
|
|
|
v->x = p->x * a + f->x * b;
|
|
|
|
v->y = p->y * a + f->y * b;
|
|
|
|
v->z = p->z * a + f->z * b;
|
|
|
|
v->w = p->w * a + f->w * b;
|
|
|
|
#else
|
|
|
|
/* approximated NLERP, original approximation by Arseny Kapoulkine, heavily optimized by me */
|
2020-03-08 20:24:01 +00:00
|
|
|
c = t - (M3D_FLOAT)0.5;
|
|
|
|
t += t * c * (t - (M3D_FLOAT)1.0) * (((M3D_FLOAT)1.0904 + d * ((M3D_FLOAT)-3.2452 + d * ((M3D_FLOAT)3.55645 - d * (M3D_FLOAT)1.43519))) * c * c + ((M3D_FLOAT)0.848013 + d * ((M3D_FLOAT)-1.06021 + d * (M3D_FLOAT)0.215638)));
|
2019-11-18 02:04:52 +00:00
|
|
|
v->x = p->x + t * (s * f->x - p->x);
|
|
|
|
v->y = p->y + t * (s * f->y - p->y);
|
|
|
|
v->z = p->z + t * (s * f->z - p->z);
|
|
|
|
v->w = p->w + t * (s * f->w - p->w);
|
2019-10-29 17:42:10 +00:00
|
|
|
d = _m3d_rsq(v->w * v->w + v->x * v->x + v->y * v->y + v->z * v->z);
|
2020-03-08 20:24:01 +00:00
|
|
|
v->x *= d;
|
|
|
|
v->y *= d;
|
|
|
|
v->z *= d;
|
|
|
|
v->w *= d;
|
2019-11-18 02:04:52 +00:00
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
ret[i].ori = j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
M3D_FREE(tmp);
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numbone; i++) {
|
|
|
|
if (ret[i].parent == M3D_UNDEF) {
|
|
|
|
_m3d_mat((M3D_FLOAT *)&ret[i].mat4, &model->vertex[ret[i].pos], &model->vertex[ret[i].ori]);
|
2019-10-29 17:42:10 +00:00
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
_m3d_mat((M3D_FLOAT *)&r, &model->vertex[ret[i].pos], &model->vertex[ret[i].ori]);
|
|
|
|
_m3d_mul((M3D_FLOAT *)&ret[i].mat4, (M3D_FLOAT *)&ret[ret[i].parent].mat4, (M3D_FLOAT *)&r);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* M3D_NOANIMATION */
|
|
|
|
|
|
|
|
#endif /* M3D_IMPLEMENTATION */
|
|
|
|
|
|
|
|
#if !defined(M3D_NODUP) && (!defined(M3D_NOIMPORTER) || defined(M3D_EXPORTER))
|
|
|
|
/**
|
|
|
|
* Free the in-memory model
|
|
|
|
*/
|
2020-03-08 20:24:01 +00:00
|
|
|
void m3d_free(m3d_t *model) {
|
2019-10-29 17:42:10 +00:00
|
|
|
unsigned int i, j;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!model) return;
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifdef M3D_ASCII
|
|
|
|
/* if model imported from ASCII, we have to free all strings as well */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->flags & M3D_FLG_FREESTR) {
|
|
|
|
if (model->name) M3D_FREE(model->name);
|
|
|
|
if (model->license) M3D_FREE(model->license);
|
|
|
|
if (model->author) M3D_FREE(model->author);
|
|
|
|
if (model->desc) M3D_FREE(model->desc);
|
|
|
|
if (model->bone)
|
|
|
|
for (i = 0; i < model->numbone; i++)
|
|
|
|
if (model->bone[i].name)
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(model->bone[i].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->shape)
|
|
|
|
for (i = 0; i < model->numshape; i++)
|
|
|
|
if (model->shape[i].name)
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_FREE(model->shape[i].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->material)
|
|
|
|
for (i = 0; i < model->nummaterial; i++)
|
|
|
|
if (model->material[i].name)
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(model->material[i].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->action)
|
|
|
|
for (i = 0; i < model->numaction; i++)
|
|
|
|
if (model->action[i].name)
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(model->action[i].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->texture)
|
|
|
|
for (i = 0; i < model->numtexture; i++)
|
|
|
|
if (model->texture[i].name)
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(model->texture[i].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->inlined)
|
|
|
|
for (i = 0; i < model->numinlined; i++) {
|
|
|
|
if (model->inlined[i].name)
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_FREE(model->inlined[i].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->inlined[i].data)
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_FREE(model->inlined[i].data);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->extra)
|
|
|
|
for (i = 0; i < model->numextra; i++)
|
|
|
|
if (model->extra[i])
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_FREE(model->extra[i]);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->label)
|
|
|
|
for (i = 0; i < model->numlabel; i++) {
|
|
|
|
if (model->label[i].name) {
|
|
|
|
for (j = i + 1; j < model->numlabel; j++)
|
|
|
|
if (model->label[j].name == model->label[i].name)
|
2019-11-18 02:04:52 +00:00
|
|
|
model->label[j].name = NULL;
|
|
|
|
M3D_FREE(model->label[i].name);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->label[i].lang) {
|
|
|
|
for (j = i + 1; j < model->numlabel; j++)
|
|
|
|
if (model->label[j].lang == model->label[i].lang)
|
2019-11-18 02:04:52 +00:00
|
|
|
model->label[j].lang = NULL;
|
|
|
|
M3D_FREE(model->label[i].lang);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->label[i].text)
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_FREE(model->label[i].text);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->preview.data)
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_FREE(model->preview.data);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->flags & M3D_FLG_FREERAW) M3D_FREE(model->raw);
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->tmap) M3D_FREE(model->tmap);
|
|
|
|
if (model->bone) {
|
|
|
|
for (i = 0; i < model->numbone; i++)
|
|
|
|
if (model->bone[i].weight)
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(model->bone[i].weight);
|
|
|
|
M3D_FREE(model->bone);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->skin) M3D_FREE(model->skin);
|
|
|
|
if (model->vertex) M3D_FREE(model->vertex);
|
|
|
|
if (model->face) M3D_FREE(model->face);
|
|
|
|
if (model->shape) {
|
|
|
|
for (i = 0; i < model->numshape; i++) {
|
|
|
|
if (model->shape[i].cmd) {
|
|
|
|
for (j = 0; j < model->shape[i].numcmd; j++)
|
|
|
|
if (model->shape[i].cmd[j].arg) M3D_FREE(model->shape[i].cmd[j].arg);
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_FREE(model->shape[i].cmd);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
M3D_FREE(model->shape);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->material && !(model->flags & M3D_FLG_MTLLIB)) {
|
|
|
|
for (i = 0; i < model->nummaterial; i++)
|
|
|
|
if (model->material[i].prop) M3D_FREE(model->material[i].prop);
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(model->material);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->texture) {
|
|
|
|
for (i = 0; i < model->numtexture; i++)
|
|
|
|
if (model->texture[i].d) M3D_FREE(model->texture[i].d);
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(model->texture);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->action) {
|
|
|
|
for (i = 0; i < model->numaction; i++) {
|
|
|
|
if (model->action[i].frame) {
|
|
|
|
for (j = 0; j < model->action[i].numframe; j++)
|
|
|
|
if (model->action[i].frame[j].transform) M3D_FREE(model->action[i].frame[j].transform);
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(model->action[i].frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
M3D_FREE(model->action);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->label) M3D_FREE(model->label);
|
|
|
|
if (model->inlined) M3D_FREE(model->inlined);
|
|
|
|
if (model->extra) M3D_FREE(model->extra);
|
2019-10-29 17:42:10 +00:00
|
|
|
free(model);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef M3D_EXPORTER
|
|
|
|
typedef struct {
|
|
|
|
char *str;
|
|
|
|
uint32_t offs;
|
|
|
|
} m3dstr_t;
|
|
|
|
|
2019-11-18 02:04:52 +00:00
|
|
|
typedef struct {
|
|
|
|
m3dti_t data;
|
|
|
|
M3D_INDEX oldidx;
|
|
|
|
M3D_INDEX newidx;
|
|
|
|
} m3dtisave_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
m3dv_t data;
|
|
|
|
M3D_INDEX oldidx;
|
|
|
|
M3D_INDEX newidx;
|
|
|
|
unsigned char norm;
|
|
|
|
} m3dvsave_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
m3ds_t data;
|
|
|
|
M3D_INDEX oldidx;
|
|
|
|
M3D_INDEX newidx;
|
|
|
|
} m3dssave_t;
|
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
m3df_t data;
|
|
|
|
int group;
|
|
|
|
uint8_t opacity;
|
|
|
|
} m3dfsave_t;
|
|
|
|
|
2019-10-29 17:42:10 +00:00
|
|
|
/* create unique list of strings */
|
2020-03-08 20:24:01 +00:00
|
|
|
static m3dstr_t *_m3d_addstr(m3dstr_t *str, uint32_t *numstr, char *s) {
|
2019-10-29 17:42:10 +00:00
|
|
|
uint32_t i;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!s || !*s) return str;
|
|
|
|
if (str) {
|
|
|
|
for (i = 0; i < *numstr; i++)
|
|
|
|
if (str[i].str == s || !strcmp(str[i].str, s)) return str;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
str = (m3dstr_t *)M3D_REALLOC(str, ((*numstr) + 1) * sizeof(m3dstr_t));
|
2019-10-29 17:42:10 +00:00
|
|
|
str[*numstr].str = s;
|
|
|
|
str[*numstr].offs = 0;
|
|
|
|
(*numstr)++;
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add strings to header */
|
2020-03-08 20:24:01 +00:00
|
|
|
m3dhdr_t *_m3d_addhdr(m3dhdr_t *h, m3dstr_t *s) {
|
2019-10-29 17:42:10 +00:00
|
|
|
int i;
|
|
|
|
char *safe = _m3d_safestr(s->str, 0);
|
2019-12-10 05:30:41 +00:00
|
|
|
i = (int)strlen(safe);
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, h->length + i + 1);
|
|
|
|
if (!h) {
|
|
|
|
M3D_FREE(safe);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
memcpy((uint8_t *)h + h->length, safe, i + 1);
|
2019-10-29 17:42:10 +00:00
|
|
|
s->offs = h->length - 16;
|
2020-03-08 20:24:01 +00:00
|
|
|
h->length += i + 1;
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(safe);
|
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* return offset of string */
|
2020-03-08 20:24:01 +00:00
|
|
|
static uint32_t _m3d_stridx(m3dstr_t *str, uint32_t numstr, char *s) {
|
2019-10-29 17:42:10 +00:00
|
|
|
uint32_t i;
|
|
|
|
char *safe;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!s || !*s) return 0;
|
|
|
|
if (str) {
|
2019-10-29 17:42:10 +00:00
|
|
|
safe = _m3d_safestr(s, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!safe) return 0;
|
|
|
|
if (!*safe) {
|
2019-10-29 17:42:10 +00:00
|
|
|
free(safe);
|
|
|
|
return 0;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numstr; i++)
|
|
|
|
if (!strcmp(str[i].str, s)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
free(safe);
|
|
|
|
return str[i].offs;
|
|
|
|
}
|
|
|
|
free(safe);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-11-18 02:04:52 +00:00
|
|
|
/* compare to faces by their material */
|
|
|
|
static int _m3d_facecmp(const void *a, const void *b) {
|
2020-03-08 20:24:01 +00:00
|
|
|
const m3dfsave_t *A = (const m3dfsave_t *)a, *B = (const m3dfsave_t *)b;
|
|
|
|
return A->group != B->group ? A->group - B->group : (A->opacity != B->opacity ? (int)B->opacity - (int)A->opacity : (int)A->data.materialid - (int)B->data.materialid);
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
/* compare face groups */
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3d_grpcmp(const void *a, const void *b) {
|
|
|
|
return *((uint32_t *)a) - *((uint32_t *)b);
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* compare UVs */
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3d_ticmp(const void *a, const void *b) {
|
|
|
|
return memcmp(a, b, sizeof(m3dti_t));
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* compare skin groups */
|
2020-03-08 20:24:01 +00:00
|
|
|
static int _m3d_skincmp(const void *a, const void *b) {
|
|
|
|
return memcmp(a, b, sizeof(m3ds_t));
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* compare vertices */
|
|
|
|
static int _m3d_vrtxcmp(const void *a, const void *b) {
|
|
|
|
int c = memcmp(a, b, 3 * sizeof(M3D_FLOAT));
|
2020-03-08 20:24:01 +00:00
|
|
|
if (c) return c;
|
|
|
|
c = ((m3dvsave_t *)a)->norm - ((m3dvsave_t *)b)->norm;
|
|
|
|
if (c) return c;
|
2019-11-18 02:04:52 +00:00
|
|
|
return memcmp(a, b, sizeof(m3dv_t));
|
|
|
|
}
|
|
|
|
/* compare labels */
|
2020-03-08 20:24:01 +00:00
|
|
|
static _inline int _m3d_strcmp(char *a, char *b) {
|
|
|
|
if (a == NULL && b != NULL) return -1;
|
|
|
|
if (a != NULL && b == NULL) return 1;
|
|
|
|
if (a == NULL && b == NULL) return 0;
|
2019-11-18 02:04:52 +00:00
|
|
|
return strcmp(a, b);
|
|
|
|
}
|
|
|
|
static int _m3d_lblcmp(const void *a, const void *b) {
|
2020-03-08 20:24:01 +00:00
|
|
|
const m3dl_t *A = (const m3dl_t *)a, *B = (const m3dl_t *)b;
|
2019-11-18 02:04:52 +00:00
|
|
|
int c = _m3d_strcmp(A->lang, B->lang);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!c) c = _m3d_strcmp(A->name, B->name);
|
|
|
|
if (!c) c = _m3d_strcmp(A->text, B->text);
|
2019-11-18 02:04:52 +00:00
|
|
|
return c;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
/* compare two colors by HSV value */
|
2020-03-08 20:24:01 +00:00
|
|
|
_inline static int _m3d_cmapcmp(const void *a, const void *b) {
|
|
|
|
uint8_t *A = (uint8_t *)a, *B = (uint8_t *)b;
|
2019-10-30 02:04:33 +00:00
|
|
|
_register int m, vA, vB;
|
2019-10-29 17:42:10 +00:00
|
|
|
/* get HSV value for A */
|
2020-03-08 20:24:01 +00:00
|
|
|
m = A[2] < A[1] ? A[2] : A[1];
|
|
|
|
if (A[0] < m) m = A[0];
|
|
|
|
vA = A[2] > A[1] ? A[2] : A[1];
|
|
|
|
if (A[0] > vA) vA = A[0];
|
2019-10-29 17:42:10 +00:00
|
|
|
/* get HSV value for B */
|
2020-03-08 20:24:01 +00:00
|
|
|
m = B[2] < B[1] ? B[2] : B[1];
|
|
|
|
if (B[0] < m) m = B[0];
|
|
|
|
vB = B[2] > B[1] ? B[2] : B[1];
|
|
|
|
if (B[0] > vB) vB = B[0];
|
2019-10-29 17:42:10 +00:00
|
|
|
return vA - vB;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create sorted list of colors */
|
2020-03-08 20:24:01 +00:00
|
|
|
static uint32_t *_m3d_addcmap(uint32_t *cmap, uint32_t *numcmap, uint32_t color) {
|
2019-10-29 17:42:10 +00:00
|
|
|
uint32_t i;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (cmap) {
|
|
|
|
for (i = 0; i < *numcmap; i++)
|
|
|
|
if (cmap[i] == color) return cmap;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
cmap = (uint32_t *)M3D_REALLOC(cmap, ((*numcmap) + 1) * sizeof(uint32_t));
|
|
|
|
for (i = 0; i < *numcmap && _m3d_cmapcmp(&color, &cmap[i]) > 0; i++)
|
|
|
|
;
|
|
|
|
if (i < *numcmap) memmove(&cmap[i + 1], &cmap[i], ((*numcmap) - i) * sizeof(uint32_t));
|
2019-10-29 17:42:10 +00:00
|
|
|
cmap[i] = color;
|
|
|
|
(*numcmap)++;
|
|
|
|
return cmap;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* look up a color and return its index */
|
2020-03-08 20:24:01 +00:00
|
|
|
static uint32_t _m3d_cmapidx(uint32_t *cmap, uint32_t numcmap, uint32_t color) {
|
2019-10-29 17:42:10 +00:00
|
|
|
uint32_t i;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numcmap >= 65536)
|
2019-11-18 02:04:52 +00:00
|
|
|
return color;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numcmap; i++)
|
|
|
|
if (cmap[i] == color) return i;
|
2019-10-29 17:42:10 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* add index to output */
|
|
|
|
static unsigned char *_m3d_addidx(unsigned char *out, char type, uint32_t idx) {
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (type) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case 1: *out++ = (uint8_t)(idx); break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case 2:
|
|
|
|
*((uint16_t *)out) = (uint16_t)(idx);
|
|
|
|
out += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*((uint32_t *)out) = (uint32_t)(idx);
|
|
|
|
out += 4;
|
|
|
|
break;
|
|
|
|
/* case 0: case 8: break; */
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* round a vertex position */
|
2020-03-08 20:24:01 +00:00
|
|
|
static void _m3d_round(int quality, m3dv_t *src, m3dv_t *dst) {
|
2019-10-30 02:04:33 +00:00
|
|
|
_register int t;
|
2019-10-29 17:42:10 +00:00
|
|
|
/* copy additional attributes */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (src != dst) memcpy(dst, src, sizeof(m3dv_t));
|
2019-10-29 17:42:10 +00:00
|
|
|
/* round according to quality */
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (quality) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case M3D_EXP_INT8:
|
2020-03-08 20:24:01 +00:00
|
|
|
t = (int)(src->x * 127 + (src->x >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5));
|
|
|
|
dst->x = (M3D_FLOAT)t / (M3D_FLOAT)127.0;
|
|
|
|
t = (int)(src->y * 127 + (src->y >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5));
|
|
|
|
dst->y = (M3D_FLOAT)t / (M3D_FLOAT)127.0;
|
|
|
|
t = (int)(src->z * 127 + (src->z >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5));
|
|
|
|
dst->z = (M3D_FLOAT)t / (M3D_FLOAT)127.0;
|
|
|
|
t = (int)(src->w * 127 + (src->w >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5));
|
|
|
|
dst->w = (M3D_FLOAT)t / (M3D_FLOAT)127.0;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case M3D_EXP_INT16:
|
2020-03-08 20:24:01 +00:00
|
|
|
t = (int)(src->x * 32767 + (src->x >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5));
|
|
|
|
dst->x = (M3D_FLOAT)t / (M3D_FLOAT)32767.0;
|
|
|
|
t = (int)(src->y * 32767 + (src->y >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5));
|
|
|
|
dst->y = (M3D_FLOAT)t / (M3D_FLOAT)32767.0;
|
|
|
|
t = (int)(src->z * 32767 + (src->z >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5));
|
|
|
|
dst->z = (M3D_FLOAT)t / (M3D_FLOAT)32767.0;
|
|
|
|
t = (int)(src->w * 32767 + (src->w >= 0 ? (M3D_FLOAT)0.5 : (M3D_FLOAT)-0.5));
|
|
|
|
dst->w = (M3D_FLOAT)t / (M3D_FLOAT)32767.0;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (dst->x == (M3D_FLOAT)-0.0) dst->x = (M3D_FLOAT)0.0;
|
|
|
|
if (dst->y == (M3D_FLOAT)-0.0) dst->y = (M3D_FLOAT)0.0;
|
|
|
|
if (dst->z == (M3D_FLOAT)-0.0) dst->z = (M3D_FLOAT)0.0;
|
|
|
|
if (dst->w == (M3D_FLOAT)-0.0) dst->w = (M3D_FLOAT)0.0;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef M3D_ASCII
|
|
|
|
/* add a bone to ascii output */
|
2020-03-08 20:24:01 +00:00
|
|
|
static char *_m3d_prtbone(char *ptr, m3db_t *bone, M3D_INDEX numbone, M3D_INDEX parent, uint32_t level, M3D_INDEX *vrtxidx) {
|
2019-10-29 17:42:10 +00:00
|
|
|
uint32_t i, j;
|
|
|
|
char *sn;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (level > M3D_BONEMAXLEVEL || !bone) return ptr;
|
|
|
|
for (i = 0; i < numbone; i++) {
|
|
|
|
if (bone[i].parent == parent) {
|
|
|
|
for (j = 0; j < level; j++)
|
|
|
|
*ptr++ = '/';
|
2019-10-29 17:42:10 +00:00
|
|
|
sn = _m3d_safestr(bone[i].name, 0);
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "%d %d %s\r\n", vrtxidx[bone[i].pos], vrtxidx[bone[i].ori], sn);
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_FREE(sn);
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr = _m3d_prtbone(ptr, bone, numbone, i, level + 1, vrtxidx);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return ptr;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function to encode an in-memory model into on storage Model 3D format
|
|
|
|
*/
|
2020-03-08 20:24:01 +00:00
|
|
|
unsigned char *m3d_save(m3d_t *model, int quality, int flags, unsigned int *size) {
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifdef M3D_ASCII
|
|
|
|
const char *ol;
|
|
|
|
char *ptr;
|
|
|
|
#endif
|
2019-11-18 02:04:52 +00:00
|
|
|
char vc_s, vi_s, si_s, ci_s, ti_s, bi_s, nb_s, sk_s, fc_s, hi_s, fi_s;
|
2019-10-29 17:42:10 +00:00
|
|
|
char *sn = NULL, *sl = NULL, *sa = NULL, *sd = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
unsigned char *out = NULL, *z = NULL, weights[M3D_NUMBONE], *norm = NULL;
|
|
|
|
unsigned int i, j, k, l, n, len, chunklen, *length;
|
|
|
|
M3D_FLOAT scale = (M3D_FLOAT)0.0, min_x, max_x, min_y, max_y, min_z, max_z;
|
|
|
|
M3D_INDEX last, *vrtxidx = NULL, *mtrlidx = NULL, *tmapidx = NULL, *skinidx = NULL;
|
|
|
|
uint32_t idx, numcmap = 0, *cmap = NULL, numvrtx = 0, maxvrtx = 0, numtmap = 0, maxtmap = 0, numproc = 0;
|
|
|
|
uint32_t numskin = 0, maxskin = 0, numstr = 0, maxt = 0, maxbone = 0, numgrp = 0, maxgrp = 0, *grpidx = NULL;
|
2020-04-28 05:39:13 +00:00
|
|
|
uint8_t *opa = nullptr;
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dcd_t *cd;
|
|
|
|
m3dc_t *cmd;
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dstr_t *str = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dvsave_t *vrtx = NULL, vertex;
|
|
|
|
m3dtisave_t *tmap = NULL, tcoord;
|
|
|
|
m3dssave_t *skin = NULL, sk;
|
|
|
|
m3dfsave_t *face = NULL;
|
2019-10-29 17:42:10 +00:00
|
|
|
m3dhdr_t *h = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
m3dm_t *m;
|
2019-10-29 17:42:10 +00:00
|
|
|
m3da_t *a;
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!model) {
|
|
|
|
if (size) *size = 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
model->errcode = M3D_SUCCESS;
|
|
|
|
#ifdef M3D_ASCII
|
2020-03-08 20:24:01 +00:00
|
|
|
if (flags & M3D_EXP_ASCII) quality = M3D_EXP_DOUBLE;
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
vrtxidx = (M3D_INDEX *)M3D_MALLOC(model->numvertex * sizeof(M3D_INDEX));
|
|
|
|
if (!vrtxidx) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(vrtxidx, 255, model->numvertex * sizeof(M3D_INDEX));
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numvertex && !(flags & M3D_EXP_NONORMAL)) {
|
|
|
|
norm = (unsigned char *)M3D_MALLOC(model->numvertex * sizeof(unsigned char));
|
|
|
|
if (!norm) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(norm, 0, model->numvertex * sizeof(unsigned char));
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->nummaterial && !(flags & M3D_EXP_NOMATERIAL)) {
|
|
|
|
mtrlidx = (M3D_INDEX *)M3D_MALLOC(model->nummaterial * sizeof(M3D_INDEX));
|
|
|
|
if (!mtrlidx) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(mtrlidx, 255, model->nummaterial * sizeof(M3D_INDEX));
|
2020-03-08 20:24:01 +00:00
|
|
|
opa = (uint8_t *)M3D_MALLOC(model->nummaterial * 2 * sizeof(M3D_INDEX));
|
|
|
|
if (!opa) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(opa, 255, model->nummaterial * 2 * sizeof(M3D_INDEX));
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numtmap && !(flags & M3D_EXP_NOTXTCRD)) {
|
|
|
|
tmapidx = (M3D_INDEX *)M3D_MALLOC(model->numtmap * sizeof(M3D_INDEX));
|
|
|
|
if (!tmapidx) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(tmapidx, 255, model->numtmap * sizeof(M3D_INDEX));
|
|
|
|
}
|
|
|
|
/** collect array elements that are actually referenced **/
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOFACE)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
/* face */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numface && model->face) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Processing mesh face");
|
2020-03-08 20:24:01 +00:00
|
|
|
face = (m3dfsave_t *)M3D_MALLOC(model->numface * sizeof(m3dfsave_t));
|
|
|
|
if (!face) goto memerr;
|
|
|
|
for (i = 0; i < model->numface; i++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
memcpy(&face[i].data, &model->face[i], sizeof(m3df_t));
|
|
|
|
face[i].group = 0;
|
|
|
|
face[i].opacity = 255;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOMATERIAL) && model->face[i].materialid < model->nummaterial) {
|
|
|
|
if (model->material[model->face[i].materialid].numprop) {
|
2019-11-18 02:04:52 +00:00
|
|
|
mtrlidx[model->face[i].materialid] = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (opa[model->face[i].materialid * 2]) {
|
2019-11-18 02:04:52 +00:00
|
|
|
m = &model->material[model->face[i].materialid];
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < m->numprop; j++)
|
|
|
|
if (m->prop[j].type == m3dp_Kd) {
|
|
|
|
opa[model->face[i].materialid * 2 + 1] = ((uint8_t *)&m->prop[j].value.color)[3];
|
2019-11-18 02:04:52 +00:00
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < m->numprop; j++)
|
|
|
|
if (m->prop[j].type == m3dp_d) {
|
2019-11-18 02:04:52 +00:00
|
|
|
opa[model->face[i].materialid * 2 + 1] = (uint8_t)(m->prop[j].value.fnum * 255);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
opa[model->face[i].materialid * 2] = 0;
|
|
|
|
}
|
|
|
|
face[i].opacity = opa[model->face[i].materialid * 2 + 1];
|
2019-10-29 17:42:10 +00:00
|
|
|
} else
|
2019-12-10 05:30:41 +00:00
|
|
|
face[i].data.materialid = M3D_UNDEF;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < 3; j++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
k = model->face[i].vertex[j];
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k < model->numvertex)
|
2019-11-18 02:04:52 +00:00
|
|
|
vrtxidx[k] = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOCMAP)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
cmap = _m3d_addcmap(cmap, &numcmap, model->vertex[k].color);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!cmap) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
k = model->face[i].normal[j];
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k < model->numvertex && !(flags & M3D_EXP_NONORMAL)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
vrtxidx[k] = 0;
|
|
|
|
norm[k] = 1;
|
|
|
|
}
|
|
|
|
k = model->face[i].texcoord[j];
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k < model->numtmap && !(flags & M3D_EXP_NOTXTCRD))
|
2019-11-18 02:04:52 +00:00
|
|
|
tmapidx[k] = 0;
|
|
|
|
}
|
|
|
|
/* convert from CW to CCW */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (flags & M3D_EXP_IDOSUCK) {
|
2019-11-18 02:04:52 +00:00
|
|
|
j = face[i].data.vertex[1];
|
|
|
|
face[i].data.vertex[1] = face[i].data.vertex[2];
|
|
|
|
face[i].data.vertex[2] = face[i].data.vertex[1];
|
|
|
|
j = face[i].data.normal[1];
|
|
|
|
face[i].data.normal[1] = face[i].data.normal[2];
|
|
|
|
face[i].data.normal[2] = face[i].data.normal[1];
|
|
|
|
j = face[i].data.texcoord[1];
|
|
|
|
face[i].data.texcoord[1] = face[i].data.texcoord[2];
|
|
|
|
face[i].data.texcoord[2] = face[i].data.texcoord[1];
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numshape && model->shape) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Processing shape face");
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numshape; i++) {
|
|
|
|
if (!model->shape[i].numcmd) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
str = _m3d_addstr(str, &numstr, model->shape[i].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!str) goto memerr;
|
|
|
|
for (j = 0; j < model->shape[i].numcmd; j++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
cmd = &model->shape[i].cmd[j];
|
2020-03-08 20:24:01 +00:00
|
|
|
if (cmd->type >= (unsigned int)(sizeof(m3d_commandtypes) / sizeof(m3d_commandtypes[0])) || !cmd->arg)
|
2019-11-18 02:04:52 +00:00
|
|
|
continue;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (cmd->type == m3dc_mesh) {
|
|
|
|
if (numgrp + 2 < maxgrp) {
|
2019-11-18 02:04:52 +00:00
|
|
|
maxgrp += 1024;
|
2020-03-08 20:24:01 +00:00
|
|
|
grpidx = (uint32_t *)realloc(grpidx, maxgrp * sizeof(uint32_t));
|
|
|
|
if (!grpidx) goto memerr;
|
|
|
|
if (!numgrp) {
|
2019-11-18 02:04:52 +00:00
|
|
|
grpidx[0] = 0;
|
|
|
|
grpidx[1] = model->numface;
|
|
|
|
numgrp += 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
grpidx[numgrp + 0] = cmd->arg[0];
|
|
|
|
grpidx[numgrp + 1] = cmd->arg[0] + cmd->arg[1];
|
|
|
|
numgrp += 2;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
cd = &m3d_commandtypes[cmd->type];
|
2020-03-08 20:24:01 +00:00
|
|
|
for (k = n = 0, l = cd->p; k < l; k++)
|
|
|
|
switch (cd->a[((k - n) % (cd->p - n)) + n]) {
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_mi_t:
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOMATERIAL) && cmd->arg[k] < model->nummaterial)
|
2019-11-18 02:04:52 +00:00
|
|
|
mtrlidx[cmd->arg[k]] = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_ti_t:
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOTXTCRD) && cmd->arg[k] < model->numtmap)
|
2019-11-18 02:04:52 +00:00
|
|
|
tmapidx[cmd->arg[k]] = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_qi_t:
|
|
|
|
case m3dcp_vi_t:
|
2020-03-08 20:24:01 +00:00
|
|
|
if (cmd->arg[k] < model->numvertex)
|
2019-11-18 02:04:52 +00:00
|
|
|
vrtxidx[cmd->arg[k]] = 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_va_t:
|
2020-03-08 20:24:01 +00:00
|
|
|
n = k + 1;
|
|
|
|
l += (cmd->arg[k] - 1) * (cd->p - k - 1);
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numface && face) {
|
|
|
|
if (numgrp && grpidx) {
|
2019-11-18 02:04:52 +00:00
|
|
|
qsort(grpidx, numgrp, sizeof(uint32_t), _m3d_grpcmp);
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = j = 0; i < model->numface && j < numgrp; i++) {
|
|
|
|
while (j < numgrp && grpidx[j] < i)
|
|
|
|
j++;
|
2019-11-18 02:04:52 +00:00
|
|
|
face[i].group = j;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
qsort(face, model->numface, sizeof(m3dfsave_t), _m3d_facecmp);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (grpidx) {
|
|
|
|
M3D_FREE(grpidx);
|
|
|
|
grpidx = NULL;
|
|
|
|
}
|
|
|
|
if (model->numlabel && model->label) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Processing annotation labels");
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numlabel; i++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
str = _m3d_addstr(str, &numstr, model->label[i].name);
|
|
|
|
str = _m3d_addstr(str, &numstr, model->label[i].lang);
|
|
|
|
str = _m3d_addstr(str, &numstr, model->label[i].text);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOCMAP)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
cmap = _m3d_addcmap(cmap, &numcmap, model->label[i].color);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!cmap) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->label[i].vertexid < model->numvertex)
|
2019-11-18 02:04:52 +00:00
|
|
|
vrtxidx[model->label[i].vertexid] = 0;
|
|
|
|
}
|
|
|
|
qsort(model->label, model->numlabel, sizeof(m3dl_t), _m3d_lblcmp);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} else if (!(flags & M3D_EXP_NOMATERIAL)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
/* without a face, simply add all materials, because it can be an mtllib */
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->nummaterial; i++)
|
2019-11-18 02:04:52 +00:00
|
|
|
mtrlidx[i] = i;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* bind-pose skeleton */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numbone && model->bone && !(flags & M3D_EXP_NOBONE)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Processing bones");
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numbone; i++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
str = _m3d_addstr(str, &numstr, model->bone[i].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!str) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
k = model->bone[i].pos;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k < model->numvertex)
|
2019-11-18 02:04:52 +00:00
|
|
|
vrtxidx[k] = 0;
|
|
|
|
k = model->bone[i].ori;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k < model->numvertex)
|
2019-11-18 02:04:52 +00:00
|
|
|
vrtxidx[k] = 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* actions, animated skeleton poses */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numaction && model->action && !(flags & M3D_EXP_NOACTION)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Processing action list");
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < model->numaction; j++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
a = &model->action[j];
|
|
|
|
str = _m3d_addstr(str, &numstr, a->name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!str) goto memerr;
|
|
|
|
if (a->numframe > 65535) a->numframe = 65535;
|
|
|
|
for (i = 0; i < a->numframe; i++) {
|
|
|
|
for (l = 0; l < a->frame[i].numtransform; l++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
k = a->frame[i].transform[l].pos;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k < model->numvertex)
|
2019-11-18 02:04:52 +00:00
|
|
|
vrtxidx[k] = 0;
|
|
|
|
k = a->frame[i].transform[l].ori;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k < model->numvertex)
|
2019-11-18 02:04:52 +00:00
|
|
|
vrtxidx[k] = 0;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (l > maxt) maxt = l;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* add colors to color map and texture names to string table */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOMATERIAL)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Processing materials");
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = k = 0; i < model->nummaterial; i++) {
|
|
|
|
if (mtrlidx[i] == M3D_UNDEF || !model->material[i].numprop) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
mtrlidx[i] = k++;
|
|
|
|
m = &model->material[i];
|
|
|
|
str = _m3d_addstr(str, &numstr, m->name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!str) goto memerr;
|
|
|
|
if (m->prop)
|
|
|
|
for (j = 0; j < m->numprop; j++) {
|
|
|
|
if (!(flags & M3D_EXP_NOCMAP) && m->prop[j].type < 128) {
|
|
|
|
for (l = 0; l < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); l++) {
|
|
|
|
if (m->prop[j].type == m3d_propertytypes[l].id && m3d_propertytypes[l].format == m3dpf_color) {
|
|
|
|
((uint8_t *)&m->prop[j].value.color)[3] = opa[i * 2 + 1];
|
2019-11-18 02:04:52 +00:00
|
|
|
cmap = _m3d_addcmap(cmap, &numcmap, m->prop[j].value.color);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!cmap) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (m->prop[j].type >= 128 && m->prop[j].value.textureid < model->numtexture &&
|
|
|
|
model->texture[m->prop[j].value.textureid].name) {
|
2019-11-18 02:04:52 +00:00
|
|
|
str = _m3d_addstr(str, &numstr, model->texture[m->prop[j].value.textureid].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!str) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/* if there's only one black color, don't store it */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numcmap == 1 && cmap && !cmap[0]) numcmap = 0;
|
2019-11-18 02:04:52 +00:00
|
|
|
|
|
|
|
/** compress lists **/
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numtmap && !(flags & M3D_EXP_NOTXTCRD)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Compressing tmap");
|
2020-03-08 20:24:01 +00:00
|
|
|
tmap = (m3dtisave_t *)M3D_MALLOC(model->numtmap * sizeof(m3dtisave_t));
|
|
|
|
if (!tmap) goto memerr;
|
|
|
|
for (i = 0; i < model->numtmap; i++) {
|
|
|
|
if (tmapidx[i] == M3D_UNDEF) continue;
|
|
|
|
switch (quality) {
|
2019-11-18 02:04:52 +00:00
|
|
|
case M3D_EXP_INT8:
|
2020-03-08 20:24:01 +00:00
|
|
|
l = (unsigned int)(model->tmap[i].u * 255);
|
|
|
|
tcoord.data.u = (M3D_FLOAT)l / (M3D_FLOAT)255.0;
|
|
|
|
l = (unsigned int)(model->tmap[i].v * 255);
|
|
|
|
tcoord.data.v = (M3D_FLOAT)l / (M3D_FLOAT)255.0;
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case M3D_EXP_INT16:
|
2020-03-08 20:24:01 +00:00
|
|
|
l = (unsigned int)(model->tmap[i].u * 65535);
|
|
|
|
tcoord.data.u = (M3D_FLOAT)l / (M3D_FLOAT)65535.0;
|
|
|
|
l = (unsigned int)(model->tmap[i].v * 65535);
|
|
|
|
tcoord.data.v = (M3D_FLOAT)l / (M3D_FLOAT)65535.0;
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
default:
|
|
|
|
tcoord.data.u = model->tmap[i].u;
|
|
|
|
tcoord.data.v = model->tmap[i].v;
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (flags & M3D_EXP_FLIPTXTCRD)
|
2019-11-18 02:04:52 +00:00
|
|
|
tcoord.data.v = (M3D_FLOAT)1.0 - tcoord.data.v;
|
|
|
|
tcoord.oldidx = i;
|
|
|
|
memcpy(&tmap[numtmap++], &tcoord, sizeof(m3dtisave_t));
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numtmap) {
|
2019-11-18 02:04:52 +00:00
|
|
|
qsort(tmap, numtmap, sizeof(m3dtisave_t), _m3d_ticmp);
|
|
|
|
memcpy(&tcoord.data, &tmap[0], sizeof(m3dti_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numtmap; i++) {
|
|
|
|
if (memcmp(&tcoord.data, &tmap[i].data, sizeof(m3dti_t))) {
|
2019-11-18 02:04:52 +00:00
|
|
|
memcpy(&tcoord.data, &tmap[i].data, sizeof(m3dti_t));
|
|
|
|
maxtmap++;
|
|
|
|
}
|
|
|
|
tmap[i].newidx = maxtmap;
|
|
|
|
tmapidx[tmap[i].oldidx] = maxtmap;
|
|
|
|
}
|
|
|
|
maxtmap++;
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numskin && model->skin && !(flags & M3D_EXP_NOBONE)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Compressing skin");
|
2020-03-08 20:24:01 +00:00
|
|
|
skinidx = (M3D_INDEX *)M3D_MALLOC(model->numskin * sizeof(M3D_INDEX));
|
|
|
|
if (!skinidx) goto memerr;
|
|
|
|
skin = (m3dssave_t *)M3D_MALLOC(model->numskin * sizeof(m3dssave_t));
|
|
|
|
if (!skin) goto memerr;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(skinidx, 255, model->numskin * sizeof(M3D_INDEX));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numvertex; i++) {
|
|
|
|
if (vrtxidx[i] != M3D_UNDEF && model->vertex[i].skinid < model->numskin)
|
2019-11-18 02:04:52 +00:00
|
|
|
skinidx[model->vertex[i].skinid] = 0;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numskin; i++) {
|
|
|
|
if (skinidx[i] == M3D_UNDEF) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
memset(&sk, 0, sizeof(m3dssave_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0, min_x = (M3D_FLOAT)0.0; j < M3D_NUMBONE && model->skin[i].boneid[j] != M3D_UNDEF &&
|
|
|
|
model->skin[i].weight[j] > (M3D_FLOAT)0.0;
|
|
|
|
j++) {
|
|
|
|
sk.data.boneid[j] = model->skin[i].boneid[j];
|
|
|
|
sk.data.weight[j] = model->skin[i].weight[j];
|
|
|
|
min_x += sk.data.weight[j];
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (j > maxbone) maxbone = j;
|
|
|
|
if (min_x != (M3D_FLOAT)1.0 && min_x != (M3D_FLOAT)0.0)
|
|
|
|
for (j = 0; j < M3D_NUMBONE && sk.data.weight[j] > (M3D_FLOAT)0.0; j++)
|
2019-11-18 02:04:52 +00:00
|
|
|
sk.data.weight[j] /= min_x;
|
|
|
|
sk.oldidx = i;
|
|
|
|
memcpy(&skin[numskin++], &sk, sizeof(m3dssave_t));
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numskin) {
|
2019-11-18 02:04:52 +00:00
|
|
|
qsort(skin, numskin, sizeof(m3dssave_t), _m3d_skincmp);
|
|
|
|
memcpy(&sk.data, &skin[0].data, sizeof(m3ds_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numskin; i++) {
|
|
|
|
if (memcmp(&sk.data, &skin[i].data, sizeof(m3ds_t))) {
|
2019-11-18 02:04:52 +00:00
|
|
|
memcpy(&sk.data, &skin[i].data, sizeof(m3ds_t));
|
|
|
|
maxskin++;
|
|
|
|
}
|
|
|
|
skin[i].newidx = maxskin;
|
|
|
|
skinidx[skin[i].oldidx] = maxskin;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
maxskin++;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
|
|
|
|
M3D_LOG("Compressing vertex list");
|
|
|
|
min_x = min_y = min_z = (M3D_FLOAT)1e10;
|
|
|
|
max_x = max_y = max_z = (M3D_FLOAT)-1e10;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (vrtxidx) {
|
|
|
|
vrtx = (m3dvsave_t *)M3D_MALLOC(model->numvertex * sizeof(m3dvsave_t));
|
|
|
|
if (!vrtx) goto memerr;
|
|
|
|
for (i = numvrtx = 0; i < model->numvertex; i++) {
|
|
|
|
if (vrtxidx[i] == M3D_UNDEF) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
_m3d_round(quality, &model->vertex[i], &vertex.data);
|
|
|
|
vertex.norm = norm ? norm[i] : 0;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (vertex.data.skinid != M3D_INDEXMAX && !vertex.norm) {
|
2019-12-10 05:30:41 +00:00
|
|
|
vertex.data.skinid = vertex.data.skinid != M3D_UNDEF && skinidx ? skinidx[vertex.data.skinid] : M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (vertex.data.x > max_x) max_x = vertex.data.x;
|
|
|
|
if (vertex.data.x < min_x) min_x = vertex.data.x;
|
|
|
|
if (vertex.data.y > max_y) max_y = vertex.data.y;
|
|
|
|
if (vertex.data.y < min_y) min_y = vertex.data.y;
|
|
|
|
if (vertex.data.z > max_z) max_z = vertex.data.z;
|
|
|
|
if (vertex.data.z < min_z) min_z = vertex.data.z;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
#ifdef M3D_VERTEXTYPE
|
|
|
|
vertex.data.type = 0;
|
|
|
|
#endif
|
|
|
|
vertex.oldidx = i;
|
|
|
|
memcpy(&vrtx[numvrtx++], &vertex, sizeof(m3dvsave_t));
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numvrtx) {
|
2019-11-18 02:04:52 +00:00
|
|
|
qsort(vrtx, numvrtx, sizeof(m3dvsave_t), _m3d_vrtxcmp);
|
|
|
|
memcpy(&vertex.data, &vrtx[0].data, sizeof(m3dv_t));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numvrtx; i++) {
|
|
|
|
if (memcmp(&vertex.data, &vrtx[i].data, vrtx[i].norm ? 3 * sizeof(M3D_FLOAT) : sizeof(m3dv_t))) {
|
2019-11-18 02:04:52 +00:00
|
|
|
memcpy(&vertex.data, &vrtx[i].data, sizeof(m3dv_t));
|
|
|
|
maxvrtx++;
|
|
|
|
}
|
|
|
|
vrtx[i].newidx = maxvrtx;
|
|
|
|
vrtxidx[vrtx[i].oldidx] = maxvrtx;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
maxvrtx++;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (skinidx) {
|
|
|
|
M3D_FREE(skinidx);
|
|
|
|
skinidx = NULL;
|
|
|
|
}
|
|
|
|
if (norm) {
|
|
|
|
M3D_FREE(norm);
|
|
|
|
norm = NULL;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
|
|
|
|
/* normalize to bounding cube */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numvrtx && !(flags & M3D_EXP_NORECALC)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Normalizing coordinates");
|
2020-03-08 20:24:01 +00:00
|
|
|
if (min_x < (M3D_FLOAT)0.0) min_x = -min_x;
|
|
|
|
if (max_x < (M3D_FLOAT)0.0) max_x = -max_x;
|
|
|
|
if (min_y < (M3D_FLOAT)0.0) min_y = -min_y;
|
|
|
|
if (max_y < (M3D_FLOAT)0.0) max_y = -max_y;
|
|
|
|
if (min_z < (M3D_FLOAT)0.0) min_z = -min_z;
|
|
|
|
if (max_z < (M3D_FLOAT)0.0) max_z = -max_z;
|
2019-10-29 17:42:10 +00:00
|
|
|
scale = min_x;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (max_x > scale) scale = max_x;
|
|
|
|
if (min_y > scale) scale = min_y;
|
|
|
|
if (max_y > scale) scale = max_y;
|
|
|
|
if (min_z > scale) scale = min_z;
|
|
|
|
if (max_z > scale) scale = max_z;
|
|
|
|
if (scale == (M3D_FLOAT)0.0) scale = (M3D_FLOAT)1.0;
|
|
|
|
if (scale != (M3D_FLOAT)1.0) {
|
|
|
|
for (i = 0; i < numvrtx; i++) {
|
|
|
|
if (vrtx[i].data.skinid == M3D_INDEXMAX) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
vrtx[i].data.x /= scale;
|
|
|
|
vrtx[i].data.y /= scale;
|
|
|
|
vrtx[i].data.z /= scale;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->scale > (M3D_FLOAT)0.0) scale = model->scale;
|
|
|
|
if (scale <= (M3D_FLOAT)0.0) scale = (M3D_FLOAT)1.0;
|
2019-11-18 02:04:52 +00:00
|
|
|
|
2019-10-29 17:42:10 +00:00
|
|
|
/* meta info */
|
2020-03-08 20:24:01 +00:00
|
|
|
sn = _m3d_safestr(model->name && *model->name ? model->name : (char *)"(noname)", 2);
|
|
|
|
sl = _m3d_safestr(model->license ? model->license : (char *)"MIT", 2);
|
2019-10-29 17:42:10 +00:00
|
|
|
sa = _m3d_safestr(model->author ? model->author : getenv("LOGNAME"), 2);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sn || !sl || !sa) {
|
|
|
|
memerr:
|
|
|
|
if (vrtxidx) M3D_FREE(vrtxidx);
|
|
|
|
if (mtrlidx) M3D_FREE(mtrlidx);
|
|
|
|
if (tmapidx) M3D_FREE(tmapidx);
|
|
|
|
if (skinidx) M3D_FREE(skinidx);
|
|
|
|
if (grpidx) M3D_FREE(grpidx);
|
|
|
|
if (norm) M3D_FREE(norm);
|
|
|
|
if (face) M3D_FREE(face);
|
|
|
|
if (cmap) M3D_FREE(cmap);
|
|
|
|
if (tmap) M3D_FREE(tmap);
|
|
|
|
if (skin) M3D_FREE(skin);
|
|
|
|
if (str) M3D_FREE(str);
|
|
|
|
if (vrtx) M3D_FREE(vrtx);
|
|
|
|
if (sn) M3D_FREE(sn);
|
|
|
|
if (sl) M3D_FREE(sl);
|
|
|
|
if (sa) M3D_FREE(sa);
|
|
|
|
if (sd) M3D_FREE(sd);
|
|
|
|
if (out) M3D_FREE(out);
|
|
|
|
if (h) M3D_FREE(h);
|
2019-10-29 17:42:10 +00:00
|
|
|
M3D_LOG("Out of memory");
|
|
|
|
model->errcode = M3D_ERR_ALLOC;
|
|
|
|
return NULL;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
|
|
|
|
M3D_LOG("Serializing model");
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifdef M3D_ASCII
|
2020-03-08 20:24:01 +00:00
|
|
|
if (flags & M3D_EXP_ASCII) {
|
2019-10-29 17:42:10 +00:00
|
|
|
/* use CRLF to make model creators on Win happy... */
|
|
|
|
sd = _m3d_safestr(model->desc, 1);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sd) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
ol = setlocale(LC_NUMERIC, NULL);
|
|
|
|
setlocale(LC_NUMERIC, "C");
|
|
|
|
/* header */
|
2019-12-10 05:30:41 +00:00
|
|
|
len = 64 + (unsigned int)(strlen(sn) + strlen(sl) + strlen(sa) + strlen(sd));
|
2020-03-08 20:24:01 +00:00
|
|
|
out = (unsigned char *)M3D_MALLOC(len);
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
|
|
|
ptr = (char *)out;
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "3dmodel %g\r\n%s\r\n%s\r\n%s\r\n%s\r\n\r\n", scale,
|
2020-03-08 20:24:01 +00:00
|
|
|
sn, sl, sa, sd);
|
|
|
|
M3D_FREE(sl);
|
|
|
|
M3D_FREE(sa);
|
|
|
|
M3D_FREE(sd);
|
2019-11-18 02:04:52 +00:00
|
|
|
sl = sa = sd = NULL;
|
|
|
|
/* preview chunk */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->preview.data && model->preview.length) {
|
2019-11-18 02:04:52 +00:00
|
|
|
sl = _m3d_safestr(sn, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (sl) {
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)20);
|
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "Preview\r\n%s.png\r\n\r\n", sl);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sl);
|
|
|
|
sl = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sn);
|
|
|
|
sn = NULL;
|
2019-10-29 17:42:10 +00:00
|
|
|
/* texture map */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numtmap && tmap && !(flags & M3D_EXP_NOTXTCRD) && !(flags & M3D_EXP_NOFACE)) {
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)(maxtmap * 32) + (uintptr_t)12);
|
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "Textmap\r\n");
|
2019-12-10 05:30:41 +00:00
|
|
|
last = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numtmap; i++) {
|
|
|
|
if (tmap[i].newidx == last) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
last = tmap[i].newidx;
|
|
|
|
ptr += sprintf(ptr, "%g %g\r\n", tmap[i].data.u, tmap[i].data.v);
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
|
|
|
/* vertex chunk */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numvrtx && vrtx && !(flags & M3D_EXP_NOFACE)) {
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)(maxvrtx * 128) + (uintptr_t)10);
|
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "Vertex\r\n");
|
2019-12-10 05:30:41 +00:00
|
|
|
last = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numvrtx; i++) {
|
|
|
|
if (vrtx[i].newidx == last) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
last = vrtx[i].newidx;
|
|
|
|
ptr += sprintf(ptr, "%g %g %g %g", vrtx[i].data.x, vrtx[i].data.y, vrtx[i].data.z, vrtx[i].data.w);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOCMAP) && vrtx[i].data.color)
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, " #%08x", vrtx[i].data.color);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOBONE) && model->numbone && maxskin && vrtx[i].data.skinid < M3D_INDEXMAX) {
|
|
|
|
if (skin[vrtx[i].data.skinid].data.weight[0] == (M3D_FLOAT)1.0)
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, " %d", skin[vrtx[i].data.skinid].data.boneid[0]);
|
2019-10-29 17:42:10 +00:00
|
|
|
else
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < M3D_NUMBONE && skin[vrtx[i].data.skinid].data.boneid[j] != M3D_UNDEF &&
|
|
|
|
skin[vrtx[i].data.skinid].data.weight[j] > (M3D_FLOAT)0.0;
|
|
|
|
j++)
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, " %d:%g", skin[vrtx[i].data.skinid].data.boneid[j],
|
2020-03-08 20:24:01 +00:00
|
|
|
skin[vrtx[i].data.skinid].data.weight[j]);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
|
|
|
/* bones chunk */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numbone && model->bone && !(flags & M3D_EXP_NOBONE)) {
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)9);
|
|
|
|
for (i = 0; i < model->numbone; i++) {
|
2019-12-10 05:30:41 +00:00
|
|
|
len += (unsigned int)strlen(model->bone[i].name) + 128;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "Bones\r\n");
|
2019-12-10 05:30:41 +00:00
|
|
|
ptr = _m3d_prtbone(ptr, model->bone, model->numbone, M3D_UNDEF, 0, vrtxidx);
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
|
|
|
/* materials */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->nummaterial && !(flags & M3D_EXP_NOMATERIAL)) {
|
|
|
|
for (j = 0; j < model->nummaterial; j++) {
|
|
|
|
if (mtrlidx[j] == M3D_UNDEF || !model->material[j].numprop || !model->material[j].prop) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
m = &model->material[j];
|
2019-10-29 17:42:10 +00:00
|
|
|
sn = _m3d_safestr(m->name, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sn) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)strlen(sn) + (uintptr_t)12);
|
|
|
|
for (i = 0; i < m->numprop; i++) {
|
|
|
|
if (m->prop[i].type < 128)
|
2019-10-29 17:42:10 +00:00
|
|
|
len += 32;
|
2020-03-08 20:24:01 +00:00
|
|
|
else if (m->prop[i].value.textureid < model->numtexture && model->texture[m->prop[i].value.textureid].name)
|
2019-12-10 05:30:41 +00:00
|
|
|
len += (unsigned int)strlen(model->texture[m->prop[i].value.textureid].name) + 16;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "Material %s\r\n", sn);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sn);
|
|
|
|
sn = NULL;
|
|
|
|
for (i = 0; i < m->numprop; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
k = 256;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (m->prop[i].type >= 128) {
|
|
|
|
for (l = 0; l < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); l++)
|
|
|
|
if (m->prop[i].type == m3d_propertytypes[l].id) {
|
2019-10-29 17:42:10 +00:00
|
|
|
sn = m3d_propertytypes[l].key;
|
|
|
|
break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sn)
|
|
|
|
for (l = 0; l < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); l++)
|
|
|
|
if (m->prop[i].type - 128 == m3d_propertytypes[l].id) {
|
2019-10-29 17:42:10 +00:00
|
|
|
sn = m3d_propertytypes[l].key;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
k = sn ? m3dpf_map : 256;
|
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
for (l = 0; l < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); l++)
|
|
|
|
if (m->prop[i].type == m3d_propertytypes[l].id) {
|
2019-10-29 17:42:10 +00:00
|
|
|
sn = m3d_propertytypes[l].key;
|
|
|
|
k = m3d_propertytypes[l].format;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (k) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case m3dpf_color: ptr += sprintf(ptr, "%s #%08x\r\n", sn, m->prop[i].value.color); break;
|
|
|
|
case m3dpf_uint8:
|
|
|
|
case m3dpf_uint16:
|
|
|
|
case m3dpf_uint32: ptr += sprintf(ptr, "%s %d\r\n", sn, m->prop[i].value.num); break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case m3dpf_float: ptr += sprintf(ptr, "%s %g\r\n", sn, m->prop[i].value.fnum); break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case m3dpf_map:
|
2020-03-08 20:24:01 +00:00
|
|
|
if (m->prop[i].value.textureid < model->numtexture &&
|
|
|
|
model->texture[m->prop[i].value.textureid].name) {
|
2019-10-29 17:42:10 +00:00
|
|
|
sl = _m3d_safestr(model->texture[m->prop[i].value.textureid].name, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sl) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
|
|
|
if (*sl)
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "map_%s %s\r\n", sn, sl);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sn);
|
|
|
|
M3D_FREE(sl);
|
|
|
|
sl = NULL;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
sn = NULL;
|
|
|
|
}
|
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* procedural face */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numinlined && model->inlined && !(flags & M3D_EXP_NOFACE)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
/* all inlined assets which are not textures should be procedural surfaces */
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < model->numinlined; j++) {
|
|
|
|
if (!model->inlined[j].name || !*model->inlined[j].name || !model->inlined[j].length || !model->inlined[j].data ||
|
|
|
|
(model->inlined[j].data[1] == 'P' && model->inlined[j].data[2] == 'N' && model->inlined[j].data[3] == 'G'))
|
2019-11-18 02:04:52 +00:00
|
|
|
continue;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = k = 0; i < model->numtexture; i++) {
|
|
|
|
if (!strcmp(model->inlined[j].name, model->texture[i].name)) {
|
|
|
|
k = 1;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
sn = _m3d_safestr(model->inlined[j].name, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sn) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)strlen(sn) + (uintptr_t)18);
|
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "Procedural\r\n%s\r\n\r\n", sn);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sn);
|
|
|
|
sn = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
/* mesh face */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numface && face && !(flags & M3D_EXP_NOFACE)) {
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)(model->numface * 128) + (uintptr_t)6);
|
2019-12-10 05:30:41 +00:00
|
|
|
last = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOMATERIAL))
|
|
|
|
for (i = 0; i < model->numface; i++) {
|
2019-12-10 05:30:41 +00:00
|
|
|
j = face[i].data.materialid < model->nummaterial ? face[i].data.materialid : M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (j != last) {
|
2019-11-18 02:04:52 +00:00
|
|
|
last = j;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (last < model->nummaterial)
|
2019-12-10 05:30:41 +00:00
|
|
|
len += (unsigned int)strlen(model->material[last].name);
|
2019-10-29 17:42:10 +00:00
|
|
|
len += 6;
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "Mesh\r\n");
|
2019-12-10 05:30:41 +00:00
|
|
|
last = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numface; i++) {
|
2019-12-10 05:30:41 +00:00
|
|
|
j = face[i].data.materialid < model->nummaterial ? face[i].data.materialid : M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOMATERIAL) && j != last) {
|
2019-11-18 02:04:52 +00:00
|
|
|
last = j;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (last < model->nummaterial) {
|
2019-11-18 02:04:52 +00:00
|
|
|
sn = _m3d_safestr(model->material[last].name, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sn) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "use %s\r\n", sn);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sn);
|
|
|
|
sn = NULL;
|
2019-10-29 17:42:10 +00:00
|
|
|
} else
|
|
|
|
ptr += sprintf(ptr, "use\r\n");
|
|
|
|
}
|
|
|
|
/* hardcoded triangles. Should be repeated as many times as the number of edges in polygon */
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < 3; j++) {
|
|
|
|
ptr += sprintf(ptr, "%s%d", j ? " " : "", vrtxidx[face[i].data.vertex[j]]);
|
2019-12-10 05:30:41 +00:00
|
|
|
k = M3D_NOTDEFINED;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOTXTCRD) && (face[i].data.texcoord[j] != M3D_UNDEF) &&
|
|
|
|
(tmapidx[face[i].data.texcoord[j]] != M3D_UNDEF)) {
|
|
|
|
k = tmapidx[face[i].data.texcoord[j]];
|
|
|
|
ptr += sprintf(ptr, "/%d", k);
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NONORMAL) && (face[i].data.normal[j] != M3D_UNDEF))
|
|
|
|
ptr += sprintf(ptr, "%s/%d", k == M3D_NOTDEFINED ? "/" : "", vrtxidx[face[i].data.normal[j]]);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* mathematical shapes face */
|
2020-06-06 10:22:01 +00:00
|
|
|
if (model->numshape !(flags & M3D_EXP_NOFACE)) {
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < model->numshape; j++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
sn = _m3d_safestr(model->shape[j].name, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sn) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)strlen(sn) + (uintptr_t)33);
|
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "Shape %s\r\n", sn);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sn);
|
|
|
|
sn = NULL;
|
|
|
|
if (model->shape[j].group != M3D_UNDEF && !(flags & M3D_EXP_NOBONE))
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "group %d\r\n", model->shape[j].group);
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->shape[j].numcmd; i++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
cmd = &model->shape[j].cmd[i];
|
2020-03-08 20:24:01 +00:00
|
|
|
if (cmd->type >= (unsigned int)(sizeof(m3d_commandtypes) / sizeof(m3d_commandtypes[0])) || !cmd->arg)
|
2019-11-18 02:04:52 +00:00
|
|
|
continue;
|
|
|
|
cd = &m3d_commandtypes[cmd->type];
|
2020-03-08 20:24:01 +00:00
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)strlen(cd->key) + (uintptr_t)3);
|
|
|
|
for (k = 0; k < cd->p; k++)
|
|
|
|
switch (cd->a[k]) {
|
|
|
|
case m3dcp_mi_t:
|
|
|
|
if (cmd->arg[k] != M3D_NOTDEFINED) {
|
|
|
|
len += (unsigned int)strlen(model->material[cmd->arg[k]].name) + 1;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case m3dcp_va_t:
|
|
|
|
len += cmd->arg[k] * (cd->p - k - 1) * 16;
|
|
|
|
k = cd->p;
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
default: len += 16; break;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "%s", cd->key);
|
2020-03-08 20:24:01 +00:00
|
|
|
for (k = n = 0, l = cd->p; k < l; k++) {
|
|
|
|
switch (cd->a[((k - n) % (cd->p - n)) + n]) {
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_mi_t:
|
2020-03-08 20:24:01 +00:00
|
|
|
if (cmd->arg[k] != M3D_NOTDEFINED) {
|
2019-11-18 02:04:52 +00:00
|
|
|
sn = _m3d_safestr(model->material[cmd->arg[k]].name, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sn) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, " %s", sn);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sn);
|
|
|
|
sn = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
|
|
|
case m3dcp_vc_t: ptr += sprintf(ptr, " %g", *((float *)&cmd->arg[k])); break;
|
|
|
|
case m3dcp_va_t:
|
|
|
|
ptr += sprintf(ptr, " %d[", cmd->arg[k]);
|
|
|
|
n = k + 1;
|
|
|
|
l += (cmd->arg[k] - 1) * (cd->p - k - 1);
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
default: ptr += sprintf(ptr, " %d", cmd->arg[k]); break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr += sprintf(ptr, "%s\r\n", l > cd->p ? " ]" : "");
|
|
|
|
}
|
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* annotation labels */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numlabel && model->label && !(flags & M3D_EXP_NOFACE)) {
|
|
|
|
for (i = 0, j = 3, length = NULL; i < model->numlabel; i++) {
|
|
|
|
if (model->label[i].name) j += (unsigned int)strlen(model->label[i].name);
|
|
|
|
if (model->label[i].lang) j += (unsigned int)strlen(model->label[i].lang);
|
|
|
|
if (model->label[i].text) j += (unsigned int)strlen(model->label[i].text);
|
2019-11-18 02:04:52 +00:00
|
|
|
j += 40;
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)j);
|
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
|
|
|
for (i = 0; i < model->numlabel; i++) {
|
|
|
|
if (!i || _m3d_strcmp(sl, model->label[i].lang) || _m3d_strcmp(sn, model->label[i].name)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
sl = model->label[i].lang;
|
|
|
|
sn = model->label[i].name;
|
|
|
|
sd = _m3d_safestr(sn, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sd) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
sn = sl = NULL;
|
|
|
|
goto memerr;
|
|
|
|
}
|
|
|
|
if (i) ptr += sprintf(ptr, "\r\n");
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "Labels %s\r\n", sd);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sd);
|
|
|
|
sd = NULL;
|
|
|
|
if (model->label[i].color)
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "color #0x%08x\r\n", model->label[i].color);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (sl && *sl) {
|
2019-11-18 02:04:52 +00:00
|
|
|
sd = _m3d_safestr(sl, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sd) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
sn = sl = NULL;
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "lang %s\r\n", sd);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sd);
|
|
|
|
sd = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
sd = _m3d_safestr(model->label[i].text, 2);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sd) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
sn = sl = NULL;
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "%d %s\r\n", model->label[i].vertexid, sd);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sd);
|
|
|
|
sd = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
sn = sl = NULL;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
/* actions */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numaction && model->action && !(flags & M3D_EXP_NOACTION)) {
|
|
|
|
for (j = 0; j < model->numaction; j++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
a = &model->action[j];
|
|
|
|
sn = _m3d_safestr(a->name, 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sn) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)strlen(sn) + (uintptr_t)48);
|
|
|
|
for (i = 0; i < a->numframe; i++)
|
2019-10-29 17:42:10 +00:00
|
|
|
len += a->frame[i].numtransform * 128 + 8;
|
2020-03-08 20:24:01 +00:00
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "Action %d %s\r\n", a->durationmsec, sn);
|
2020-03-08 20:24:01 +00:00
|
|
|
M3D_FREE(sn);
|
|
|
|
sn = NULL;
|
|
|
|
for (i = 0; i < a->numframe; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "frame %d\r\n", a->frame[i].msec);
|
2020-03-08 20:24:01 +00:00
|
|
|
for (k = 0; k < a->frame[i].numtransform; k++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "%d %d %d\r\n", a->frame[i].transform[k].boneid,
|
2020-03-08 20:24:01 +00:00
|
|
|
vrtxidx[a->frame[i].transform[k].pos], vrtxidx[a->frame[i].transform[k].ori]);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* inlined assets */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numinlined && model->inlined) {
|
|
|
|
for (i = j = 0; i < model->numinlined; i++)
|
|
|
|
if (model->inlined[i].name)
|
2019-12-10 05:30:41 +00:00
|
|
|
j += (unsigned int)strlen(model->inlined[i].name) + 6;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (j > 0) {
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)j + (uintptr_t)16);
|
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "Assets\r\n");
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numinlined; i++)
|
|
|
|
if (model->inlined[i].name)
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "%s%s\r\n", model->inlined[i].name, strrchr(model->inlined[i].name, '.') ? "" : ".png");
|
|
|
|
ptr += sprintf(ptr, "\r\n");
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
/* extra info */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numextra && (flags & M3D_EXP_EXTRA)) {
|
|
|
|
for (i = 0; i < model->numextra; i++) {
|
|
|
|
if (model->extra[i]->length < 9) continue;
|
|
|
|
ptr -= (uintptr_t)out;
|
|
|
|
len = (unsigned int)((uintptr_t)ptr + (uintptr_t)17 + (uintptr_t)(model->extra[i]->length * 3));
|
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len);
|
|
|
|
ptr += (uintptr_t)out;
|
|
|
|
if (!out) {
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
|
|
|
goto memerr;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr += sprintf(ptr, "Extra %c%c%c%c\r\n",
|
2020-03-08 20:24:01 +00:00
|
|
|
model->extra[i]->magic[0] > ' ' ? model->extra[i]->magic[0] : '_',
|
|
|
|
model->extra[i]->magic[1] > ' ' ? model->extra[i]->magic[1] : '_',
|
|
|
|
model->extra[i]->magic[2] > ' ' ? model->extra[i]->magic[2] : '_',
|
|
|
|
model->extra[i]->magic[3] > ' ' ? model->extra[i]->magic[3] : '_');
|
|
|
|
for (j = 0; j < model->extra[i]->length; j++)
|
2019-11-18 02:04:52 +00:00
|
|
|
ptr += sprintf(ptr, "%02x ", *((unsigned char *)model->extra + sizeof(m3dchunk_t) + j));
|
2019-10-29 17:42:10 +00:00
|
|
|
ptr--;
|
|
|
|
ptr += sprintf(ptr, "\r\n\r\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setlocale(LC_NUMERIC, ol);
|
2019-12-10 05:30:41 +00:00
|
|
|
len = (unsigned int)((uintptr_t)ptr - (uintptr_t)out);
|
2020-03-08 20:24:01 +00:00
|
|
|
out = (unsigned char *)M3D_REALLOC(out, len + 1);
|
|
|
|
if (!out) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
out[len] = 0;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
/* stricly only use LF (newline) in binary */
|
|
|
|
sd = _m3d_safestr(model->desc, 3);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!sd) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
/* header */
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_MALLOC(sizeof(m3dhdr_t) + strlen(sn) + strlen(sl) + strlen(sa) + strlen(sd) + 4);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h, "HEAD", 4);
|
2019-10-29 17:42:10 +00:00
|
|
|
h->length = sizeof(m3dhdr_t);
|
|
|
|
h->scale = scale;
|
2020-03-08 20:24:01 +00:00
|
|
|
i = (unsigned int)strlen(sn);
|
|
|
|
memcpy((uint8_t *)h + h->length, sn, i + 1);
|
|
|
|
h->length += i + 1;
|
|
|
|
M3D_FREE(sn);
|
|
|
|
i = (unsigned int)strlen(sl);
|
|
|
|
memcpy((uint8_t *)h + h->length, sl, i + 1);
|
|
|
|
h->length += i + 1;
|
|
|
|
M3D_FREE(sl);
|
|
|
|
i = (unsigned int)strlen(sa);
|
|
|
|
memcpy((uint8_t *)h + h->length, sa, i + 1);
|
|
|
|
h->length += i + 1;
|
|
|
|
M3D_FREE(sa);
|
|
|
|
i = (unsigned int)strlen(sd);
|
|
|
|
memcpy((uint8_t *)h + h->length, sd, i + 1);
|
|
|
|
h->length += i + 1;
|
|
|
|
M3D_FREE(sd);
|
2019-10-29 17:42:10 +00:00
|
|
|
sn = sl = sa = sd = NULL;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->inlined)
|
|
|
|
for (i = 0; i < model->numinlined; i++) {
|
|
|
|
if (model->inlined[i].name && *model->inlined[i].name && model->inlined[i].length > 0) {
|
2019-10-29 17:42:10 +00:00
|
|
|
str = _m3d_addstr(str, &numstr, model->inlined[i].name);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!str) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (str)
|
|
|
|
for (i = 0; i < numstr; i++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
h = _m3d_addhdr(h, &str[i]);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!h) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
vc_s = quality == M3D_EXP_INT8 ? 1 : (quality == M3D_EXP_INT16 ? 2 : (quality == M3D_EXP_DOUBLE ? 8 : 4));
|
2019-11-18 02:04:52 +00:00
|
|
|
vi_s = maxvrtx < 254 ? 1 : (maxvrtx < 65534 ? 2 : 4);
|
2019-10-29 17:42:10 +00:00
|
|
|
si_s = h->length - 16 < 254 ? 1 : (h->length - 16 < 65534 ? 2 : 4);
|
|
|
|
ci_s = !numcmap || !cmap ? 0 : (numcmap < 254 ? 1 : (numcmap < 65534 ? 2 : 4));
|
2019-11-18 02:04:52 +00:00
|
|
|
ti_s = !maxtmap || !tmap ? 0 : (maxtmap < 254 ? 1 : (maxtmap < 65534 ? 2 : 4));
|
2020-03-08 20:24:01 +00:00
|
|
|
bi_s = !model->numbone || !model->bone || (flags & M3D_EXP_NOBONE) ? 0 : (model->numbone < 254 ? 1 : (model->numbone < 65534 ? 2 : 4));
|
2019-11-18 02:04:52 +00:00
|
|
|
nb_s = maxbone < 2 ? 1 : (maxbone == 2 ? 2 : (maxbone <= 4 ? 4 : 8));
|
|
|
|
sk_s = !bi_s || !maxskin || !skin ? 0 : (maxskin < 254 ? 1 : (maxskin < 65534 ? 2 : 4));
|
|
|
|
fc_s = maxt < 254 ? 1 : (maxt < 65534 ? 2 : 4);
|
2020-03-08 20:24:01 +00:00
|
|
|
hi_s = !model->numshape || !model->shape || (flags & M3D_EXP_NOFACE) ? 0 : (model->numshape < 254 ? 1 : (model->numshape < 65534 ? 2 : 4));
|
|
|
|
fi_s = !model->numface || !model->face || (flags & M3D_EXP_NOFACE) ? 0 : (model->numface < 254 ? 1 : (model->numface < 65534 ? 2 : 4));
|
|
|
|
h->types = (vc_s == 8 ? (3 << 0) : (vc_s == 2 ? (1 << 0) : (vc_s == 1 ? (0 << 0) : (2 << 0)))) |
|
|
|
|
(vi_s == 2 ? (1 << 2) : (vi_s == 1 ? (0 << 2) : (2 << 2))) |
|
|
|
|
(si_s == 2 ? (1 << 4) : (si_s == 1 ? (0 << 4) : (2 << 4))) |
|
|
|
|
(ci_s == 2 ? (1 << 6) : (ci_s == 1 ? (0 << 6) : (ci_s == 4 ? (2 << 6) : (3 << 6)))) |
|
|
|
|
(ti_s == 2 ? (1 << 8) : (ti_s == 1 ? (0 << 8) : (ti_s == 4 ? (2 << 8) : (3 << 8)))) |
|
|
|
|
(bi_s == 2 ? (1 << 10) : (bi_s == 1 ? (0 << 10) : (bi_s == 4 ? (2 << 10) : (3 << 10)))) |
|
|
|
|
(nb_s == 2 ? (1 << 12) : (nb_s == 1 ? (0 << 12) : (2 << 12))) |
|
|
|
|
(sk_s == 2 ? (1 << 14) : (sk_s == 1 ? (0 << 14) : (sk_s == 4 ? (2 << 14) : (3 << 14)))) |
|
|
|
|
(fc_s == 2 ? (1 << 16) : (fc_s == 1 ? (0 << 16) : (2 << 16))) |
|
|
|
|
(hi_s == 2 ? (1 << 18) : (hi_s == 1 ? (0 << 18) : (hi_s == 4 ? (2 << 18) : (3 << 18)))) |
|
|
|
|
(fi_s == 2 ? (1 << 20) : (fi_s == 1 ? (0 << 20) : (fi_s == 4 ? (2 << 20) : (3 << 20))));
|
2019-10-29 17:42:10 +00:00
|
|
|
len = h->length;
|
2019-11-18 02:04:52 +00:00
|
|
|
/* preview image chunk, must be the first if exists */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->preview.data && model->preview.length) {
|
2019-11-18 02:04:52 +00:00
|
|
|
chunklen = 8 + model->preview.length;
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "PRVW", 4);
|
|
|
|
*((uint32_t *)((uint8_t *)h + len + 4)) = chunklen;
|
|
|
|
memcpy((uint8_t *)h + len + 8, model->preview.data, model->preview.length);
|
2019-11-18 02:04:52 +00:00
|
|
|
len += chunklen;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
/* color map */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numcmap && cmap && ci_s < 4 && !(flags & M3D_EXP_NOCMAP)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
chunklen = 8 + numcmap * sizeof(uint32_t);
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "CMAP", 4);
|
|
|
|
*((uint32_t *)((uint8_t *)h + len + 4)) = chunklen;
|
|
|
|
memcpy((uint8_t *)h + len + 8, cmap, chunklen - 8);
|
2019-10-29 17:42:10 +00:00
|
|
|
len += chunklen;
|
2020-03-08 20:24:01 +00:00
|
|
|
} else
|
|
|
|
numcmap = 0;
|
2019-10-29 17:42:10 +00:00
|
|
|
/* texture map */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numtmap && tmap && !(flags & M3D_EXP_NOTXTCRD) && !(flags & M3D_EXP_NOFACE)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
chunklen = 8 + maxtmap * vc_s * 2;
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "TMAP", 4);
|
|
|
|
length = (uint32_t *)((uint8_t *)h + len + 4);
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-12-10 05:30:41 +00:00
|
|
|
last = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numtmap; i++) {
|
|
|
|
if (tmap[i].newidx == last) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
last = tmap[i].newidx;
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (vc_s) {
|
|
|
|
case 1:
|
|
|
|
*out++ = (uint8_t)(tmap[i].data.u * 255);
|
|
|
|
*out++ = (uint8_t)(tmap[i].data.v * 255);
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 2:
|
2020-03-08 20:24:01 +00:00
|
|
|
*((uint16_t *)out) = (uint16_t)(tmap[i].data.u * 65535);
|
|
|
|
out += 2;
|
|
|
|
*((uint16_t *)out) = (uint16_t)(tmap[i].data.v * 65535);
|
|
|
|
out += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*((float *)out) = tmap[i].data.u;
|
|
|
|
out += 4;
|
|
|
|
*((float *)out) = tmap[i].data.v;
|
|
|
|
out += 4;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
*((double *)out) = tmap[i].data.u;
|
|
|
|
out += 8;
|
|
|
|
*((double *)out) = tmap[i].data.v;
|
|
|
|
out += 8;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
*length = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
2019-10-29 17:42:10 +00:00
|
|
|
out = NULL;
|
2019-11-18 02:04:52 +00:00
|
|
|
len += *length;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
/* vertex */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numvrtx && vrtx) {
|
2019-11-18 02:04:52 +00:00
|
|
|
chunklen = 8 + maxvrtx * (ci_s + sk_s + 4 * vc_s);
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "VRTS", 4);
|
|
|
|
length = (uint32_t *)((uint8_t *)h + len + 4);
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-12-10 05:30:41 +00:00
|
|
|
last = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numvrtx; i++) {
|
|
|
|
if (vrtx[i].newidx == last) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
last = vrtx[i].newidx;
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (vc_s) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case 1:
|
2019-11-18 02:04:52 +00:00
|
|
|
*out++ = (int8_t)(vrtx[i].data.x * 127);
|
|
|
|
*out++ = (int8_t)(vrtx[i].data.y * 127);
|
|
|
|
*out++ = (int8_t)(vrtx[i].data.z * 127);
|
|
|
|
*out++ = (int8_t)(vrtx[i].data.w * 127);
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 2:
|
2020-03-08 20:24:01 +00:00
|
|
|
*((int16_t *)out) = (int16_t)(vrtx[i].data.x * 32767);
|
|
|
|
out += 2;
|
|
|
|
*((int16_t *)out) = (int16_t)(vrtx[i].data.y * 32767);
|
|
|
|
out += 2;
|
|
|
|
*((int16_t *)out) = (int16_t)(vrtx[i].data.z * 32767);
|
|
|
|
out += 2;
|
|
|
|
*((int16_t *)out) = (int16_t)(vrtx[i].data.w * 32767);
|
|
|
|
out += 2;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 4:
|
2020-03-27 10:38:38 +00:00
|
|
|
memcpy(out, &vrtx[i].data.x, sizeof(float));
|
2020-03-08 20:24:01 +00:00
|
|
|
out += 4;
|
2020-03-27 10:38:38 +00:00
|
|
|
memcpy(out, &vrtx[i].data.y, sizeof(float));
|
2020-03-08 20:24:01 +00:00
|
|
|
out += 4;
|
2020-03-27 10:38:38 +00:00
|
|
|
memcpy(out, &vrtx[i].data.z, sizeof(float));
|
2020-03-08 20:24:01 +00:00
|
|
|
out += 4;
|
2020-03-27 10:38:38 +00:00
|
|
|
memcpy(out, &vrtx[i].data.w, sizeof(float));
|
2020-03-08 20:24:01 +00:00
|
|
|
out += 4;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
case 8:
|
2020-03-08 20:24:01 +00:00
|
|
|
*((double *)out) = vrtx[i].data.x;
|
|
|
|
out += 8;
|
|
|
|
*((double *)out) = vrtx[i].data.y;
|
|
|
|
out += 8;
|
|
|
|
*((double *)out) = vrtx[i].data.z;
|
|
|
|
out += 8;
|
|
|
|
*((double *)out) = vrtx[i].data.w;
|
|
|
|
out += 8;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
idx = _m3d_cmapidx(cmap, numcmap, vrtx[i].data.color);
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (ci_s) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case 1: *out++ = (uint8_t)(idx); break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case 2:
|
|
|
|
*((uint16_t *)out) = (uint16_t)(idx);
|
|
|
|
out += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*((uint32_t *)out) = vrtx[i].data.color;
|
|
|
|
out += 4;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, sk_s, vrtx[i].data.skinid);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-27 14:39:49 +00:00
|
|
|
uint32_t v = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
|
|
|
memcpy(length, &v, sizeof(uint32_t));
|
2020-03-27 14:28:32 +00:00
|
|
|
//*length = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
2019-10-29 17:42:10 +00:00
|
|
|
out = NULL;
|
2020-03-27 15:27:07 +00:00
|
|
|
len += v;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
/* bones chunk */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numbone && model->bone && !(flags & M3D_EXP_NOBONE)) {
|
|
|
|
i = 8 + bi_s + sk_s + model->numbone * (bi_s + si_s + 2 * vi_s);
|
2019-10-29 17:42:10 +00:00
|
|
|
chunklen = i + numskin * nb_s * (bi_s + 1);
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "BONE", 4);
|
|
|
|
length = (uint32_t *)((uint8_t *)h + len + 4);
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, bi_s, model->numbone);
|
|
|
|
out = _m3d_addidx(out, sk_s, maxskin);
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numbone; i++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, bi_s, model->bone[i].parent);
|
|
|
|
out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, model->bone[i].name));
|
|
|
|
out = _m3d_addidx(out, vi_s, vrtxidx[model->bone[i].pos]);
|
|
|
|
out = _m3d_addidx(out, vi_s, vrtxidx[model->bone[i].ori]);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (numskin && skin && sk_s) {
|
2019-12-10 05:30:41 +00:00
|
|
|
last = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < numskin; i++) {
|
|
|
|
if (skin[i].newidx == last) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
last = skin[i].newidx;
|
2019-10-29 17:42:10 +00:00
|
|
|
memset(&weights, 0, nb_s);
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < (uint32_t)nb_s && skin[i].data.boneid[j] != M3D_UNDEF &&
|
|
|
|
skin[i].data.weight[j] > (M3D_FLOAT)0.0;
|
|
|
|
j++)
|
|
|
|
weights[j] = (uint8_t)(skin[i].data.weight[j] * 255);
|
|
|
|
switch (nb_s) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case 1: weights[0] = 255; break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case 2:
|
|
|
|
*((uint16_t *)out) = *((uint16_t *)&weights[0]);
|
|
|
|
out += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*((uint32_t *)out) = *((uint32_t *)&weights[0]);
|
|
|
|
out += 4;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
*((uint64_t *)out) = *((uint64_t *)&weights[0]);
|
|
|
|
out += 8;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < (uint32_t)nb_s && skin[i].data.boneid[j] != M3D_UNDEF && weights[j]; j++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, bi_s, skin[i].data.boneid[j]);
|
2019-10-29 17:42:10 +00:00
|
|
|
*length += bi_s;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
*length = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
2019-10-29 17:42:10 +00:00
|
|
|
out = NULL;
|
|
|
|
len += *length;
|
|
|
|
}
|
|
|
|
/* materials */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->nummaterial && !(flags & M3D_EXP_NOMATERIAL)) {
|
|
|
|
for (j = 0; j < model->nummaterial; j++) {
|
|
|
|
if (mtrlidx[j] == M3D_UNDEF || !model->material[j].numprop || !model->material[j].prop) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
m = &model->material[j];
|
2019-10-29 17:42:10 +00:00
|
|
|
chunklen = 12 + si_s + m->numprop * 5;
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "MTRL", 4);
|
|
|
|
length = (uint32_t *)((uint8_t *)h + len + 4);
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-10-29 17:42:10 +00:00
|
|
|
out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, m->name));
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < m->numprop; i++) {
|
|
|
|
if (m->prop[i].type >= 128) {
|
|
|
|
if (m->prop[i].value.textureid >= model->numtexture ||
|
|
|
|
!model->texture[m->prop[i].value.textureid].name) continue;
|
2019-10-29 17:42:10 +00:00
|
|
|
k = m3dpf_map;
|
|
|
|
} else {
|
2020-03-08 20:24:01 +00:00
|
|
|
for (k = 256, l = 0; l < sizeof(m3d_propertytypes) / sizeof(m3d_propertytypes[0]); l++)
|
|
|
|
if (m->prop[i].type == m3d_propertytypes[l].id) {
|
|
|
|
k = m3d_propertytypes[l].format;
|
|
|
|
break;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k == 256) continue;
|
2019-10-29 17:42:10 +00:00
|
|
|
*out++ = m->prop[i].type;
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (k) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case m3dpf_color:
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOCMAP)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
idx = _m3d_cmapidx(cmap, numcmap, m->prop[i].value.color);
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (ci_s) {
|
2019-10-29 17:42:10 +00:00
|
|
|
case 1: *out++ = (uint8_t)(idx); break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case 2:
|
|
|
|
*((uint16_t *)out) = (uint16_t)(idx);
|
|
|
|
out += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*((uint32_t *)out) = (uint32_t)(m->prop[i].value.color);
|
|
|
|
out += 4;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
} else
|
|
|
|
out--;
|
|
|
|
break;
|
|
|
|
case m3dpf_uint8: *out++ = m->prop[i].value.num; break;
|
|
|
|
case m3dpf_uint16:
|
|
|
|
*((uint16_t *)out) = m->prop[i].value.num;
|
|
|
|
out += 2;
|
|
|
|
break;
|
|
|
|
case m3dpf_uint32:
|
|
|
|
*((uint32_t *)out) = m->prop[i].value.num;
|
|
|
|
out += 4;
|
|
|
|
break;
|
|
|
|
case m3dpf_float:
|
|
|
|
*((float *)out) = m->prop[i].value.fnum;
|
|
|
|
out += 4;
|
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
case m3dpf_map:
|
|
|
|
idx = _m3d_stridx(str, numstr, model->texture[m->prop[i].value.textureid].name);
|
|
|
|
out = _m3d_addidx(out, si_s, idx);
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
*length = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
2019-10-29 17:42:10 +00:00
|
|
|
len += *length;
|
|
|
|
out = NULL;
|
|
|
|
}
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* procedural face */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numinlined && model->inlined && !(flags & M3D_EXP_NOFACE)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
/* all inlined assets which are not textures should be procedural surfaces */
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < model->numinlined; j++) {
|
|
|
|
if (!model->inlined[j].name || !model->inlined[j].name[0] || model->inlined[j].length < 4 ||
|
|
|
|
!model->inlined[j].data || (model->inlined[j].data[1] == 'P' && model->inlined[j].data[2] == 'N' && model->inlined[j].data[3] == 'G'))
|
2019-11-18 02:04:52 +00:00
|
|
|
continue;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = k = 0; i < model->numtexture; i++) {
|
|
|
|
if (!strcmp(model->inlined[j].name, model->texture[i].name)) {
|
|
|
|
k = 1;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
numproc++;
|
|
|
|
chunklen = 8 + si_s;
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "PROC", 4);
|
|
|
|
*((uint32_t *)((uint8_t *)h + len + 4)) = chunklen;
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, model->inlined[j].name));
|
|
|
|
out = NULL;
|
|
|
|
len += chunklen;
|
|
|
|
}
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
/* mesh face */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numface && face && !(flags & M3D_EXP_NOFACE)) {
|
2019-10-29 17:42:10 +00:00
|
|
|
chunklen = 8 + si_s + model->numface * (6 * vi_s + 3 * ti_s + si_s + 1);
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "MESH", 4);
|
|
|
|
length = (uint32_t *)((uint8_t *)h + len + 4);
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-12-10 05:30:41 +00:00
|
|
|
last = M3D_UNDEF;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->numface; i++) {
|
|
|
|
if (!(flags & M3D_EXP_NOMATERIAL) && face[i].data.materialid != last) {
|
2019-11-18 02:04:52 +00:00
|
|
|
last = face[i].data.materialid;
|
|
|
|
idx = last < model->nummaterial ? _m3d_stridx(str, numstr, model->material[last].name) : 0;
|
|
|
|
*out++ = 0;
|
|
|
|
out = _m3d_addidx(out, si_s, idx);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
/* hardcoded triangles. */
|
|
|
|
k = (3 << 4) |
|
2019-12-10 05:30:41 +00:00
|
|
|
(((flags & M3D_EXP_NOTXTCRD) || !ti_s || face[i].data.texcoord[0] == M3D_UNDEF ||
|
2020-03-08 20:24:01 +00:00
|
|
|
face[i].data.texcoord[1] == M3D_UNDEF || face[i].data.texcoord[2] == M3D_UNDEF) ?
|
|
|
|
0 :
|
|
|
|
1) |
|
2019-12-10 05:30:41 +00:00
|
|
|
(((flags & M3D_EXP_NONORMAL) || face[i].data.normal[0] == M3D_UNDEF ||
|
2020-03-08 20:24:01 +00:00
|
|
|
face[i].data.normal[1] == M3D_UNDEF || face[i].data.normal[2] == M3D_UNDEF) ?
|
|
|
|
0 :
|
|
|
|
2);
|
2019-10-29 17:42:10 +00:00
|
|
|
*out++ = k;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (j = 0; j < 3; j++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, vi_s, vrtxidx[face[i].data.vertex[j]]);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k & 1)
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, ti_s, tmapidx[face[i].data.texcoord[j]]);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k & 2)
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, vi_s, vrtxidx[face[i].data.normal[j]]);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 17:23:22 +00:00
|
|
|
uint32_t v = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
|
|
|
memcpy(length, &v, sizeof(uint32_t));
|
|
|
|
len += v;
|
2019-10-29 17:42:10 +00:00
|
|
|
out = NULL;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
/* mathematical shapes face */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numshape && model->shape && !(flags & M3D_EXP_NOFACE)) {
|
|
|
|
for (j = 0; j < model->numshape; j++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
chunklen = 12 + si_s + model->shape[j].numcmd * (M3D_CMDMAXARG + 1) * 4;
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "SHPE", 4);
|
|
|
|
length = (uint32_t *)((uint8_t *)h + len + 4);
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, model->shape[j].name));
|
|
|
|
out = _m3d_addidx(out, bi_s, model->shape[j].group);
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = 0; i < model->shape[j].numcmd; i++) {
|
2019-11-18 02:04:52 +00:00
|
|
|
cmd = &model->shape[j].cmd[i];
|
2020-03-08 20:24:01 +00:00
|
|
|
if (cmd->type >= (unsigned int)(sizeof(m3d_commandtypes) / sizeof(m3d_commandtypes[0])) || !cmd->arg)
|
2019-11-18 02:04:52 +00:00
|
|
|
continue;
|
|
|
|
cd = &m3d_commandtypes[cmd->type];
|
|
|
|
*out++ = (cmd->type & 0x7F) | (cmd->type > 127 ? 0x80 : 0);
|
2020-03-08 20:24:01 +00:00
|
|
|
if (cmd->type > 127) *out++ = (cmd->type >> 7) & 0xff;
|
|
|
|
for (k = n = 0, l = cd->p; k < l; k++) {
|
|
|
|
switch (cd->a[((k - n) % (cd->p - n)) + n]) {
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_mi_t:
|
2020-03-08 20:24:01 +00:00
|
|
|
out = _m3d_addidx(out, si_s, cmd->arg[k] < model->nummaterial ? _m3d_stridx(str, numstr, model->material[cmd->arg[k]].name) : 0);
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_vc_t:
|
2020-03-08 20:24:01 +00:00
|
|
|
min_x = *((float *)&cmd->arg[k]);
|
|
|
|
switch (vc_s) {
|
2019-11-18 02:04:52 +00:00
|
|
|
case 1: *out++ = (int8_t)(min_x * 127); break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case 2:
|
|
|
|
*((int16_t *)out) = (int16_t)(min_x * 32767);
|
|
|
|
out += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*((float *)out) = min_x;
|
|
|
|
out += 4;
|
|
|
|
break;
|
|
|
|
case 8:
|
|
|
|
*((double *)out) = min_x;
|
|
|
|
out += 8;
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
case m3dcp_hi_t: out = _m3d_addidx(out, hi_s, cmd->arg[k]); break;
|
|
|
|
case m3dcp_fi_t: out = _m3d_addidx(out, fi_s, cmd->arg[k]); break;
|
|
|
|
case m3dcp_ti_t: out = _m3d_addidx(out, ti_s, cmd->arg[k]); break;
|
|
|
|
case m3dcp_qi_t:
|
|
|
|
case m3dcp_vi_t: out = _m3d_addidx(out, vi_s, cmd->arg[k]); break;
|
|
|
|
case m3dcp_i1_t: out = _m3d_addidx(out, 1, cmd->arg[k]); break;
|
|
|
|
case m3dcp_i2_t: out = _m3d_addidx(out, 2, cmd->arg[k]); break;
|
|
|
|
case m3dcp_i4_t: out = _m3d_addidx(out, 4, cmd->arg[k]); break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case m3dcp_va_t:
|
|
|
|
out = _m3d_addidx(out, 4, cmd->arg[k]);
|
|
|
|
n = k + 1;
|
|
|
|
l += (cmd->arg[k] - 1) * (cd->p - k - 1);
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-27 17:23:22 +00:00
|
|
|
uint32_t v = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
|
|
|
memcpy( length, &v, sizeof(uint32_t));
|
|
|
|
len += v;
|
2019-11-18 02:04:52 +00:00
|
|
|
out = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* annotation labels */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numlabel && model->label) {
|
|
|
|
for (i = 0, length = NULL; i < model->numlabel; i++) {
|
|
|
|
if (!i || _m3d_strcmp(sl, model->label[i].lang) || _m3d_strcmp(sn, model->label[i].name)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
sl = model->label[i].lang;
|
|
|
|
sn = model->label[i].name;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (length) {
|
|
|
|
*length = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
2019-11-18 02:04:52 +00:00
|
|
|
len += *length;
|
|
|
|
}
|
|
|
|
chunklen = 8 + 2 * si_s + ci_s + model->numlabel * (vi_s + si_s);
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) {
|
|
|
|
sn = NULL;
|
|
|
|
sl = NULL;
|
|
|
|
goto memerr;
|
|
|
|
}
|
|
|
|
memcpy((uint8_t *)h + len, "LBLS", 4);
|
|
|
|
length = (uint32_t *)((uint8_t *)h + len + 4);
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, model->label[l].name));
|
|
|
|
out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, model->label[l].lang));
|
|
|
|
idx = _m3d_cmapidx(cmap, numcmap, model->label[i].color);
|
2020-03-08 20:24:01 +00:00
|
|
|
switch (ci_s) {
|
2019-11-18 02:04:52 +00:00
|
|
|
case 1: *out++ = (uint8_t)(idx); break;
|
2020-03-08 20:24:01 +00:00
|
|
|
case 2:
|
|
|
|
*((uint16_t *)out) = (uint16_t)(idx);
|
|
|
|
out += 2;
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
*((uint32_t *)out) = model->label[i].color;
|
|
|
|
out += 4;
|
|
|
|
break;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
out = _m3d_addidx(out, vi_s, vrtxidx[model->label[i].vertexid]);
|
|
|
|
out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, model->label[l].text));
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (length) {
|
2020-03-27 17:23:22 +00:00
|
|
|
uint32_t v = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
|
|
|
memcpy( length, &v, sizeof(uint32_t));
|
|
|
|
len += v;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
|
|
|
out = NULL;
|
|
|
|
sn = sl = NULL;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
/* actions */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numaction && model->action && model->numbone && model->bone && !(flags & M3D_EXP_NOACTION)) {
|
|
|
|
for (j = 0; j < model->numaction; j++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
a = &model->action[j];
|
2019-11-18 02:04:52 +00:00
|
|
|
chunklen = 14 + si_s + a->numframe * (4 + fc_s + maxt * (bi_s + 2 * vi_s));
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "ACTN", 4);
|
|
|
|
length = (uint32_t *)((uint8_t *)h + len + 4);
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-10-29 17:42:10 +00:00
|
|
|
out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, a->name));
|
2020-03-08 20:24:01 +00:00
|
|
|
*((uint16_t *)out) = (uint16_t)(a->numframe);
|
|
|
|
out += 2;
|
|
|
|
*((uint32_t *)out) = (uint32_t)(a->durationmsec);
|
|
|
|
out += 4;
|
|
|
|
for (i = 0; i < a->numframe; i++) {
|
|
|
|
*((uint32_t *)out) = (uint32_t)(a->frame[i].msec);
|
|
|
|
out += 4;
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, fc_s, a->frame[i].numtransform);
|
2020-03-08 20:24:01 +00:00
|
|
|
for (k = 0; k < a->frame[i].numtransform; k++) {
|
2019-10-29 17:42:10 +00:00
|
|
|
out = _m3d_addidx(out, bi_s, a->frame[i].transform[k].boneid);
|
2019-11-18 02:04:52 +00:00
|
|
|
out = _m3d_addidx(out, vi_s, vrtxidx[a->frame[i].transform[k].pos]);
|
|
|
|
out = _m3d_addidx(out, vi_s, vrtxidx[a->frame[i].transform[k].ori]);
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
}
|
2020-03-27 17:23:22 +00:00
|
|
|
uint32_t v = (uint32_t)((uintptr_t)out - (uintptr_t)((uint8_t *)h + len));
|
|
|
|
memcpy( length, &v, sizeof(uint32_t));
|
|
|
|
len += v;
|
2019-10-29 17:42:10 +00:00
|
|
|
out = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* inlined assets */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numinlined && model->inlined && (numproc || (flags & M3D_EXP_INLINE))) {
|
|
|
|
for (j = 0; j < model->numinlined; j++) {
|
|
|
|
if (!model->inlined[j].name || !model->inlined[j].name[0] || model->inlined[j].length < 4 || !model->inlined[j].data)
|
2019-10-29 17:42:10 +00:00
|
|
|
continue;
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_INLINE)) {
|
|
|
|
if (model->inlined[j].data[1] == 'P' && model->inlined[j].data[2] == 'N' && model->inlined[j].data[3] == 'G')
|
2019-11-18 02:04:52 +00:00
|
|
|
continue;
|
2020-03-08 20:24:01 +00:00
|
|
|
for (i = k = 0; i < model->numtexture; i++) {
|
|
|
|
if (!strcmp(model->inlined[j].name, model->texture[i].name)) {
|
|
|
|
k = 1;
|
|
|
|
break;
|
|
|
|
}
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (k) continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
chunklen = 8 + si_s + model->inlined[j].length;
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "ASET", 4);
|
|
|
|
*((uint32_t *)((uint8_t *)h + len + 4)) = chunklen;
|
|
|
|
out = (uint8_t *)h + len + 8;
|
2019-10-29 17:42:10 +00:00
|
|
|
out = _m3d_addidx(out, si_s, _m3d_stridx(str, numstr, model->inlined[j].name));
|
|
|
|
memcpy(out, model->inlined[j].data, model->inlined[j].length);
|
|
|
|
out = NULL;
|
|
|
|
len += chunklen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* extra chunks */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (model->numextra && model->extra && (flags & M3D_EXP_EXTRA)) {
|
|
|
|
for (j = 0; j < model->numextra; j++) {
|
|
|
|
if (!model->extra[j] || model->extra[j]->length < 8)
|
2019-10-29 17:42:10 +00:00
|
|
|
continue;
|
2019-11-18 02:04:52 +00:00
|
|
|
chunklen = model->extra[j]->length;
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + chunklen);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, model->extra[j], chunklen);
|
2019-10-29 17:42:10 +00:00
|
|
|
len += chunklen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* add end chunk */
|
2020-03-08 20:24:01 +00:00
|
|
|
h = (m3dhdr_t *)M3D_REALLOC(h, len + 4);
|
|
|
|
if (!h) goto memerr;
|
|
|
|
memcpy((uint8_t *)h + len, "OMD3", 4);
|
2019-10-29 17:42:10 +00:00
|
|
|
len += 4;
|
|
|
|
/* zlib compress */
|
2020-03-08 20:24:01 +00:00
|
|
|
if (!(flags & M3D_EXP_NOZLIB)) {
|
2019-11-18 02:04:52 +00:00
|
|
|
M3D_LOG("Deflating chunks");
|
2020-03-08 20:24:01 +00:00
|
|
|
z = stbi_zlib_compress((unsigned char *)h, len, (int *)&l, 9);
|
|
|
|
if (z && l > 0 && l < len) {
|
|
|
|
len = l;
|
|
|
|
M3D_FREE(h);
|
|
|
|
h = (m3dhdr_t *)z;
|
|
|
|
}
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
|
|
|
/* add file header at the begining */
|
|
|
|
len += 8;
|
2020-03-08 20:24:01 +00:00
|
|
|
out = (unsigned char *)M3D_MALLOC(len);
|
|
|
|
if (!out) goto memerr;
|
2019-10-29 17:42:10 +00:00
|
|
|
memcpy(out, "3DMO", 4);
|
2020-03-08 20:24:01 +00:00
|
|
|
*((uint32_t *)(out + 4)) = len;
|
2019-10-29 17:42:10 +00:00
|
|
|
memcpy(out + 8, h, len - 8);
|
|
|
|
}
|
2020-03-08 20:24:01 +00:00
|
|
|
if (size) *size = out ? len : 0;
|
|
|
|
if (vrtxidx) M3D_FREE(vrtxidx);
|
|
|
|
if (mtrlidx) M3D_FREE(mtrlidx);
|
|
|
|
if (tmapidx) M3D_FREE(tmapidx);
|
|
|
|
if (skinidx) M3D_FREE(skinidx);
|
|
|
|
if (norm) M3D_FREE(norm);
|
|
|
|
if (face) M3D_FREE(face);
|
|
|
|
if (cmap) M3D_FREE(cmap);
|
|
|
|
if (tmap) M3D_FREE(tmap);
|
|
|
|
if (skin) M3D_FREE(skin);
|
|
|
|
if (str) M3D_FREE(str);
|
|
|
|
if (vrtx) M3D_FREE(vrtx);
|
|
|
|
if (h) M3D_FREE(h);
|
2019-10-29 17:42:10 +00:00
|
|
|
return out;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
#ifdef __cplusplus
|
2019-10-29 17:42:10 +00:00
|
|
|
}
|
2019-11-01 02:39:36 +00:00
|
|
|
#ifdef M3D_CPPWRAPPER
|
2019-10-29 17:42:10 +00:00
|
|
|
#include <memory>
|
2020-03-08 20:24:01 +00:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
/*** C++ wrapper class ***/
|
|
|
|
namespace M3D {
|
|
|
|
#ifdef M3D_IMPLEMENTATION
|
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
class Model {
|
|
|
|
public:
|
|
|
|
m3d_t *model;
|
2019-10-29 17:42:10 +00:00
|
|
|
|
2020-03-08 20:24:01 +00:00
|
|
|
public:
|
|
|
|
Model() {
|
|
|
|
this->model = (m3d_t *)malloc(sizeof(m3d_t));
|
|
|
|
memset(this->model, 0, sizeof(m3d_t));
|
|
|
|
}
|
|
|
|
Model(_unused const std::string &data, _unused m3dread_t ReadFileCB,
|
|
|
|
_unused m3dfree_t FreeCB, _unused M3D::Model mtllib) {
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifndef M3D_NOIMPORTER
|
2020-03-08 20:24:01 +00:00
|
|
|
this->model = m3d_load((unsigned char *)data.data(), ReadFileCB, FreeCB, mtllib.model);
|
2019-10-29 17:42:10 +00:00
|
|
|
#else
|
2020-03-08 20:24:01 +00:00
|
|
|
Model();
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
Model(_unused const std::vector<unsigned char> data, _unused m3dread_t ReadFileCB,
|
|
|
|
_unused m3dfree_t FreeCB, _unused M3D::Model mtllib) {
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifndef M3D_NOIMPORTER
|
2020-03-08 20:24:01 +00:00
|
|
|
this->model = m3d_load((unsigned char *)&data[0], ReadFileCB, FreeCB, mtllib.model);
|
2019-10-29 17:42:10 +00:00
|
|
|
#else
|
2020-03-08 20:24:01 +00:00
|
|
|
Model();
|
2019-11-18 02:04:52 +00:00
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
Model(_unused const unsigned char *data, _unused m3dread_t ReadFileCB,
|
|
|
|
_unused m3dfree_t FreeCB, _unused M3D::Model mtllib) {
|
2019-11-18 02:04:52 +00:00
|
|
|
#ifndef M3D_NOIMPORTER
|
2020-03-08 20:24:01 +00:00
|
|
|
this->model = m3d_load((unsigned char *)data, ReadFileCB, FreeCB, mtllib.model);
|
2019-11-18 02:04:52 +00:00
|
|
|
#else
|
2020-03-08 20:24:01 +00:00
|
|
|
Model();
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
~Model() { m3d_free(this->model); }
|
|
|
|
|
|
|
|
public:
|
|
|
|
m3d_t *getCStruct() { return this->model; }
|
|
|
|
std::string getName() { return std::string(this->model->name); }
|
|
|
|
void setName(std::string name) { this->model->name = (char *)name.c_str(); }
|
|
|
|
std::string getLicense() { return std::string(this->model->license); }
|
|
|
|
void setLicense(std::string license) { this->model->license = (char *)license.c_str(); }
|
|
|
|
std::string getAuthor() { return std::string(this->model->author); }
|
|
|
|
void setAuthor(std::string author) { this->model->author = (char *)author.c_str(); }
|
|
|
|
std::string getDescription() { return std::string(this->model->desc); }
|
|
|
|
void setDescription(std::string desc) { this->model->desc = (char *)desc.c_str(); }
|
|
|
|
float getScale() { return this->model->scale; }
|
|
|
|
void setScale(float scale) { this->model->scale = scale; }
|
|
|
|
std::vector<unsigned char> getPreview() { return this->model->preview.data ?
|
|
|
|
std::vector<unsigned char>(this->model->preview.data, this->model->preview.data + this->model->preview.length) :
|
|
|
|
std::vector<unsigned char>(); }
|
|
|
|
std::vector<uint32_t> getColorMap() { return this->model->cmap ? std::vector<uint32_t>(this->model->cmap,
|
|
|
|
this->model->cmap + this->model->numcmap) :
|
|
|
|
std::vector<uint32_t>(); }
|
|
|
|
std::vector<m3dti_t> getTextureMap() { return this->model->tmap ? std::vector<m3dti_t>(this->model->tmap,
|
|
|
|
this->model->tmap + this->model->numtmap) :
|
|
|
|
std::vector<m3dti_t>(); }
|
|
|
|
std::vector<m3dtx_t> getTextures() { return this->model->texture ? std::vector<m3dtx_t>(this->model->texture,
|
|
|
|
this->model->texture + this->model->numtexture) :
|
|
|
|
std::vector<m3dtx_t>(); }
|
|
|
|
std::string getTextureName(int idx) { return idx >= 0 && (unsigned int)idx < this->model->numtexture ?
|
|
|
|
std::string(this->model->texture[idx].name) :
|
|
|
|
nullptr; }
|
|
|
|
std::vector<m3db_t> getBones() { return this->model->bone ? std::vector<m3db_t>(this->model->bone, this->model->bone +
|
|
|
|
this->model->numbone) :
|
|
|
|
std::vector<m3db_t>(); }
|
|
|
|
std::string getBoneName(int idx) { return idx >= 0 && (unsigned int)idx < this->model->numbone ?
|
|
|
|
std::string(this->model->bone[idx].name) :
|
|
|
|
nullptr; }
|
|
|
|
std::vector<m3dm_t> getMaterials() { return this->model->material ? std::vector<m3dm_t>(this->model->material,
|
|
|
|
this->model->material + this->model->nummaterial) :
|
|
|
|
std::vector<m3dm_t>(); }
|
|
|
|
std::string getMaterialName(int idx) { return idx >= 0 && (unsigned int)idx < this->model->nummaterial ?
|
|
|
|
std::string(this->model->material[idx].name) :
|
|
|
|
nullptr; }
|
|
|
|
int getMaterialPropertyInt(int idx, int type) {
|
|
|
|
if (idx < 0 || (unsigned int)idx >= this->model->nummaterial || type < 0 || type >= 127 ||
|
|
|
|
!this->model->material[idx].prop) return -1;
|
|
|
|
for (int i = 0; i < this->model->material[idx].numprop; i++) {
|
|
|
|
if (this->model->material[idx].prop[i].type == type)
|
|
|
|
return this->model->material[idx].prop[i].value.num;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
uint32_t getMaterialPropertyColor(int idx, int type) { return this->getMaterialPropertyInt(idx, type); }
|
|
|
|
float getMaterialPropertyFloat(int idx, int type) {
|
|
|
|
if (idx < 0 || (unsigned int)idx >= this->model->nummaterial || type < 0 || type >= 127 ||
|
|
|
|
!this->model->material[idx].prop) return -1.0f;
|
|
|
|
for (int i = 0; i < this->model->material[idx].numprop; i++) {
|
|
|
|
if (this->model->material[idx].prop[i].type == type)
|
|
|
|
return this->model->material[idx].prop[i].value.fnum;
|
|
|
|
}
|
|
|
|
return -1.0f;
|
|
|
|
}
|
|
|
|
m3dtx_t *getMaterialPropertyMap(int idx, int type) {
|
|
|
|
if (idx < 0 || (unsigned int)idx >= this->model->nummaterial || type < 128 || type > 255 ||
|
|
|
|
!this->model->material[idx].prop) return nullptr;
|
|
|
|
for (int i = 0; i < this->model->material[idx].numprop; i++) {
|
|
|
|
if (this->model->material[idx].prop[i].type == type)
|
|
|
|
return this->model->material[idx].prop[i].value.textureid < this->model->numtexture ?
|
|
|
|
&this->model->texture[this->model->material[idx].prop[i].value.textureid] :
|
|
|
|
nullptr;
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
std::vector<m3dv_t> getVertices() { return this->model->vertex ? std::vector<m3dv_t>(this->model->vertex,
|
|
|
|
this->model->vertex + this->model->numvertex) :
|
|
|
|
std::vector<m3dv_t>(); }
|
|
|
|
std::vector<m3df_t> getFace() { return this->model->face ? std::vector<m3df_t>(this->model->face, this->model->face +
|
|
|
|
this->model->numface) :
|
|
|
|
std::vector<m3df_t>(); }
|
|
|
|
std::vector<m3dh_t> getShape() { return this->model->shape ? std::vector<m3dh_t>(this->model->shape,
|
|
|
|
this->model->shape + this->model->numshape) :
|
|
|
|
std::vector<m3dh_t>(); }
|
|
|
|
std::string getShapeName(int idx) { return idx >= 0 && (unsigned int)idx < this->model->numshape &&
|
|
|
|
this->model->shape[idx].name && this->model->shape[idx].name[0] ?
|
|
|
|
std::string(this->model->shape[idx].name) :
|
|
|
|
nullptr; }
|
|
|
|
unsigned int getShapeGroup(int idx) { return idx >= 0 && (unsigned int)idx < this->model->numshape ?
|
|
|
|
this->model->shape[idx].group :
|
|
|
|
0xFFFFFFFF; }
|
|
|
|
std::vector<m3dc_t> getShapeCommands(int idx) { return idx >= 0 && (unsigned int)idx < this->model->numshape &&
|
|
|
|
this->model->shape[idx].cmd ?
|
|
|
|
std::vector<m3dc_t>(this->model->shape[idx].cmd, this->model->shape[idx].cmd +
|
|
|
|
this->model->shape[idx].numcmd) :
|
|
|
|
std::vector<m3dc_t>(); }
|
|
|
|
std::vector<m3dl_t> getAnnotationLabels() { return this->model->label ? std::vector<m3dl_t>(this->model->label,
|
|
|
|
this->model->label + this->model->numlabel) :
|
|
|
|
std::vector<m3dl_t>(); }
|
|
|
|
std::vector<m3ds_t> getSkin() { return this->model->skin ? std::vector<m3ds_t>(this->model->skin, this->model->skin +
|
|
|
|
this->model->numskin) :
|
|
|
|
std::vector<m3ds_t>(); }
|
|
|
|
std::vector<m3da_t> getActions() { return this->model->action ? std::vector<m3da_t>(this->model->action,
|
|
|
|
this->model->action + this->model->numaction) :
|
|
|
|
std::vector<m3da_t>(); }
|
|
|
|
std::string getActionName(int aidx) { return aidx >= 0 && (unsigned int)aidx < this->model->numaction ?
|
|
|
|
std::string(this->model->action[aidx].name) :
|
|
|
|
nullptr; }
|
|
|
|
unsigned int getActionDuration(int aidx) { return aidx >= 0 && (unsigned int)aidx < this->model->numaction ?
|
|
|
|
this->model->action[aidx].durationmsec :
|
|
|
|
0; }
|
|
|
|
std::vector<m3dfr_t> getActionFrames(int aidx) { return aidx >= 0 && (unsigned int)aidx < this->model->numaction ?
|
|
|
|
std::vector<m3dfr_t>(this->model->action[aidx].frame, this->model->action[aidx].frame +
|
|
|
|
this->model->action[aidx].numframe) :
|
|
|
|
std::vector<m3dfr_t>(); }
|
|
|
|
unsigned int getActionFrameTimestamp(int aidx, int fidx) { return aidx >= 0 && (unsigned int)aidx < this->model->numaction ?
|
|
|
|
(fidx >= 0 && (unsigned int)fidx < this->model->action[aidx].numframe ?
|
|
|
|
this->model->action[aidx].frame[fidx].msec :
|
|
|
|
0) :
|
|
|
|
0; }
|
|
|
|
std::vector<m3dtr_t> getActionFrameTransforms(int aidx, int fidx) {
|
|
|
|
return aidx >= 0 && (unsigned int)aidx < this->model->numaction ? (
|
|
|
|
fidx >= 0 && (unsigned int)fidx < this->model->action[aidx].numframe ?
|
|
|
|
std::vector<m3dtr_t>(this->model->action[aidx].frame[fidx].transform,
|
|
|
|
this->model->action[aidx].frame[fidx].transform + this->model->action[aidx].frame[fidx].numtransform) :
|
|
|
|
std::vector<m3dtr_t>()) :
|
|
|
|
std::vector<m3dtr_t>();
|
|
|
|
}
|
|
|
|
std::vector<m3dtr_t> getActionFrame(int aidx, int fidx, std::vector<m3dtr_t> skeleton) {
|
|
|
|
m3dtr_t *pose = m3d_frame(this->model, (unsigned int)aidx, (unsigned int)fidx,
|
|
|
|
skeleton.size() ? &skeleton[0] : nullptr);
|
|
|
|
return std::vector<m3dtr_t>(pose, pose + this->model->numbone);
|
|
|
|
}
|
|
|
|
std::vector<m3db_t> getActionPose(int aidx, unsigned int msec) {
|
|
|
|
m3db_t *pose = m3d_pose(this->model, (unsigned int)aidx, (unsigned int)msec);
|
|
|
|
return std::vector<m3db_t>(pose, pose + this->model->numbone);
|
|
|
|
}
|
|
|
|
std::vector<m3di_t> getInlinedAssets() { return this->model->inlined ? std::vector<m3di_t>(this->model->inlined,
|
|
|
|
this->model->inlined + this->model->numinlined) :
|
|
|
|
std::vector<m3di_t>(); }
|
|
|
|
std::vector<std::unique_ptr<m3dchunk_t>> getExtras() { return this->model->extra ?
|
|
|
|
std::vector<std::unique_ptr<m3dchunk_t>>(this->model->extra,
|
|
|
|
this->model->extra + this->model->numextra) :
|
|
|
|
std::vector<std::unique_ptr<m3dchunk_t>>(); }
|
|
|
|
std::vector<unsigned char> Save(_unused int quality, _unused int flags) {
|
2019-10-29 17:42:10 +00:00
|
|
|
#ifdef M3D_EXPORTER
|
2020-03-08 20:24:01 +00:00
|
|
|
unsigned int size;
|
|
|
|
unsigned char *ptr = m3d_save(this->model, quality, flags, &size);
|
|
|
|
return ptr && size ? std::vector<unsigned char>(ptr, ptr + size) : std::vector<unsigned char>();
|
2019-10-29 17:42:10 +00:00
|
|
|
#else
|
2020-03-08 20:24:01 +00:00
|
|
|
return std::vector<unsigned char>();
|
2019-10-29 17:42:10 +00:00
|
|
|
#endif
|
2020-03-08 20:24:01 +00:00
|
|
|
}
|
|
|
|
};
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
#else
|
2020-03-08 20:24:01 +00:00
|
|
|
class Model {
|
|
|
|
public:
|
|
|
|
m3d_t *model;
|
|
|
|
|
|
|
|
public:
|
|
|
|
Model(const std::string &data, m3dread_t ReadFileCB, m3dfree_t FreeCB);
|
|
|
|
Model(const std::vector<unsigned char> data, m3dread_t ReadFileCB, m3dfree_t FreeCB);
|
|
|
|
Model(const unsigned char *data, m3dread_t ReadFileCB, m3dfree_t FreeCB);
|
|
|
|
Model();
|
|
|
|
~Model();
|
|
|
|
|
|
|
|
public:
|
|
|
|
m3d_t *getCStruct();
|
|
|
|
std::string getName();
|
|
|
|
void setName(std::string name);
|
|
|
|
std::string getLicense();
|
|
|
|
void setLicense(std::string license);
|
|
|
|
std::string getAuthor();
|
|
|
|
void setAuthor(std::string author);
|
|
|
|
std::string getDescription();
|
|
|
|
void setDescription(std::string desc);
|
|
|
|
float getScale();
|
|
|
|
void setScale(float scale);
|
|
|
|
std::vector<unsigned char> getPreview();
|
|
|
|
std::vector<uint32_t> getColorMap();
|
|
|
|
std::vector<m3dti_t> getTextureMap();
|
|
|
|
std::vector<m3dtx_t> getTextures();
|
|
|
|
std::string getTextureName(int idx);
|
|
|
|
std::vector<m3db_t> getBones();
|
|
|
|
std::string getBoneName(int idx);
|
|
|
|
std::vector<m3dm_t> getMaterials();
|
|
|
|
std::string getMaterialName(int idx);
|
|
|
|
int getMaterialPropertyInt(int idx, int type);
|
|
|
|
uint32_t getMaterialPropertyColor(int idx, int type);
|
|
|
|
float getMaterialPropertyFloat(int idx, int type);
|
|
|
|
m3dtx_t *getMaterialPropertyMap(int idx, int type);
|
|
|
|
std::vector<m3dv_t> getVertices();
|
|
|
|
std::vector<m3df_t> getFace();
|
|
|
|
std::vector<m3dh_t> getShape();
|
|
|
|
std::string getShapeName(int idx);
|
|
|
|
unsigned int getShapeGroup(int idx);
|
|
|
|
std::vector<m3dc_t> getShapeCommands(int idx);
|
|
|
|
std::vector<m3dl_t> getAnnotationLabels();
|
|
|
|
std::vector<m3ds_t> getSkin();
|
|
|
|
std::vector<m3da_t> getActions();
|
|
|
|
std::string getActionName(int aidx);
|
|
|
|
unsigned int getActionDuration(int aidx);
|
|
|
|
std::vector<m3dfr_t> getActionFrames(int aidx);
|
|
|
|
unsigned int getActionFrameTimestamp(int aidx, int fidx);
|
|
|
|
std::vector<m3dtr_t> getActionFrameTransforms(int aidx, int fidx);
|
|
|
|
std::vector<m3dtr_t> getActionFrame(int aidx, int fidx, std::vector<m3dtr_t> skeleton);
|
|
|
|
std::vector<m3db_t> getActionPose(int aidx, unsigned int msec);
|
|
|
|
std::vector<m3di_t> getInlinedAssets();
|
|
|
|
std::vector<std::unique_ptr<m3dchunk_t>> getExtras();
|
|
|
|
std::vector<unsigned char> Save(int quality, int flags);
|
|
|
|
};
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
#endif /* impl */
|
2020-03-08 20:24:01 +00:00
|
|
|
} // namespace M3D
|
2020-03-01 12:15:45 +00:00
|
|
|
|
2020-03-15 10:47:05 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
# pragma warning(pop)
|
|
|
|
#endif // _WIN32
|
2020-03-01 12:15:45 +00:00
|
|
|
|
2019-11-01 02:39:36 +00:00
|
|
|
#endif
|
2019-10-29 17:42:10 +00:00
|
|
|
|
|
|
|
#endif /* __cplusplus */
|
|
|
|
|
|
|
|
#endif
|