Advertisement
nux95

PolygonizeHierarchy (Cinema 4D, untested)

Jul 23rd, 2012
143
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.81 KB | None | 0 0
  1. bool PolygonizeHierarchy(BaseObject* origin, HierarchyHelp* hh,
  2.                          VirtualObjectConnection*& desthead,
  3.                          VirtualObjectConnection*& desttail,
  4.                          BaseObject** destroot, AllocatorProc allocator) {
  5.  
  6.     // Ensure we can actually convert the object to geometry. Therefore, we
  7.     // have to check if the object is a generator, not a spline and when it
  8.     // is using it's input (OBJECT_INPUT flag) it must also be handled
  9.     // seperately.
  10.     LONG info = origin->GetInfo();
  11.     bool isgenerator = info & OBJECT_GENERATOR;
  12.     bool isspline    = info & OBJECT_ISSPLINE;
  13.     bool ispolygonal = info & OBJECT_POLYGONOBJECT;
  14.  
  15.     // This flag is only set in connection with OBJECT_GENERATOR
  16.     bool isinput     = info & OBJECT_INPUT;
  17.  
  18.     if (!(isgenerator && !isspline) && !ispolygonal) {
  19.         return NULL;
  20.     }
  21.  
  22.     // The reference to the new polygonized object is stored in this variable.
  23.     BaseObject* new_object;
  24.  
  25.     // When the object is already a PolygonObject, we will create a copy of it.
  26.     if (ispolygonal) {
  27.         COPYFLAGS cloneflag = COPYFLAGS_NO_HIERARCHY | COPYFLAGS_NO_ANIMATION;
  28.         new_object = (BaseObject*) origin->GetClone(cloneflag, NULL);
  29.         if (!new_object) return NULL;
  30.  
  31.         // The object may have been "touched" before and may also have some
  32.         // bit's that we don't want it to have. (E.g. BIT_ACTIVE will make
  33.         // the object being highlighted as it was selected, although it
  34.         // actually exists in the virtual-hierarchy of a Generator object).
  35.         new_object->SetAllBits(0);
  36.     }
  37.  
  38.     // Now, as we have already excluded the object being a spline or not
  39.     // generator, we can safely obtain the internal ObjectData from the object
  40.     // an create it's virtual objects which will also be convertex.
  41.     else {
  42.         ObjectData* data = (ObjectData*) origin->GetNodeData();
  43.         new_object = data->GetVirtualObjects(origin, hh);
  44.  
  45.         if (!new_object) return NULL;
  46.         if (!new_object->IsInstanceOf(Opolygon)) {
  47.             // We have to create a temporary object, as after conversion,
  48.             // we will have to free the object we've obtained by
  49.             // ObjectData::GetVirtualObjects().
  50.             // Note that we don't check if the object could be a spline or
  51.             // similar, because this is done by the call to
  52.             // PolygonizeHierarchy(). Yes, yes. It's well thought out. :)
  53.             BaseObject* temp = NULL;
  54.             PolygonizeHierarchy(new_object, hh, desthead, desttail, &temp,
  55.                                 allocator);
  56.             BaseObject::Free(new_object);
  57.             new_object = temp;
  58.         }
  59.     }
  60.  
  61.     // The object obtained from the call to PolygonizeHierarchy() after
  62.     // obtaining an object from the ObjectData could be NULL. ;)
  63.     if (new_object) {
  64.         // We will fill the value pointed by *destroot* with the current object
  65.         // when the pointer is not NULL. Afterwards, we will set it to be NULL
  66.         // so it's not overwritten by following calls to this function.
  67.         if (destroot) {
  68.             *destroot = new_object;
  69.             destroot  = NULL;
  70.         }
  71.  
  72.         // Well, the virtual object doesn't actually have the same position
  73.         // as the original, so we need to adjust that.
  74.         new_object->SetMl(origin->GetMl());
  75.  
  76.         // And now comes the tricky thing about generator objects that accept
  77.         // objects as input. If the object is an input generator, we will NOT
  78.         // go down it's actual hierarchy recursively, because they've been
  79.         // touched by the original generator.
  80.         if (!isinput) {
  81.             BaseObject* temp;
  82.             BaseObject* new_child;
  83.             for (temp=origin->GetDown(); temp; temp=temp->GetNext()) {
  84.                 new_child = NULL;
  85.                 PolygonizeHierarchy(temp, hh, desthead, desttail, &new_child,
  86.                                     allocator);
  87.                 if (new_child) new_child->InsertUnder(new_object);
  88.             }
  89.         }
  90.  
  91.         // Ok, we're almost done. The last thing we will need to do, is to
  92.         // establish the connection of the original object with the new, virtual
  93.         // object.
  94.         VirtualObjectConnection* new_connection = (VirtualObjectConnection*)
  95.                 allocator(sizeof(VirtualObjectConnection));
  96.         if (!new_connection) return FALSE;
  97.         if (!desttail) desttail = new_connection;
  98.         else {
  99.             desttail->next = new_connection;
  100.             desttail = desttail->next;
  101.         }
  102.         if (!desthead) desthead = desttail;
  103.         new_connection->origin  = origin;
  104.         new_connection->cache   = new_object;
  105.         new_connection->normals = NULL;
  106.         new_connection->seqinfo = NULL;
  107.     }
  108.  
  109.     return TRUE;
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement