c:先來瞧瞧頂點的權重設定:
路人:解釋一下左上角數值要怎麼填吧.
c:那個值就是你在選擇頂點的時候,頂點會被改成這個權重值,最大只能輸入1,
當你的一個頂點會被2個骨骼平均影響的話,那這個值要填0.5,
然後先選第一個影響的骨骼,再選那個頂點,之後再選第二個影響的骨骼,
最後再選同一個點頂就完成了.
路人:你是在繞口令嗎?這樣誰聽得懂呀!
c:配合影片,然後再自行體會吧...
//網格計算
memcpy(Form1->Mmd->Mesh, Form1->Mmd->MeshCopy, sizeof(Vector) * Form1->Mmd->MeshCount );
Form1->BoneCopy->ComputeWPos();
Form1->DeformBone( Form1->BoneCopy->Bone, Form1->Mmd->MeshCopy, Form1->Mmd->Mesh );
//最重要的,將頂點乘上權矩陣後,依對應的骨骼與權重作運算
void TForm1::DeformBone(gBone *rootBone, Vector *meshdata, Vector *defdata)
{
if( !rootBone || !meshdata || !CV_weight)
return;
int loop,boneloop;
float weight=0;
Vector pre,post;
gBone *curBone = rootBone->children;
for (boneloop = 0; boneloop < rootBone->childCnt; boneloop++)
{
for (loop=0;loop<Mmd->MeshCount;loop++)
{
weight = CV_weight[rootBone->children[boneloop].id][loop];
if (weight > 0.0)
{
pre[0] = meshdata[loop][0] - rootBone->children[boneloop].wpos[0];
pre[1] = meshdata[loop][1] - rootBone->children[boneloop].wpos[1];
pre[2] = meshdata[loop][2] - rootBone->children[boneloop].wpos[2];
gVector tpost;
MultVectorByMatrix(&rootBone->children[boneloop].matrix, pre, &tpost);
post[0]=tpost.x,post[1]=tpost.y,post[2]=tpost.z;
//處理權重
defdata[loop][0] += ((post[0] - meshdata[loop][0]) * weight);
defdata[loop][1] += ((post[1] - meshdata[loop][1]) * weight);
defdata[loop][2] += ((post[2] - meshdata[loop][2]) * weight);
}
}
if (curBone->childCnt > 0)
DeformBone(curBone, meshdata, defdata);
curBone++;
}
}
//頂點與矩陣做運算
void TForm1::MultVectorByMatrix(gMatrix *mat, Vector v, gVector *result)
{
result->x = (mat->m[0] * v[0]) +
(mat->m[4] * v[1]) +
(mat->m[8] * v[2]) +
mat->m[12];
result->y = (mat->m[1] * v[0]) +
(mat->m[5] * v[1]) +
(mat->m[9] * v[2]) +
mat->m[13];
result->z = (mat->m[2] * v[0]) +
(mat->m[6] * v[1]) +
(mat->m[10] * v[2]) +
mat->m[14];
}
沒有留言:
張貼留言