Difference between revisions of "Item orb"

From The Foundry MODO SDK wiki
Jump to: navigation, search
Line 204: Line 204:
  
 
===[[Server_Tags|Server Tags]]===
 
===[[Server_Tags|Server Tags]]===
 +
 +
Servers tags are examined when the server is initialized, and give information about the server. We set the tags in this case by taking a descinfo[] array and associating the relevant data with the corresponding flags.
  
 
These tags indicate that the class COrbPackage is a subtype of the super type locator and has the internal name of test-orb.
 
These tags indicate that the class COrbPackage is a subtype of the super type locator and has the internal name of test-orb.
Line 216: Line 218:
  
 
===[[Initialize_(index)|Initialize]]===
 
===[[Initialize_(index)|Initialize]]===
 +
 +
Servers are extensible set of features that we add to modo, usually through plugins. Intialize is called when we add the plugin to modo, and is the utility that exports the server.
  
 
This method indicates that we will be exporting one server that will be dependent on the COrbPackage class. All of the interfaces of the classes that it inherited will also be added and the server will have the name of test.orb.
 
This method indicates that we will be exporting one server that will be dependent on the COrbPackage class. All of the interfaces of the classes that it inherited will also be added and the server will have the name of test.orb.

Revision as of 21:00, 12 September 2013

Item_orb.cpp is a basic example plugin. This wiki page is intended as a walkthrough of the code in order to help you better understand the SDK.

Item_orb.cpp is a plugin that adds the orb item to modo's toolkit.

Orbshot.png

Orb Item pictured above

Code Walkthrough

Class Declarations

We want this class to be able to write out to the log, so we inherit from CLxLuxologyLogMessage.

class COrbLog : public CLxLuxologyLogMessage
{
   public:
       COrbLog () : CLxLuxologyLogMessage ("orb-item") { }

       const char *	 GetFormat  () { return "Orb Object"; }
};

This is a gear part that will be used in rendering.

class COrbPart
{
   ...
};

In order to display the part in the viewport, we inherit from CLxImpl_TableauSurface and CLxTableauInstance. We also need to inherit from COrbPart, as that is where the actual gear part is created.

class COrbElement :
       public CLxImpl_TableauSurface,
       public CLxImpl_TableauInstance,
       public COrbPart
{
       COrbLog		 orb_log;

   public:
       CLxUser_TableauVertex	 vrt_desc;
       float			 m_radius;
       int			 m_resolution;
       int			 f_pos[4];
       LXtVector		 m_offset;
       LXtMatrix		 m_xfrm;

       LxResult	 tsrf_Bound (LXtTableauBox bbox) LXx_OVERRIDE;
       unsigned	 tsrf_FeatureCount (LXtID4 type) LXx_OVERRIDE;
       LxResult	 tsrf_FeatureByIndex (
                               LXtID4		 type,
                               unsigned int	 index,
                               const char	**name) LXx_OVERRIDE;
       LxResult	 tsrf_SetVertex (ILxUnknownID vdesc) LXx_OVERRIDE;
       LxResult	 tsrf_Sample (
                               const LXtTableauBox bbox,
                               float scale,
                               ILxUnknownID trisoup) LXx_OVERRIDE;

       LxResult	 tins_Properties (
                               ILxUnknownID	 vecstack) LXx_OVERRIDE;

       LxResult	 tins_GetTransform (
                               unsigned	 endPoint,
                               LXtVector	 offset,
                               LXtMatrix	 xfrm) LXx_OVERRIDE;
};

In order to create the item's surface, we need to inherit from CLxImpl_Surface. Among other things, this will perform binning, or the grouping of the primitives that make up the item's surface.

class COrbItemSurface :
       public CLxImpl_Surface
{
       /*
        * The parts of the gear (side and endcaps)
        */
       COrbPackage	*src_pkg;

   public:
       LxResult	 surf_GetBBox (LXtBBox *bbox) LXx_OVERRIDE;

       LxResult	 surf_FrontBBox (
                               const LXtVector	 pos,
                               const LXtVector	 dir,
                               LXtBBox		*bbox) LXx_OVERRIDE;

       LxResult	 surf_RayCast (
                               const LXtRayInfo *ray,
                               LXtRayHit *hit) LXx_OVERRIDE;

       LxResult	 surf_BinCount (unsigned int *count) LXx_OVERRIDE;

       LxResult	 surf_BinByIndex (
                               unsigned int	 index,
                               void		**ppvObj) LXx_OVERRIDE;

       LxResult	 surf_TagCount (
                               LXtID4		 type,
                               unsigned int	*count) LXx_OVERRIDE;

       LxResult	 surf_TagByIndex (
                               LXtID4		 type,
                               unsigned int	 index,
                               const char	**stag) LXx_OVERRIDE;

       LxResult	 Initialize (
                               COrbPackage		*pkg,
                               CLxUser_Item		&m_item,
                               CLxUser_ChannelRead	&chanRead);

       LxResult	 Initialize (
                               COrbPackage		*pkg,
                               double			 radius);
};

We want this class to create an instance of the orb. In order to do that, we will need to implement the related package, so we inherit from CLxImpl_PackageInstance. Additionally, we will want to set the tags of the instance, so we inherit from CLxImpl_PackageInstance. Finally, we want to display the object in the viewport, so we inherit from CLxImpl_ViewItem3D, which has basic 3D display methods, and CLxImpl_TableauSource, which makes it so that channels update the preview.

class COrbInstance :
       public CLxImpl_PackageInstance,
       public CLxImpl_StringTag,
       public CLxImpl_TableauSource,
       public CLxImpl_ViewItem3D
{
       COrbLog		 	orb_log;

   public:
       COrbPackage	*src_pkg;
       CLxUser_Item	 m_item;
       ILxUnknownID	 inst_ifc;

       LxResult	 pins_Initialize (
                               ILxUnknownID	 item,
                               ILxUnknownID	 super) LXx_OVERRIDE;
       void		 pins_Cleanup (void) LXx_OVERRIDE;
       LxResult	 pins_SynthName (char *buf, unsigned len) LXx_OVERRIDE;
       unsigned	 pins_DupType (void) LXx_OVERRIDE;
       LxResult	 pins_TestParent (ILxUnknownID item) LXx_OVERRIDE;
       LxResult	 pins_Newborn (ILxUnknownID original) LXx_OVERRIDE;
       LxResult	 pins_Loading (void) LXx_OVERRIDE;
       LxResult	 pins_AfterLoad (void) LXx_OVERRIDE;
       void		 pins_Doomed (void) LXx_OVERRIDE;

       LxResult	 stag_Get (LXtID4 type, const char **tag) LXx_OVERRIDE;

       LxResult	 tsrc_Elements (ILxUnknownID tblx) LXx_OVERRIDE;
       LxResult	 tsrc_PreviewUpdate (
                               int	 chanIndex,
                               int	*update) LXx_OVERRIDE;

       LxResult	 vitm_Draw (
                               ILxUnknownID	 itemChanRead,
                               ILxUnknownID	 viewStrokeDraw,
                               int		 selectionFlags,
                               LXtVector	 itemColor) LXx_OVERRIDE;

       LxResult	 vitm_HandleCount (
                               int		*count) LXx_OVERRIDE;

       LxResult	 vitm_HandleMotion (
                               int		 handleIndex,
                               int		*motionType,
                               double		*min,
                               double		*max,
                               LXtVector	 plane,
                               LXtVector	 offset) LXx_OVERRIDE;

       LxResult	 vitm_HandleChannel (
                               int		 handleIndex,
                               int		*chanIndex) LXx_OVERRIDE;

       LxResult	 vitm_HandleValueToPosition (
                               int		 handleIndex,
                               double		*chanValue,
                               LXtVector	 position) LXx_OVERRIDE;

       LxResult	 vitm_HandlePositionToValue (
                               int		 handleIndex,
                               LXtVector	 position,
                               double		*chanValue) LXx_OVERRIDE;
};

We want to create a package for the orb item, so we have our class inherit from ClxImplPackage. Additionally, we want to have this class listen for global selection events, so we inherit from CLxImpl_SelectionListener.

class COrbPackage :
       public CLxImpl_Package,
       public CLxImpl_SelectionListener
{
   public:
       static LXtTagInfoDesc		 descInfo[];
       CLxPolymorph<COrbInstance>	 orb_factory;
       CLxPolymorph<COrbElement>	 elt_factory;

       COrbPackage ();

       LxResult		pkg_SetupChannels (
                                       ILxUnknownID addChan) LXx_OVERRIDE;
       LxResult		pkg_TestInterface (const LXtGUID *guid) LXx_OVERRIDE;
       LxResult		pkg_Attach (void **ppvObj) LXx_OVERRIDE;

       void			selevent_Add     (
                                       LXtID4		 type,
                                       unsigned int	 subtType) LXx_OVERRIDE;
       void			selevent_Current (LXtID4 type) LXx_OVERRIDE;
};

Server Tags

Servers tags are examined when the server is initialized, and give information about the server. We set the tags in this case by taking a descinfo[] array and associating the relevant data with the corresponding flags.

These tags indicate that the class COrbPackage is a subtype of the super type locator and has the internal name of test-orb.

LXtTagInfoDesc	 COrbPackage::descInfo[] = {
       { LXsPKG_SUPERTYPE,	"locator"	},
       { LXsPKG_IS_MASK,	"."		},
       { LXsSRV_LOGSUBSYSTEM,	"test-orb"	},
       { 0 }
};


Initialize

Servers are extensible set of features that we add to modo, usually through plugins. Intialize is called when we add the plugin to modo, and is the utility that exports the server.

This method indicates that we will be exporting one server that will be dependent on the COrbPackage class. All of the interfaces of the classes that it inherited will also be added and the server will have the name of test.orb.

       void
initialize ()
{
       CLxGenericPolymorph		*srv;

       srv = new CLxPolymorph<COrbPackage>;
       srv->AddInterface (new CLxIfc_Package          <COrbPackage>);
       srv->AddInterface (new CLxIfc_StaticDesc       <COrbPackage>);
       srv->AddInterface (new CLxIfc_SelectionListener<COrbPackage>);
       thisModule.AddServer ("test.orb", srv);
}

Implementations

These functions create a tableau surface element that lives in the tableau and generates geometry for the renderer. It has a bounding box, vertex features, and a sample method.

LxResult
COrbElement::tsrf_Bound (
       LXtTableauBox		 bbox)
{
       ...
}

       unsigned
COrbElement::tsrf_FeatureCount (
       LXtID4			 type)
{
      ...
}

       LxResult
COrbElement::tsrf_FeatureByIndex (
       LXtID4			 type,
       unsigned		 index,
       const char	       **name)
{
       ...
}

       LxResult
COrbElement::tsrf_SetVertex (
       ILxUnknownID		 vdesc)
{
       ...
}

       LxResult 
COrbElement::tsrf_Sample (
       const LXtTableauBox	 bbox,
       float			 scale,
       ILxUnknownID		 trisoup)
{
       ...
}

The instance is the implementation of the item, and there will be one allocated for each item in the scene. The functions here all perform a function(generally made evident by their name) for the instance.

       LxResult
COrbInstance::pins_Initialize (
       ILxUnknownID		 item,
       ILxUnknownID		 super)
{
       ...
}

       void
COrbInstance::pins_Cleanup (void)
{
       ...
}

       LxResult
COrbInstance::pins_SynthName (
       char			*buf,
       unsigned		 len)
{
      ...
}

       unsigned
COrbInstance::pins_DupType (void)
{
       ...
}

       LxResult
COrbInstance::pins_TestParent (
       ILxUnknownID		 item)
{
       ...
}

       LxResult
COrbInstance::pins_Newborn (ILxUnknownID original)
{ 
       ...
}

       LxResult
COrbInstance::pins_Loading (void)
{
       ...
}

       LxResult
COrbInstance::pins_AfterLoad (void)
{
       ...
}

       void
COrbInstance::pins_Doomed (void)
{
}

The instance also presents a StringTag interface so it can pretend to have part and material tags for finding a shader.

LxResult
COrbInstance::stag_Get (
       LXtID4			 type,
       const char	       **tag)
{
      ...
}

The instance's TableauSource interface allows it to place elements into the tableau, in this case our orb element.

       LxResult
COrbInstance::tsrc_Elements (
       ILxUnknownID		 tblx)
{
       ...
}

       LxResult
COrbInstance::tsrc_PreviewUpdate (
       int			 chanIndex,
       int			*update)
{
       ...
}

Based on the channel values, draw the abstract item representation using the stroke drawing interface.

LxResult
COrbInstance::vitm_Draw (
       ILxUnknownID	 itemChanRead,
       ILxUnknownID	 viewStrokeDraw,
       int		 selectionFlags,
       LXtVector	 itemColor)
{
       ...
}

       LxResult
COrbInstance::vitm_HandleCount (
       int		*count)
{
       ...
}

       LxResult
COrbInstance::vitm_HandleMotion (
       int		 handleIndex,
       int		*motionType,
       double		*min,
       double		*max,
       LXtVector	 plane,
       LXtVector	 offset)
{
       ...
}

       LxResult
COrbInstance::vitm_HandleChannel (
       int		 handleIndex,
       int		*chanIndex)
{ 
       ...
}

       LxResult
COrbInstance::vitm_HandleValueToPosition (
       int		 handleIndex,
       double		*chanValue,
       LXtVector	 position)
{
       ...
}

       LxResult
COrbInstance::vitm_HandlePositionToValue (
       int		 handleIndex,
       LXtVector	 position,
       double		*chanValue)
{
      ...
}