How to Hi-Light a vgObject

 

Vega Code Example

 

 

 

 

 

 
 

This example function illustrates how to create a Performer high-light and apply it to a vgObject.

 

  • How to apply a high-light to a vgObject (see setHighlightOnVegaObject)
  • How to remove a high-light that has been applied to a vgObject (see remHighlightFromVegaObject)
  • How to create a pfHighlight (see createHighlight )
  • How to use pfNewHlight
  • How to use Vega's shared Arena with vgGetSharedArena
  • How to set the Highlight style of a pfHighlight with pfHlightMode
  • How to set the color of a pfHighlight using pfHlightColor
  • How to set the Line width for line style pfHighlight with pfHlightLineWidth
  • How to set the lengths of Normals when displaying normals using pfHlightNormalLength
  • How to write Performer node draw call backs
  • How to pass data in to perfomer node call-backs using pfNodeTravData
  • How so save the performer Draw state using pfPushState
  • How to get the pfNode from a pfTraverser using pfGetTravNode
  • How to enable and disable highlighting using pfEnable
  • How to apply and make a pfHighlight current with pfApplyHlight
  • How to restore the performer Draw state using pfPopState
  • How to get the performer node from a vgObject using vgGetObjPfNode
  • How to add Pre and Post draw call-back functions to  a pfNode using pfNodeTravFuncs
  • How to retrieve the user data from a pfNode using pfGetNodeTravData
  • How to remove pfNode call-backs using pfNodeTravFuncs
  • How to call a call-back function only once ( see remhighlightPostDrawTravFuncFIX)
  • How to make a call-back remove its self ( see remhighlightPostDrawTravFuncFIX)
  • How to remove the user data from a call-back using pfNodeTravData
  • How to delete a pfHighlight  instance using pfDelete

 

  • This also shows how to work around an apparent bug in Vega NT where the high-light is not correctly remove

 

 
   
 

 

 
 
 
 

 

#include "vg.h"          // Required for the standard Vega classes
#include "vgperf.h"      // Required for the vgGetXXXPfNode functions
#include "pf.h"          // Required for the standard Performer classes
#include "vgutil.h"
#include "pfutil.h"      // Required for pfFindNode

//

// Declare the Pre and Post Node Draw as STATIC these have to be static

//

//

 

 

static int highlightPreDrawTravFunc       ( pfTraverser *trav, void *data );

static int highlightPostDrawTravFunc      ( pfTraverser *trav, void *data );

static int remhighlightPostDrawTravFuncFIX( pfTraverser *trav, void *data);

 

pfHighlight* createHighlight( const float r ,const float g ,const float b ,const unsigned int fill );

 

void       remHighlightFromVegaObject( vgObject *object );

void       setHighlightOnVegaObject  ( vgObject *object );

 

 

 

 

pfHighlight*

createHighlight( const float r

                ,const float g

                ,const float b

                ,const unsigned int fill ) {

// ####################################################

// # Public  function                                 

// #

// # Create a and return a new pfHighlight instance    

// #

// # Note it is upto the caller to manage the deletion

// #      of this instance

// #

// ####################################################

        

    

    //

    // create the new blank highlight instance

    //

    pfHighlight *hlight = pfNewHlight( vgGetSharedArena());

 

 

    // Set the fill/ out line mode for the highlight

    //

    // The following are possible high style note thatsome may not supported

    // on all platforms, also note that the style can be or'd together

    // to form combinations of high lights

    //

    // See the Performer PGuide and man pages for further details

    //

    //

    //      PFHL_LINES        PFHL_LINES_R    

    //      PFHL_LINESPAT     PFHL_LINESPAT2  

    //      PFHL_LINESMASK    PFHL_FILL       

    //      PFHL_FILLPAT      PFHL_FILLPAT2   

    //      PFHL_FILLTEX      PFHL_FILL_R     

    //      PFHL_FILLMASK     PFHL_SKIP_BASE  

    //      PFHL_POINTS       PFHL_NORMALS    

    //      PFHL_BBOX_LINES   PFHL_BBOX_FILL      

    //

    //

    pfHlightMode(  hlight, fill ) ;

 

    //

    // Set the forground and background color for the highlight

    //

    pfHlightColor( hlight, PFHL_FGCOLOR, r, g, b );

    pfHlightColor( hlight, PFHL_BGCOLOR, r, g, b );

    

    //

    // Set the alpha to opaque as transparent is not supported

    // on many systems

    //

    pfHlightAlpha( hlight, 1.0f );

 

 

    //

    // Set the line width for line styles

    //

    pfHlightLineWidth( hlight, 1 .0f);

 

    //

    //

    // When showing normals make them short

    //

    pfHlightNormalLength( hlight, 0.5f, 0.0f );

 

 

return hlight;

 

} // createHighlight

 

 

 

int     

highlightPreDrawTravFunc( pfTraverser *trav, void *data){

// ####################################################

// #

// # STATIC Function:  (PFTRAV_DRAW)   

// #

// # Pre Draw traversal function placed on a node which

// # enables and applies the highlight to the node and

// # all the nodes children

// #

// # note this function MUST be declared as STATIC

// #

// ####################################################

    

    //

    // We need to first save the current Performer State

    //

    pfPushState();

 

 

    //

    // Now we can enable the highlighting

    //

    pfEnable( PFEN_HIGHLIGHTING );

    

    //

    // Now retrieve the highlight from the passed user data

    //

    pfHighlight *hlight = (pfHighlight *)data;

    

    //

    // If we have a highlight then make it current

    //

    if( hlight)

        pfApplyHlight( hlight );

 

//

// We need to tell Performer to continue the traverse of this node

//

return (PFTRAV_CONT);

 

} // highlightPreDrawTravFunc

 

 

 

int     

highlightPostDrawTravFunc( pfTraverser *trav, void *data){

// ######################################################

// #

// # STATIC Function:  (PFTRAV_DRAW)   

// #

// # Post Draw traversal function on a node which disables

// # the highlight applied in the corresponding pre draw

// # fucntion

// #

// # note this function MUST be declared as STATIC

// #

// ######################################################

 

   

    //

    // Now we disable the highlighting we enabled

    //

    pfDisable( PFEN_HIGHLIGHTING );

 

    //

    // And restore the Performer state

    //

    pfPopState();

    

//

// We need to tell Performer to continue the traverse of this node

//

return (PFTRAV_CONT);

 

 

}  //  highlightPostDrawTravFunc

 

 

 

 

int     

remhighlightPostDrawTravFuncFIX( pfTraverser *trav, void *data){

// #################################################################

// #

// # STATIC Function:  (PFTRAV_DRAW)   

// #

// # Post Draw traversal function on a node which disables

// # the highlight applied in the corresponding pre draw

// # fucntion

// #

// #

// # This should be added in the remove highligh function

// # because when pfNodeTravFuncs( node, PFTRAV_DRAW, NULL, NULL )

// # called the call backs are removed but an apparent bug in Vega

// # means that the Predraw gets applied but the post draw function

// # does not and the highlight stays on this fixes the problem by

// # explicitly disbaling the highlighitng

// #

// #

// # note this function MUST be declared as STATIC

// #

// #################################################################

 

   

    //

    // Now we disable the highlighting we enabled

    //

    pfDisable( PFEN_HIGHLIGHTING );

 

    //

    // Retrieve which node we are

    //

    pfNode *node = pfGetTravNode( trav );

 

    //

    // We only need to call this one so we can self remove

    //

    pfNodeTravFuncs( node, PFTRAV_DRAW, NULL, NULL);

    

//

// We need to tell Performer to continue the traverse of this node

//

return (PFTRAV_CONT);

 

} // remhighlightPostDrawTravFuncFIX

 

 

void        

setHighlightOnVegaObject( vgObject *object ){

// ##########################################################

// #

// # Public Function

// #

// #   Example of how to add Highlightinh to a Vega Object

// #                                                        

// ##########################################################

 

    

    //

    //  Sanity check we need a object

    //

    if( object == NULL )

        return;

 

    //

    // Get the root Performer node of the object

    //

    pfNode *node = vgGetObjPfNode( object );

    if( node == NULL )

        return;

    

    //

    // Create the Highlight instance, Red and Filled

    //

    pfHighlight *hlight = createHighlight( 1.0f, 0.0f, 0.0f, PFHL_FILL);

    if( hlight == NULL )

        return;

    

 

    //

    // Set the Pre and Post draw callback which enable and disable the highlight

    //

    pfNodeTravFuncs( node, PFTRAV_DRAW, highlightPreDrawTravFunc, highlightPostDrawTravFunc );

    

    //

    // Set the highlight instance as the user data for the callbacks

    //

    pfNodeTravData( node, PFTRAV_DRAW, hlight );

 

 

} //setHighlightOnVegaObject

 

 

 

void        

remHighlightFromVegaObject( vgObject *object ){

// ##########################################################

// #

// # Public Function

// #

// #    Remove the highlight Callbacks from the passed object

// #

// ##########################################################

    

 

    //

    //  Sanity check we need a object

    //

    if( object == NULL )

        return;

      

    

    //

    // Get the root Performer node of the object

    //

    pfNode *node = vgGetObjPfNode( object );

    if( node == NULL )

        return;

 

    //

    // Retrieve the highlight instance from the user data

    //

    pfHighlight *hlight = (pfHighlight *)pfGetNodeTravData( node, PFTRAV_DRAW );

 

    //

    // Remove the highlight call back functions by simply setting

    // them to NULL on the Node, Performer only supports one call

    // pre and post traversal callbacks per node

    //

    pfNodeTravFuncs( node, PFTRAV_DRAW, NULL, NULL);

 

    

    //

    //

    // Theres an apperent bug in Vega where the Highlight will stay on

    // becuase the removal of the draw function gets called with the

    // highlight enabled but the post never gets called to remove the

    // highlight, so the state is wrong this is a work around that will

    // ensure that the highlight is removed

    //

    //

    pfNodeTravFuncs( node, PFTRAV_DRAW, NULL, remhighlightPostDrawTravFuncFIX);

    

    //

    // And remove the data simply by

    //

    pfNodeTravData( node, PFTRAV_DRAW, NULL );

 

 

    //

    // If we got a highlight back from the user data

    // then we delete it

    //

    if( hlight != NULL )

        pfDelete( hlight);

 

 

} // remHighlightFromVegaObject

 

 

 
 

 

 

Copyright 2004 Gordon Tomlinson  All Rights Reserved.

All logos, trademarks and copyrights in this site are property of their respective owner.