Search code examples
androidopengl-eskeyframeskeletal-animation

OpenGL ES2 skeletal animation


"I try to learn skeletal animation so I have many misunderstands" I have a very simple JSON format file exported from Blender which has one cube and one bone rotated for 4 frames only, so I load the VBO which has 4 attributes the position vec4, normal vec4, weight vec2 and bone index vec2 which is rendered correctly, the bone has pos, rotq and scale so I used the following function to create the transformation matrix of the bone

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;
}

so I calculate the bone final matrix with multiplying the bind by its inverse "as I understand" the for each frame I multiply this matrix by the key frame matrix which also I generate by the previous method from pos, rotq and scl, finally I upload these matrices to GPU by mat4[] uniform, this is the vertex shader

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);
}

when rendering the result is unexpected some faces disappears and random vertex moving. Rendering animation enter image description here


Solution

  • The problem was in the quaternion rotation, I used this function to convert quaternion to rotation matrix

    private static float[] quaternionToMatrix(float[] q) {
        float[] m = new float[16];
        final float xx = q[0] * q[0];
        final float xy = q[0] * q[1];
        final float xz = q[0] * q[2];
        final float xw = q[0] * q[3];
        final float yy = q[1] * q[1];
        final float yz = q[1] * q[2];
        final float yw = q[1] * q[3];
        final float zz = q[2] * q[2];
        final float zw = q[2] * q[3];
        m[0] = 1 - 2 * (yy + zz);
        m[1] = 2 * (xy - zw);
        m[2] = 2 * (xz + yw);
        m[3] = 0;
        m[4] = 2 * (xy + zw);
        m[5] = 1 - 2 * (xx + zz);
        m[6] = 2 * (yz - xw);
        m[7] = 0;
        m[8] = 2 * (xz - yw);
        m[9] = 2 * (yz + xw);
        m[10] = 1 - 2 * (xx + yy);
        m[11] = 0;
        m[12] = 0;
        m[13] = 0;
        m[14] = 0;
        m[15] = 1;
        return m;
    }
    

    finally I get the skeletal animation work.