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 >
Wrap
C/C++ Source or Header
|
1998-07-07
|
6KB
|
247 lines
/*
flock_test.cxx
*/
#include <iostream.h>
#include "v3dsys.h"
#include "v3duniv.h"
#include "v3dviewp.h"
#include "v3dobj.h"
#include "v3dlight.h"
#include "v3dmouse.h"
#include "v3dkeybd.h"
#include "v3dmotionstar.h"
#include "wt.h"
#define HALF_GRID_SIZE 100
#define AXIS_RADIUS 0.5
#define AXIS_TESS 3
void
create_grid()
{
int i;
v3dPos from_pos (0, 0, -HALF_GRID_SIZE), to_pos(0, 0, HALF_GRID_SIZE);
for (i = -HALF_GRID_SIZE; i <= HALF_GRID_SIZE; i += 10) {
from_pos.x(i);
to_pos.x(i);
v3dLineObject *line = new v3dLineObject(from_pos, to_pos,
AXIS_RADIUS, AXIS_TESS);
if (i == 0) // Color the z-axis blue.
line->SetColor(v3dColor(0, 0, 255));
else if (!(i % 50)) // Color every 50th line green.
line->SetColor(v3dColor(0, 255, 0));
}
from_pos.x(-HALF_GRID_SIZE);
to_pos.x(HALF_GRID_SIZE);
for (i = -HALF_GRID_SIZE; i <= HALF_GRID_SIZE; i += 10) {
from_pos.z(i);
to_pos.z(i);
v3dLineObject *line = new v3dLineObject(from_pos, to_pos,
AXIS_RADIUS, AXIS_TESS);
if (i == 0) // Color the x-axis red.
line->SetColor(v3dColor(255, 0, 0));
else if (!(i % 50)) // Color every 50th line green.
line->SetColor(v3dColor(0, 255, 0));
}
}
v3dObject *
create_bird_axes()
{
float thickness = AXIS_RADIUS * 2;
v3dBlockObject *origin = new v3dBlockObject(1, 1, 1);
v3dBlockObject *x_axis = new v3dBlockObject(10, thickness, thickness);
x_axis->MoveAbsolute(v3dDir(5, 0, 0));
x_axis->SetColor(v3dColor(255, 0, 0));
origin->Add(x_axis);
v3dBlockObject *y_axis = new v3dBlockObject(thickness, 10, thickness);
y_axis->MoveAbsolute(v3dDir(0, 5, 0));
y_axis->SetColor(v3dColor(0, 255, 0));
origin->Add(y_axis);
v3dBlockObject *z_axis = new v3dBlockObject(thickness, thickness, 10);
z_axis->MoveAbsolute(v3dDir(0, 0, 5));
z_axis->SetColor(v3dColor(0, 0, 255));
origin->Add(z_axis);
return origin;
}
// Preset directions of the viewpoints
v3dDir preset_dir[7] =
{
v3dDir(0.0, 0.0, 1.0),
v3dDir(0.0, 1.0, 0.0), v3dDir( 0.0, -1.0, 0.0),
v3dDir(1.0, 0.0, 0.0), v3dDir(-1.0, 0.0, 0.0),
v3dDir(0.0, 0.0, 1.0), v3dDir( 0.0, 0.0, -1.0)
};
v3dOrient preset_orient[7];
v3dPos preset_pos[7];
void setup_viewpoint(void)
{
v3dViewPoint *viewpoint =
v3dSystem::GetCurrentUniverse()->GetCurrentViewPoint();
preset_pos[0] = viewpoint->GetPosition();
preset_orient[0] = viewpoint->GetOrientation();
// Setup viewpoint presets
for (int i = 1; i < 7; i++) {
viewpoint->SetDirection(preset_dir[i]);
viewpoint->ZoomAll();
preset_pos[i] = viewpoint->GetPosition();
preset_orient[i] = viewpoint->GetOrientation();
viewpoint->MoveTo(preset_pos[0], preset_orient[0]);
}
}
v3dEventHandlerCallback key_press_handler;
v3dMotionStar *motion_star;
int num_birds = 1;
int
main(int argc, char *argv[])
{
// Parse the command line options.
char *client_IP_address = 0;
bool make_grid = true;
int initial_value_count = 0;
double measurement_rate = 100;
for (int i = 1; i < (argc - 1); i += 2) {
// Set client's IP address.
if (strcmp(argv[i], "-i") == 0) {
client_IP_address = argv[i+i];
}
// Set the number of bird sensors to use.
if (strcmp(argv[i], "-n") == 0) {
num_birds = atoi(argv[i+1]);
}
// Set the measurement rate.
if (strcmp(argv[i], "-m") == 0) {
measurement_rate = atoi(argv[i+1]);
}
// Turn off the grid.
if (strcmp(argv[i], "-g") == 0) {
make_grid = false;
}
// Use initial (i.e., relative) tracker values.
if (strcmp(argv[i], "-r") == 0) {
initial_value_count = atoi(argv[i+1]);
}
}
if (client_IP_address == 0)
client_IP_address = strdup("128.95.74.207");
// #
// #1 Initialization of the system and setting up the Universe
// #
cerr << "Initializing the system ..." << endl;
// v3dSystem *system = v3dSystem::StartUp(Mono);
v3dSystem *system = v3dSystem::StartUp(Stereo_2_Windows);
v3dUniverse *universe = system->GetCurrentUniverse();
// Set initial background colour to black, set ambient light to 0.5
universe->SetBgColor(0);
universe->SetAmbient(0.5);
v3dPos light_pos = v3dVector (1200, -800, -1000);
v3dDir light_dir = v3dVector ( -.68, .48, .58);
cerr << "Loading lights ... " << endl;
v3dLight *light = new v3dLight(light_pos, light_dir, 1.0);
WTwindow *wleft = WTuniverse_getwindows();
WTwindow *wright = WTwindow_next(wleft);
WTwindow_setposition(wleft,1,1,640,480);
WTwindow_setposition(wright,642,1,640,480);
v3dSystem::GetInstance()->RegisterEventSender (universe);
// Create the grid objects.
if (make_grid)
create_grid();
// Initialize the MotionStar.
motion_star = new v3dMotionStar(client_IP_address,
"128.95.74.211", num_birds, measurement_rate,
initial_value_count);
// Add bird objects.
for (int j = 0; j < num_birds; ++j) {
v3dObject *bird_obj = create_bird_axes();
motion_star->GetBird(j+1)->AttachObject(bird_obj);
}
v3dMouse mouse;
WTviewpoint_addsensor (WTuniverse_getviewpoint(),
(WTsensor *)mouse.GetImplement()->GetSensorHandler());
v3dKeyboard keyboard;
v3dAutoEventReceiver keyboard_receiver (&keyboard, KEYBOARD_KEY_PRESS,
&key_press_handler, 0);
// Set up the user's viewpoints.
setup_viewpoint();
// Enter main loop
cerr << "Starting an application!" << endl;
system->Go();
return 0;
}
void key_press_handler(const v3dEventMessage *message)
{
int key = *(KEY_TYPE *) message->GetCallData();
fprintf (stderr, "Key: %c\n", key);
v3dUniverse *universe = v3dSystem::GetCurrentUniverse();
v3dViewPoint *viewpoint = universe->GetCurrentViewPoint();
switch (key) {
case '0':
viewpoint->DetachSensor();
break;
case '1': case '2': case '3':
case '4': case '5': case '6':
case '7': case '8':
if (key - '0' <= num_birds) {
v3dBird *the_bird = motion_star->GetBird(key - '0');
viewpoint->MoveAbsolute(the_bird->GetAbsolutePosition());
viewpoint->SetAbsoluteOrientation(the_bird->GetAbsoluteOrientation());
viewpoint->DetachSensor();
viewpoint->AttachSensor(*the_bird);
}
break;
case 'a':
case 'b': case 'c': case 'd':
case 'e': case 'f': case 'g':
cerr << preset_pos[key - 'a'] << endl;
viewpoint->MoveTo(preset_pos[key - 'a'],
preset_orient[key - 'a']);
break;
case 'q':
exit(1);
break;
}
}