CG橙子精彩导航: CG橙子搜索引擎 | 博客 | 动画视频
广告|项目|培训|竞价排名  
86CG > CG教程 > 虚拟现实 > Multigen > 学习Vega最好的起步程序

学习Vega最好的起步程序

类型:整理 | 来自:86CG | 时间:2007-11-29 |  点击:
#include                        /* definition of printf          */
#include                       /* definition of exit            */
#include                       /* definition of strcmp          */
#include                           /* main include file for Vega    */
static void change((vgWindow *win, vgGfx *gfx, vgChannel *chan, vgEnv *env );
static void toggleGfx( vgGfx *gfx, int what );
static void ChangePosition();
vgIsector *make_z_isector(vgScene *scene, unsigned int isclass, float minz,
                          float maxz, float zoffset);
float compute_z(vgIsector *zisector, float x, float y);
vgObserver* g_Observer;
vgIsector* g_Isector;
struct PositionStruct
{
double x;
double y;
double z;
double p;
double h;
double r;
double step;
double angle_rate;
};
struct PositionStruct g_Position;
/*
============================================================================
   main -- simple visual system programming example
============================================================================
*/
void main ( int argc , char *argv[] )
{
    vgObserver *obs;  
    vgChannel  *chan;
    vgEnv      *env;
    vgWindow   *win;
    vgGfx      *gfx;

    /* init, define, and config the system */
    vgInitSys();


     vgDefineSys(("simple.adf");
    vgConfigSys();

    /* there has to be at least one window, get the first one */
    win  = vgGetWin( 0 );
    obs  = vgGetObserv( 0 );  /* do the same for the observer */
    chan = vgGetObservChan( obs, 0 ); /* has to have a channel */
    gfx  = vgGetObservGfx( obs ); /* has to have gfx */
    env  = vgGetEnv( 0 );  /* env optional, but needed here */
    if( env == NULL )
{
  printf("couldn't find the environment\n");
  vgExit( -1 );
    }

    g_Isector = make_z_isector(vgGetScene(0), VGIS_TERRALL,
                                    -10000.0f, 10000.0f, 3.0f);

g_Observer = vgGetObserv( 0 );
g_Position.x = 500;
g_Position.y = 500;
g_Position.z = 500;
g_Position.h = 0;
g_Position.p = 0;
g_Position.r = 0;
g_Position.step=10;
g_Position.angle_rate = 0.5;
ChangePosition();
    while ( 1 ) {                       /* forever */
  vgSyncFrame ();  
  vgFrame ();    
  change( win, gfx, chan, env );
    }
}

static void change( vgWindow *win, vgGfx *gfx, vgChannel *chan, vgEnv *env )
{
  
int    i, j;
    static int   stats = 0;
    static float tod = 1.0;
    static float first = 1;
    
vgMouse mouse;
vgGetMouse(&mouse);
if( fabs(mouse.nmx)>0.05 )
{
  g_Position.h -= mouse.nmx * g_Position.angle_rate;
  ChangePosition();
}
if ( fabs(mouse.nmy)>0.05 )
{
  g_Position.p += mouse.nmy * g_Position.angle_rate;
  ChangePosition();
}
    if (first) {
  first = 0;
  tod = vgGetProp(env, VGENV_TOD );
    }

    while( (i = vgGetWinKey( win )) != 0 ) {
  
  switch( i ) {
  
  case 'k':
   j = vgGetProp( chan, VGCOMMON_ENABLED );
   switch( j ) {
   case VG_TRUE:
    vgProp( chan, VGCOMMON_ENABLED, VG_FALSE );
    break;
   case VG_FALSE:
    vgProp( chan, VGCOMMON_ENABLED, VG_TRUE );
    break;
   }
   break;
  
  case 'j':
   j = vgGetProp( chan, VGCHAN_RENDER );
   switch( j ) {
   case VG_TRUE:
    vgProp( chan, VGCHAN_RENDER, VG_FALSE );
    break;
   case VG_FALSE:
    vgProp( chan, VGCHAN_RENDER, VG_TRUE );
    break;
   }
   break;
  
  case 'a':
   j = vgGetProp( win, VGWIN_AA );
   switch( j ) {
    case VG_TRUE:
     vgProp( win, VGWIN_AA, VG_FALSE );
     break;
    case VG_FALSE:
     vgProp( win, VGWIN_AA, VG_TRUE );
     break;
    }
    break;
    
  case 'b':
   j = vgGetProp( win, VGWIN_BUFMODE );
   switch( j ) {
   case VGWIN_DOUBLEBUF:
    vgProp( win, VGWIN_BUFMODE, VGWIN_SINGLEBUF );
    break;
   case VGWIN_SINGLEBUF:
    vgProp( win, VGWIN_BUFMODE, VGWIN_DOUBLEBUF );
    break;
   }
   break;
  
  case 'd':
   tod -= 0.01f;
   if( tod  1.0 )
    tod = 1.0;
   vgProp( env, VGENV_TOD, tod );
   break;  
  
  case 'f':
   toggleGfx( gfx, VGGFX_FOG );
   break;  
  
  case 'p':
   toggleGfx( gfx, VGGFX_TRANSPARENCY );
   break;  
  
  case 's':
   stats += 1;
   if( stats > VGCHAN_FILL+1 )
    stats = VGCHAN_STATSOFF;
   vgProp( chan, VGCHAN_STATSSEL, stats );
   break;  
  
  case 't':
   toggleGfx( gfx, VGGFX_TEXTURE );
   break;  
  
  case 'w':
   toggleGfx( gfx, VGGFX_WIREFRAME );
   break;  
  
  case 'z':
   toggleGfx( gfx, VGGFX_ZBUFFER );
   break;  
  
  case 173: //UP
   g_Position.x -= sin(g_Position.h/57.3)*g_Position.step;
   g_Position.y += cos(g_Position.h/57.3)*g_Position.step;
   ChangePosition();
   break;
  case 171: //DOWN
   g_Position.x += sin(g_Position.h/57.3)*g_Position.step;
   g_Position.y -= cos(g_Position.h/57.3)*g_Position.step;
   ChangePosition();
   break;
  case 170: //LEFT
   g_Position.x -= cos(g_Position.h/57.3)*g_Position.step;
   g_Position.y -= sin(g_Position.h/57.3)*g_Position.step;
   ChangePosition();
   break;
  case 172: //RIGHT
   g_Position.x += cos(g_Position.h/57.3)*g_Position.step;
   g_Position.y += sin(g_Position.h/57.3)*g_Position.step;
   ChangePosition();
   break;
  case 'q': //INCREASE HEIGHT
   g_Position.z += g_Position.step;
   ChangePosition();
   break;
  case 'Q': //DECREASE HEIGHT
   g_Position.z -= g_Position.step;
   ChangePosition();
   break;
  case '=':
   g_Position.step *= 1.1;
   break;
  case '-':
   g_Position.step /= 1.1;
   break;
  default:
   break;  
  
  }
    }
}
static void toggleGfx( vgGfx *gfx, int what )
{
    int i;

    i = (int)vgGetProp( gfx,  what );
    if( i )
  vgProp( gfx, what, VG_OFF );
    else
  vgProp( gfx, what, VG_ON );
}
static void ChangePosition()
{
vgPosition  *pos;
    float z = compute_z(g_Isector, g_Position.x, g_Position.y);
g_Position.z = g_Position.z>(z+5)?g_Position.z:(z+5);
pos = vgNewPos();

vgPosVec( pos,
  g_Position.x, g_Position.y, g_Position.z,
  g_Position.h, g_Position.p, g_Position.r );
vgPos(g_Observer,pos);
vgDelPos(pos);
// printf("x=%f    y=%f    z=%f\n", g_Position.x, g_Position.y, z);
}

vgIsector *make_z_isector(vgScene *scene, unsigned int isclass, float minz,
                          float maxz, float zoffset)
{
     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_ZOFFSET, zoffset); /* z offset                   */
     vgProp(zisector, VGIS_RENDER, VG_ON);    /* draw isector               */
                                              /* green : intersection found
                                                  red : no intersection     */
     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 intersection found\n");
          result = -1.0;
     }
     return(result);                              /* new Z */
}
(完)
可打印版本 | 文章评论 | 我来纠错

|网友评论

    笔名:

    内容:

        

    |热点关注

    新歌 白狐 左边 拉拉爱 校园网 自由飞翔 感恩的心 边做边爱 为你写诗 北京欢迎你 范跑跑之歌 遇上你是我的缘 坏女人 N81 星星 火花 放生 不值得 手机网 分手那天 忘不掉的伤 电子杂志 网上展会 天使 城府 爱死了昨天 BT电影下载 N70 最后一次的温柔 小小 承诺 有没有人告诉你 N73 光荣 葬爱 大海 日不落 有缘人 躲避的爱 香水有毒 一定要爱你 求佛 爱在离别时 北极星的眼泪 假如 flash 歌曲 启示录 那滋味 独家记忆 放手去爱 丁香花 会呼吸的痛 音乐排行榜 爱上别人的人 对不起我爱你 N70 6300 会有天使替我爱你 DJ 爱上你是我的错 不要在我寂寞的时候说爱我 爱上你是一个错 爱你爱的好疲惫 怎么会狠心伤害我 MP3 音乐手机 拍照手机 智能手机 CDMA手机 怒放的生命 老人与海 等爱的玫瑰 爱情里没有谁对谁错 做你的爱人 感动天感动地 做我老婆好不好 你的承诺 王子 心碎 舍不得 摇啊摇 泪的告白 寂寞才说爱 擦肩而过 阿里阿里 中国基金网 新不了情 小情歌 离歌

    关于我们 - 版权隐私 - 友情链接 - 广告服务 - 项目合作 - 网站地图 - 联系方式

    ©Copyright by 86CG.COM, 2006-2008. All rights reserved 京ICP备06059503号