WoW骨骼動畫的GLSL實現
收藏在OpenGL.org論壇上不幸被告知VERTEX_BLEND_ARB擴充不屬OpenGL核心標準,被ARB卡查掉了,Nvidia也不實現了,所以在OpenGL下要做骨骼動畫只好靠Shader了,讓CPU計算,效率會很慘,寫寫Demo還湊和.
使用指令將頂點的骨骼和權重資訊作為Generic Attribute屬性傳入給Shader對象.
GLint indexBone = glGetAttribLocationARB(m_glPrograme, "bone");
GLint indexWeight = glGetAttribLocationARB(m_glPrograme, "weight");
glVertexAttribPointerARB(indexBone, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(MDL_VERTEX), m_arrMdlVertices[0].bones);
glVertexAttribPointerARB(indexWeight, 4, GL_UNSIGNED_BYTE, GL_FALSE, sizeof(MDL_VERTEX), m_arrMdlVertices[0].weights);
glEnableVertexAttribArrayARB(indexBone);
glEnableVertexAttribArrayARB(indexWeight);
在每幀在CPU中計算骨骼矩陣,並將計算好的骨骼矩陣數組傳入Shader對象.
GLint indexBoneMatrices = glGetUniformLocationARB(m_model.m_glPrograme, "boneMatrices");
for(unsigned int i = 0; i < m_animSkeleton.m_vecBones.size(); i++)
{
CM2Bone* pBone = m_animSkeleton.m_vecBones[i];
glUniformMatrix4fvARB(indexBoneMatrices + i, 1, GL_TRUE, pBone->m_transformMatrix);
}
搞定,運行,發現wow的獸人模型竟然有128個骨骼,而N6200的shader的Uniform Matrix參數最多隻給60個,還得考慮低端機器,玻璃渣是怎麼做的?發現DX的實現從來就是送入硬體處理的,節省了額外的頂點記憶體,加快了速度,確實是不二的選擇.
attribute vec4 bone;
attribute vec4 weight;
uniform mat4 boneMatrices[60];
const float fw = 1.0 / 255.0;
void main()
{
vec4 blendVertex;
vec4 blendWeight = weight;
ivec4 blendBone = (ivec4)bone;
for(int i = 0; i < 4; i++)
{
if(blendWeight.x > 0.0)
{
blendVertex += vec4((boneMatrices[blendBone.x] * gl_Vertex).xyz, 1.0) * blendWeight.x * fw;
blendBone = blendBone.yzwx;
blendWeight = blendWeight.yzwx;
}
else break;
}
gl_Position = gl_ModelViewProjectionMatrix * blendVertex;
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;
gl_FrontColor = gl_Color;
}