Пример #1
0
bool core::ModelLoader::loadModel(const char *fp, Model *m){
	Assimp::Importer importer;

	const aiScene *scene = importer.ReadFile(fp, aiProcess_Triangulate | aiProcess_OptimizeMeshes | aiProcess_JoinIdenticalVertices | aiProcess_FlipUVs);

	if(!scene)
		return false;

	m->rootPath = (std::string)fp;
	for(int x = m->rootPath.size() - 1; x >= 0; x--){
		if(m->rootPath[x] == '/' || m->rootPath[x] == '\\'){
			m->rootPath = m->rootPath.substr(0, x + 1);
			x = -1;
		}
	}

	processAnimations(scene, m);
	processNode(scene, scene->mRootNode, m);

	if(scene->HasAnimations())
		m->animations[m->currentAnim].buildBoneTree(scene, scene->mRootNode, &m->animations[m->currentAnim].root, m);

	m->modelTrans = glm::mat4(1.f);
	m->modelLoaded = true;

	return true;
};
Пример #2
0
static int processBonesAndAnimations(glhckObject *object, const struct aiScene *sc)
{
    unsigned int i, b,  numBones = 0, oldNumBones;
    const struct aiMesh *mesh;
    glhckObject *child;
    glhckBone **bones = NULL;
    glhckSkinBone **skinBones = NULL;

    /* import bones */
    if (!(bones = _glhckCalloc(ASSIMP_BONES_MAX, sizeof(glhckBone*))))
        goto fail;
    if (!(skinBones = _glhckCalloc(ASSIMP_BONES_MAX, sizeof(glhckSkinBone*))))
        goto fail;

    for (i = 0; i != sc->mNumMeshes; ++i) {
        mesh = sc->mMeshes[i];

        /* FIXME: UGLY */
        char pointer[16];
        snprintf(pointer, sizeof(pointer), "%p", mesh);
        if (!(child = findObject(object, pointer))) continue;

        if (mesh->mNumBones)  {
            oldNumBones = numBones;
            processBones(sc->mRootNode, sc->mRootNode, mesh, bones, skinBones, &numBones);
            if (numBones) glhckObjectInsertSkinBones(child, skinBones+oldNumBones, numBones-oldNumBones);
            for (b = oldNumBones; b < numBones; ++b) glhckSkinBoneFree(skinBones[b]);
        }
    }

    /* we don't need skin bones anymore */
    NULLDO(_glhckFree, skinBones);

    /* store all bones in root object */
    if (numBones) glhckObjectInsertBones(object, bones, numBones);
    for (b = 0; b < numBones; ++b) glhckBoneFree(bones[b]);
    NULLDO(_glhckFree, bones);

    /* import animations */
    if (sc->mNumAnimations && processAnimations(object, sc) != RETURN_OK)
        goto fail;

    return RETURN_OK;

fail:
    if (bones) {
        for (i = 0; i < ASSIMP_BONES_MAX; ++i)
            if (bones[i]) glhckBoneFree(bones[i]);
        _glhckFree(bones);
    }
    if (skinBones) {
        for (i = 0; i < ASSIMP_BONES_MAX; ++i)
            if (skinBones[i]) glhckSkinBoneFree(skinBones[i]);
        _glhckFree(skinBones);
    }
    return RETURN_FAIL;
}
Пример #3
0
void YafFile::loadFile(char* filename)
{
	TiXmlDocument* doc=new TiXmlDocument( filename );
	bool loadOkay = doc->LoadFile();
	if ( !loadOkay )
	{
		printf( "Could not load file '%s'. Error='%s'. Exiting.\n", filename, doc->ErrorDesc() );
		exit( 1 );
	}

	TiXmlElement* yafElement= doc->FirstChildElement( "yaf" );

	if (yafElement == NULL)
	{
		printf("Main yaf block element not found! Exiting!\n");
		exit(1);
	}
	TiXmlElement* globalsElement = yafElement->FirstChildElement( "globals" );
	processGlobals(globalsElement);
	TiXmlElement* lightingElement = yafElement->FirstChildElement("lighting");
	processGlobalLight(lightingElement);
	processLights(lightingElement);
	TiXmlElement* camerasElement = yafElement->FirstChildElement("cameras");
	processCameras(camerasElement);
	TiXmlElement* texturesElement = yafElement->FirstChildElement("textures");
	processTextures(texturesElement);

	TiXmlElement* appearanceElement = yafElement->FirstChildElement("appearances");
	processAppearances(appearanceElement);

	TiXmlElement* animationsElement = yafElement->FirstChildElement("animations");
	if(animationsElement!=NULL)
	{
		processAnimations(animationsElement);
		map<string,Animation*>::iterator it = animations.begin();
		for(;it!=animations.end();it++)
		{
			(*it->second).print();
		}
	}

	TiXmlElement* graphElement = yafElement->FirstChildElement("graph");
	processGraph(graphElement);
}
Пример #4
0
void FBXConverter::convert( const char* input , const char* output )
{
	if ( converting ) return;
	converting = true;
	FbxManager* fbxManger = FbxManager::Create();

	FbxIOSettings* ios = FbxIOSettings::Create( fbxManger , IOSROOT );
	fbxManger->SetIOSettings( ios );

	FbxImporter* importer = FbxImporter::Create( fbxManger , "" );

	bool status = importer->Initialize( input , -1 , fbxManger->GetIOSettings() );
	if ( !status )
	{
		std::cout << importer->GetStatus().GetErrorString() << std::endl;
	}

	FbxScene* scene = FbxScene::Create( fbxManger , "theScene" );
	importer->Import( scene );
	importer->Destroy();

	FbxMesh* theMesh = findMesh( scene->GetRootNode() );
	

	if ( theMesh )
	{
		std::vector<VertexData> vertices;
		std::vector<IndexData> indices;

		FbxStringList uvSets;
		theMesh->GetUVSetNames( uvSets );

		processPolygons( theMesh , vertices , indices , uvSets );

		std::vector<JointData> skeleton;
		processSkeletonHierarchy( scene->GetRootNode() , skeleton );
		if ( skeleton.size() ) processAnimations( theMesh->GetNode() , skeleton , vertices , indices );

		std::string modelData;
		for ( unsigned int i = 0; i < vertices.size(); ++i )
		{
			modelData += DATASTRING( vertices[i].position );
			modelData += DATASTRING( vertices[i].uv );
			modelData += DATASTRING( vertices[i].normal );
			modelData += DATASTRING( vertices[i].tangent );
			modelData += DATASTRING( vertices[i].bitangent );
			
			for ( unsigned int j = 0; j < 4 ; ++j )
			{
				if ( j < vertices[i].blendingInfo.size() )
				{
					int blendingIndex = vertices[i].blendingInfo[j].blendingIndex;
					modelData += DATASTRING( blendingIndex );
				}
				else
				{
					int blendingIndex = -1;
					modelData += DATASTRING( blendingIndex );
				}
			}
			for ( unsigned int j = 0; j < 4; ++j )
			{
				if ( j < vertices[i].blendingInfo.size() )
				{
					float blendingIndex = vertices[i].blendingInfo[j].blendingWeight;
					modelData += DATASTRING( blendingIndex );
				}
				else
				{
					float blendingIndex = -1;
					modelData += DATASTRING( blendingIndex );
				}
			}
		}

		for ( unsigned int i = 0; i < indices.size(); ++i)
		{
			modelData += DATASTRING( indices[i].index );
		}

		std::string boneData;
		std::vector<unsigned int> boneChildren;
		std::vector<AnimationData> boneAnimation;
		for ( unsigned int i = 0; i < skeleton.size(); ++i )
		{
			boneData += DATASTRING( skeleton[i].offsetMatrix );
			int childDataStart , childDataEnd , animationDataStart , animationDataEnd;
			if ( skeleton[i].children.size() )
			{
				childDataStart = boneChildren.size();
				for ( unsigned int j = 0; j < skeleton[i].children.size(); ++j )
				{
					boneChildren.push_back( skeleton[i].children[j] );
				}
				childDataEnd = boneChildren.size();
			}
			else
			{
				childDataStart = -1;
				childDataEnd = -1;
			}

			if ( skeleton[i].animation.size() )
			{
				animationDataStart = boneAnimation.size();
				for ( unsigned int j = 0; j < skeleton[i].animation.size(); ++j )
				{
					boneAnimation.push_back( skeleton[i].animation[j] );
				}
				animationDataEnd = boneAnimation.size();
			}
			else
			{
				animationDataStart = -1;
				animationDataEnd = -1;
			}
			boneData += DATASTRING( childDataStart );
			boneData += DATASTRING( childDataEnd );
			boneData += DATASTRING( animationDataStart );
			boneData += DATASTRING( animationDataEnd );
		}
		unsigned int sizeofAnimationRangeInfo;
		AnimationFrameRangeInfo frameRange;
		if ( boneAnimation.size() > 0 )
		{
			sizeofAnimationRangeInfo = 1;
			frameRange.nextAnimationFrameInfo = 0;
			frameRange.firstFrame = 1;
			frameRange.lastFrame = boneAnimation[boneAnimation.size() - 1].frame;
		}
		else
		{
			sizeofAnimationRangeInfo = 0;
		}

		std::fstream stream( output , std::ios_base::binary | std::ios_base::out | std::ios_base::trunc );

		unsigned int sizeofVertices = vertices.size();
		stream.write( reinterpret_cast< char* >( &sizeofVertices ) , sizeof( sizeofVertices ));
		unsigned int sizeofIndices = indices.size();
		stream.write( reinterpret_cast< char* >( &sizeofIndices ) , sizeof( sizeofIndices ) );

		unsigned int sizeofBoneData = skeleton.size();
		stream.write( reinterpret_cast<char*>( &sizeofBoneData ) , sizeof( sizeofBoneData ) );

		unsigned int sizeofBoneChildData = boneChildren.size();
		stream.write( reinterpret_cast< char* >( &sizeofBoneChildData ) , sizeof( sizeofBoneChildData ));

		unsigned int sizeofBoneAnimationData = boneAnimation.size();
		stream.write( reinterpret_cast< char* >( &sizeofBoneAnimationData ) , sizeof( sizeofBoneAnimationData ) );

		stream.write( reinterpret_cast< char* >( &sizeofAnimationRangeInfo ) , sizeof( sizeofAnimationRangeInfo ) );

		stream.write( modelData.c_str() , modelData.size() );

		stream.write( boneData.c_str() , boneData.size() );

		for ( unsigned int i = 0; i < boneChildren.size(); ++i )
		{
			stream.write( reinterpret_cast< char* >( &boneChildren[i] ) , sizeof( boneChildren[i] ) );
		}

		for ( unsigned int i = 0; i < boneAnimation.size(); ++i )
		{
			stream.write( reinterpret_cast< char* >( &boneAnimation[i] ) , sizeof( boneAnimation[i] ) );
		}

		
		if(sizeofAnimationRangeInfo) stream.write( reinterpret_cast< char* >( &frameRange ) , sizeof( frameRange ) );
		stream.close();
	}
	converting = false;
}