/******************************************************************************* // // kishan@hackorama.com http://www.hackorama.com Mon Nov 1 19:17:26 PST 1999 // // g++ hellocube.cc -lpf -o hellocube // *******************************************************************************/ #include #include #include #include #include #include #include #include #include #include #include #include #include static pfNode *makecube(); static void mydraw( pfChannel *chan, void *data ); main (int argc, char *argv[]) { pfSphere bsphere; pfVec3 view_xyz, view_hpr; /* Initialise Performer processes and configs */ pfInit(); pfMultiprocess( PFMP_DEFAULT ); pfConfig(); /* Craete a Pipe and a pipewindow */ pfPipe *mypipe = pfGetPipe(0); pfPipeWindow *pipewin = new pfPipeWindow(mypipe); pipewin->setWinType(PFPWIN_TYPE_X); pipewin->setName("hello cube" ); pipewin->setOriginSize(0,0,512,512); pipewin->open(); /* Build the cube node */ pfNode *cube_node = makecube(); /* Create a Scene and add the cube node to it */ pfScene *myscene = new pfScene; /* before adding cube create a Dynamic Coordinate System */ pfDCS *dcs = new pfDCS; /* now add cube to the DCS so that it can rotate */ dcs->addChild( cube_node ); /* add the DCS to the scene */ myscene->addChild(dcs); /* light up the scene */ myscene->addChild(new pfLightSource); /* Create a Channel on the pipe and attach the myscene to it */ pfChannel *mychan = new pfChannel(mypipe); mychan->setScene(myscene); /* Set the Camera or Viewpoint x y z h p r and Field Of View */ mychan->setFOV(45.0f, 0.0f); dcs->getBound(&bsphere); mychan->setNearFar(1.0f, bsphere.radius * 10.0f ); view_xyz.set( bsphere.center[0], bsphere.center[1] - (bsphere.radius * 4) , bsphere.center[2] ); view_hpr.set( 0.0, 0.0, 0.0 ); mychan->setView(view_xyz, view_hpr); mychan->setTravFunc( PFTRAV_DRAW, mydraw ); mychan->setViewport( 0.0, 1.0, 0.0, 1.0 ); /* Run the simulation loop */ while(1) { static float angle = 0.0; angle++; pfSync(); /* Add rotation to the dcs which will rotate the cube */ dcs->setRot( angle/4, angle/4, 0.0 ); pfFrame(); } /* since its an infinite loop above */ /* there is no proper exit, please ctrl+c out */ /* in actual case you should check keyboard events */ /* or some thing else to exit gracefully :) */ /* exit(0); */ } /* the draw function */ /* just clear the scene and redraw it */ static void mydraw( pfChannel *chan, void *data ) { chan->clear(); pfDraw(); } /* Creating the cube node */ /* */ /* Creates a gemetry set from a set of vertices */ /* the geometry set is attched to a geometry node */ /* which is returned as pfNode */ static pfNode *makecube( ) { pfGeode *gnode; pfGeoSet *gset; pfGeoState *gstate; int i=0, j=0, c=0; /* get pointer to shared arena for mem allocation */ void *arena = pfGetSharedArena(); /* define the vertices */ float points[][3] = { { -1., -1., -1. }, { 1., -1., -1. }, { 1., 1., -1. }, { -1., 1., -1. }, { -1., -1., 1. }, { 1., -1., 1. }, { 1., 1., 1. }, { -1., 1., 1. } }; /* define the polygons connectivity of vertices */ int polygons[][4] = { { 3, 2, 1, 0 }, { 2, 6, 5, 1 }, { 6, 7, 4, 5 }, { 7, 3, 0, 4 }, { 7, 6, 2, 3 }, { 4, 0, 1, 5 } }; /* define arrays for coordinates and an index array into coordinates array */ pfVec3 *coords = (pfVec3 *)pfMalloc( 8 * sizeof( pfVec3 ), arena ); ushort *index = (ushort *)pfMalloc( 24 * sizeof( ushort ), arena ); /* store the points in the coordinate array */ for( i = 0; i < 8; i++ ) { coords[i].set( points[i][0], points[i][1], points[i][2] ); } /* store the connectivity of polygons into index */ c = 0; for( i = 0; i < 6; i++ ) { for( j = 0; j < 4; j++ ) { index[c] = polygons[i][j]; c++; } } /* Craete a Geometry Set and set attributes */ gset = new pfGeoSet( ); gset->setAttr( PFGS_COORD3, PFGS_PER_VERTEX, coords, index); /* Set primitive type as quad poly */ gset->setPrimType( PFGS_QUADS); gset->setNumPrims( 6 ); /* Set the rendering mode as wireframe */ gstate = new pfGeoState; gstate->setMode( PFSTATE_ENWIREFRAME, 1 ); /* Create a geometry node and attch the geometry set to it */ gset->setGState( gstate ); gnode = new pfGeode; gnode->addGSet( gset ); return (pfNode *)gnode; }