Exemplo n.º 1
0
ModelInterface::ModelInterface(TextureManager* manager, const QString filename)
: has_animations(false)
, meshes(0)
, animations(0)
, bones(0)
, textureManager(manager)
{
    if(filename.contains("/"))
    {
        int index = filename.lastIndexOf("/") + 1;

        filePath = filename.left(index);
        fileName = filename.right(filename.size() - index);
    }
    else
        fileName = filename;

    Assimp::Importer importer;
    importer.SetPropertyInteger(AI_CONFIG_PP_LBW_MAX_WEIGHTS, 4);

    const aiScene* scene = importer.ReadFile(filename.toStdString(), aiProcess_GenSmoothNormals | aiProcess_Triangulate | aiProcess_CalcTangentSpace/* | aiProcess_FlipUVs*/);

    if(!scene || scene->mFlags == AI_SCENE_FLAGS_INCOMPLETE || !scene->mRootNode)
    {
        throw QString("The file wasn't successfuly opened");

        return;
    }

    if(scene->HasAnimations())
    {
        has_animations = true;
        bones          = loadBones(scene);

        buildSkeleton(scene->mRootNode, NULL);

        animations = new Animations();
        animations->setBones(bones);

        for(uint i = 0; i < scene->mNumAnimations; ++i)
            animations->add(loadAnimation(scene->mAnimations[i], i));
    }

    meshes = new Meshes();

    for(uint i = 0; i < scene->mNumMeshes; ++i)
        meshes->add(loadMesh(scene->mMeshes[i], scene->mMaterials[scene->mMeshes[i]->mMaterialIndex], i));

    initialize(scene);
    // try delete scene probably crash
}
void FBXModel::load(const GraphicsDevice& device, const std::string& filename, unsigned keyframes)
{
	if (effect.resource == 0)
		effect = Effect::createFromFile<FBXEffect>(device, Config::getValue(ConfigKeys::fbxEffectPath));

	defaultTexture = Texture::createFromFile(device, defaultTexturePath);
	
	vertexDeclaration = device.createVertexDeclaration(FBXInstance::vertexElements);

	KFbxSdkManager* sdkManager = KFbxSdkManager::Create();
	KFbxIOSettings* ios = KFbxIOSettings::Create(sdkManager, IOSROOT);
	sdkManager->SetIOSettings(ios);

	// Create an importer using our sdk manager.
	KFbxImporter* importer = KFbxImporter::Create(sdkManager, "");

	importer->Initialize(filename.c_str(), -1, sdkManager->GetIOSettings());

	// Create a new scene so it can be populated by the imported file.
	KFbxScene* scene = KFbxScene::Create(sdkManager, "");

	// Import the contents of the file into the scene.
	importer->Import(scene);

	KFbxNode* rootBone = 0;
	KFbxNode* rootNode = scene->GetRootNode();

	KFbxAnimStack* animStack = KFbxCast<KFbxAnimStack>(scene->GetSrcObject(FBX_TYPE(KFbxAnimStack), 0));
	KFbxAnimLayer* animLayer = 0;

	if (animStack)
	{
		animLayer = animStack->GetMember(FBX_TYPE(KFbxAnimLayer), 0);
		scene->GetEvaluator()->SetContext(animStack);
	}

	loadBones(rootNode, &rootBone, animLayer);
	loadMeshes(rootNode, device, KFbxGeometryConverter(sdkManager));

	if (animLayer)
	{
		for (unsigned i = 0; i <= keyframes; ++i)
			boneMatricesMap[i] = traverseBones(i, rootBone, Matrix::identity, MatrixCollection(bones.size()));
	}

	sdkManager->Destroy();

	loaded = true;
}
void FBXModel::loadBones(KFbxNode* node, KFbxNode** rootBone, KFbxAnimLayer* animLayer)
{
	const char* name = node->GetName();

	if (node->GetNodeAttribute())
	{
		KFbxNodeAttribute::EAttributeType attributeType = node->GetNodeAttribute()->GetAttributeType();

		if(attributeType == KFbxNodeAttribute::eSKELETON)
		{
			if (!animLayer)
				return;

			if (*rootBone == 0)
				*rootBone = node;

			Matrix globalTransform = convert(node->EvaluateGlobalTransform());

			FBXBone bone;

			bone.index = bones.size();

			bone.rotationCurves[0] = node->LclRotation.GetCurve<KFbxAnimCurve>(animLayer, KFCURVENODE_T_X);
			bone.rotationCurves[1] = node->LclRotation.GetCurve<KFbxAnimCurve>(animLayer, KFCURVENODE_T_Y);
			bone.rotationCurves[2] = node->LclRotation.GetCurve<KFbxAnimCurve>(animLayer, KFCURVENODE_T_Z);

			bone.translationCurves[0] = node->LclTranslation.GetCurve<KFbxAnimCurve>(animLayer, KFCURVENODE_T_X);
			bone.translationCurves[1] = node->LclTranslation.GetCurve<KFbxAnimCurve>(animLayer, KFCURVENODE_T_Y);
			bone.translationCurves[2] = node->LclTranslation.GetCurve<KFbxAnimCurve>(animLayer, KFCURVENODE_T_Z);

			fbxDouble3 translation = node->LclTranslation.Get();
			fbxDouble3 rotation = node->LclRotation.Get();

			bone.translation = Vector3((float)translation[0], (float)translation[1], (float)translation[2]);
			bone.rotation = Vector3((float)rotation[0], (float)rotation[1], (float)rotation[2]);

			bone.globalTransform = globalTransform;
			bone.globalTransformInverse = Matrix::invert(globalTransform);

			bones[node->GetName()] = bone;
		}
	}

	for(int i = 0; i < node->GetChildCount(); i++)
		loadBones(node->GetChild(i), rootBone, animLayer);
}
Exemplo n.º 4
0
void Mesh::initMesh(
                    unsigned int _meshIndex,
                    const aiMesh* _aiMesh,
                    std::vector<ngl::Vec3>& o_positions,
                    std::vector<ngl::Vec3>& o_normals,
                    std::vector<ngl::Vec2>& o_texCoords,
                    std::vector<VertexBoneData>& o_bones,
                    std::vector<unsigned int>& o_indices
                    )
{
  ngl::Vec3 Zero3D(0.0f, 0.0f, 0.0f);

  // Populate the vertex attribute vectors
  for (unsigned int i = 0 ; i < _aiMesh->mNumVertices ; ++i)
  {
  ngl::Vec3 pos  = AIU::aiVector3DToNGLVec3(_aiMesh->mVertices[i]);
  ngl::Vec3 normal = AIU::aiVector3DToNGLVec3(_aiMesh->mNormals[i]);
  ngl::Vec3 tex = _aiMesh->HasTextureCoords(0) ? AIU::aiVector3DToNGLVec3(_aiMesh->mTextureCoords[0][i]) : Zero3D;

  o_positions.push_back(pos);
  o_normals.push_back(normal);
  o_texCoords.push_back(ngl::Vec2(tex.m_x,tex.m_y) );
  }

  loadBones(_meshIndex, _aiMesh, o_bones);

  // Populate the index buffer
  for (unsigned int i = 0 ; i < _aiMesh->mNumFaces ; ++i)
  {
    const aiFace& Face = _aiMesh->mFaces[i];
    o_indices.push_back(Face.mIndices[0]);
    o_indices.push_back(Face.mIndices[1]);
    o_indices.push_back(Face.mIndices[2]);
  }

}
Exemplo n.º 5
0
AAction::AAction(Player &player, int id, Hit *hit) :
	_player(player), _hit(hit), _open(1), _count(0), _bones(player.bones()), _id(id)
{
  loadBones();
}
Exemplo n.º 6
0
bool SpriteMeshLoader::loadSpriteMesh( string filename, string meshID ){


    //check if meshID already exists!
    if (renderer->vboList[meshID]){
        renderer->vboList.erase(meshID);
        }

    // XML File Open

    cout << "Loading file..." << filename <<endl;

    TiXmlDocument doc( filename );
    if (!doc.LoadFile()) return false;

    TiXmlHandle hDoc(&doc);
    TiXmlElement * element;
    TiXmlHandle hRoot(0);

    //***********************************************************************
    //Skip over first Element
    //***********************************************************************
    element=hDoc.FirstChildElement().Element();
    // should always have a valid root but handle gracefully if it doesn't
    if (!element) return false;

    // save this for later
    hRoot=TiXmlHandle(element);
    //end XML file open;

    //setup new MeshData
    MeshData* myMesh=new MeshData;
    myMesh->bIsSkeletal=false;
    myMesh->bIsHead=false;
    myMesh->bVertexColor=true;
    myMesh->boneCount=0;
    myMesh->texCoordPerVertexCount=3;
    myMesh->verticesPerShapeCount=4;
    myMesh->vertexInterpretation=GL_POINTS;
    myMesh->drawType=DRAW_VBOMESH;
    renderer->vboList[meshID]=myMesh;


    loadVertices(meshID, hRoot.FirstChild("vertices").Element());
    loadNormals(meshID, hRoot.FirstChild("normals").Element());
    loadTexCoords(meshID, hRoot.FirstChild("texCoords").Element());

    loadColors(meshID, hRoot.FirstChild("colors").Element());
    loadSecondaryColors(meshID, hRoot.FirstChild("secondaryColors").Element());

    loadBoneReferences(meshID, hRoot.FirstChild("boneReferences").Element());
    loadVertexWeights(meshID, hRoot.FirstChild("vertexWeights").Element());

    cout << "loading bones..." << endl;

    loadBones(meshID, hRoot.FirstChild("bone").Element());


	//fill vertex data for editing
	if (!renderer->vboList[meshID]->vData.empty())
		renderer->vboList[meshID]->vData.clear();

	for (int i=0;i<vertexCount;i++){
        vertexData myVData;
        myVData.location=vertices[i];
        myVData.normal=normals[i];
        myVData.color=colors[i];
        myVData.secondaryColor=secondaryColors[i];
        myVData.birth=0.0f;
        myVData.texCoord=texCoords[i];
        myVData.vertexWeights=vertexWeights[i];
        myVData.boneReferences=boneReference[i];
        renderer->vboList[meshID]->vData.push_back(myVData);
    }

    cout << "before creating vbos..." << endl;


    createVBOs(meshID);

    cout << "after creating vbos..." << endl;


   //now free our resources:
	delete(vertices);
	delete(normals);
	delete(texCoords);
	delete(colors);
	delete(secondaryColors);
	delete(vertexWeights);
	delete(boneReference);

    doc.Clear();

    cout << "finished loading SpriteMesh" << endl;

    return true;
}