WoW骨骼動畫的GLSL實現

來源:互聯網
上載者:User
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;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.