📄 hostdata.c
字号:
/****************************************************************************** ** hostdata.c --** Application specific functions related to handling data between* separately executing master and slave programs********************************************************************************/#include <g_sys.h>#include <string.h>#include <g_consts.h>#include <g_timer.h>#include <gvu_sge.h>#include <tkui.h>#include <tparse.h>#include <tcevents.h>#include "hostdata.h"#include "train.h"/* Globals */Dnet_host_data host_data;int host_data_length;GV_Obi vobi[NUM_VEHICLES];/* Global throughout this module */static TParse_data * tdata = NULL;static GV_Obi spotlight_cone = NULL;static GVU_Plsr spotlight = NULL;static GV_Obi owntrain = NULL;static GV_Obi ownspot = NULL;static GV_Light sunlight = NULL;/* General pointers used for opcode,data events */static int * ptr_int = NULL;static float * ptr_float = NULL;/*************************************************************************** ** slave_discrete_event --** A new discrete event has been sent from the master to the slaves.* Handle the event now.*****************************************************************************/static int slave_discrete_event( Tevent * tevent ){ int status = G_SUCCESS; if( !tevent ) return G_FAILURE; /* Error */ if( !ownspot ) { GV_obi_inq_by_name( "OWNTRAIN", &owntrain ); GV_obi_inq_by_name_relative( owntrain, ".../spotlight", &ownspot ); } if( !spotlight ) GVU_plsr_inq_by_name( TSPOTLIGHT_NAME, &spotlight ); if( spotlight_cone ) GV_obi_inq_by_name( TSPOTLIGHT_CONE_NAME, &spotlight_cone ); if( tevent->opcode >= TCEVENT_OPCODE_USER_EVENT_START ) { switch( tevent->opcode ) { default: /* Error - no handler */ break; case APP_EVENT_SEASON_CHANGE: ptr_int = (int *)&tevent->data[0]; /* Pointer to data */ if( ptr_int ) { Season newseason = (Season)*ptr_int; season_change( newseason ); #if DEBUG fprintf( stdout, "slave_discrete_event: " "season = %d\n", (int)newseason); #endif } break; case APP_EVENT_SPOTLIGHT_STATE: ptr_int = (int *)&tevent->data[0]; /* Pointer to data */ if( spotlight && ptr_int ) { G_State state = (G_State)*ptr_int; GVU_plsr_set_state( spotlight, state ); #if DEBUG fprintf( stdout, "slave_discrete_event: " "spotlight state=%d\n", (int)state ); #endif } break; case APP_EVENT_SPOTLIGHT_CONE_STATE: ptr_int = (int *)&tevent->data[0]; /* Pointer to data */ if( spotlight_cone && ptr_int ) { G_State state = (G_State)*ptr_int; GV_obi_set_state( spotlight_cone, state ); #if DEBUG { G_Position pos; GV_obi_inq_position( spotlight_cone, &pos ); fprintf( stdout, "slave_discrete_event: " "spotlight cone state=%d\n", (int)state ); fprintf( stdout, "slave_discrete_event: " "spotlight cone pos=%f,%f,%f\n", pos.x,pos.y,pos.z); } #endif } break; case APP_EVENT_SPOTLIGHT_ATTENUATION: ptr_float = (float *)&tevent->data[0]; /* Pointer to data */ if( spotlight && ptr_float ) { float atten = *ptr_float; GVU_plsr_set_attenuation( spotlight, GL_QUADRATIC_ATTENUATION, atten ); } break; case APP_EVENT_SPOTLIGHT_FALLOFF: ptr_float = (float *)&tevent->data[0]; /* Pointer to data */ if( spotlight && ptr_float ) { float falloff = *ptr_float; GVU_plsr_set_spot_exponent( spotlight, falloff ); } break; } } else { /* Common GIFTS event; go process it */ status = tcevent_process_netevent( tevent ); } return status;}/****************************************************************************** host_slave_get --** Called once per frame when new data has arrived (latest packet)******************************************************************************/int host_slave_get( GV_Obi owntrain, GV_Camera camera ){ int status = G_SUCCESS; static int discrete = -1; int vehicle; GV_Channel chnhdl; GV_Scene scene; GV_chn_inq_input_focus( &chnhdl ); GV_chn_inq_scene( chnhdl, &scene ); GV_obi_set_position_d( owntrain, &host_data.ownpos ); GV_obi_set_rotation_d( owntrain, &host_data.ownrot ); GV_obi_set_gfx_state( owntrain, host_data.ownstate ); GV_cam_set_position_d( camera, BASE, &host_data.campos ); GV_cam_set_rotation_d( camera, BASE, &host_data.camrot ); if( !sunlight ) GV_lsr_inq_by_name( "SUN", &sunlight ); GV_lsr_set_direction( sunlight, &host_data.sun_direction ); for( vehicle=0; vehicle<NUM_VEHICLES; vehicle++) { if( vobi[vehicle] ) { GV_obi_set_state( vobi[vehicle], host_data.vstate[vehicle] ); if( host_data.vstate[vehicle] == G_ON ) { GV_obi_set_position_d( vobi[vehicle], &host_data.vpos[vehicle] ); GV_obi_set_rotation_d( vobi[vehicle], &host_data.vrot[vehicle] ); } } } if (host_data.dhd_dgd.dgd_quit) { fprintf( stdout, "host_slave_get: detected shutdown of master... " "slave quitting now too\n"); GV_sys_set_mode( GV_SYS_MODE_SHUTDOWN ); GV_sys_shutdown(); exit( EXIT_SUCCESS ); } if( discrete != host_data.discrete_count ) { /* A discrete just changed things, so act upon this... */ slave_discrete_event( &host_data.event ); discrete = host_data.discrete_count; } return status;}/*************************************************************************** ** host_master_put --** Copy graphics resource data to the network packet;* this task is only intended to be called by the master*****************************************************************************/int host_master_put( GV_Obi owntrain, GV_Camera camera ){ int status = G_SUCCESS; static GV_Camera orbit_camera = NULL; int vehicle; G_timer_inq_frame_number( &host_data.frame ); G_timer_inq_time( &host_data.simtime ); G_timer_inq_frame_period_actual( &host_data.dt); GV_lsr_inq_direction( sunlight, &host_data.sun_direction ); /* The orbit camera has a callback... that is where the owntrain object and orbit camera must be updated to keep data in synch */ if( owntrain ) { GV_obi_inq_gfx_state( owntrain, &host_data.ownstate ); GV_obi_inq_position_d( owntrain, &host_data.ownpos ); GV_obi_inq_rotation_d( owntrain, &host_data.ownrot ); GV_cam_set_position_d( camera, BASE, &host_data.ownpos ); if( camera != orbit_camera ) GV_cam_set_rotation_d( camera, BASE, &host_data.ownrot ); } /* Transmit information about the "camera" object */ if( camera ) { GV_cam_inq_pos_rot_world_d(camera, GV_CAM_PLATFORM_LAST, &host_data.campos, &host_data.camrot); } /* Transmit information about each active vehicle (if any). By application convention, moving vehicles are implemented here as motion paths and they don't change (e.g., vobi[x] is always the same model definition / appearance. */ for( vehicle=0; vehicle<NUM_VEHICLES; vehicle++) { GV_Obi inst; inst = vobi[vehicle]; if( inst ) { GV_obi_inq_gfx_state( inst, &host_data.vstate[vehicle] ); if( host_data.vstate[vehicle] == G_ON ) { GV_obi_inq_position_d( inst, &host_data.vpos[vehicle] ); GV_obi_inq_rotation_d( inst, &host_data.vrot[vehicle] ); } } } return status;}/*************************************************************************** ** host_master_init --** Initialization of host data (master to slaves)*****************************************************************************/int host_master_init( GV_Obi owntrain, GV_Camera camera ){ int status = G_SUCCESS; int vehicle; (void) camera; /* Not used currently */ /* Get pointer to command line argument database */ tparse_inq_data( &tdata ); /* Clear dnet-specific header, including dgd_quit flag. */ memset( (void *) &host_data, 0, sizeof (Dnet_gen_data) ) ; GV_lsr_inq_by_name( "SUN", &sunlight ); /* For consistency checking between master and slave. */ host_data.fixed_size = offsetof(Dnet_host_data, discrete_count) ; host_data.frame = 0; /* Initial host (master) frame count*/ host_data.simtime = 0.0; /* Simulation time */ host_data.dt = 1.0/30.0; /* Start with a reasonable value */ GV_obi_inq_gfx_state( owntrain, &host_data.ownstate ); GV_obi_inq_position_d( owntrain, &host_data.ownpos ); GV_obi_inq_rotation_d( owntrain, &host_data.ownrot ); host_data_length = sizeof(host_data); host_data.discrete_count=0; /* Discrete counter for changes */ GV_lsr_inq_direction( sunlight, &host_data.sun_direction ); for( vehicle=0; vehicle<NUM_VEHICLES; vehicle++) { /* For this application, all moving objects are currently implemented with GVU_path (see path.c) */ if( vobi[vehicle] ) { GV_obi_inq_name( vobi[vehicle], host_data.vname[vehicle] ) ; GV_obi_inq_state( vobi[vehicle], &host_data.vstate[vehicle] ); GV_obi_inq_position_d( vobi[vehicle], &host_data.vpos[vehicle] ); GV_obi_inq_rotation_d( vobi[vehicle], &host_data.vrot[vehicle] ); } /* The owntrain model is driven by its own position/rotation/state within hostdata.h */ if( vobi[vehicle] == owntrain ) host_data.vstate[vehicle] = G_OFF; } return status;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -