[Bf-blender-cvs] SVN commit: /data/svn/bf-blender [15592] trunk/blender/source/gameengine: BGE patch: DUPLIGROUP option supported in BGE.

Benoit Bolsee benoit.bolsee at online.be
Tue Jul 15 22:05:23 CEST 2008


Revision: 15592
          http://projects.blender.org/plugins/scmsvn/viewcvs.php?view=rev&root=bf-blender&revision=15592
Author:   ben2610
Date:     2008-07-15 22:05:23 +0200 (Tue, 15 Jul 2008)

Log Message:
-----------
BGE patch: DUPLIGROUP option supported in BGE.

Blender duplicates groups in the 3D view at the location of objects having the DUPLIGROUP option set. This feature is now supported in the BGE: the groups will be instantiated as in the 3D view when the scene is converted. This is useful to populate a scene with multiple enemies without having to actually duplicate the objects in the blend file.

Notes: * The BGE applies the same criteria to instantiate the group as Blender to display them: if you see the group in the 3D view, it will be instantiated in the BGE.
       * Groups are instantiated as if the object having the DUPLIGROUP option (usually an empty) executed an AddObject actuator on the top objects of the group (objects without parent).
       * As a result, only intra-group parent relationship is supported: the BGE will not instantiate objects that have parents outside the group.
       * Intra-group logic bricks connections are preserved between the duplicated objects, even between the top objects of the group.
       * For best result, the state engine of the objects in the group should be self-contained: logic bricks should only have intra-group connections. Use messages to communicate with state engines outside the group.
       * Nested groups are supported: if one or more objects in the group have the DUPLIGROUP option set, the corresponding groups will be instantiated at the corresponding position and orientation. 
       * Nested groups are instantiated as separate groups, not as one big group.
       * Linked groups are supported as well as groups containing objects from the active layers.
       * There is a difference in the way Blender displays the groups in the 3D view and how BGE instantiates them: Blender does not take into account the parent relationship in the group and displays the objects as if they were all children of the object having the DUPLIGROUP option. That's correct for the top objects of the group but not for the children. Hence the orientation of the children objects may be different in the BGE.
       * An AddGroup actuator will be added in a future release.

Modified Paths:
--------------
    trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp
    trunk/blender/source/gameengine/Ketsji/KX_GameObject.h
    trunk/blender/source/gameengine/Ketsji/KX_RadarSensor.cpp
    trunk/blender/source/gameengine/Ketsji/KX_Scene.cpp
    trunk/blender/source/gameengine/Ketsji/KX_Scene.h

Modified: trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp
===================================================================
--- trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp	2008-07-15 19:38:48 UTC (rev 15591)
+++ trunk/blender/source/gameengine/Converter/BL_BlenderDataConversion.cpp	2008-07-15 20:05:23 UTC (rev 15592)
@@ -1847,7 +1847,10 @@
 	int aspect_width;
 	int aspect_height;
 	vector<MT_Vector3> inivel,iniang;
-	
+	set<Group*> grouplist;	// list of groups to be converted
+	set<Object*> allblobj;	// all objects converted
+	set<Object*> groupobj;	// objects from groups (never in active layer)
+
 	if (alwaysUseExpandFraming) {
 		frame_type = RAS_FrameSettings::e_frame_extend;
 		aspect_width = canvas->GetWidth();
@@ -1919,6 +1922,8 @@
 	for (SETLOOPER(blenderscene, base))
 	{
 		Object* blenderobject = base->object;
+		allblobj.insert(blenderobject);
+
 		KX_GameObject* gameobj = gameobject_from_blenderobject(
 										base->object, 
 										kxscene, 
@@ -2046,7 +2051,9 @@
 				
 				gameobj->NodeUpdateGS(0,true);
 				gameobj->Bucketize();
-				
+		
+				if (gameobj->IsDupliGroup())
+					grouplist.insert(blenderobject->dup_group);
 			}
 			else
 			{
@@ -2073,6 +2080,188 @@
 
 	}
 
+	if (!grouplist.empty())
+	{
+		// now convert the group referenced by dupli group object
+		// keep track of all groups already converted
+		set<Group*> allgrouplist = grouplist;
+		set<Group*> tempglist;
+		// recurse
+		while (!grouplist.empty())
+		{
+			set<Group*>::iterator git;
+			tempglist.clear();
+			tempglist.swap(grouplist);
+			for (git=tempglist.begin(); git!=tempglist.end(); git++)
+			{
+				Group* group = *git;
+				GroupObject* go;
+				for(go=(GroupObject*)group->gobject.first; go; go=(GroupObject*)go->next) 
+				{
+					Object* blenderobject = go->ob;
+					if (converter->FindGameObject(blenderobject) == NULL)
+					{
+						allblobj.insert(blenderobject);
+						groupobj.insert(blenderobject);
+						KX_GameObject* gameobj = gameobject_from_blenderobject(
+														blenderobject, 
+														kxscene, 
+														rendertools, 
+														converter,
+														blenderscene);
+										
+						// this code is copied from above except that
+						// object from groups are never is active layer
+						bool isInActiveLayer = false;
+						bool addobj=true;
+						
+						if (converter->addInitFromFrame)
+							if (!isInActiveLayer)
+								addobj=false;
+														
+						if (gameobj&&addobj)
+						{
+							MT_Point3 posPrev;			
+							MT_Matrix3x3 angor;			
+							if (converter->addInitFromFrame) 
+								blenderscene->r.cfra=blenderscene->r.sfra;
+							
+							MT_Point3 pos = MT_Point3(
+								blenderobject->loc[0]+blenderobject->dloc[0],
+								blenderobject->loc[1]+blenderobject->dloc[1],
+								blenderobject->loc[2]+blenderobject->dloc[2]
+							);
+							MT_Vector3 eulxyz = MT_Vector3(
+								blenderobject->rot[0],
+								blenderobject->rot[1],
+								blenderobject->rot[2]
+							);
+							MT_Vector3 scale = MT_Vector3(
+								blenderobject->size[0],
+								blenderobject->size[1],
+								blenderobject->size[2]
+							);
+							if (converter->addInitFromFrame){//rcruiz
+								float eulxyzPrev[3];
+								blenderscene->r.cfra=blenderscene->r.sfra-1;
+								update_for_newframe();
+								MT_Vector3 tmp=pos-MT_Point3(blenderobject->loc[0]+blenderobject->dloc[0],
+															blenderobject->loc[1]+blenderobject->dloc[1],
+															blenderobject->loc[2]+blenderobject->dloc[2]
+													);
+								eulxyzPrev[0]=blenderobject->rot[0];
+								eulxyzPrev[1]=blenderobject->rot[1];
+								eulxyzPrev[2]=blenderobject->rot[2];
+
+								double fps = (double) blenderscene->r.frs_sec/
+									(double) blenderscene->r.frs_sec_base;
+
+								tmp.scale(fps, fps, fps);
+								inivel.push_back(tmp);
+								tmp=eulxyz-eulxyzPrev;
+								tmp.scale(fps, fps, fps);
+								iniang.push_back(tmp);
+								blenderscene->r.cfra=blenderscene->r.sfra;
+								update_for_newframe();
+							}		
+										
+							gameobj->NodeSetLocalPosition(pos);
+							gameobj->NodeSetLocalOrientation(MT_Matrix3x3(eulxyz));
+							gameobj->NodeSetLocalScale(scale);
+							gameobj->NodeUpdateGS(0,true);
+							
+							BL_ConvertIpos(blenderobject,gameobj,converter);
+							// TODO: expand to multiple ipos per mesh
+							Material *mat = give_current_material(blenderobject, 1);
+							if(mat) BL_ConvertMaterialIpos(mat, gameobj, converter);	
+					
+							sumolist->Add(gameobj->AddRef());
+							
+							BL_ConvertProperties(blenderobject,gameobj,timemgr,kxscene,isInActiveLayer);
+							
+					
+							gameobj->SetName(blenderobject->id.name);
+					
+							// templist to find Root Parents (object with no parents)
+							templist->Add(gameobj->AddRef());
+							
+							// update children/parent hierarchy
+							if ((blenderobject->parent != 0)&&(!converter->addInitFromFrame))
+							{
+								// blender has an additional 'parentinverse' offset in each object
+								SG_Node* parentinversenode = new SG_Node(NULL,NULL,SG_Callbacks());
+							
+								// define a normal parent relationship for this node.
+								KX_NormalParentRelation * parent_relation = KX_NormalParentRelation::New();
+								parentinversenode->SetParentRelation(parent_relation);
+					
+								parentChildLink pclink;
+								pclink.m_blenderchild = blenderobject;
+								pclink.m_gamechildnode = parentinversenode;
+								vec_parent_child.push_back(pclink);
+
+								float* fl = (float*) blenderobject->parentinv;
+								MT_Transform parinvtrans(fl);
+								parentinversenode->SetLocalPosition(parinvtrans.getOrigin());
+								parentinversenode->SetLocalOrientation(parinvtrans.getBasis());
+								
+								parentinversenode->AddChild(gameobj->GetSGNode());
+							}
+							
+							// needed for python scripting
+							logicmgr->RegisterGameObjectName(gameobj->GetName(),gameobj);
+
+							// needed for dynamic object morphing
+							logicmgr->RegisterGameObj(gameobj, blenderobject);
+							for (int i = 0; i < gameobj->GetMeshCount(); i++)
+								logicmgr->RegisterGameMeshName(gameobj->GetMesh(i)->GetName(), blenderobject);
+					
+							converter->RegisterGameObject(gameobj, blenderobject);	
+							// this was put in rapidly, needs to be looked at more closely
+							// only draw/use objects in active 'blender' layers
+					
+							logicbrick_conversionlist->Add(gameobj->AddRef());
+							
+							if (converter->addInitFromFrame){
+								posPrev=gameobj->NodeGetWorldPosition();
+								angor=gameobj->NodeGetWorldOrientation();
+							}
+							if (isInActiveLayer)
+							{
+								objectlist->Add(gameobj->AddRef());
+								//tf.Add(gameobj->GetSGNode());
+								
+								gameobj->NodeUpdateGS(0,true);
+								gameobj->Bucketize();
+						
+							}
+							else
+							{
+								//we must store this object otherwise it will be deleted 
+								//at the end of this function if it is not a root object
+								inactivelist->Add(gameobj->AddRef());
+
+							}
+							if (gameobj->IsDupliGroup())
+							{
+								// check that the group is not already converted
+								if (allgrouplist.insert(blenderobject->dup_group).second)
+									grouplist.insert(blenderobject->dup_group);
+							}
+							if (converter->addInitFromFrame){
+								gameobj->NodeSetLocalPosition(posPrev);
+								gameobj->NodeSetLocalOrientation(angor);
+							}
+										
+						}
+						if (gameobj)
+							gameobj->Release();
+					}
+				}
+			}
+		}
+	}
+
 	if (blenderscene->camera) {
 		KX_Camera *gamecamera= (KX_Camera*) converter->FindGameObject(blenderscene->camera);
 		
@@ -2081,15 +2270,18 @@
 	}
 
 	//	Set up armatures
-	for(SETLOOPER(blenderscene, base)){
-		if (base->object->type==OB_MESH){
-			Mesh *me = (Mesh*)base->object->data;
+	set<Object*>::iterator oit;
+	for(oit=allblobj.begin(); oit!=allblobj.end(); oit++)
+	{
+		Object* blenderobj = *oit;
+		if (blenderobj->type==OB_MESH){
+			Mesh *me = (Mesh*)blenderobj->data;
 	
 			if (me->dvert){
-				KX_GameObject *obj = converter->FindGameObject(base->object);
+				KX_GameObject *obj = converter->FindGameObject(blenderobj);
 	
-				if (base->object->parent && base->object->parent->type==OB_ARMATURE && base->object->partype==PARSKEL){
-					KX_GameObject *par = converter->FindGameObject(base->object->parent);
+				if (obj && blenderobj->parent && blenderobj->parent->type==OB_ARMATURE && blenderobj->partype==PARSKEL){
+					KX_GameObject *par = converter->FindGameObject(blenderobj->parent);
 					if (par)
 						((BL_SkinDeformer*)(((BL_DeformableGameObject*)obj)->m_pDeformer))->SetArmature((BL_ArmatureObject*) par);
 				}
@@ -2174,7 +2366,8 @@
 		{
 			meshobj = gameobj->GetMesh(0);
 		}
-		BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,activeLayerBitInfo,physics_engine,converter,processCompoundChildren);
+		int layerMask = (groupobj.find(blenderobject) == groupobj.end()) ? activeLayerBitInfo : 0;
+		BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,layerMask,physics_engine,converter,processCompoundChildren);
 	}
 
 	processCompoundChildren = true;
@@ -2189,7 +2382,8 @@
 		{
 			meshobj = gameobj->GetMesh(0);
 		}
-		BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,activeLayerBitInfo,physics_engine,converter,processCompoundChildren);
+		int layerMask = (groupobj.find(blenderobject) == groupobj.end()) ? activeLayerBitInfo : 0;
+		BL_CreatePhysicsObjectNew(gameobj,blenderobject,meshobj,kxscene,layerMask,physics_engine,converter,processCompoundChildren);
 	}
 	
 	
@@ -2311,22 +2505,25 @@
 	{
 		KX_GameObject* gameobj = static_cast<KX_GameObject*>(logicbrick_conversionlist->GetValue(i));
 		struct Object* blenderobj = converter->FindBlenderObject(gameobj);
-		bool isInActiveLayer = (blenderobj->lay & activeLayerBitInfo)!=0;
-		BL_ConvertActuators(maggie->name, blenderobj,gameobj,logicmgr,kxscene,ketsjiEngine,executePriority, activeLayerBitInfo,isInActiveLayer,rendertools,converter);
+		int layerMask = (groupobj.find(blenderobj) == groupobj.end()) ? activeLayerBitInfo : 0;
+		bool isInActiveLayer = (blenderobj->lay & layerMask)!=0;
+		BL_ConvertActuators(maggie->name, blenderobj,gameobj,logicmgr,kxscene,ketsjiEngine,executePriority, layerMask,isInActiveLayer,rendertools,converter);
 	}
 	for ( i=0;i<logicbrick_conversionlist->GetCount();i++)
 	{

@@ Diff output truncated at 10240 characters. @@




More information about the Bf-blender-cvs mailing list