Skip to content

Instantly share code, notes, and snippets.

@reinsteam
Last active December 25, 2015 21:19
Show Gist options
  • Save reinsteam/682043b798e741f556a2 to your computer and use it in GitHub Desktop.
Save reinsteam/682043b798e741f556a2 to your computer and use it in GitHub Desktop.
Sample of parsing Microsoft .sdkmesh format frequently used in samples
#pragma once
typedef unsigned long long u64;
typedef unsigned int u32;
typedef unsigned short u16;
typedef unsigned char u8;
typedef signed char i8;
typedef float f32;
#define MAX_PATH 260
#ifdef __cplusplus
# define INLINE __forceinline
#else
# define INLINE __inline
#endif
/* 104 bytes */
typedef struct sdkmesh_header
{
u32 version;
u8 big_end;
u8 padding0;
u16 padding1;
u64 header_data_size;
u64 helper_data_size;
u64 buffer_data_size;
u32 vb_count;
u32 ib_count;
u32 mesh_data_count;
u32 segm_data_count;
u32 anim_data_count;
u32 mtrl_data_count;
u64 vb_header_offset;
u64 ib_header_offset;
u64 mesh_data_offset;
u64 segm_data_offset;
u64 anim_data_offset;
u64 mtrl_data_offset;
} sdkmesh_header;
/* 32 + 256 bytes */
typedef struct sdkmesh_vb_header
{
u64 num_verts;
u64 num_bytes;
u64 vstride;
u64 vformat[32];
u64 voffset;
} sdkmesh_vb_header;
/* 32 bytes */
typedef struct sdkmesh_ib_header
{
u64 num_idxes;
u64 num_bytes;
u32 iformat;
u32 padding;
u64 ioffset;
} sdkmesh_ib_header;
typedef struct sdkmesh_mesh_data
{
i8 name[100];
u8 vb_count;
u8 padding0;
u16 padding1;
u32 vb_data[16];
u32 ib_data;
u32 segm_count;
u32 bone_count;
f32 bbox_center[3];
f32 bbox_extend[3];
u64 segm_data_offset;
u64 bone_data_offset;
} sdkmesh_mesh_data;
typedef struct sdkmesh_segm_data
{
i8 name[100];
u32 mtrl_id;
u32 prim_id;
u64 idx_start;
u64 idx_count;
u64 vtx_start;
u64 vtx_count;
} sdkmesh_segm_data;
typedef struct sdkmesh_anim_data
{
i8 name[100];
u32 mesh;
u32 parent_data;
u32 child_data;
u32 sibling_data;
f32 transform[16];
} sdkmesh_anim_data;
typedef struct sdkmesh_mtrl_data
{
i8 name[100];
i8 instance_path[MAX_PATH];
i8 diff_tex_path[MAX_PATH];
i8 norm_tex_path[MAX_PATH];
i8 spec_tex_path[MAX_PATH];
f32 diffuse_color[4];
f32 ambient_color[4];
f32 specular_color[4];
f32 emissive_color[4];
f32 power;
u64 force[6];
} sdkmesh_mtrl_data;
#define declare_getter(name) \
static INLINE sdkmesh_##name const * sdkmesh_get_##name(sdkmesh_header const * sdkmesh, u32 idx) \
{ \
return (sdkmesh_##name const *)((u8 const *)sdkmesh + sdkmesh->name##_offset) + idx; \
}
declare_getter(vb_header);
declare_getter(ib_header);
declare_getter(mesh_data);
declare_getter(segm_data);
declare_getter(mtrl_data);
static INLINE sdkmesh_segm_data * const sdkmesh_get_mesh_segm_data(sdkmesh_header const * sdkmesh, sdkmesh_mesh_data const * mesh, u32 idx)
{
sdkmesh_segm_data const * segm_data = sdkmesh_get_segm_data(sdkmesh, 0);
u32 * const segm_inds = (u32 const *)((u8 const *)sdkmesh + mesh->segm_data_offset);
return segm_data + segm_inds[idx];
}
static INLINE u8 * const sdkmesh_get_vb_data(sdkmesh_header * const sdkmesh, u32 idx)
{
return (u8 const *)sdkmesh + sdkmesh_get_vb_header(sdkmesh, idx)->voffset;
}
static INLINE u8 * const sdkmesh_get_ib_data(sdkmesh_header * const sdkmesh, u32 idx)
{
return (u8 const *)sdkmesh + sdkmesh_get_ib_header(sdkmesh, idx)->ioffset;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment