利用者:Koilz/2.56 code review armature modiier

提供: wiki
< 利用者:Koilz
2013年2月23日 (土) 01:36時点におけるwiki>Koilzによる版 (Created page with "just explaining how it works, as good as i can. \source\blender\modifiers\intern\MOD_armature.c ModifierTypeInfo modifierType_Armature = { name: "Armature...")
(差分) ← 古い版 | 最新版 (差分) | 新しい版 → (差分)
移動先: 案内検索

just explaining how it works, as good as i can.


\source\blender\modifiers\intern\MOD_armature.c

ModifierTypeInfo modifierType_Armature = {
	/* name */              "Armature",
	/* structName */        "ArmatureModifierData",
	/* structSize */        sizeof(ArmatureModifierData),
	/* type */              eModifierTypeType_OnlyDeform,
	/* flags */             eModifierTypeFlag_AcceptsCVs
							| eModifierTypeFlag_SupportsEditmode,

	/* copyData */          copyData,                   // copy data
	/* deformVerts */       deformVerts,                // deform vertices pose object mode
	/* deformMatrices */    deformMatrices,             // deform matrices pose object mode
	/* deformVertsEM */     deformVertsEM,              // deform vertices edit mode
	/* deformMatricesEM */  deformMatricesEM,           // deform matrices edit mode
	/* applyModifier */     NULL,
	/* applyModifierEM */   NULL,
	/* initData */          initData,                   // init code
	/* requiredDataMask */  requiredDataMask,
	/* freeData */          NULL,
	/* isDisabled */        isDisabled,
	/* updateDepgraph */    updateDepgraph,
	/* dependsOnTime */     NULL,
	/* dependsOnNormals */	NULL,
	/* foreachObjectLink */ foreachObjectLink,
	/* foreachIDLink */     NULL,
};

This is the struct that sets the modifier, the main functions to carry out the process in MOD_armature.c are:

deformVerts,                // deform vertices pose object mode
deformMatrices,             // deform matrices pose object mode
deformVertsEM,              // deform vertices edit mode
deformMatricesEM,           // deform matrices edit mode

These functions work pritty much the same, then they all execute "armature_deform_verts()" in source\blender\blenkernel\intern\armature.c

Most the code for the modifier is written in armature.c and some more armature code that other modifiers and other parts of blender use.

More about "armature_deform_verts()" later, first the "pose solver" "pchan_to_mat4()".

\source\blender\blenkernel\intern\armature.c

/* ********************** THE POSE SOLVER ******************* */
/* loc/rot/size to given mat4 */
void pchan_to_mat4(bPoseChannel *pchan, float chan_mat[4][4])
{
	..
}

This function takes the pose bone rotation (dependant on mode) and scale, it converts them into matrices, then it converts both of them into a final matrice "chan_mat[4][4]".

"chan_mat" channel matrice, is used later as a deformation matrice to deform mesh verts. Its later accessed via the pose bone, pchan->chan_mat.

Allot of cool pose bone data is in the bPoseChannel struct in \source\blender\makesdna\DNA_action_types.h

Back to the "armature_deform_verts()" function, this function is a bit complex due to many features.

In blender, say you setup an armature with 2 bones that deforms a mesh with 3 verts.

This is the process, how the armature modifier deforms the verts.

Loop verts
	Loop bones/vertex_groups connected to the vert
		execute pchan_bone_deform();
vert[0]
	bone[0]	pchan_bone_deform();
vert[1]
	bone[0]	pchan_bone_deform();
	bone[1]	pchan_bone_deform();
vert[2]
	bone[1]	pchan_bone_deform();

Thats how it works in a basic form, of course the code is more complicated.

pchan_bone_deform() deforms the verts based on bone data, first it multiplys a copy of the mesh vert "cop[3]" by the pose matrice "chan_mat[4][4]".

mul_m4_v3(pchan->chan_mat, cop);

Then it adds the deformed vert position onto the original vert position by the weight of the bone.

vec[0]+=(cop[0]-co[0])*weight;
vec[1]+=(cop[1]-co[1])*weight;
vec[2]+=(cop[2]-co[2])*weight;

Vert origins in this instance "cop" and "co" (the zero position) are from the armatures objects origin.

The armature modifier has alot more processess dependant on different setting you can use (envelopes, preserve volume, multi modifier).