我要发帖 回复

中级会员

10

主题

106

积分

0

专家分

兴趣点(最多三项):

视景仿真

私信
发表时间 : 2012-9-20 11:19:20 | 浏览 : 971    评论 : 1
#include<vg.h>

vgIsector *make_los_isector( vgScene *scene, unsigned int isclass,
                             float minz, float maxz, float range );
int compute_los( vgIsector *isector, vgPosition *pos,float *result);
//int compute_los( vgIsector *isector, vgPosition *pos);
                                //, float *result );

vgIsector *make_z_isector( vgScene *scene, unsigned int isclass,
                           float minz, float maxz );
float compute_z( vgIsector *zisector, float x, float y );
void main()
{
          vgObject *obj;
        vgDataSet *ds;
        vgObserver *obs;
        vgMotion *mot;
        vgPlayer *plyr;
    vgIsector  *isector;//float *result
        vgPosition *pos_car1,*pos1,*pos2;
    vgIsector  *z_isector;
     float      *los;
     float      zobs;
     float      dx, dy;
     float      length;
     float      hx, hy;
     float      cs, sn;
     vgPosition *pos;
     vgPosition *obs_pos;
     float  x, y, z, h, p, r;


    vgPosition *method_pos;
    method_pos=vgNewPos();
        vgInitSys();
        vgDefineSys("town.adf");
        vgConfigSys();
     
        ds=vgNewDS();
        vgName(ds,"esprit.flt");
        vgLoadDS(ds);
        obj=vgNewObj();
        vgName(obj,"car_1");
    vgProp(obj,VGOBJ_CS,VGOBJ_DYNAMIC);
        vgProp(obj,VGOBJ_SCALE,4);
    pos_car1=vgNewPos();
        vgPosVec(pos_car1,0,0,0,90,0,0);
        vgPos(obj,pos_car1);
        vgObjDS(obj,ds);
    vgMakeObj( obj, VGOBJ_USE );
        vgAddSceneObj(vgGetScene(0),obj);

   plyr=vgNewPlyr();
   vgName(plyr,"player_car");
   vgProp(plyr,VGPLYR_CSREF,VGPLYR_ABSOLUTE);//relative to the world
   vgProp(plyr,VGPLYR_MOTION,VG_ON);//player motion model
   vgProp(plyr,VGCOMMON_ENABLED, VG_ON );//player state
   vgProp( plyr, VGPLYR_TYPE, VG_GROUND );// type of player
   vgAddPlyrObj( plyr, obj);

   mot=vgNewMot();
   vgProp(mot,VGCOMMON_ENABLED,VG_ON);
   vgProp(mot, VGMOT_MODEL, VGMOT_DRIVE);
   pos1=vgNewPos();
   vgPosVec(pos1,2500,2500,0,0,0,0);
   vgPos(mot,pos1);
   vgPlyrMot(plyr,mot);
      
   obs=vgGetObserv(0);
   vgProp(obs,VGOBS_STATE,VG_ON);
   vgProp(obs,VGOBS_TETHERSTATE,VGOBS_FOLLOW);
   vgObservTetherOffset(obs, 0, -35, 9);
   vgProp(obs,VGOBS_TETHERCOORD,VGOBS_TPLAYER);
   vgObservPlyr(obs, plyr );

     obs = vgGetObserv( 0 ); /* must be at least one observer defined    */
     pos = vgNewPos();       /* create a position instance for later use */
     obs_pos = vgNewPos();   /* create a position instance for observer  */
     dx = 2.5f;
     dy = 2.5f;
          h = fatan2( dy, dx ) * 57.295779513082323 - 90.0f;/* Compute heading in degrees from dy, dx */

         length = fsqrt( dx*dx + dy*dy );  /* Compute length of dx, dy vector */

         isector = make_los_isector( vgGetScene( 0 ), VGIS_TERRALL,
                                -1.0f, 1.0f, 20.0f * length ); /* Create the los isector */
        z_isector = make_z_isector( vgGetScene( 0 ), VGIS_TERRALL,
                                 0.0f, 0.0f );  /* Create the z isector for the observer */

     /* Set the Initial position for isector */
     /* Isector is above observer.  x,y,z define the los starting point.
          h, p, r define the los direction  */
    vgPosVec( pos, 500.0f, 500.0f, 50.0f, 0.0f, 0.0f, 0.0f );
     /* Set the initial position for observer */
     /* Move position along line (h+90), but look at h-90) */
     /* compute vector components of los range in direction of heading */
     vgGetSinCos( h, &sn, &cs );
     hx = -sn * (length + 20.0f);
     hy =  cs * (length + 20.0f);
     vgPosVec( obs_pos, 500.0f - hy, 500.0f + hx, 0.0f, h-90.0f, 0.0f, 0.0f );  
     los = (float *) vgMalloc( sizeof (float)*4, vgGetSharedArena() );

  vgAddPlyrIsect( plyr,isector );
  vgAddPlyrIsect( plyr, z_isector );
while(1)
  {
   vgSyncFrame();
   vgFrame();
   vgGetPosVec( obs_pos, &x, &y, &z, &h, &p, &r );  
          /* Compute new position for observer */
          x += dx; y += dy;  
          /* Use z isector to compute terrain elevation for observer,
             add 3.0 so that the eye is above the ground            */
          zobs = compute_z( z_isector, x, y ) +10.0f;  
          /* Position the observer */
          vgPosVec( obs_pos, x, y, zobs, h, p, r );
          vgPos( obs, obs_pos );  
          /* Get the current vector values for the isector position */
          vgGetPosVec( pos, &x, &y, &z, &h, &p, &r );   
          /* Compute new x,y position for the isector */
          x += dx; y += dy;
          vgPosVec( pos, x, y, z, h, p, r );  
          /* Set the los Isector position and compute results */
      
        compute_los( isector, pos,los);
                                                                         }

   vgFree( los );

}
vgIsector *make_los_isector( vgScene *scene, unsigned int isclass,
                             float minz, float maxz, float range )
{
     vgIsector *isector;
     isector = vgNewIsect();
     vgIsectTarget( isector, scene );
     vgIsectClass( isector, isclass );
     vgProp( isector,VGCOMMON_ENABLED,VG_ON);        
     vgProp( isector, VGIS_METHOD, VGIS_LOS );
     vgProp( isector, VGIS_MINZ, minz );          /* min elevation */
     vgProp( isector, VGIS_MAXZ, maxz );          /* max elevation */
     vgProp( isector, VGIS_LOS_RANGE, range );
     vgProp( isector, VGIS_RENDER, VG_ON );
     return( isector );
}


int compute_los( vgIsector *isector, vgPosition *pos, float *result )
{
     int     status;
     /* Position the isector, x,y,z, h,p,r are all used! */
     vgPos( isector, pos );

     /* Calculate the intersection and store results */
     vgUpdate( isector );

     /* Query the Isector for the results */
     status = vgGetIsectResult( isector, VGIS_GETLOS,result );

     /* The number of results in result buffer is returned. */
if (status <1 )
{

              printf("no  los intersection found is %f\n",status);
}
else{
          printf(" los hhahha intersection found is %f\n",status);
          
}
return (*result);
}

vgIsector *make_z_isector( vgScene *scene, unsigned int isclass,
                           float minz, float maxz )
{
     vgIsector *zisector;

     zisector = vgNewIsect();
     vgIsectTarget( zisector, scene );
     vgIsectClass( zisector, isclass );
     vgProp( zisector, VGIS_METHOD, VGIS_Z );
     vgProp( zisector, VGIS_MINZ, minz );          /* min elevation */
     vgProp( zisector, VGIS_MAXZ, maxz );          /* max elevation */
      vgProp( zisector, VGIS_RENDER, VG_ON );
     return( zisector );
}

float compute_z( vgIsector *zisector, float x, float y )
{
     static vgPosition   *pos = NULL;
     int                 status;
     float               result;

     if ( !pos )
          pos = vgNewPos();

     /* Only care about x, y position */
     /* z,h,p,r can be passed in, but will be ignored because of method */
     vgPosVec( pos, x, y, 0.0f, 0.0f, 0.0f, 0.0f );

     /* Position the isector */
     vgPos( zisector, pos );

     /* Calculate the intersection and store results */
     vgUpdate( zisector );

     /* Query the Isector for the results */
     status = vgGetIsectResult( zisector, VGIS_GETZ, &result );

     /* The number of results in result buffer is returned. */
     if ( status < 1 )
     {
          printf( "No Z intersection found\n" );
          result = -1.0;
     }

     return( result );
}  

最近VR访客

lwd8805 评论于2012-9-20 16:20:08
真没人帮忙啊

手机版|VR开发网 统计 津ICP备18009691号
网安备12019202000257

GMT+8, 2021-7-28 06:32 PM

返回顶部