Ejemplo n.º 1
0
    def generate_mesh(self, data):

        if "error" in data:
            print(data["error"])
            return Mesh()

        meshes = []

        for i in range(len(data["mesh_informations"])):

            mesh_data = {}
            mesh_infos = {}

            indi_clip = (data["mesh_informations"][i]["first_triangle"] *
                         IQM_TRIANGLE_SIZE,
                         (data["mesh_informations"][i]["first_triangle"] +
                          data["mesh_informations"][i]["num_triangle"]) *
                         IQM_TRIANGLE_SIZE)
            used_indices = data["indices"][indi_clip[0]:indi_clip[1]]

            mesh_infos["num_triangles"] = data["mesh_informations"][i][
                "num_triangle"]
            mesh_infos["num_vertices"] = data["mesh_informations"][i][
                "num_vertices"]
            mesh_infos["num_bones"] = 0

            if "animation_data" in data:
                mesh_infos["num_bones"] = len(data["animation_data"]["joints"])

            for j in range(len(data["buffers"])):

                name = "str_" + str(j)
                buf = data["buffers"][j]

                type = buf["data_type"][1]
                buf_data = buf["data"]
                mesh_data[name] = {}

                mesh_data[name]["type"] = GL_ARRAY_BUFFER
                mesh_data[name]["size"] = ctypes.sizeof(type * len(buf_data))

                mesh_data[name]["data"] = (type * len(buf_data))(*buf_data)
                mesh_data[name]["attributes"] = [{}]

                mesh_data[name]["attributes"][0]["size"] = buf["size"]
                mesh_data[name]["attributes"][0]["normalized"] = buf[
                    "normalized"]
                mesh_data[name]["attributes"][0]["type"] = buf["data_type"][0]
                mesh_data[name]["attributes"][0]["stride"] = 0
                mesh_data[name]["attributes"][0]["offset"] = 0

            mesh_data["ibo"] = {}
            mesh_data["ibo"]["type"] = GL_ELEMENT_ARRAY_BUFFER
            mesh_data["ibo"]["size"] = ctypes.sizeof(GLuint *
                                                     len(used_indices))
            mesh_data["ibo"]["data"] = (GLuint *
                                        len(used_indices))(*used_indices)

            mesh = Mesh(data["mesh_informations"][i]["str_name"])
            mesh.get_buffer().prepare_buffer(GL_TRIANGLES, len(used_indices),
                                             GL_UNSIGNED_INT, mesh_data)
            mesh.get_buffer().create_buffer()
            mesh.set_aabb(data["bboxes"][0])
            mesh.assign_default_material(
                data["mesh_informations"][i]["material_obj"])

            mesh.assign_mesh_data(data)
            mesh.set_informations(mesh_infos)

            if "animation_data" in data:
                mesh.set_animation_player_class(IQMMeshAnimationPlayer)

            meshes.append(mesh)

        return meshes