How to create  Opengl Geometry based Vega Objects

 

Vega Code Example

 

 

 

 

 

 
 

This example how to create a vgDataSet and then create a vgObject from the vgDataSet and how to then use Opengl to draw geometry when the vgObject is added to the vgScene.

 

This example further  illustrates how to :

  • How to use Opengl with a vgObject
  • Create your own vgObject in code
  • Create your vgDataSet from Performer geometry
  • How to create Pre-draw Call-back for a pfNode
  • How to pfNode traversal mask
  • How to set call-back function on a pfNode
  • How to retrieve the Performer root node from a vgObject
  • How to use vgGetSharedArena
  • How to use vgName
  • How to use vgDelete
  • How to create  a pfNode
  • How to create a pfGroup
  • How to use pfAddChild
  • How to save the Performer state
  • How to save the Performer matrices
  • How to restore  the Performer state
  • How to restore the Performer matrices
  • How to disable the Z-buffer testing
  • How to enable the Z-buffer testing
  • How to disable the Performer lighting
  • How to enable Blending

 

 
   
 

 

 
 
 

 

#include "vg.h"          // for the standard Vega classes and defines

#include "vgperf.h"      // for the Vega Performer node functions

#include "pf.h"          // for the standard Performer classes

#include "pfutil.h"      // for the Performer util functions  

#include <GL/gl.h>       // for Opengl functions

 

//

// Declare the Node Draw call back function that will be the

// container from were the OpenGL commands are run

// note this has to be static function

//

 

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

 

vgObject*

createAnOpenGlbasedvgObject(){

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

// #

// # Public function

// #

// #    Create the new vgObject that will be our be the    

// # container for the Opengl code this allows us to easily

// # add and remove from the  scene and to get a local

// # positionable coordinate system        

// #

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

vgObject   *oglObj  = NULL;

vgDataSet  *dataset = NULL;

pfGroup    *group   = NULL;

pfNode     *node    = NULL;

    

    

    //

    // 1st create an empty dataset  

    //

    dataset = vgNewDS();

    if( dataset == NULL )

        return NULL;

    

    vgName( dataset, "_oglDataSet" );

    

    //

    // Then we need a group node for the dataset

    //

    group = pfNewGroup();

    pfNodeName( group, "_oglGroup" );

    if( dataset == NULL ){

        vgDelete( dataset );

        return NULL;

        }

    

    //

    // Set the draw and the cull masks so that the node  

    // will get drawn and not culled we could also add

    // a bounding sphere but this works just as well

    //

    pfNodeTravMask( group, PFTRAV_DRAW, 0xFFFFFFFF, PFTRAV_SELF, PF_SET);

    pfNodeTravMask( group, PFTRAV_CULL, 0x00000000, PFTRAV_SELF, PF_SET);

    

    //

    // Now create the dataset using the supplied geometry

    //

    if ( vgMakeDS( dataset, group, VGDS_GEOMETRY ) == VG_SUCCESS) {

        

        //

        // * Create a new Dynamic object

        //

        oglObj = vgNewObj();

        vgName   ( oglObj, "_oglObj" );

        vgProp   ( oglObj, VGOBJ_CS, VGOBJ_DYNAMIC );

        vgObjDS  ( oglObj, dataset );

        vgMakeObj( oglObj, VGOBJ_USE );

        vgUpdate ( oglObj);

    }

    

    //

    // Dataset was not created so clean up and bail

    //

    else{

        vgDelete(dataset);

        pfUnrefDelete(group);

        return NULL;

    }

    

    //

    // Sanity check set the draw and cull masks

    //

    node = (pfNode*)vgGetObjPfNode( oglObj );

    pfNodeTravMask( node, PFTRAV_DRAW, 0xFFFFFFFF, PFTRAV_SELF, PF_SET);

    pfNodeTravMask( node, PFTRAV_CULL, 0x00000000, PFTRAV_SELF, PF_SET);

    

    //

    // Add the Pre call-back to the node ( no Post Draw)

    //

    pfNodeTravFuncs( node, PFTRAV_DRAW, oglGeometryDraw_CB, NULL);

    pfNodeTravData ( node, PFTRAV_DRAW, NULL );

    

    

//

// Return our completed vgObject

//

return oglObj;     

 

} // createAnOpenGlbasedvgObject

 

 

 

int

oglGeometryDraw_CB(pfTraverser *trav, void *data ){

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

// #

// # Static Public function

// #

// #    Node callback function, in this case used to

// # setup the Opengl State machine and to draw some

// # opengl Primitives

// #

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

int           trans    = 0;

int           pflight  = 0;

int           pftex    = 0;

GLboolean     cullface = false;

GLboolean     blend    = false;

GLint         depth    = 0;

    

    //

    // Get and Save our current Openl states we change

    //

    glGetBooleanv( GL_BLEND ,     &blend   );

    glGetBooleanv( GL_CULL_FACE,  &cullface);

    glGetIntegerv( GL_DEPTH_FUNC, &depth   );

    trans = pfGetTransparency();

    

    //

    // Save the current Performer states

    //

    pfPushState();

    pfBasicState();

    pfPushMatrix();

    

    //

    //  Disable the states we do not want

    //  while we draw the grids            

    //

    trans = pfGetTransparency();

    pfTransparency( PFTR_FAST );

    

    glEnable   ( GL_BLEND     );      

    glDepthFunc( GL_ALWAYS    );

    glDisable  ( GL_CULL_FACE );

    

    //

    // For this example  we need to disable lighting while drawing as

    // we dont want light affecting this geom as we have no normals

    //

    pflight = pfGetEnable(PFEN_LIGHTING);

    pfDisable( PFEN_LIGHTING );

    

    

    //

    // Ensure we get no unwanted texture leaking in

    //

    pftex = pfGetEnable(PFEN_LIGHTING);

    pfDisable( PFEN_TEXTURE  );

    

    //

    //  We always want to overlay this geometry

    //  so we need to disable Z buffer testing

    //

    glDisable( GL_DEPTH_TEST );

    

    

    //

    //  Set the color of the polygon

    //

    glColor3f( 1.0f,  0.2f, 0.0f);  

    

    

    //

    // Draw our single quad that is our poly

    //

    glBegin( GL_QUADS );

    

        glVertex3f( -10.0f ,-10.0f, 3.0f );

        glVertex3f(  10.0f ,-10.0f, 3.0f );      

        glVertex3f(  10.0f , 10.0f, 3.0f );     

        glVertex3f( -10.0f , 10.0f, 3.0f );

    

    glEnd( );

    

        

    //

    // enable lighting & reset transparency and culling

    //

    if(pflight)  

       pfEnable( PFEN_LIGHTING );

 

    if(pftex)  

       pfEnable( PFEN_TEXTURE   );

    

    

    //

    // Restore the previous Depth function and Cull

    //

    glDepthFunc (depth);

    

    if( cullface == GL_TRUE )

        glEnable( GL_CULL_FACE );

    

    

    //

    // Reset the transparency state

    //

    pfTransparency( trans );    

    

    //

    // Re-enable depth testing now were finished

    //

    glEnable( GL_DEPTH_TEST );

    

    //

    //  Restore the Previous/entry states  and matirces

    //

    pfPopState ();  

    pfPopMatrix();

    

//

// We all ways need to retrun contiune

//

return PFTRAV_CONT;  

 

} // oglGeometryDraw_CB

 

 

 

Copyright 2004 Gordon Tomlinson  All Rights Reserved.

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

 

 

 

 

 

  .