提高Shader Model 2.0 蒙皮骨骼动画的骨骼限制

1649 篇文章 11 订阅
1623 篇文章 22 订阅

传统的蒙皮骨骼动画混合方法易于理解,但是在SM 2.0的256常量限制下,骨骼数保守计算最多50根骨头,因此对美术的工作流程以及模型渲染方法造成了很大的障碍

float4x4 matBoneArray[40]; //  这是传输的瓶颈
 
VS_OUTPUT vs_main( SkinnedVS_INPUT In )
{
 
    VS_OUTPUT Out = (VS_OUTPUT)0;
 
    float4x4 skinTransform = 0;
 
    skinTransform += matBoneArray[In.BoneIndices.x] * In.BoneWeights.x;
    skinTransform += matBoneArray[In.BoneIndices.y] * In.BoneWeights.y;
    skinTransform += matBoneArray[In.BoneIndices.z] * In.BoneWeights.z;
    skinTransform += matBoneArray[In.BoneIndices.w] * In.BoneWeights.w;
    float4 localpos = mul(In.Position, skinTransform);
    
    Out.Position = mul( localpos, matViewProj ); 
    Out.TexCoord = In.TexCoord;
  
    return Out;
}

matBoneArray这个数组是骨骼的LocalRot和LocalTranslation 通过以下函数build出来

    Matrix4& Matrix4::FromTranslationRotation( const Vector3& translation, const Quaternion& rotation )
    {
        float xx = rotation.x * rotation.x * 2.0f, yy = rotation.y * rotation.y * 2.0f, zz = rotation.z * rotation.z * 2.0f;
        float xy = rotation.x * rotation.y * 2.0f, zw = rotation.z * rotation.w * 2.0f, xz = rotation.x * rotation.z * 2.0f;
        float yw = rotation.y * rotation.w * 2.0f, yz = rotation.y * rotation.z * 2.0f, xw = rotation.x * rotation.w * 2.0f;
 
        m[0][0] = 1.0f - yy - zz; m[0][1] =        xy + zw; m[0][2] =        xz - yw; m[0][3] = 0.0f;
        m[1][0] =        xy - zw; m[1][1] = 1.0f - xx - zz; m[1][2] =        yz + xw; m[1][3] = 0.0f;
        m[2][0] =        xz + yw; m[2][1] =        yz - xw; m[2][2] = 1.0f - xx - yy; m[2][3] = 0.0f;
        m[3][0] =  translation.x; m[3][1] =  translation.y; m[3][2] =  translation.z; m[3][3] = 1.0f;
 
        return *this;
    }

从这里你可以发现, 本来每根骨头只需要2个float4 传递变换信息的,现在却需要4个float4,也就是一个矩阵来传递,矩阵中还有很多不使用的变量也被传输到GPU中,这里就是优化的点.

重新调整后的Shader代码:

float4x4 BuildFromTransRot( float4 translation, float4 rot )
{
    float4 rotation = rot;
    
    float xx = rotation.x * rotation.x * 2.0f, yy = rotation.y * rotation.y * 2.0f, zz = rotation.z * rotation.z * 2.0f;
    float xy = rotation.x * rotation.y * 2.0f, zw = rotation.z * rotation.w * 2.0f, xz = rotation.x * rotation.z * 2.0f;
    float yw = rotation.y * rotation.w * 2.0f, yz = rotation.y * rotation.z * 2.0f, xw = rotation.x * rotation.w * 2.0f;
    float4x4 m = { 
    {1.0f - yy - zz,             xy + zw,             xz - yw,         0},
    {xy - zw,              1.0f - xx - zz,             yz + xw,         0},
    {xz + yw,                      yz - xw,     1.0f - xx - yy,         0},
    {translation.x,       translation.y,       translation.z,         1}
    
    };
    
    return m;
}
 
float4x4 GetBoneElement( float index )
{
    return BuildFromTransRot( vecBoneLocalTrans[index], vecBoneLocalRot[index] );
}

VS_OUTPUT vs_main( SkinnedVS_INPUT In )
{
 
    VS_OUTPUT Out = (VS_OUTPUT)0;
 
    float4x4 skinTransform = 0;
 
    skinTransform += GetBoneElement(In.BoneIndices.x) * In.BoneWeights.x;
    skinTransform += GetBoneElement(In.BoneIndices.y) * In.BoneWeights.y;
    skinTransform += GetBoneElement(In.BoneIndices.z) * In.BoneWeights.z;
    skinTransform += GetBoneElement(In.BoneIndices.w) * In.BoneWeights.w;
    float4 localpos = mul(In.Position, skinTransform);
    
    Out.Position = mul( localpos, matViewProj ); 
    Out.TexCoord = In.TexCoord;
  
    return Out;
}

我们将骨头的local旋转及偏移传递至GPU,然后在GPU内重组,虽然对GPU性能计算有部分损耗,但是骨骼数量就能保守提高到100个.

 

http://www.cppblog.com/sunicdavy/archive/2010/04/26/113578.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
加载骨骼动画需要使用Assimp库中的骨骼数据,同时利用OpenGL进行渲染。下面是一个简单的示例代码: 首先,你需要在Qt的.pro文件中添加以下库和头文件: ```qmake LIBS += -lassimp INCLUDEPATH += /usr/local/include/assimp ``` 然后,在你的OpenGL窗口类中添加以下成员变量: ```cpp #include <assimp/Importer.hpp> #include <assimp/scene.h> #include <assimp/postprocess.h> // 骨骼动画相关 Assimp::Importer importer; const aiScene* scene; std::vector<glm::mat4> boneTransforms; std::vector<glm::mat4> boneOffsets; std::map<std::string, int> boneMapping; int numBones; float animationTime; ``` 在OpenGL窗口类的初始化函数中,加载模型和骨骼动画数据: ```cpp // 加载模型和骨骼动画数据 scene = importer.ReadFile("path/to/model", aiProcess_Triangulate | aiProcess_FlipUVs | aiProcess_GenSmoothNormals | aiProcess_LimitBoneWeights | aiProcess_ValidateDataStructure | aiProcess_ImproveCacheLocality | aiProcess_RemoveRedundantMaterials | aiProcess_OptimizeMeshes | aiProcess_CalcTangentSpace | aiProcess_JoinIdenticalVertices | aiProcess_FindInvalidData | aiProcess_GenUVCoords | aiProcess_TransformUVCoords | aiProcess_FixInfacingNormals | aiProcess_FindDegenerates | aiProcess_SortByPType | aiProcess_GenBoundingBoxes); if (!scene) { qCritical() << "Error loading model: " << importer.GetErrorString(); return; } // 加载骨骼动画数据 if (scene->HasAnimations()) { numBones = 0; for (unsigned int i = 0; i < scene->mNumMeshes; i++) { aiMesh* mesh = scene->mMeshes[i]; for (unsigned int j = 0; j < mesh->mNumBones; j++) { std::string boneName(mesh->mBones[j]->mName.data); if (boneMapping.find(boneName) == boneMapping.end()) { int boneIndex = numBones++; boneMapping[boneName] = boneIndex; boneOffsets.push_back(glm::transpose(glm::make_mat4(&mesh->mBones[j]->mOffsetMatrix.a1))); } } } boneTransforms.resize(numBones); animationTime = 0.0f; } ``` 在OpenGL窗口类的绘制函数中,更新骨骼动画的状态: ```cpp // 更新骨骼动画状态 if (scene->HasAnimations()) { animationTime += deltaTime; float ticksPerSecond = (float)(scene->mAnimations[0]->mTicksPerSecond != 0 ? scene->mAnimations[0]->mTicksPerSecond : 25.0f); float timeInTicks = fmod(animationTime * ticksPerSecond, (float)scene->mAnimations[0]->mDuration); float animationTimeInSeconds = timeInTicks / ticksPerSecond; glm::mat4 identity; for (int i = 0; i < numBones; i++) { boneTransforms[i] = identity; } aiMatrix4x4 globalTransform = scene->mRootNode->mTransformation; updateBoneTransform(scene->mAnimations[0], animationTimeInSeconds, scene->mRootNode, globalTransform); for (unsigned int i = 0; i < boneTransforms.size(); i++) { boneTransforms[i] = boneTransforms[i] * boneOffsets[i]; } } // 设置骨骼动画的变换矩阵 for (unsigned int i = 0; i < shader.numBones; i++) { glm::mat4 boneTransform; if (i < boneTransforms.size()) { boneTransform = boneTransforms[i]; } glUniformMatrix4fv(shader.boneTransforms[i], 1, GL_FALSE, glm::value_ptr(boneTransform)); } ``` 最后,在你的OpenGL着色器中使用骨骼动画的变换矩阵: ```glsl #version 330 core layout(location = 0) in vec3 position; layout(location = 1) in vec2 texCoord; layout(location = 2) in vec3 normal; layout(location = 3) in vec3 tangent; layout(location = 4) in vec3 bitangent; layout(location = 5) in ivec4 boneIDs; layout(location = 6) in vec4 boneWeights; uniform mat4 modelMatrix; uniform mat4 viewMatrix; uniform mat4 projectionMatrix; uniform mat3 normalMatrix; uniform mat4 boneTransforms[100]; out vec2 fragTexCoord; out vec3 fragNormal; out vec3 fragTangent; out vec3 fragBitangent; void main() { vec4 pos = vec4(position, 1.0); vec4 norm = vec4(normal, 0.0); vec4 tang = vec4(tangent, 0.0); vec4 bitang = vec4(bitangent, 0.0); // 骨骼动画变换 mat4 boneTransform = boneTransforms[boneIDs.x] * boneWeights.x; boneTransform += boneTransforms[boneIDs.y] * boneWeights.y; boneTransform += boneTransforms[boneIDs.z] * boneWeights.z; boneTransform += boneTransforms[boneIDs.w] * boneWeights.w; pos = boneTransform * pos; norm = boneTransform * norm; tang = boneTransform * tang; bitang = boneTransform * bitang; gl_Position = projectionMatrix * viewMatrix * modelMatrix * pos; fragTexCoord = texCoord; fragNormal = normalize(normalMatrix * norm.xyz); fragTangent = normalize(normalMatrix * tang.xyz); fragBitangent = normalize(normalMatrix * bitang.xyz); } ``` 这样,你就可以在Qt中使用OpenGL和Assimp库加载骨骼动画模型了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值