在Android上通过OpenGL ES显示混合器3D角色的方法

时间:2014-06-17 17:30:12

标签: android python animation opengl-es blender

我想创建我个人的轻量级Android 3D引擎来展示3D动画 由Blender配置。

我已经在Android上成功加载了由OpenGL ES创建的Blender创建的3D模型。现在, 我想进一步介绍3D模型的动画 - 由Blender制作的动画角色 Android上的OpenGL ES。

以下是我的步骤:
[1]使用Blender 2.69创建一个3D角色并在不同的帧中制作动画,完全为
250个帧,其中3D字符应用于metarig电枢对象和
保证每个顶点至少包含一个顶点组和专用权重 [2]修改./2.69/scripts/addons/io_scene_obj/export.py并添加必要的
Blender python定义了导出骨架整个骨骼系统的功能 实际上我以.obj格式导出3D角色并添加我个人定义的标签
对于骨骼系统,我将标签命名为 - bonelib。将其存储在文件中* .bones
[3]修改./2.69/scripts/addons/io_scene_obj/export.py,以便它可以导出
以帧为单位的动画,每帧包含变换 - 过渡,
旋转,电枢中每个不同骨骼的比例。我添加了我个人定义的标签
对于动画帧 - framelib。将其存储在文件* .fms中 [4]我编写了一个java应用程序来将* .obj,* .bones,*。fms翻译成十六进制二进制格式
* .bin,*。frames文件,可以成功加载3D模型并恢复每个细节 有关在Android中使用OpenGL ES构建3D模型的方法 [5]我添加了一些函数来计算每个骨骼的正确变换矩阵和
将它应用到每个顶点,并在其所属的顶点组中使用加权值。

经过这么多的努力,我没能显示我的3D角色的正确动画,它 是扭曲的,无法告诉它究竟是什么3D模型。

我将代码分为两部分:
[Part-1] Blender python导出3D模型,骨骼系统,动画数据

要导出骨架,我在下面定义了两个函数:

def matrix_to_string(m, dim=4):
s = ""

for i in range(0, dim):
    for j in range(0, dim):
        s += "%+.2f "%(m[i][j])
return(s)

def export_armature(path_dir, objects):
#Build up the dictionary for the armature parented by all mesh objects
ArmatureDict = {}vertex coordinate

for ob in objects:
    b_export_armature = True

    if ob.parent and (ob.parent_type == 'OBJECT' or ob.parent_type == 'ARMATURE' or ob.parent_type == 'BONE'):
        if ob.parent_type == 'OBJECT':
            if ob.parent.type != 'ARMATURE':
                b_export_armature = False

        if b_export_armature == True:
            p = ArmatureDict.get(ob.parent.name)

            if p is None:
                ArmatureDict[ob.parent.name] = ob.parent

#print("Total %d armatures to be exported\n" %(len(ArmatureDict)))

for key in ArmatureDict.keys():
    a_obj = ArmatureDict.get(key)
    filename = a_obj.name + ".bones"
    bonesfilepath = path_dir + '/' + filename
    file = open(bonesfilepath, "w", encoding="utf8", newline="\n")
    fw = file.write

    #Write Header
    fw('#Blender v%s BONS File: %s\n' %(bpy.app.version_string, filename))
    fw('#Author: mjtsai1974\n')
    fw('\n')

    #Armature architecture portion
    list_bones = a_obj.data.bones[:]

    fw('armature %s\n' %(a_obj.name))

    for bone in list_bones:
        fw('bonechain %s' %(bone.name))
        child_bones = bone.children
        for child in child_bones:
            fw(' %s' %(child.name))
        fw('\n')

    fw('\n')

    #Armature and its bone chain restpose portion
    mat_rot = mathutils.Matrix.Rotation(math.radians(90.0), 4, 'X')
    m = mat_rot * a_obj.matrix_world
    fw('restpose armature %s %s\n' %(a_obj.name, matrix_to_string(m, 4)))

    for bone in list_bones:
        m = mat_rot * bone.matrix_local
        fw('restpose bone %s %s\n' %(bone.name, matrix_to_string(m, 4)))

    file.close()

要从图形编辑器导出,我定义了以下功能:

def get_bone_action_location(action, bonename, frame=1):
loc = Vector()
if action == None:
    return(loc)
data_path = 'pose.bones["%s"].location'%(bonename)
for fc in action.fcurves:
    if fc.data_path == data_path:
        loc[fc.array_index] = fc.evaluate(frame)
return(loc)

def get_bone_action_rotation(action, bonename, frame=1):
rot = Quaternion( (1, 0, 0, 0) )  #the default quat is not 0
if action == None:
    return(rot)
data_path = 'pose.bones["%s"].rotation_quaternion'%(bonename)
for fc in action.fcurves:
    if fc.data_path == data_path: # and frame > 0 and frame-1 <= len(fc.keyframe_points):
        rot[fc.array_index] = fc.evaluate(frame)
return(rot)

def export_animation_by_armature(filepath, frames, objects):
#Build up the dictionary for the armature parented by all mesh objects
ArmatureDict = {}

mat_rot = mathutils.Matrix.Rotation(math.radians(90.0), 4, 'X')

for ob in objects:
    b_export_armature = True

    if ob.parent and (ob.parent_type == 'OBJECT' or ob.parent_type == 'ARMATURE' or ob.parent_type == 'BONE'):
        if ob.parent_type == 'OBJECT':
            if ob.parent.type != 'ARMATURE':
                b_export_armature = False

        if b_export_armature == True:
            p = ArmatureDict.get(ob.parent.name)

            if p is None:
                ArmatureDict[ob.parent.name] = ob.parent

#print("Total %d armatures to be exported\n" %(len(ArmatureDict)))

path_dir = os.path.dirname(filepath) 

for key in ArmatureDict.keys():
    a_obj = ArmatureDict.get(key)
    filename = a_obj.name + ".fms"
    bonesfilepath = path_dir + '/' + filename
    file = open(bonesfilepath, "w", encoding="utf8", newline="\n")
    fw = file.write

    #Write Header
    fw('#Blender v%s FRAMES File: %s\n' %(bpy.app.version_string, filename))
    fw('#Author: mjtsai1974\n')
    fw('\n')

    #Use armature pose bone chain for 
    list_posebones = a_obj.pose.bones[:]

    fw('animator %s\n' %(a_obj.name))

    for frame in frames:
        bpy.context.scene.frame_set(frame)

        action =a_obj.animation_data.action
        #action = bpy.data.objects[a_obj.name].animation_data.action

        if action == None:
            print("Armature %s has no action" %(a_obj.name))

        fw('frame %d\n' %(frame))

        for bone in list_posebones:
            m = get_bone_action_rotation(action, bone.name, frame) #read the fcurve-animation rotation
            l = get_bone_action_location(action, bone.name, frame) #read the fcurve-animation location

            #m = m.to_matrix().to_4x4()
            #m = mat_rot * m
            q =  Quaternion((m.w, m.x, -m.z, m.y))

            tl = mathutils.Matrix.Translation(l)
            tr = mat_rot * tl
            loc = tr.to_translation()

            fw('bone %s t %+.2f %+.2f %+.2f\n' %(bone.name, loc[0], loc[1], loc[2]))
            fw('bone %s r %+.2f %+.2f %+.2f %+.2f\n' %(bone.name, q.w, q.x, q.y, q.z))
            fw('bone %s s %+.2f %+.2f %+.2f\n' %(bone.name, 1.0, 1.0, 1.0))

        fw('\n')

    file.close()

#[mjtsai@20140517]append the .frames information at the end of .obj file
file = open(filepath, "a+", encoding="utf8", newline="\n")
fw = file.write

for key in ArmatureDict.keys():
    a_obj = ArmatureDict.get(key)
    filename = a_obj.name + ".fms"

    fw('\nframelib %s\n' %(filename))

file.close()

以上所有我认为我已经正确地将所有骨骼输出到电枢中 从Blender坐标系到OpenGL坐标系。任何人都可以指出在哪里 我可能错误的代码段可能吗?

[Part-2] Android APK找出正确的OpenGL坐标

这是我计算不同bine的转换数据的函数:

public void buildTransformationDataByBoneNameAtFrame(String sz_Name, Armature ar_Obj, FrameLib fl_Obj, int idx_Frame) {
        float [] f_ary_Matrix_Inverted = new float[16];
        float [] f_ary_Matrix_Total = new float[16];
        float [] f_ary_Matrix_Self = new float[16];
        float [] f_ary_Data_Matrix_Parent = null;
        float [] f_ary_Data_Self = null;
        float [] f_ary_Data_Parent = null;
        AnimatorUnit au_Obj = fl_Obj.getAnimatorUnit();
        ArrayList<String> ary_list_Strings = ar_Obj.buildFromRootToBoneByName(sz_Name);
        FrameUnit fu_Obj = au_Obj.getFrameByIndex(idx_Frame);

        if (ary_list_Strings == null) {
            LoggerConfig.Log(String.format("Failed in building array list for Bone[%s] ", sz_Name));

            //new RuntimeException(String.format("Failed in building array list for Bone[%s] ", sz_Name));
            return;
        }

        if (fu_Obj == null) {
            LoggerConfig.Log(String.format("FrameUnit - %d doesn't exist ", idx_Frame));

            return;
        }

        String sz_LastBoneName = ary_list_Strings.get(ary_list_Strings.size() - 1);

        //Suppose the very last one in the array list should be the same bone name to sz_Name
        if (!sz_LastBoneName.equals(sz_Name)) {
            LoggerConfig.Log(String.format("LAST_BONE_NAME[%s] != Bone[%s]", sz_LastBoneName, sz_Name));

            return;
        }

        for (int Index = 0; Index < ary_list_Strings.size(); Index++) {
            String sz_ParentBoneName = "";
            String sz_BoneName = ary_list_Strings.get(Index);
            AnimationUnit amu_Obj = fu_Obj.getAnimationUnitByName(sz_BoneName);
            AnimationUnit amu_ParentObj = null;
            Restpose rp_Obj = ar_Obj.getRestposeByName(sz_BoneName);
            Restpose rp_ParentObj = null;

            if (Index != 0) {
                //This means that it is child bone
                //Get parent bone
                sz_ParentBoneName = ary_list_Strings.get(Index - 1);
                rp_ParentObj = ar_Obj.getRestposeByName(sz_ParentBoneName);
                amu_ParentObj = fu_Obj.getAnimationUnitByName(sz_ParentBoneName);
                f_ary_Data_Matrix_Parent =  amu_ParentObj.getTransformationData();
                f_ary_Data_Parent = rp_ParentObj.getData();  //parent bone's local matrix

                Matrix.invertM(f_ary_Matrix_Inverted, 0, f_ary_Data_Parent, 0);  //parent bone's inverse local matrix

                //Get child bone itself
                f_ary_Data_Self = rp_Obj.getData();  //child bone's local matrix

                //Multiply child bone's local matrix by parent bone's inverse local matrix
                Matrix.multiplyMM(f_ary_Matrix_Self, 0, f_ary_Matrix_Inverted, 0, f_ary_Data_Self, 0);

                //Multiply (child bone's local matrix by parent bone's inverse local matrix) by parent bone's transformation matrix
                Matrix.multiplyMM(f_ary_Matrix_Total, 0, f_ary_Data_Matrix_Parent, 0, f_ary_Matrix_Self, 0);

                amu_Obj.inflateTransformationData();
                amu_Obj.finalizeTransformationData(f_ary_Matrix_Total);
            }   else {
                //This means that it is root bone
                f_ary_Data_Self = rp_Obj.getData();

                amu_Obj.inflateTransformationData();
                amu_Obj.finalizeTransformationData(f_ary_Data_Self);
            }
        }

        //Before we return float array, free the arraylist just returned from ar_Obj.buildFromRootToBoneByName(sz_Name);
        ary_list_Strings.clear();

        //For garbage collection
        ary_list_Strings = null;
        f_ary_Matrix_Inverted = null;
        f_ary_Matrix_Total = null;
        f_ary_Matrix_Self = null;
    }

下面我列出了用于构建转换数据的调用者代码片段:
    FrameLibInfoWavefrontObjectToBinary framelibInfo = new FrameLibInfoWavefrontObjectToBinary(m_WavefrontObject);

i_ary_Statistics[0] = i_ary_Statistics[1] = 0;

framelibInfo.read(dis, i_ary_Statistics);

FrameLib fl_Obj = m_WavefrontObject.getFrameLib();
AnimatorUnit au_Obj = null;
BoneLib bl_Obj = null;
Armature ar_Obj = null;
ArrayList<BoneChain> bc_Objs = null;
BoneChain bc_Obj = null;
Bone b_Obj = null;
int count_Frames = 0;
String sz_BoneName = ""; 

if (fl_Obj != null) {
    au_Obj = fl_Obj.getAnimatorUnit();

    count_Frames = au_Obj.getFrameCount();

    if (count_Frames > 0) {
        bl_Obj = m_WavefrontObject.getBoneLibs().get(0);  //By default, we have only one bonelib

        ar_Obj = bl_Obj.getArmature();

        bc_Objs = ar_Obj.getBoneChains();

        for (int i_Frame = 0; i_Frame < count_Frames; i_Frame++)  {
            for (int i_BC = 0; i_BC < bc_Objs.size(); i_BC++) {
                bc_Obj = bc_Objs.get(i_BC);

                b_Obj = bc_Obj.getParentBone();

                framelibInfo.buildTransformationDataByBoneNameAtFrame(b_Obj.getName(),  ar_Obj, fl_Obj, i_Frame);
            }
        }
    }
}

framelibInfo.dispose();
framelibInfo = null;

[问题]我无法显示Blender应用的3D动画。我不知道在哪里 出错。对于坐标系转换问题,我认为我已经完全了 在python函数中实现。计算正确的转换数据 每个骨骼在不同的框架中,我也以相同的顺序恢复了matrix_local
将导出骨骼对象和骨架对象的matrix_world 该算法的灵感来自http://blenderartists.org/forum/showthread.php?209221-calculate-bone-location-rotation-from-fcurve-animation-data以下

我花了将近一个月的时间来评估这个python脚本,其中应用了绑定 我的3D角色,发现骨骼的计算最终坐标完全匹配
使用Blender本机内置骨骼坐标。

但是,为什么我不能在Android的3D角色中显示Blender应用的动画 OpenGL ES ???

1 个答案:

答案 0 :(得分:0)

为什么要创建自己的游戏引擎,当网上有几个非常好的,免费的和开源的实现在Android上运行良好时。我强烈建议您仔细查看jMonkeyEngine SDK或LWJGL for Java或PowerVR SDK或Assimp for C ++。我使用Blender的PowerVR插件导出到他们的POD和Collada格式,效果非常好。

其中一些开源解决方案可能会让您了解代码中缺少的内容。有一个完整的Android here游戏引擎列表。