Example #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;
}