“我尝试学习骨骼动画,所以我有很多误解” 我有一个从 Blender 导出的非常简单的 JSON 格式文件,其中一个立方体和一个骨骼仅旋转 4 帧,所以我加载了具有 4 个属性的 VBO,位置 vec4,正常 vec4、权重 vec2 和骨骼索引 vec2 正确渲染,骨骼有 pos、rotq 和 scale 所以我使用以下函数创建骨骼的变换矩阵
public static float[] createMat4(float[] t, float[] r, float[] s) {
float[] mat4 = new float[16];
float[] T = new float[16];
float[] R = new float[16];
float[] S = new float[16];
setIdentityM(T, 0);
setIdentityM(R, 0);
setIdentityM(S, 0);
translateM(T, 0, t[0], t[1], t[2]);
rotateM(R, 0, r[3], r[0], r[1], r[2]);
scaleM(S, 0, s[0], s[1], s[2]);
float[] temp = new float[16];
multiplyMM(temp, 0, T, 0, R, 0);
multiplyMM(mat4, 0, temp, 0, S, 0);
return mat4;
}
所以我计算骨骼最终矩阵,将绑定乘以它的逆“据我所知”对于每一帧我将此矩阵乘以关键帧矩阵,该矩阵也是我通过前一种方法从 pos、rotq 和 scl 生成的,最后我上传这些矩阵通过 mat4[] 统一到 GPU,这是顶点着色器
uniform mat4 modelview;
uniform mat4 projection;
uniform mat4 bones[BONES];
uniform int animated;
attribute vec4 vertexPosition;
attribute vec4 vertexNormal;
attribute vec2 textureCoords;
attribute vec2 skinweight;
attribute vec2 skinindex;
varying vec2 vTextureCoords;
varying vec4 viewDir;
varying vec4 modelviewNormal;
varying mat4 mv;
void main() {
mv=modelview;
vec4 newVertex;
vec4 newNormal;
if(animated==1){
int index;
index=int(skinindex.x);
newVertex += (bones[index] * vertexPosition * skinweight.x) ;
newNormal += (bones[index] * vertexPosition * skinweight.x) ;
index=int(skinindex.y);
newVertex += (bones[index] * vertexPosition * skinweight.y);
newNormal += (bones[index] * vertexNormal* skinweight.y);
}
else{
newVertex=vertexPosition;
newNormal=vertexNormal;
}
vec4 modelviewVertex=(modelview * newNormal);
modelviewNormal = normalize(modelviewVertex);
viewDir = normalize(-modelview*newVertex);
vTextureCoords = textureCoords;
gl_Position = (projection * modelview )* vec4(newVertex.xyz, 1.0);
}