示例#1
0
PyObject *KX_MeshProxy::pyattr_get_materials(void *self_v, const KX_PYATTRIBUTE_DEF *attrdef)
{
	KX_MeshProxy* self = static_cast<KX_MeshProxy*>(self_v);
	
	int tot= self->m_meshobj->NumMaterials();
	int i;
	
	PyObject *materials = PyList_New( tot );
	
	list<RAS_MeshMaterial>::iterator mit= self->m_meshobj->GetFirstMaterial();
	
	
	for (i=0; i<tot; mit++, i++) {
		RAS_IPolyMaterial *polymat = mit->m_bucket->GetPolyMaterial(); 	 
		
		/* Why do we need to check for RAS_BLENDERMAT if both are cast to a (PyObject *)? - Campbell */
		if (polymat->GetFlag() & RAS_BLENDERMAT) 	 
		{ 	 
			KX_BlenderMaterial *mat = static_cast<KX_BlenderMaterial*>(polymat); 	 
			PyList_SET_ITEM(materials, i, mat->GetProxy());
		}
		else {
			KX_PolygonMaterial *mat = static_cast<KX_PolygonMaterial*>(polymat);
			PyList_SET_ITEM(materials, i, mat->GetProxy());
		}
	}
	return materials;
}
示例#2
0
// get material ID
short getMaterialID (PyObject * obj, char * name)
{
	// search for material
	for (short matID = 0;; ++matID)
	{
		// get material
		RAS_IPolyMaterial * mat = getMaterial(obj, matID);
		// if material is not available, report that no material was found
		if (mat == NULL) 
			break;
		// name is a material name if it starts with MA and a UV texture name if it starts with IM
		if (name[0] == 'I' && name[1] == 'M')
		{
			// if texture name matches
			if (strcmp(mat->GetTextureName().ReadPtr(), name) == 0)
				return matID;
		} else 
		{
			// if material name matches
			if (strcmp(mat->GetMaterialName().ReadPtr(), name) == 0)
				return matID;
		}
	}
	// material was not found
	return -1;
}
bool KX_BlenderSceneConverter::MergeScene(KX_Scene *to, KX_Scene *from)
{
	{
		vector<pair<KX_Scene *, KX_WorldInfo *> >::iterator itp = m_worldinfos.begin();
		while (itp != m_worldinfos.end()) {
			if (itp->first == from)
				itp->first = to;
			itp++;
		}
	}

	{
		vector<pair<KX_Scene *, RAS_IPolyMaterial *> >::iterator itp = m_polymaterials.begin();
		while (itp != m_polymaterials.end()) {
			if (itp->first == from) {
				itp->first = to;

				/* also switch internal data */
				RAS_IPolyMaterial *mat = itp->second;
				mat->Replace_IScene(to);
			}
			itp++;
		}
	}

	{
		vector<pair<KX_Scene *, RAS_MeshObject *> >::iterator itp = m_meshobjects.begin();
		while (itp != m_meshobjects.end()) {
			if (itp->first == from)
				itp->first = to;
			itp++;
		}
	}

	{
		vector<pair<KX_Scene *, BL_Material *> >::iterator itp = m_materials.begin();
		while (itp != m_materials.end()) {
			if (itp->first == from)
				itp->first = to;
			itp++;
		}
	}

	MaterialCache::iterator matcacheit = m_mat_cache.find(from);
	if (matcacheit != m_mat_cache.end()) {
		// Merge cached BL_Material map.
		m_mat_cache[to].insert(matcacheit->second.begin(), matcacheit->second.end());
		m_mat_cache.erase(matcacheit);
	}

	PolyMaterialCache::iterator polymatcacheit = m_polymat_cache.find(from);
	if (polymatcacheit != m_polymat_cache.end()) {
		// Merge cached RAS_IPolyMaterial map.
		m_polymat_cache[to].insert(polymatcacheit->second.begin(), polymatcacheit->second.end());
		m_polymat_cache.erase(polymatcacheit);
	}

	return true;
}
void RAS_MeshObject::AddMeshUser(void *clientobj, SG_QList *head, RAS_Deformer* deformer)
{
	list<RAS_MeshMaterial>::iterator it;
	list<RAS_MeshMaterial>::iterator mit;

	for (it = m_materials.begin();it!=m_materials.end();++it) {
		/* always copy from the base slot, which is never removed 
		 * since new objects can be created with the same mesh data */
		if (deformer && !deformer->UseVertexArray())
		{
			// HACK! 
			// this deformer doesn't use vertex array => derive mesh
			// we must keep only the mesh slots that have unique material id
			// this is to match the derived mesh drawing function
			// Need a better solution in the future: scan the derive mesh and create vertex array
			RAS_IPolyMaterial* curmat = it->m_bucket->GetPolyMaterial();
			if (curmat->GetFlag() & RAS_BLENDERGLSL) 
			{
				for (mit = m_materials.begin(); mit != it; ++mit)
				{
					RAS_IPolyMaterial* mat = mit->m_bucket->GetPolyMaterial();
					if ((mat->GetFlag() & RAS_BLENDERGLSL) && 
						mat->GetMaterialIndex() == curmat->GetMaterialIndex())
						// no need to convert current mesh slot
						break;
				}
				if (mit != it)
					continue;
			}
		}
		RAS_MeshSlot *ms = it->m_bucket->CopyMesh(it->m_baseslot);
		ms->m_clientObj = clientobj;
		ms->SetDeformer(deformer);
		it->m_slots.insert(clientobj, ms);
		head->QAddBack(ms);
	}
}
bool RAS_OpenGLRasterizer::SetMaterial(const RAS_IPolyMaterial& mat)
{
	return mat.Activate(this, m_materialCachingInfo);
}
示例#6
0
// Texture object initialization
int Texture_init (Texture *self, PyObject *args, PyObject *kwds)
{
	// parameters - game object with video texture
	PyObject * obj = NULL;
	// material ID
	short matID = 0;
	// texture ID
	short texID = 0;
	// texture object with shared texture ID
	Texture * texObj = NULL;

	static const char *kwlist[] = {"gameObj", "materialID", "textureID", "textureObj", NULL};

	// get parameters
	if (!PyArg_ParseTupleAndKeywords(args, kwds, "O|hhO!",
		const_cast<char**>(kwlist), &obj, &matID, &texID, &TextureType,
		&texObj))
		return -1; 

	// if parameters are available
	if (obj != NULL)
	{
		// process polygon material or blender material
		try
		{
			// get pointer to texture image
			RAS_IPolyMaterial * mat = getMaterial(obj, matID);
			if (mat != NULL)
			{
				// is it blender material or polygon material
				if (mat->GetFlag() & RAS_BLENDERGLSL) 
				{
					self->m_imgTexture = static_cast<KX_BlenderMaterial*>(mat)->getImage(texID);
					self->m_useMatTexture = false;
				} else if (mat->GetFlag() & RAS_BLENDERMAT)
				{
					// get blender material texture
					self->m_matTexture = static_cast<KX_BlenderMaterial*>(mat)->getTex(texID);
					self->m_useMatTexture = true;
				}
				else
				{
					// get texture pointer from polygon material
					MTFace * tface = static_cast<KX_PolygonMaterial*>(mat)->GetMTFace();
					self->m_imgTexture = (Image*)tface->tpage;
					self->m_useMatTexture = false;
				}
			}
			// check if texture is available, if not, initialization failed
			if (self->m_imgTexture == NULL && self->m_matTexture == NULL)
				// throw exception if initialization failed
				THRWEXCP(MaterialNotAvail, S_OK);

			// if texture object is provided
			if (texObj != NULL)
			{
				// copy texture code
				self->m_actTex = texObj->m_actTex;
				self->m_mipmap = texObj->m_mipmap;
				if (texObj->m_source != NULL)
					Texture_setSource(self, reinterpret_cast<PyObject*>(texObj->m_source), NULL);
			}
			else
				// otherwise generate texture code
				glGenTextures(1, (GLuint*)&self->m_actTex);
		}
		catch (Exception & exp)
		{
			exp.report();
			return -1;
		}
	}
	// initialization succeded
	return 0;
}