add weight and joint accessors

This commit is contained in:
Zack Buhman 2026-01-03 16:21:14 -06:00
parent ee4deb53fb
commit 9111df6257
2 changed files with 53 additions and 31 deletions

View File

@ -1,29 +1,37 @@
template <typename T>
struct Array {
T * e;
int length;
};
struct Mesh { struct Mesh {
D3DXVECTOR3 * position; const D3DXVECTOR3 * position;
D3DXVECTOR3 * normal; const DWORD position_size;
D3DXVECTOR2 * texcoord_0;
DWORD * indices; const D3DXVECTOR3 * normal;
const DWORD normal_size;
const D3DXVECTOR2 * texcoord_0;
const DWORD texcoord_0_size;
const D3DXVECTOR4 * weights_0;
const DWORD weights_0_size;
const D3DXVECTOR4 * joints_0;
const DWORD joints_0_size;
const DWORD * indices;
const DWORD indices_size;
}; };
struct Skin; struct Skin;
struct Node { struct Node {
Skin * skin; // skin index (global) const Skin * skin; // skin index (global)
Mesh * mesh; // mesh index (global) const Mesh * mesh; // mesh index (global)
D3DXVECTOR3 scale; const D3DXVECTOR3 translation;
D3DXVECTOR3 translation; const D3DXQUATERNION rotation;
D3DXQUATERNION rotation; const D3DXVECTOR3 scale;
}; };
struct Skin { struct Skin {
D3DXMATRIX * inverse_bind_matrices; // accessor const D3DXMATRIX * inverse_bind_matrices; // accessor
Array<Node *> joints; const Node ** joints;
DWORD joints_length;
}; };
enum AnimationChannelPath { enum AnimationChannelPath {
@ -34,19 +42,20 @@ enum AnimationChannelPath {
}; };
struct AnimationSampler { struct AnimationSampler {
float * input; // accessor index, containing keyframe timestamps const float * input; // accessor index, containing keyframe timestamps
void * output; // accessor index, containing keyframe values (type depends on channel target path) const void * output; // accessor index, containing keyframe values (type depends on channel target path)
const int length;
}; };
struct AnimationChannel { struct AnimationChannel {
AnimationSampler * sampler; // sampler index, this animation const AnimationSampler * sampler; // sampler index, this animation
struct { struct {
Node * node; // node index const Node * node; // node index
AnimationChannelPath path; // property to animate const AnimationChannelPath path; // property to animate
} target; } target;
}; };
struct Animation { //struct Animation {
Array<AnimationChannel> channels; // const AnimationChannel * channels;
Array<AnimationSampler> samplers; // const AnimationSampler * samplers;
}; //};

View File

@ -75,12 +75,23 @@ def render_meshes(gltf):
position = attributes["POSITION"] position = attributes["POSITION"]
normal = attributes.get("NORMAL", None) normal = attributes.get("NORMAL", None)
texcoord_0 = attributes.get("TEXCOORD_0", None) texcoord_0 = attributes.get("TEXCOORD_0", None)
weights_0 = attributes.get("WEIGHTS_0", None)
joints_0 = attributes.get("JOINTS_0", None)
indices = primitive["indices"] indices = primitive["indices"]
yield f"const Mesh mesh_{mesh_ix} = {{" yield f"const Mesh mesh_{mesh_ix} = {{"
yield f"accessor_{position}, // position" yield f"accessor_{position}, // position"
yield f"accessor_{normal}, // normal" if normal is not None else "NULL," yield f"(sizeof (accessor_{position})),"
yield f"accessor_{texcoord_0}, // texcoord_0" if texcoord_0 is not None else "NULL," yield f"accessor_{normal}, // normal" if normal is not None else "NULL, // normal"
yield f"(sizeof (accessor_{normal}))," if normal is not None else "0,"
yield f"accessor_{texcoord_0}, // texcoord_0" if texcoord_0 is not None else "NULL, // texcoord_0"
yield f"(sizeof (accessor_{texcoord_0}))," if texcoord_0 is not None else "0,"
yield f"accessor_{weights_0}, // weights_0" if weights_0 is not None else "NULL, // weights_0"
yield f"(sizeof (accessor_{weights_0}))," if weights_0 is not None else "0,"
yield f"accessor_{joints_0}, // joints_0" if joints_0 is not None else "NULL, // joints_0"
yield f"(sizeof (accessor_{joints_0}))," if joints_0 is not None else "0,"
yield f"accessor_{indices}, // indices" yield f"accessor_{indices}, // indices"
yield f"(sizeof (accessor_{indices})),"
yield "};" yield "};"
def render_nodes(gltf): def render_nodes(gltf):
@ -88,7 +99,7 @@ def render_nodes(gltf):
if "skin" not in node: if "skin" not in node:
continue continue
skin = node["skin"] skin = node["skin"]
yield f"const Skin skin_{skin};" yield f"extern const Skin skin_{skin};"
for node_ix, node in enumerate(gltf.json["nodes"]): for node_ix, node in enumerate(gltf.json["nodes"]):
skin = f"&skin_{node['skin']}" if "skin" in node else "NULL" skin = f"&skin_{node['skin']}" if "skin" in node else "NULL"
@ -107,9 +118,9 @@ def render_nodes(gltf):
yield f"const Node node_{node_ix} = {{" yield f"const Node node_{node_ix} = {{"
yield f"{skin}, // skin" yield f"{skin}, // skin"
yield f"{mesh}, // mesh" yield f"{mesh}, // mesh"
yield f"{render_value(scale, 'D3DXVECTOR3')}, // scale"
yield f"{render_value(translation, 'D3DXVECTOR3')}, // translation" yield f"{render_value(translation, 'D3DXVECTOR3')}, // translation"
yield f"{render_value(rotation, 'D3DXVECTOR4')}, // rotation" yield f"{render_value(rotation, 'D3DXVECTOR4')}, // rotation"
yield f"{render_value(scale, 'D3DXVECTOR3')}, // scale"
yield "};" yield "};"
def render_skins(gltf): def render_skins(gltf):
@ -123,7 +134,8 @@ def render_skins(gltf):
inverse_bind_matrices = skin["inverseBindMatrices"] inverse_bind_matrices = skin["inverseBindMatrices"]
yield f"const Skin skin_{skin_ix} = {{" yield f"const Skin skin_{skin_ix} = {{"
yield f"accessor_{inverse_bind_matrices}, // inverse bind matrices" yield f"accessor_{inverse_bind_matrices}, // inverse bind matrices"
yield f"{{ skin_{skin_ix}__joints, {len(skin['joints'])} }}," yield f"skin_{skin_ix}__joints, // joints"
yield f"{len(skin['joints'])}, // joints length"
yield "};" yield "};"
def render_animation_samplers(animation_ix, samplers): def render_animation_samplers(animation_ix, samplers):
@ -131,6 +143,7 @@ def render_animation_samplers(animation_ix, samplers):
yield f"const AnimationSampler animation_{animation_ix}__sampler_{sampler_ix} = {{" yield f"const AnimationSampler animation_{animation_ix}__sampler_{sampler_ix} = {{"
yield f"accessor_{sampler['input']}, // input, keyframe timestamps" yield f"accessor_{sampler['input']}, // input, keyframe timestamps"
yield f"accessor_{sampler['output']}, // output, keyframe values (void *)" yield f"accessor_{sampler['output']}, // output, keyframe values (void *)"
yield f"accessor_{sampler['input']}_length, // length"
yield "};" yield "};"