平民程序 - linghuye's blog

天下风云出我辈,一入江湖岁月催。皇图霸业谈笑中,不胜人生一场醉。提剑跨骑挥鬼雨,白骨如山鸟惊飞。尘事如潮人如水,只笑江湖几人回。

随笔 - 221, 文章 - 0, 评论 - 680, 引用 - 0
数据加载中……

HLSL骨骼动画实现

最近将我的DestinyMatrix图形接口用DirectX实现了一遍,过程中接口又进化了一次,现在可以像WoW一样选择使用那个API来渲染WoW的场景,觉得挺不错的.又用HLSL实现了一次骨骼动画:
struct VS_INPUT
{
    float3 position : POSITION;
    float2 texCoord    : TEXCOORD0;
    float4 weight     : BLENDWEIGHT;
    float4 bone        : BLENDINDICES;  
}
;

struct VS_OUTPUT
{
    float4 position : POSITION;
    float2 texCoord : TEXCOORD0;
    float4 color     : COLOR;
}
;

uniform matrix modelViewProj;
uniform matrix boneMatrices[
60];

void main(in VS_INPUT inVert, out VS_OUTPUT outVert)
{    
    float4 blendVertex 
= float4(0.0f0.0f0.0f0.0f);
    float4 blendWeight 
= inVert.weight;
    int4 blendBone 
= int4(inVert.bone);
    
    
for(int i = 0; i < 4; i++)
    
{    
        
if(blendWeight.x > 0.0f)
        
{    
            blendVertex 
+= mul(float4(inVert.position, 1.0f), boneMatrices[blendBone.x]) * blendWeight.x;
            blendBone 
= blendBone.yzwx;
            blendWeight 
= blendWeight.yzwx;
        }

    }

    
    outVert.position 
= mul(blendVertex, modelViewProj);    
    outVert.texCoord 
= inVert.texCoord;
    outVert.color 
= float4(1.01.0f1.0f1.0f);
}
对比HLSL和GLSL,
1.OpenGL里的顶点属性attribute在DirectX里是通过SetStreamSource设置进去,感觉OpenGL的顶点attribute的思想更灵活.
2.为了实现接口,DirectX使用了多流模式与OpenGL的glXXXPointer保持思想上的兼容.
3.GLSL和Cg的顶点计算是标准数学的mul(mat, v),而HLSL是mul(v, mat),我调试了很久才发现 :(
4.HLSL的modelViewProj需要外部程序使用LPD3DXCONSTANTTABLE设置Matrix变量,而GLSL是内置的变量不需要外部传入.

posted on 2007-01-22 22:58 linghuye 阅读(3819) 评论(2)  编辑 收藏 引用 所属分类: 3D图形学研究我的3D引擎 -DestinyMatrix

评论

# re: HLSL骨骼动画实现  回复  更多评论   

mul(a, b)
Performs matrix multiplication between a and b. If a is a vector, it is treated as a row vector. If b is a vector, it is treated as a column vector. The inner dimension acolumns and brows must be equal. The result has the dimension arows x bcolumns.

好像在nVidia显卡上才有你说的那种情况
2008-05-03 23:48 | 逍遥剑客

# re: HLSL骨骼动画实现  回复  更多评论   

楼主,你忘了计算法线,你这样子光照会不正确!
2009-08-20 23:20 | xhn
只有注册用户登录后才能发表评论。