学习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 */
}
#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 */
}
(完)
- 上篇文章: 怎样在程序中从运动模式变换为自动漫游模式
- 返回:Multigen
- 下篇文章:用MFC编写vega程序的原始工程文件
CG橙子精彩导航: 










