home *** CD-ROM | disk | FTP | other *** search
- //-----------------------------------------------------------------------
- //
- // Inventor Barcelona Lab #2
- //
- // This program illustrates a simple Inventor program which reads
- // in an Inventor file and views it with the `Walkthrough viewer'.
- // If the `Table' is picked then play some audio...
- //
- // EXERCISE:
- // Modify the program so that picking the `Lamp' causes
- // ./sounds/lampON.aiff and ./sounds/lampOFF.aiff to play.
- // Alternate these two sounds each time.
- //
- // (Hint: you will need to create a boolean flag for the lamp status...)
- //
- // Type: make walk2
- //
- //-----------------------------------------------------------------------
-
- #include <stdio.h>
- #include <Inventor/SoDB.h>
- #include <Inventor/actions/SoSearchAction.h>
- #include <Inventor/events/SoMouseButtonEvent.h>
- #include <Inventor/nodes/SoEventCallback.h>
- #include <Inventor/nodes/SoSeparator.h>
- #include <Inventor/Xt/SoXt.h>
- #include <Inventor/Xt/viewers/SoXtWalkViewer.h>
-
-
- void tableCallback( void *, SoEventCallback *cb ) {
- // Make sure that this is a MOUSE PRESS event
- if ( !SO_MOUSE_PRESS_EVENT( cb->getEvent(), ANY )) return;
-
- system( "playaiff sounds/table.aiff &" );
- }
-
-
- void tableInit( SoSeparator *root )
- {
- // Find the `Table' node
- SoGroup *table = (SoGroup *) root->getByName( "Table" );
-
- if ( (table == NULL) || (!table->isOfType( SoGroup::getClassTypeId())) ) {
- printf( "DEBUG: `Table' not found or wrong type\n" );
- exit( 0 );
- }
-
- // Get the path to `Table'
- SoSearchAction sa;
- sa.setFind( SoSearchAction::NODE );
- sa.setNode( table );
- sa.apply( root );
- SoPath *path = sa.getPath();
-
- // Create an event callback node that calls a function if the
- // `Table' is picked.
-
- SoEventCallback *tableCB = new SoEventCallback;
- tableCB->setPath(path);
- tableCB->addEventCallback( SoMouseButtonEvent::getClassTypeId(),
- tableCallback );
- table->addChild( tableCB );
- }
-
-
- main(int argc, char **argv)
- {
- // Initialize Inventor
- Widget myWindow = SoXt::init( argv[0] );
- if (myWindow == NULL) exit(1);
-
- // Make a viewer part of the window
- SoXtWalkViewer *viewer = new SoXtWalkViewer(myWindow);
-
- // Read the object from a file
- SoInput myInput;
- if (!myInput.openFile("./scene.iv")) return(1);
- SoSeparator *scene = SoDB::readAll(&myInput);
- if (scene == NULL) {
- printf( "Error: scene.iv file read failed!\n" );
- exit(1);
- }
- scene->ref();
-
- // Search for the different objects by name
- tableInit( scene );
-
- // Viewer setup
- viewer->setSceneGraph( scene );
- viewer->setTitle( "Walkthrough Program" );
- viewer->show();
- SoXt::show(myWindow);
-
- SoXt::mainLoop();
- }
-