How to create Performer Geometry and add attach to a vgObject

 

Vega Code Example

 

 

 

 

 

 
 

This example function illustrates how to :

  • Create you own Performer Geometry and add to an existing vgObject
  • How to retrieve the Performer root node from a vgObject
  • How to use pfCalloc
  • How to create a pfGroup
  • How to create a pfGeode
  • How to create a pfGeoSet
  • How to create a pfGeoState
  • How to use pfGSetAttr
  • How to use pfAddChild
  • How to use vgGetSharedArena
  • How to create and construct  a  pfNode
  • How to create and set vertices
  • How to create and set the colour for the vertices
  • How to create normals for the vertices

 

 
   
 

 

 
 
 
    
 

 

#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 "pfutil.h"      //  

 

void

addCustomGeometryToVgObject( vgObject *obj){

 

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

// #

// # Public Function

// #

// # This function show a relatively simple example of how to create some

// # to create some simple untextured Performer Geometry and then adding 

// # the pfNodes to an existing vgObject

// #

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

 

int i = 0, numVerts    = 0;

char        name[]     = { "my_pfNode" };

float      *v0         = NULL;

float      *v1         = NULL;

float      *v2         = NULL;

pfNode     *objNode    = NULL;

pfGroup    *group      = NULL;

pfGeode    *sidesGeode = NULL;

pfGeode    *topGeode   = NULL;

pfGeoSet   *sidesGset  = NULL;

pfGeoSet   *topGset    = NULL;

pfGeoState *geostate   = NULL

pfVec3     *verts      = NULL;

pfVec3     *norms      = NULL;

pfVec4     *color      = NULL;

PFINT      *lengths    = NULL;

pfVec3      vecList[4];

pfVec3      vec0,vec1;

 

 

    //

    //  We need an object

    //

    if( obj == NULL )

        return;

  

    //

    // Find the Objects performer root  

    // this is where we attach         

    //

    objNode = vgGetObjPfNode( obj );

    if( objNode == NULL)

        return;

 

    //

    // Create some verts list to use as a template we

    // will construct walla and a roof around these

    //

    numvert       = 4;

    vecList[0][0] = -5.0f;

    vecList[0][1] = -5.0f;

    vecList[0][2] =  1.0f;

 

    vecList[1][0] =  5.0f;

    vecList[1][1] = -5.0f;

    vecList[1][2] =  1.0f;

  

    vecList[2][0] =  5.0f;

    vecList[2][1] =  5.0f;

    vecList[2][2] =  1.0f;

  

    vecList[3][0] = -5.0f;

    vecList[3][1] =  5.0f;

    vecList[4][2] =  1.0f;

 

    //

    //  create a geoset consisting of one quad

    //

    sidesGeode = pfNewGeode();

    group      = pfNewGroup();

    sidesGset  = pfNewGSet( vgGetSharedArena() );

  

    assert( sidesGeode);

    assert( group    );

    assert( sidesGset );

    

    //

    //  Create the verts and nomrals for the geoset

    //

    verts = (pfVec3 *)pfCalloc( 4 * numVerts,sizeof(pfVec3),vgGetSharedArena());

    norms = (pfVec3 *)pfCalloc( 1 * numVerts,sizeof(pfVec3),vgGetSharedArena());

    color = (pfVec4 *)pfCalloc( 1 * numVerts,sizeof(pfVec4),vgGetSharedArena());

    assert( verts );

    assert( norms );

    assert( color );

    

 

    //

    // Create some walls except the last one as we assume

    // that the last vertwx is not the first one defined

    //

    int cv = 0;

    for( i = 0; i < numVerts - 1; i++ ){

 

        //

        //  Lower Left corner of the polygon

        //

        pfCopyVec3( verts[cv], vecList[i] );

        v0 = verts[cv];

        cv++;

 

        //

        //  Lower Right corner of the polygon

        //

        pfCopyVec3( verts[cv], vecList[i+1] );

        v1 = verts[cv];

        cv++;

 

        //

        // Upper Right Corner of the polygon

        //

        pfCopyVec3( verts[cv], vecList[i+1] );

        verts[cv][2] = height;

        v2 = verts[cv];

        cv++;

 

        //

        //  Upper Left Corner of the polygon

        //

        pfCopyVec3( verts[cv], vecList[i] );

        verts[cv][2] = height;

        cv++;

  

        //

        //  Calculate the normal for each polygon

        //

        PFSUB_VEC3( vec0, v1, v0 );

        PFSUB_VEC3( vec1, v2, v1 );

        pfCrossVec3( norms[i], vec0, vec1 );

        pfNormalizeVec3( norms[i] );

 

 

        }

 

 

    //

    //  Set the color to grey

    //

    for( i = 0; i < numVerts ; i++ ){

       pfSetVec4( color[i], 0.5f, 0.5f, 0.5f, 1.0f );

       }

 

 

    //

    //  Now define the last closing poly as we assume

    //  the last is not equal to the first            

    //                                     

    //  Lower Left Corner

    //

    pfCopyVec3( verts[cv], vecList[numVerts -1 ] );

    v0 = verts[cv];

    cv++;

 

    //

    // Lower Right Corner

    //

    pfCopyVec3( verts[cv], vecList[0] );

    v1 = verts[cv];

    cv++;

 

    //

    //  Upper Right Corner

    //

    pfCopyVec3( verts[cv], vecList[0] );

    verts[cv][2] = height;

    v2 = verts[cv];

    cv++;

 

    //

    // Upper Left Corner

    //

    pfCopyVec3( verts[cv], vecList[numVerts - 1]  );

    verts[cv][2] = height;

 

    //

    //  Calculate the normal for each polygon

    //

    PFSUB_VEC3( vec0, v1, v0 );

    PFSUB_VEC3( vec1, v2, v1 );

    pfCrossVec3( norms[ numVerts - 1], vec0, vec1 );

    pfNormalizeVec3( norms[ numVerts - 1] );

                

    //

    // We are numVerts quads only    

    //

    pfGSetNumPrims( sidesGset, numVerts );

    pfGSetPrimType( sidesGset, PFGS_QUADS);

 

 

    //

    //  Set the attributtes on the geoset  

    //

    pfGSetAttr( sidesGset, PFGS_COORD3,  PFGS_PER_VERTEX, verts, NULL );

    pfGSetAttr( sidesGset, PFGS_NORMAL3, PFGS_PER_PRIM,   norms, NULL );

    pfGSetAttr( sidesGset, PFGS_COLOR4,  PFGS_PER_PRIM,   color, NULL );

 

    pfGSetGState( sidesGset,  geostate  );

    pfAddGSet   ( sidesGeode, sidesGset );

          

    //

    // Now we create the top   

    //

    topGeode = pfNewGeode();

    topGset  = pfNewGSet( vgGetSharedArena() );

    assert( topGeode );

    assert( topGset  );

     

  

    //

    // Create the verts and normals for the geoset

    //

    verts   = (pfVec3 *)pfCalloc( numVerts+1,sizeof(pfVec3),vgGetSharedArena());

    norms   = (pfVec3 *)pfCalloc( 1,         sizeof(pfVec3),vgGetSharedArena());

    color   = (pfVec4 *)pfCalloc( 1,         sizeof(pfVec4),vgGetSharedArena());

    lengths = (PFINT  *)pfCalloc( 1,         sizeof(PFINT ),vgGetSharedArena());

 

    assert( verts   );

    assert( norms   );

    assert( color   );

    assert( lengths );

    

    lengths[0] = numVerts + 1;

 

    //  

    // Top only has one polygon     

    //

    pfGSetNumPrims   ( topGset, 1 );

    pfGSetPrimType   ( topGset, PFGS_POLYS );

    pfGSetPrimLengths( topGset, lengths );

 

    //

    // Create the one polygon that is the top

    // leave it performer/jolt to triangulate  

    //

    for( i = 0; i < numVerts; i++ ){

        pfCopyVec3( verts[i], vecList[i] );

        verts[i][2] = height;

        }

 

    pfCopyVec3( verts[i], vecList[0] );

    verts[i][2] = height;

 

    //

    // point the normal straight up

    //

    pfSetVec3( norms[0],  0.0f,  0.0f, 1.0f );

 

    //

    //  Set the color to grey

    //

    pfSetVec4( color[0], 0.6f, 0.6f, 0.6f, 1.0f );

 

    pfGSetAttr  ( topGset,  PFGS_COORD3,  PFGS_PER_VERTEX, verts, NULL );

    pfGSetAttr  ( topGset,  PFGS_NORMAL3, PFGS_PER_PRIM,   norms, NULL );

    pfGSetAttr  ( topGset,  PFGS_COLOR4,  PFGS_PER_PRIM,   color, NULL );

    pfGSetGState( topGset,  geostate );

    pfAddGSet   ( topGeode, topGset );

 

    // 

    // Add the sides and top to the object

    //  

    pfAddChild( group,   topGeode );

    pfAddChild( group,   sidesGeode );

    pfAddChild( objNode, group );

 

 

 

} // addCustomGeometryToVgObject

 
    
 

 

 

Copyright 2004 Gordon Tomlinson  All Rights Reserved.

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