Exemplo n.º 1
0
bool CEditableObject::Import_LWO(const char* fn, bool bNeedOptimize)
{
	lwObject *I=0;
//	UI->SetStatus("Importing...");
//	UI->ProgressStart(100,"Read file:");
//	UI->ProgressUpdate(1);
    string512 fname;
    strcpy(fname,fn);
#ifdef _EDITOR
	I=LWO_ImportObject(fname,I);
#else
	unsigned int failID;
	int failpos;
	I = lwGetObject( fname, &failID, &failpos );
#endif
//	UI->ProgressUpdate(100);
	if (I){
        bool bResult=true;
        ELog.Msg( mtInformation, "CEditableObject: import lwo %s...", fname );

        // parse lwo object
        {
        	m_Meshes.reserve	(I->nlayers);
            m_Surfaces.reserve	(I->nsurfs);

            // surfaces
            st_lwSurface* Isf=0;
            {
                int i=0;
//                UI->ProgressStart(I->nsurfs,"Check surf:");
                for (Isf=I->surf; Isf; Isf=Isf->next){
//                    UI->ProgressUpdate(i);
                    Isf->alpha_mode=i; // перетираем для внутренних целей !!!
                    CSurface* Osf = new CSurface();
                    m_Surfaces.push_back(Osf);
                    if (Isf->name&&Isf->name[0]) Osf->SetName(Isf->name); else Osf->SetName("Default");
                    Osf->m_Flags.set(CSurface::sf2Sided,(Isf->sideflags==3)?TRUE:FALSE);
                    AnsiString en_name="default", lc_name="default", gm_name="default";
                    XRShader* sh_info = 0;
                    if (Isf->nshaders&&(stricmp(Isf->shader->name,SH_PLUGIN_NAME)==0)){
                    	sh_info 	= (XRShader*)Isf->shader->data;
                        en_name 	= sh_info->en_name;
                        lc_name 	= sh_info->lc_name;
                        gm_name		= sh_info->gm_name;
                    }else
						ELog.Msg(mtError,"CEditableObject: Shader not found on surface '%s'.",Osf->_Name());
#ifdef _EDITOR
					if (!Device.Resources->_FindBlender(en_name.c_str())){
						ELog.Msg(mtError,"CEditableObject: Render shader '%s' - can't find in library.\nUsing 'default' shader on surface '%s'.", en_name.c_str(), Osf->_Name());
	                    en_name = "default";
					}
					if (!Device.ShaderXRLC.Get(lc_name.c_str())){
						ELog.Msg(mtError,"CEditableObject: Compiler shader '%s' - can't find in library.\nUsing 'default' shader on surface '%s'.", lc_name.c_str(), Osf->_Name());
	                    lc_name = "default";
					}
					if (!GMLib.GetMaterial(gm_name.c_str())){
						ELog.Msg(mtError,"CEditableObject: Game material '%s' - can't find in library.\nUsing 'default' material on surface '%s'.", lc_name.c_str(), Osf->_Name());
	                    gm_name = "default";
					}
#endif
                    // fill texture layers
                    int cidx;
                    st_lwClip* Icl;
                    u32 dwNumTextures=0;
                    for (st_lwTexture* Itx=Isf->color.tex; Itx; Itx=Itx->next){
                        string1024 tname="";
                        dwNumTextures++;
                        cidx = -1;
                        if (Itx->type==ID_IMAP) cidx=Itx->param.imap.cindex;
                        else{
                            ELog.DlgMsg(mtError, "Import LWO (Surface '%s'): 'Texture' is not Image Map!",Osf->_Name());
                            bResult=false;
                            break;
                        }
                        if (cidx!=-1){
                            // get textures
                            for (Icl=I->clip; Icl; Icl=Icl->next)
                                if ((cidx==Icl->index)&&(Icl->type==ID_STIL)){
                                    strcpy(tname,Icl->source.still.name);
                                    break;
                                }
                            if (tname[0]==0){
                                ELog.DlgMsg(mtError, "Import LWO (Surface '%s'): 'Texture' name is empty or non 'STIL' type!",Osf->_Name());
                                bResult=false;
                                break;
                            }
                            string256 tex_name;
                            _splitpath( tname, 0, 0, tex_name, 0 );
							Osf->SetTexture(EFS.AppendFolderToName(tex_name,1,TRUE));
                            // get vmap refs
                            Osf->SetVMap(Itx->param.imap.vmap_name);
                        }
                    }
                    if (!bResult) break;
                    if (!Osf->_VMap()||!Osf->_VMap()[0]){
						ELog.DlgMsg(mtError, "Invalid surface '%s'. VMap empty.",Osf->_Name());
                        bResult = false;
						break;
                    }
                    if (!Osf->_Texture()||!Osf->_Texture()[0]){
						ELog.DlgMsg(mtError, "Can't create shader. Invalid surface '%s'. Textures empty.",Osf->_Name());
                        bResult = false;
						break;
                    }
                    if (en_name.c_str()==0){
						ELog.DlgMsg(mtError, "Can't create shader. Invalid surface '%s'. Shader empty.",Osf->_Name());
                        bResult = false;
						break;
                    }

                    Osf->SetShader		(en_name.c_str());
					Osf->SetShaderXRLC	(lc_name.c_str());
                    Osf->SetGameMtl		(gm_name.c_str());
                    Osf->SetFVF			(D3DFVF_XYZ|D3DFVF_NORMAL|(dwNumTextures<<D3DFVF_TEXCOUNT_SHIFT));
                    i++;
                }
		    }
			if (bResult){
                // mesh layers
            	st_lwLayer* Ilr=0;
	            int k=0;
 	           	for (Ilr=I->layer; Ilr; Ilr=Ilr->next){
                    // create new mesh
                    CEditableMesh* MESH= new CEditableMesh(this);
                    m_Meshes.push_back(MESH);

                    if (Ilr->name)	MESH->SetName(Ilr->name); else MESH->SetName("");
                    MESH->m_Box.set(Ilr->bbox[0],Ilr->bbox[1],Ilr->bbox[2], Ilr->bbox[3],Ilr->bbox[4],Ilr->bbox[5]);

                    // parse mesh(lwo-layer) data
                    // vmaps
                    st_lwVMap* Ivmap=0;
                    int vmap_count=0;
                    if (Ilr->nvmaps==0){
                        ELog.DlgMsg(mtError, "Import LWO: Mesh layer must contain UV!");
                        bResult=false;
                        break;
                    }

                    // индексы соответствия импортируемых мап
					static VMIndexLink VMIndices;
				    VMIndices.clear();

                    for (Ivmap=Ilr->vmap; Ivmap; Ivmap=Ivmap->next){
                    	switch(Ivmap->type){
                        case ID_TXUV:{
                            if (Ivmap->dim!=2){
                                ELog.DlgMsg(mtError, "Import LWO: 'UV Map' must contain 2 value!");
                                bResult=false;
                                break;
                            }
                            MESH->m_VMaps.push_back(new st_VMap(Ivmap->name,vmtUV,!!Ivmap->perpoly));
                            st_VMap* Mvmap=MESH->m_VMaps.back();
                            int vcnt=Ivmap->nverts;
                            // VMap
                            Mvmap->copyfrom(*Ivmap->val,vcnt);

                            // flip uv
                            for (int k=0; k<Mvmap->size(); k++){
                            	Fvector2& uv = Mvmap->getUV(k);
                                uv.y=1.f-uv.y;
                            }
                            // vmap index
                            VMIndices[Ivmap] = vmap_count++;
                        }break;
						case ID_WGHT:{
                            if (Ivmap->dim!=1){
                                ELog.DlgMsg(mtError, "Import LWO: 'Weight' must contain 1 value!");
                                bResult=false;
                                break;
                            }
                            MESH->m_VMaps.push_back(new st_VMap(Ivmap->name,vmtWeight,false));
                            st_VMap* Mvmap=MESH->m_VMaps.back();
                            int vcnt=Ivmap->nverts;
                            // VMap
                            Mvmap->copyfrom(*Ivmap->val,vcnt);
                            // vmap index
                            VMIndices[Ivmap] = vmap_count++;
                        }break;
						case ID_PICK: ELog.Msg(mtError,"Found 'PICK' VMAP. Import failed."); bResult = false; break;
						case ID_MNVW: ELog.Msg(mtError,"Found 'MNVW' VMAP. Import failed."); bResult = false; break;
						case ID_MORF: ELog.Msg(mtError,"Found 'MORF' VMAP. Import failed."); bResult = false; break;
						case ID_SPOT: ELog.Msg(mtError,"Found 'SPOT' VMAP. Import failed."); bResult = false; break;
						case ID_RGB:  ELog.Msg(mtError,"Found 'RGB' VMAP. Import failed.");  bResult = false; break;
						case ID_RGBA: ELog.Msg(mtError,"Found 'RGBA' VMAP. Import failed."); bResult = false; break;
                        }
	                    if (!bResult) break;
                    }
                    if (!bResult) break;
                    // points
//					UI->ProgressStart(Ilr->point.count,"Fill points:");
                    {
                    	MESH->m_VertCount		= Ilr->point.count;
                        MESH->m_Vertices 		= xr_alloc<Fvector>(MESH->m_VertCount);
	                    int id 					= Ilr->polygon.count/50;

	                    if (id==0)
                        	id = 1;

                        for (int i=0; i<Ilr->point.count; ++i)
                        {
                            st_lwPoint& Ipt = Ilr->point.pt[i];
                            Fvector& Mpt	= MESH->m_Vertices[i];
                            Mpt.set			(Ipt.pos);
                        }
                    }
                    if (!bResult) break;
                    // polygons
					MESH->m_FaceCount		= Ilr->polygon.count;
                    MESH->m_Faces			= xr_alloc<st_Face>(MESH->m_FaceCount);
					MESH->m_SmoothGroups	= xr_alloc<u32>(MESH->m_FaceCount);
				    Memory.mem_fill32		(MESH->m_SmoothGroups,u32(-1),MESH->m_FaceCount);
                    MESH->m_VMRefs.reserve	(Ilr->polygon.count*3);
                    IntVec surf_ids;
                    surf_ids.resize(Ilr->polygon.count);
                    int id = Ilr->polygon.count/50;

                    if (id==0)
                    	id = 1;
                    for (int i=0; i<Ilr->polygon.count; ++i)
                    {
                        st_Face&		Mpol=MESH->m_Faces[i];
                        st_lwPolygon&   Ipol=Ilr->polygon.pol[i];
                        if (Ipol.nverts!=3) {
							ELog.DlgMsg(mtError, "Import LWO: Face must contain only 3 vertices!");
                        	bResult=false;
                            break;
                        }
                        for (int pv_i=0; pv_i<3; ++pv_i)
                        {
                        	st_lwPolVert& 	Ipv=Ipol.v[pv_i];
                            st_FaceVert&  	Mpv=Mpol.pv[pv_i];
                            Mpv.pindex		=Ipv.index;

							MESH->m_VMRefs.push_back(st_VMapPtLst());
							st_VMapPtLst&	m_vm_lst = MESH->m_VMRefs.back();

                            DEFINE_VECTOR	(st_VMapPt,VMapPtVec,VMapPtIt);
                            VMapPtVec		vm_lst;

							Mpv.vmref 		= MESH->m_VMRefs.size()-1;

							// parse uv-map
							int vmpl_cnt		=Ipv.nvmaps;
							st_lwPoint& Ipt 	=Ilr->point.pt[Mpv.pindex];
                            int vmpt_cnt		=Ipt.nvmaps;
                            if (!vmpl_cnt&&!vmpt_cnt){
                                ELog.DlgMsg	(mtError,"Found mesh without UV's!",0);
                                bResult		= false;
                                break;
                            }
                            AStringVec names;
							if (vmpl_cnt){
                            	// берем из poly
    							for (int vm_i=0; vm_i<vmpl_cnt; vm_i++){
									if (Ipv.vm[vm_i].vmap->type!=ID_TXUV) continue;
									vm_lst.push_back(st_VMapPt());
									st_VMapPt& pt	= vm_lst.back();
        							pt.vmap_index	= VMIndices[Ipv.vm[vm_i].vmap];// номер моей VMap
                                    names.push_back	(Ipv.vm[vm_i].vmap->name);
            						pt.index 		= Ipv.vm[vm_i].index;
                				}
							}
                            if (vmpt_cnt){
                            	// берем из points
                                for (int vm_i=0; vm_i<vmpt_cnt; vm_i++){
									if (Ipt.vm[vm_i].vmap->type!=ID_TXUV) continue;
                                    if (std::find(names.begin(),names.end(),Ipt.vm[vm_i].vmap->name)!=names.end()) continue;
									vm_lst.push_back(st_VMapPt());
									st_VMapPt& pt	= vm_lst.back();
									pt.vmap_index	= VMIndices[Ipt.vm[vm_i].vmap]; // номер моей VMap
									pt.index 		= Ipt.vm[vm_i].index;
                                }
							}

                            std::sort(vm_lst.begin(),vm_lst.end(),CompareFunc);

							// parse weight-map
                            int vm_cnt		=Ipt.nvmaps;
                            for (int vm_i=0; vm_i<vm_cnt; vm_i++){
								if (Ipt.vm[vm_i].vmap->type!=ID_WGHT) continue;
								vm_lst.push_back(st_VMapPt());
								st_VMapPt& pt	= vm_lst.back();
        	                    pt.vmap_index	= VMIndices[Ipt.vm[vm_i].vmap]; // номер моей VMap
            	                pt.index 		= Ipt.vm[vm_i].index;
                            }
                            m_vm_lst.count		= vm_lst.size();
                            m_vm_lst.pts		= xr_alloc<st_VMapPt>(m_vm_lst.count);
                            Memory.mem_copy		(m_vm_lst.pts,&*vm_lst.begin(),m_vm_lst.count*sizeof(st_VMapPt));
                        }
                        if (!bResult) break;
						// Ipol.surf->alpha_mode - заполнено как номер моего surface
                        surf_ids[i]			= Ipol.surf->alpha_mode;
                    }
                    if (!bResult) break;
                    for (u32 pl_id=0; pl_id<MESH->GetFCount(); pl_id++)
						MESH->m_SurfFaces[m_Surfaces[surf_ids[pl_id]]].push_back(pl_id);
                        
                    if (!bResult) break;
                    k++;
                    //MESH->DumpAdjacency();
                    if (bNeedOptimize) MESH->OptimizeMesh(false);
                    //MESH->DumpAdjacency();
                    MESH->RebuildVMaps(); // !!!!!!
                }
    		}
        }
#ifdef _EDITOR
		LWO_CloseFile(I);
#else
		lwFreeObject(I);
#endif
//		UI->ProgressEnd();
//	    UI->SetStatus("");
    	if (bResult) 	VerifyMeshNames();
        else			ELog.DlgMsg(mtError,"Can't parse LWO object.");
		if (bResult)	m_LoadName = (strext(fname))? strcpy(strext(fname),".object"):strcat(strext(fname),".object");
        return bResult;
    }else
		ELog.DlgMsg(mtError,"Can't import LWO object file.");
//	UI->ProgressEnd();
//	UI->SetStatus("");
    return false;
}
Exemplo n.º 2
0
bool CEditableMesh::Convert( INode *node )
{
	// prepares & checks
	BOOL bDeleteObj;
	bool bResult = true;
	TriObject *obj = ExtractTriObject( node, bDeleteObj );

	if( !obj ){
		ELog.Msg(mtError,"%s -> Can't convert to TriObject", node->GetName() );
		return false; }

	if( obj->mesh.getNumFaces() <=0 ){
		ELog.Msg(mtError,"%s -> There are no faces ?", node->GetName() );
		if (bDeleteObj) delete (obj);
		return false; }

	Mtl *pMtlMain = node->GetMtl();
	DWORD cSubMaterials=0;

	if (pMtlMain){
		// There is at least one material. We're in case 1) or 2)
		cSubMaterials = pMtlMain->NumSubMtls();
		if (cSubMaterials < 1){
			// Count the material itself as a submaterial.
			cSubMaterials = 1;
		}
	}

	// build normals
	obj->mesh.buildRenderNormals();

	// vertices
	m_VertCount = obj->mesh.getNumVerts();
	m_Vertices = xr_alloc<Fvector>(m_VertCount);
	for (int v_i=0; v_i<m_VertCount; v_i++){
		Point3* p = obj->mesh.verts+v_i;
		m_Vertices[v_i].set(p->x,p->y,p->z);
	}

	// set smooth group MAX type
	m_Flags.set(flSGMask,TRUE);

	// faces
	m_FaceCount		= obj->mesh.getNumFaces();
	m_Faces			= xr_alloc<st_Face>	(m_FaceCount);
	m_SmoothGroups	= xr_alloc<u32>		(m_FaceCount);

	m_VMRefs.reserve(m_FaceCount*3);
	if (0==obj->mesh.mapFaces(1))
	{
		bResult = false;
		ELog.Msg(mtError,"'%s' hasn't UV mapping!", node->GetName());
	}
	if (bResult)
	{
		CSurface* surf=0;
		for (int f_i=0; f_i<m_FaceCount; ++f_i)
		{
			Face*	vf = obj->mesh.faces+f_i;
			TVFace* tf = obj->mesh.mapFaces(1) + f_i;
			if (!tf)
			{
				bResult = false;
				ELog.Msg(mtError,"'%s' hasn't UV mapping!", node->GetName());
				break;
			}
			m_SmoothGroups[f_i]					= vf->getSmGroup();
			for (int k=0; k<3; ++k)
			{
				m_Faces[f_i].pv[k].pindex = vf->v[k];
				m_VMRefs.push_back(st_VMapPtLst());
				st_VMapPtLst&	vm_lst = m_VMRefs.back();
				vm_lst.count	= 1;
				vm_lst.pts		= xr_alloc<st_VMapPt>(vm_lst.count);
				for (DWORD vm_i=0; vm_i<vm_lst.count; ++vm_i)
				{
					vm_lst.pts[vm_i].vmap_index	= 0;
					vm_lst.pts[vm_i].index 		= tf->t[k];
				}
				m_Faces[f_i].pv[k].vmref	= m_VMRefs.size()-1;
				if (!bResult) break;
			}
			if (pMtlMain)
			{
				int m_id = obj->mesh.getFaceMtlIndex(f_i);
				if (cSubMaterials == 1)
				{
					m_id = 0;
				}else
				{
					// SDK recommends mod'ing the material ID by the valid # of materials, 
					// as sometimes a material number that's too high is returned.
					m_id %= cSubMaterials;
				}
				surf = m_Parent->CreateSurface(pMtlMain,m_id);
				if (!surf) bResult = false;
			}
			if (!bResult) break;
			m_SurfFaces[surf].push_back(f_i);
		}
	}

	// vmaps
	if( bResult ){
		int vm_cnt = obj->mesh.getNumTVerts();
		m_VMaps.resize(1);
		st_VMap*& VM = m_VMaps.back();
		VM = xr_new<st_VMap>("Texture",vmtUV,false);
		for (int tx_i=0; tx_i<vm_cnt; tx_i++){
			UVVert* tv = obj->mesh.tVerts + tx_i;
			VM->appendUV(tv->x,1-tv->y);
		}
	}

	if ((GetVertexCount()<4)||(GetFaceCount()<2))
	{
		ELog.Msg(mtError,"Invalid mesh: '%s'. Faces<2 or Verts<4");
		bResult = false;
	}

	if (bResult ){
		ELog.Msg(mtInformation,"Model '%s' contains: %d points, %d faces",
			node->GetName(), m_VertCount, m_FaceCount);
	}

	if (bResult)
	{
		RecomputeBBox	();
		OptimizeMesh	(false);
		RebuildVMaps	();
		ELog.Msg(mtInformation,"Model '%s' converted: %d points, %d faces",
			node->GetName(), GetVertexCount(), GetFaceCount());
	}

	if (bDeleteObj) delete (obj);
	return bResult;
}
Exemplo n.º 3
0
MStatus CXRayObjectExport::ExportPart(CEditableObject* O, MDagPath& mdagPath, MObject&  mComponent)
{
	MStatus stat = MS::kSuccess;
	MSpace::Space space = MSpace::kWorld;

	MFnMesh fnMesh( mdagPath, &stat );
	if ( MS::kSuccess != stat) {
		fprintf(stderr,"Failure in MFnMesh initialization.\n");
		return MS::kFailure;
	}

	MString mdagPathNodeName = fnMesh.name();

	MFnDagNode dagNode1(mdagPath);
	u32 pc = dagNode1.parentCount();
	for(u32 ip=0;ip<pc;++ip)
	{
		MObject object_parent = dagNode1.parent(ip, &stat);
		if(object_parent.hasFn(MFn::kTransform))
		{
			MFnTransform parent_transform(object_parent,&stat);
	
			if ( MS::kSuccess == stat) 
			{
				mdagPathNodeName = parent_transform.name();
				break;
			}
		}
	}

	MItMeshPolygon meshPoly( mdagPath, mComponent, &stat );
	if ( MS::kSuccess != stat) {
		fprintf(stderr,"Failure in MItMeshPolygon initialization.\n");
		return MS::kFailure;
	}

	MItMeshVertex vtxIter( mdagPath, mComponent, &stat );
	if ( MS::kSuccess != stat) {
		fprintf(stderr,"Failure in MItMeshVertex initialization.\n");
		return MS::kFailure;
	}

	// If the shape is instanced then we need to determine which
	// instance this path refers to.
	//
	int instanceNum = 0;
	if (mdagPath.isInstanced())
		instanceNum = mdagPath.instanceNumber();

	// Get a list of all shaders attached to this mesh
	MObjectArray rgShaders;
	MIntArray texMap;
	MStatus status;
	status = fnMesh.getConnectedShaders (instanceNum, rgShaders, texMap);
	if (status == MStatus::kFailure)
	{
		Log("! Unable to load shaders for mesh");
		return (MStatus::kFailure);
	}

	XRShaderDataVec xr_data;
	{
		for ( int i=0; i<(int)rgShaders.length(); i++ ) {
			MObject shader = rgShaders[i];

			xr_data.push_back(SXRShaderData());
			SXRShaderData& D = xr_data.back();

			status = parseShader(shader, D);
			if (status == MStatus::kFailure) {
				status.perror ("Unable to retrieve filename of texture");
				continue;
			}
		}
	}

	CEditableMesh* MESH = new CEditableMesh(O);
	MESH->SetName(mdagPathNodeName.asChar());
	O->AppendMesh(MESH);

	int objectIdx, length;

	// Find i such that objectGroupsTablePtr[i] corresponds to the
	// object node pointed to by mdagPath
	length = objectNodeNamesArray.length();
	{
		for( int i=0; i<length; i++ ) {
			if( objectNodeNamesArray[i] == mdagPathNodeName ) {
				objectIdx = i;
				break;
			}
		}
	}
	// Reserve uv table
	{
		VMapVec& _vmaps	= MESH->m_VMaps;
		_vmaps.resize	(1);
		st_VMap*& VM	= _vmaps.back();
		VM				= new st_VMap("Texture",vmtUV,false);
	}

	// write faces
	{
		using FaceVec = xr_vector<st_Face>;
		using FaceIt = FaceVec::iterator;

		VMapVec& _vmaps			= MESH->m_VMaps;
		SurfFaces& _surf_faces	= MESH->m_SurfFaces;
		VMRefsVec& _vmrefs		= MESH->m_VMRefs;
		
		// temp variables
		FvectorVec	_points;
		FaceVec _faces;
		U32Vec _sgs;

		int f_cnt				= fnMesh.numPolygons();

		_sgs.reserve	(f_cnt);
		_faces.reserve	(f_cnt);
		_vmrefs.reserve	(f_cnt*3);

//		int lastSmoothingGroup = INITIALIZE_SMOOTHING;
		MPointArray rgpt;
		MIntArray rgint;

		PtLookupMap ptMap;
		CSurface* surf	= 0;
		for ( ; !meshPoly.isDone(); meshPoly.next()){
			// Write out the smoothing group that this polygon belongs to
			// We only write out the smoothing group if it is different
			// from the last polygon.
			//
			int compIdx	= meshPoly.index();
			int smoothingGroup = polySmoothingGroups[ compIdx ];
			// for each polygon, first setup the reverse mapping
			// between object-relative vertex indices and face-relative
			// vertex indices
			ptMap.clear();
			for (int i=0; i<(int)meshPoly.polygonVertexCount(); i++)
				ptMap.insert (PtLookupMap::value_type(meshPoly.vertexIndex(i), i) );

			// verify polygon zero area
			if (meshPoly.zeroArea()){
				status = MS::kFailure;
				Log("! polygon have zero area:",meshPoly.index());
				return status;
			}

			// verify polygon zero UV area
/*			if (meshPoly.zeroUVArea()){
				status = MS::kFailure;
				Log("! polygon have zero UV area:",meshPoly.index());
				return status;
			}
*/
			// verify polygon has UV information
			if (!meshPoly.hasUVs (&status)) {
				status = MS::kFailure;
				Log("! polygon is missing UV information:",meshPoly.index());
				return status;
			}

			int cTri;
			// now iterate through each triangle on this polygon and create a triangle object in our list
			status = meshPoly.numTriangles (cTri);	
			if (!status) {
				Log("! can't getting triangle count");
				return status;
			}

			for (int i=0; i < cTri; i++) {

				// for each triangle, first get the triangle data
				rgpt.clear();//triangle vertices
				rgint.clear();//triangle vertex indices 

				// triangles that come from object are retrieved in world space
				status = meshPoly.getTriangle (i, rgpt, rgint, MSpace::kWorld);

				if (!status) {
					Log("! can't getting triangle for mesh poly");
					return status;
				}

				if ((rgpt.length() != 3) || (rgint.length() != 3)) {
					Msg("! 3 points not returned for triangle");
					return MS::kFailure;
				}

				// Write out vertex/uv index information
				//
				R_ASSERT2(fnMesh.numUVs()>0,"Can't find uvmaps.");
				_faces.push_back(st_Face());
				_sgs.push_back(smoothingGroup);
				//set_smooth
				set_smoth_flags( _sgs.back(), rgint );

				st_Face& f_it		= _faces.back();
				for ( int vtx=0; vtx<3; vtx++ ) {
					// get face-relative vertex
					PtLookupMap::iterator mapIt;

					int vtLocal, vtUV;
					int vt = rgint[vtx];
					mapIt = ptMap.find(vt);
					Fvector2 uv;
					if (mapIt == ptMap.end()){
						Msg("! Can't find local index.");
						return MS::kFailure;
					}
					vtLocal = (*mapIt).second;

					status = meshPoly.getUVIndex (vtLocal, vtUV, uv.x, uv.y); 
					if (!status) {
						Msg("! error getting UV Index for local vertex '%d' and object vertex '%d'",vtLocal,vt);
						return status;
					}

					// flip v-part 
					uv.y=1.f-uv.y;

					f_it.pv[2-vtx].pindex	= AppendVertex(_points,rgpt[vtx]);
					f_it.pv[2-vtx].vmref	= _vmrefs.size();
					_vmrefs.push_back		(st_VMapPtLst());
					st_VMapPtLst& vm_lst	= _vmrefs.back();
					vm_lst.count			= 1;
					vm_lst.pts				= xr_alloc<st_VMapPt>(vm_lst.count);
					vm_lst.pts[0].vmap_index= 0;
					vm_lst.pts[0].index 	= AppendUV(_vmaps.back(),uv);
				}
				// out face material
				int iTexture	= texMap[meshPoly.index()];
				if (iTexture<0)
					xrDebug::Fatal(DEBUG_INFO,"Can't find material for polygon: %d",meshPoly.index());
				SXRShaderData& D= xr_data[iTexture];

				int compIdx = meshPoly.index();
				surf		= MESH->Parent()->CreateSurface(getMaterialName(mdagPath, compIdx, objectIdx),D);
				if (!surf)	return MStatus::kFailure;	
				_surf_faces[surf].push_back(_faces.size()-1);
			}
		}
		{
			// copy from temp
			MESH->m_VertCount	= _points.size();
			MESH->m_FaceCount	= _faces.size();
			MESH->m_Vertices	= xr_alloc<Fvector>(MESH->m_VertCount);
			Memory.mem_copy		(MESH->m_Vertices,&*_points.begin(),MESH->m_VertCount*sizeof(Fvector));
			MESH->m_Faces		= xr_alloc<st_Face>(MESH->m_FaceCount);
			Memory.mem_copy		(MESH->m_Faces,&*_faces.begin(),MESH->m_FaceCount*sizeof(st_Face));
			MESH->m_SmoothGroups = xr_alloc<u32>(MESH->m_FaceCount);
			Memory.mem_copy		(MESH->m_SmoothGroups,&*_sgs.begin(),MESH->m_FaceCount*sizeof(u32));

			MESH->RecomputeBBox	();
		}
		if ((MESH->GetVertexCount()<4)||(MESH->GetFaceCount()<2))
		{
			Log		("! Invalid mesh: '%s'. Faces<2 or Verts<4",*MESH->Name());
			return MS::kFailure;
		}
	}
	return stat;
}