- 积分
- 63
- 帖子
- 37
- 主题
- 5
- 精华
- 0
- 最后登录
- 2010-7-15
- 在线时间
- 45 小时
- 私信
|
发表时间 : 2006-9-5 14:53:38
|
浏览 : 2870 评论 : 4
本程序演示了如何借助OPENGL ,使用代码产生一个 Object
#include "vg.h" // for the standard Vega classes and defines
#include "vgperf.h" // for the Vega Performer node functions
#include "pf.h" // for the standard Performer classes
#include "pfutil.h" // for the Performer util functions
#include // for Opengl functions
//
static int oglGeometryDraw_CB( pfTraverser *trav, void *data );
vgObject*createAnOpenGlbasedvgObject(){
// ###########################################################
// #
// # Public function
// #
// # Create the new vgObject that will be our be the
// # container for the Opengl code this allows us to easily
// # add and remove from the scene and to get a local
// # positionable coordinate system
// #
// ###########################################################
vgObject *oglObj = NULL;
vgDataSet *dataset = NULL;
pfGroup *group = NULL;
pfNode *node = NULL;
//
// 1st create an empty dataset
//
dataset = vgNewDS();
if( dataset == NULL )
return NULL;
vgName( dataset, "_oglDataSet" );
//
// Then we need a group node for the dataset
//
group = pfNewGroup();
pfNodeName( group, "_oglGroup" );
if( dataset == NULL ){
vgDelete( dataset );
return NULL;
}
//
// Set the draw and the cull masks so that the node
// will get drawn and not culled we could also add
// a bounding sphere but this works just as well
//
pfNodeTravMask( group, PFTRAV_DRAW, 0xFFFFFFFF, PFTRAV_SELF, PF_SET);
pfNodeTravMask( group, PFTRAV_CULL, 0x00000000, PFTRAV_SELF, PF_SET);
//
// Now create the dataset using the supplied geometry
//
if ( vgMakeDS( dataset, group, VGDS_GEOMETRY ) == VG_SUCCESS) {
//
// * Create a new Dynamic object
//
oglObj = vgNewObj();
vgName ( oglObj, "_oglObj" );
vgProp ( oglObj, VGOBJ_CS, VGOBJ_DYNAMIC );
vgObjDS ( oglObj, dataset );
vgMakeObj( oglObj, VGOBJ_USE );
vgUpdate ( oglObj);
}
//
// Dataset was not created so clean up and bail
//
else{
vgDelete(dataset);
pfUnrefDelete(group);
return NULL;
}
//
// Sanity check set the draw and cull masks
//
node = (pfNode*)vgGetObjPfNode( oglObj );
pfNodeTravMask( node, PFTRAV_DRAW, 0xFFFFFFFF, PFTRAV_SELF, PF_SET);
pfNodeTravMask( node, PFTRAV_CULL, 0x00000000, PFTRAV_SELF, PF_SET);
//
// Add the Pre call-back to the node ( no Post Draw)
//
pfNodeTravFuncs( node, PFTRAV_DRAW, oglGeometryDraw_CB, NULL);
pfNodeTravData ( node, PFTRAV_DRAW, NULL );
//
// Return our completed vgObject
//
return oglObj;
} // createAnOpenGlbasedvgObject
Int oglGeometryDraw_CB(pfTraverser *trav, void *data ){
// #################################################
// #
// # Static Public function
// #
// # Node callback function, in this case used to
// # setup the Opengl State machine and to draw some
// # opengl Primitives
// #
// #################################################
int trans = 0;
int pflight = 0;
int pftex = 0;
GLboolean cullface = false;
GLboolean blend = false;
GLint depth = 0;
//
// Get and Save our current Openl states we change
//
glGetBooleanv( GL_BLEND , &blend );
glGetBooleanv( GL_CULL_FACE, &cullface);
glGetIntegerv( GL_DEPTH_FUNC, &depth );
trans = pfGetTransparency();
//
// Save the current Performer states
//
pfPushState();
pfBasicState();
pfPushMatrix();
//
// Disable the states we do not want
// while we draw the grids
//
trans = pfGetTransparency();
pfTransparency( PFTR_FAST );
glEnable ( GL_BLEND );
glDepthFunc( GL_ALWAYS );
glDisable ( GL_CULL_FACE );
//
// For this example we need to disable lighting while drawing as
// we dont want light affecting this geom as we have no normals
//
pflight = pfGetEnable(PFEN_LIGHTING);
pfDisable( PFEN_LIGHTING );
//
// Ensure we get no unwanted texture leaking in
//
pftex = pfGetEnable(PFEN_LIGHTING);
pfDisable( PFEN_TEXTURE );
//
// We always want to overlay this geometry
// so we need to disable Z buffer testing
//
glDisable( GL_DEPTH_TEST );
//
// Set the color of the polygon
//
glColor3f( 1.0f, 0.2f, 0.0f);
//
// Draw our single quad that is our poly
//
glBegin( GL_QUADS );
glVertex3f( -10.0f ,-10.0f, 3.0f );
glVertex3f( 10.0f ,-10.0f, 3.0f );
glVertex3f( 10.0f , 10.0f, 3.0f );
glVertex3f( -10.0f , 10.0f, 3.0f );
glEnd( );
//
// enable lighting & reset transparency and culling
//
if(pflight)
pfEnable( PFEN_LIGHTING );
if(pftex)
pfEnable( PFEN_TEXTURE );
//
// Restore the previous Depth function and Cull
//
glDepthFunc (depth);
if( cullface == GL_TRUE )
glEnable( GL_CULL_FACE );
//
// Reset the transparency state
//
pfTransparency( trans );
//
// Re-enable depth testing now were finished
//
glEnable( GL_DEPTH_TEST );
//
// Restore the Previous/entry states and matirces
//
pfPopState ();
pfPopMatrix();
//
// We all ways need to retrun contiune
//
return PFTRAV_CONT;
} // oglGeometryDraw_CB
上面的代码用处很大,可以解决OpenGL物体随视角转动,定位不准确。
还可以间接解决GL Studio在Vega中使用的各种问题。使用的话,只要把
glColor3f( 1.0f, 0.2f, 0.0f);
glBegin( GL_QUADS );
glVertex3f( -10.0f ,-10.0f, 3.0f );
glVertex3f( 10.0f ,-10.0f, 3.0f );
glVertex3f( 10.0f , 10.0f, 3.0f );
glVertex3f( -10.0f , 10.0f, 3.0f );
glEnd( );
这几句改成自己设计好的东东就好使了。
再用vgAddSence加入场景即可。
怎么说,大家都会用函数对vgObject进行各种操作的 |
|