Beispiel #1
0
/*********************************************************************************
  *Function: OnReadOnePart(FILE* fp) 
  *Description: 导入网格模型中的一个part,并挂载到场景结构中
  *Input:  fp-文件指针
  *Return: 成功true 否则false 
              *CREATED BY:  [8/17/2015 niewenchao]
**********************************************************************************/
bool VImport::OnReadOnePart(FILE* fp)
{
	char buf[512];
	fscanf(fp,"#Part\n");

	fscanf(fp,"n %s\n",buf);									//	mesh的名称
	VMesh* m_mesh = new VMesh();
	VNode* node = m_pScence->CreateNode(m_mesh);
	node->SetNodeName(buf);
	float mat[12];
	fscanf(fp,"m %f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f,%f\n",			//读取对象矩阵
		&mat[0],&mat[1],&mat[2],&mat[3],&mat[4],&mat[5],&mat[6],
		&mat[7],&mat[8],&mat[9],&mat[10],&mat[11]);
	VMatrix3* pmat = new VMatrix3;
	pmat->SetValue(mat);
	node->GetTMController()->SetTM(pmat);
	// 问题,忽略最后一个字符行
	fscanf(fp,"t %s\n",buf);
	

	bool nExist=false;
	bool tExist=false;
	while(fscanf(fp,"%s",buf) != EOF && strcmp(buf,"#PartEnd") != 0) {
		switch(buf[0]){
		case 'v':
			fseek(fp,-1L,SEEK_CUR);
			OnImportVertex(fp,m_mesh);	//导入顶点数据	
			break;
		case 'n':
			nExist = true;
			fseek(fp,-1L,SEEK_CUR);
			OnImportNormal(fp,m_mesh);	//导入法线数据
			break;
		case 't':
			tExist = true;
			fseek(fp,-1L,SEEK_CUR);
			OnImportTVert(fp,m_mesh);		//导入纹理顶点数据
			break;
		case 'f':
			fseek(fp,-1L,SEEK_CUR);
			OnImportFace(fp,m_mesh,nExist,tExist);		//导入面片数据
			break;
		}
	}
	fscanf(fp,"\n");
	m_mesh->ComputeBoundBox();
	return true;
}
Beispiel #2
0
/*********************************************************************************
  *Function:DoImport(string fileName)  
  *Description:导入VCC模型文件,目前支持仅含有网格数据的VCC模型,材质、光照、骨骼动作数据待加入 
  *Input:  fileName-模型文件路径
  *Return: 导入成功返回true 否则false 
              *CREATED BY:  [8/17/2015 niewenchao]
**********************************************************************************/
bool VImport::DoImport(string fileName)
{
	string ext = fileName.substr(fileName.rfind('.')+1);
	if (ext == "VCC" || ext  == "vcc")
	{
		FILE* fp = fopen(fileName.c_str(), "r+");
		if (NULL == fp) return false;
		char buf[512];
		//	#Head------------->#HeadEnd
		bool bone_exist = false;
		bool skin_exist = false;
		bool mesh_exist = false;
		bool mtl_exist =false;
		while(fscanf(fp,"%[^\n]\n",buf))
		{
			if(0 == strcmp(buf, "#HeadEnd"))
			{
				break;
			}
			switch(buf[0])
			{
			case 'b':bone_exist = true;break;
			case 's':skin_exist = true;break;
			case 'm':mesh_exist = true;break;
			case 't':mtl_exist = true;break;
			}
			
	    }
		// #Mesh----------------->#MeshEnd
		if(mesh_exist) 
			return  OnImportMesh(fp);
	}
	else
	{
		Assimp::Importer Importer; 
		const aiScene* pScene = Importer.ReadFile(fileName,aiProcess_JoinIdenticalVertices); 
		if (pScene)   
		{ 
			for (int k=0;k<pScene->mNumMeshes;k++)
			{

				const aiMesh* paiMesh = pScene->mMeshes[k];
				VMesh* mesh = new VMesh;						//新建一个三角风格的对象
				VNode* node = m_pScence->CreateNode(mesh);				//创建一个节点并挂载到场景跟节点中
				VMatrix3* pmat = new VMatrix3;
				node->GetTMController()->SetTM(pmat);
				int vNum = paiMesh->mNumVertices;
				mesh->SetNumVerts(vNum);	
				for (int i=0;i<vNum;i++)
				{
					mesh->SetVert(i,paiMesh->mVertices[i].x,paiMesh->mVertices[i].y,paiMesh->mVertices[i].z);									//设置顶点坐标
				}
				int fNum = paiMesh->mNumFaces;
				mesh->SetNumFaces(fNum);										//设置网格面片数目
				for (int i=0;i<fNum;i++)
				{
					mesh->SetFace(i, (paiMesh->mFaces[i].mIndices[0]), (paiMesh->mFaces[i].mIndices[1]), (paiMesh->mFaces[i].mIndices[2]));
				}
				mesh->ComputeBoundBox();
			}

		}
		else 
		{ 
			printf("Error parsing '%s': '%s'\n", fileName.c_str(), Importer.GetErrorString()); 
			return false;
		} 
		return true;
	}
}