Esempio n. 1
0
Import::Import(const char* pathName, Interface* ip, ImpInterface* impip)
{
    // set the path for textures
    char* ptr = NULL;
    sprintf (m_path, "%s", pathName);
    for (int i = 0; m_path[i]; i ++) {
        if ((m_path[i] == '\\') || (m_path[i] == '/') ) {
            ptr = &m_path[i];
        }
    }
    *ptr = 0;


    m_ip = ip;
    m_impip = impip;
    m_succes = TRUE;
    MaterialCache materialCache (NewDefaultMultiMtl());

    SetSceneParameters();

    dFloat scale;
    scale = float (GetMasterScale(UNITS_METERS));
    dMatrix scaleMatrix (GetIdentityMatrix());
    scaleMatrix[0][0] = 1.0f / scale;
    scaleMatrix[1][1] = 1.0f / scale;
    scaleMatrix[2][2] = 1.0f / scale;
    dMatrix globalRotation (scaleMatrix * dPitchMatrix(3.14159265f * 0.5f) * dRollMatrix(3.14159265f * 0.5f));

    NewtonWorld* newton = NewtonCreate();
    dScene scene (newton);

    scene.Deserialize (pathName);
//	dScene::Iterator iter (scene);
//	for (iter.Begin(); iter; iter ++) {
//		dScene::dTreeNode* node = iter.GetNode();
//		dNodeInfo* info = scene.GetInfoFromNode(node);
//		if (info->GetTypeId() == dMeshNodeInfo::GetRttiType()) {
//			dMeshNodeInfo* mesh = (dMeshNodeInfo*) scene.GetInfoFromNode(node);
//			mesh->ConvertToTriangles();
//		}
//	}
    scene.BakeTransform (globalRotation);

    GeometryCache geometryCache;
    MaxNodeChache maxNodeCache;

    LoadMaterials (scene, materialCache);
    LoadGeometries (scene, geometryCache, materialCache);
    LoadNodes (scene, geometryCache, materialCache.m_multiMat, maxNodeCache);
    ApplyModifiers (scene, maxNodeCache);

    scene.CleanUp();
    NewtonDestroy(newton);
}
	//------------------------------
	bool MaterialCreator::createAndAssingMaxMaterial( const DocumentImporter::NodeMaterialBindingsPair& materialBindingPair )
	{
		const DocumentImporter::MaterialBindingVector& materialBindings = materialBindingPair.materialBindings;
		INode* maxNode = materialBindingPair.maxNode;

		if ( materialBindings.size() == 0 )
			return true;

		const COLLADAFW::UniqueId& geometryUniqueId = getUniqueIdByObjectINode( maxNode );
		assert( geometryUniqueId.isValid() );

		// calculate largest material id
		int largestMaterialId = 0;
		DocumentImporter::MaterialBindingVector::const_iterator it = materialBindings.begin();

		for ( ; it != materialBindings.end(); ++it)
		{
			int materialId = (int)it->getMaterialId();
			largestMaterialId = materialId;
		}

		if ( !materialBindings.empty() )
		{
			if ( materialBindings.size() == 1 )
			{
				const COLLADAFW::MaterialBinding& materialBinding = materialBindings[0];
				const COLLADAFW::Effect* effect = getEffect(materialBinding);
				if ( !effect )
					return true;
				Mtl* newMaterial = getMaxMaterial(*effect, materialBinding, geometryUniqueId);
				maxNode->SetMtl( newMaterial );
			}
			else
			{
				MultiMtl * multiMaterial = NewDefaultMultiMtl();
				multiMaterial->SetNumSubMtls( largestMaterialId + 1 );
				String multiMaterialName = String(maxNode->GetName()) + "-MultiMaterial";
				multiMaterial->SetName(multiMaterialName.c_str());

				it = materialBindings.begin();
				for ( ; it != materialBindings.end(); ++it)
				{
					const COLLADAFW::MaterialBinding& materialBinding = *it;
					const COLLADAFW::Effect* effect = getEffect(materialBinding);
					if ( !effect )
						continue;
					Mtl* newMaterial = getMaxMaterial(*effect, materialBinding, geometryUniqueId);
					multiMaterial->SetSubMtl( materialBinding.getMaterialId(), newMaterial);
				}
				maxNode->SetMtl( multiMaterial );
			}
		}
		return true;
	}
Esempio n. 3
0
Mtl* ComposeMultiMaterial(std::map<std::vector<std::string>, Mtl*>& multi_mats, MtlBaseLib* library, Interface* max_interface, int& slot, const std::vector<IfcGeom::Material>& materials, const std::string& object_type, const std::vector<int>& material_ids) {
	std::vector<std::string> material_names;
	bool needs_default = std::find(material_ids.begin(), material_ids.end(), -1) != material_ids.end();
	if (needs_default) {
		material_names.push_back(object_type);
	}
	for (auto it = materials.begin(); it != materials.end(); ++it) {
		material_names.push_back(it->name());
	}
	Mtl* default_material = 0;
	if (needs_default) {
		default_material = FindMaterialByName(library, object_type);
		if (default_material == 0) {
			default_material = NewDefaultStdMat();
			default_material->SetName(S(object_type));
			library->Add(default_material);
			if (slot < NUM_MATERIAL_SLOTS) {
				max_interface->PutMtlToMtlEditor(default_material, slot++);
			}
		}
	}
	if (material_names.size() == 1) {
		if (needs_default) {
			return default_material;
		} else {
			return FindOrCreateMaterial(library, max_interface, slot, *materials.begin());
		}
	}
	std::map<std::vector<std::string>, Mtl*>::const_iterator i = multi_mats.find(material_names);
	if (i != multi_mats.end()) {
		return i->second;
	}
	MultiMtl* multi_mat = NewDefaultMultiMtl();
	multi_mat->SetNumSubMtls(material_names.size());
	int mtl_id = 0;
	if (needs_default) {
		multi_mat->SetSubMtlAndName(mtl_id ++, default_material, default_material->GetName());
	}
	for (auto it = materials.begin(); it != materials.end(); ++it) {
		Mtl* mtl = FindOrCreateMaterial(library, max_interface, slot, *it);
		multi_mat->SetSubMtl(mtl_id ++, mtl);
	}
	library->Add(multi_mat);
	if (slot < NUM_MATERIAL_SLOTS) {
		max_interface->PutMtlToMtlEditor(multi_mat,slot++);
	}
	multi_mats.insert(std::pair<std::vector<std::string>, Mtl*>(material_names, multi_mat));
	return multi_mat;
}