home *** CD-ROM | disk | FTP | other *** search
/ ftp.hitl.washington.edu / ftp.hitl.washington.edu.tar / ftp.hitl.washington.edu / pub / people / peter / ER / flock_test.cxx < prev    next >
C/C++ Source or Header  |  1998-07-07  |  6KB  |  247 lines

  1. /*
  2.  flock_test.cxx
  3. */
  4.  
  5. #include <iostream.h>
  6.  
  7. #include "v3dsys.h"
  8. #include "v3duniv.h"
  9. #include "v3dviewp.h"
  10. #include "v3dobj.h"
  11.  
  12. #include "v3dlight.h"
  13. #include "v3dmouse.h"
  14. #include "v3dkeybd.h"
  15.  
  16. #include "v3dmotionstar.h"
  17.  
  18. #include "wt.h"
  19.  
  20. #define HALF_GRID_SIZE    100
  21. #define    AXIS_RADIUS    0.5
  22. #define    AXIS_TESS    3
  23.  
  24. void
  25. create_grid()
  26. {
  27.     int i;
  28.     v3dPos from_pos (0, 0, -HALF_GRID_SIZE), to_pos(0, 0, HALF_GRID_SIZE);
  29.     for (i = -HALF_GRID_SIZE; i <= HALF_GRID_SIZE; i += 10) {
  30.         from_pos.x(i);
  31.         to_pos.x(i);
  32.         v3dLineObject *line = new v3dLineObject(from_pos, to_pos,
  33.             AXIS_RADIUS, AXIS_TESS);
  34.         if (i == 0)    // Color the z-axis blue.
  35.             line->SetColor(v3dColor(0, 0, 255));
  36.         else if (!(i % 50))    // Color every 50th line green.
  37.             line->SetColor(v3dColor(0, 255, 0));
  38.     }
  39.     from_pos.x(-HALF_GRID_SIZE);
  40.     to_pos.x(HALF_GRID_SIZE);
  41.     for (i = -HALF_GRID_SIZE; i <= HALF_GRID_SIZE; i += 10) {
  42.         from_pos.z(i);
  43.         to_pos.z(i);
  44.         v3dLineObject *line = new v3dLineObject(from_pos, to_pos,
  45.             AXIS_RADIUS, AXIS_TESS);
  46.         if (i == 0)    // Color the x-axis red.
  47.             line->SetColor(v3dColor(255, 0, 0));
  48.         else if (!(i % 50))    // Color every 50th line green.
  49.             line->SetColor(v3dColor(0, 255, 0));
  50.     }
  51. }
  52.  
  53. v3dObject *
  54. create_bird_axes()
  55. {
  56.     float thickness = AXIS_RADIUS * 2;
  57.     v3dBlockObject *origin = new v3dBlockObject(1, 1, 1);
  58.     v3dBlockObject *x_axis = new v3dBlockObject(10, thickness, thickness);
  59.     x_axis->MoveAbsolute(v3dDir(5, 0, 0));
  60.     x_axis->SetColor(v3dColor(255, 0, 0));
  61.     origin->Add(x_axis);
  62.  
  63.     v3dBlockObject *y_axis = new v3dBlockObject(thickness, 10, thickness);
  64.     y_axis->MoveAbsolute(v3dDir(0, 5, 0));
  65.     y_axis->SetColor(v3dColor(0, 255, 0));
  66.     origin->Add(y_axis);
  67.  
  68.     v3dBlockObject *z_axis = new v3dBlockObject(thickness, thickness, 10);
  69.     z_axis->MoveAbsolute(v3dDir(0, 0, 5));
  70.     z_axis->SetColor(v3dColor(0, 0, 255));
  71.     origin->Add(z_axis);
  72.  
  73.     return origin;
  74. }
  75.  
  76. // Preset directions of the viewpoints
  77. v3dDir preset_dir[7] =
  78.     {
  79.         v3dDir(0.0, 0.0, 1.0),
  80.         v3dDir(0.0, 1.0, 0.0), v3dDir( 0.0, -1.0,  0.0),
  81.         v3dDir(1.0, 0.0, 0.0), v3dDir(-1.0,  0.0,  0.0),
  82.         v3dDir(0.0, 0.0, 1.0), v3dDir( 0.0,  0.0, -1.0)
  83.     };
  84. v3dOrient preset_orient[7];
  85. v3dPos preset_pos[7];
  86.  
  87. void setup_viewpoint(void)
  88. {
  89.     v3dViewPoint *viewpoint =
  90.         v3dSystem::GetCurrentUniverse()->GetCurrentViewPoint();
  91.  
  92.     preset_pos[0] = viewpoint->GetPosition();
  93.     preset_orient[0] = viewpoint->GetOrientation();
  94.  
  95.     // Setup viewpoint presets
  96.     for (int i = 1; i < 7; i++) {
  97.         viewpoint->SetDirection(preset_dir[i]);
  98.         viewpoint->ZoomAll();
  99.         preset_pos[i] = viewpoint->GetPosition();
  100.  
  101.         preset_orient[i] = viewpoint->GetOrientation();
  102.         viewpoint->MoveTo(preset_pos[0], preset_orient[0]);
  103.     }
  104. }
  105.  
  106. v3dEventHandlerCallback key_press_handler;
  107. v3dMotionStar *motion_star;
  108. int num_birds = 1;
  109.  
  110. int
  111. main(int argc, char *argv[])
  112. {
  113.  
  114.     // Parse the command line options.
  115.     char *client_IP_address = 0;
  116.     bool make_grid = true;
  117.     int initial_value_count = 0;
  118.     double measurement_rate = 100;
  119.     for (int i = 1; i < (argc - 1); i += 2) {
  120.         // Set client's IP address.
  121.         if (strcmp(argv[i], "-i") == 0) {
  122.             client_IP_address = argv[i+i];
  123.         }
  124.  
  125.         // Set the number of bird sensors to use.
  126.         if (strcmp(argv[i], "-n") == 0) {
  127.             num_birds = atoi(argv[i+1]);
  128.         }
  129.  
  130.         // Set the measurement rate.
  131.         if (strcmp(argv[i], "-m") == 0) {
  132.             measurement_rate = atoi(argv[i+1]);
  133.         }
  134.         // Turn off the grid.
  135.         if (strcmp(argv[i], "-g") == 0) {
  136.             make_grid = false;
  137.         }
  138.         // Use initial (i.e., relative) tracker values.
  139.         if (strcmp(argv[i], "-r") == 0) {
  140.             initial_value_count = atoi(argv[i+1]);
  141.         }
  142.     }
  143.     if (client_IP_address == 0)
  144.         client_IP_address = strdup("128.95.74.207");
  145.  
  146.  
  147.     // #
  148.     // #1 Initialization of the system and setting up the Universe
  149.     // #
  150.  
  151.     cerr << "Initializing the system ..." << endl;
  152. //    v3dSystem *system = v3dSystem::StartUp(Mono);
  153.     v3dSystem *system = v3dSystem::StartUp(Stereo_2_Windows);
  154.     v3dUniverse *universe = system->GetCurrentUniverse();
  155.  
  156.     // Set initial background colour to black,  set ambient light to 0.5
  157.     universe->SetBgColor(0);
  158.     universe->SetAmbient(0.5);
  159.  
  160.     v3dPos light_pos = v3dVector (1200, -800, -1000);
  161.     v3dDir light_dir = v3dVector ( -.68, .48, .58);
  162.  
  163.     cerr << "Loading lights ... " << endl;
  164.     v3dLight *light = new v3dLight(light_pos, light_dir, 1.0);
  165.  
  166.     WTwindow *wleft = WTuniverse_getwindows();
  167.     WTwindow *wright = WTwindow_next(wleft);
  168.  
  169.     WTwindow_setposition(wleft,1,1,640,480);
  170.     WTwindow_setposition(wright,642,1,640,480);
  171.  
  172.     v3dSystem::GetInstance()->RegisterEventSender (universe);
  173.  
  174.     // Create the grid objects.
  175.     if (make_grid)
  176.         create_grid();
  177.  
  178.     // Initialize the MotionStar.
  179.     motion_star = new v3dMotionStar(client_IP_address,
  180.             "128.95.74.211", num_birds, measurement_rate,
  181.             initial_value_count);
  182.  
  183.     // Add bird objects.
  184.     for (int j = 0; j < num_birds; ++j) {
  185.         v3dObject *bird_obj = create_bird_axes();
  186.         motion_star->GetBird(j+1)->AttachObject(bird_obj);
  187.     }
  188.  
  189.     v3dMouse mouse;
  190.     WTviewpoint_addsensor (WTuniverse_getviewpoint(),
  191.     (WTsensor *)mouse.GetImplement()->GetSensorHandler());
  192.  
  193.     v3dKeyboard keyboard;
  194.     v3dAutoEventReceiver keyboard_receiver (&keyboard, KEYBOARD_KEY_PRESS,
  195.                         &key_press_handler, 0);
  196.  
  197.     // Set up the user's viewpoints.
  198.     setup_viewpoint();
  199.  
  200.     // Enter main loop 
  201.     cerr << "Starting an application!" << endl;
  202.     system->Go();
  203.  
  204.     return 0;
  205. }
  206.  
  207. void key_press_handler(const v3dEventMessage *message)
  208. {
  209.     int  key = *(KEY_TYPE *) message->GetCallData();
  210. fprintf (stderr, "Key: %c\n", key);
  211.  
  212.     v3dUniverse *universe = v3dSystem::GetCurrentUniverse();
  213.     v3dViewPoint *viewpoint = universe->GetCurrentViewPoint();
  214.  
  215.     switch (key) {
  216.         case '0':
  217.             viewpoint->DetachSensor();
  218.             break;
  219.         case '1': case '2': case '3':
  220.         case '4': case '5': case '6':
  221.         case '7': case '8':
  222.             if (key - '0' <= num_birds) {
  223.                 v3dBird *the_bird = motion_star->GetBird(key - '0');
  224.                 viewpoint->MoveAbsolute(the_bird->GetAbsolutePosition());
  225.                 viewpoint->SetAbsoluteOrientation(the_bird->GetAbsoluteOrientation());
  226.                 viewpoint->DetachSensor();
  227.                 viewpoint->AttachSensor(*the_bird);
  228.             }
  229.             break;
  230.  
  231.         case 'a':
  232.         case 'b': case 'c': case 'd':
  233.         case 'e': case 'f': case 'g':
  234. cerr << preset_pos[key - 'a'] << endl;
  235.             viewpoint->MoveTo(preset_pos[key - 'a'],
  236.                 preset_orient[key - 'a']);
  237.         break;
  238.  
  239.         case 'q':
  240.             exit(1);
  241.         break;
  242.  
  243.     }
  244. }
  245.  
  246.  
  247.