Re: Database Units

New Message Reply Date view Thread view Subject view Author view

Calvin Lu (calvin++at++paradigmsim.com)
Wed, 15 Dec 1999 12:58:50 -0600


The database units information is stored in the header
which can be accessed through the loader callback. The
attached code shows you how to do it.

Calvin Lu

Anthony Bavuso wrote:

> Greetings,
>
> Question about open flight data base physical units. When I create my open
> flight database in creator, I specify the physical units for the database
> i.e. feet, meters, kilometers, etc. Is there anyway from the performer API
> at runtime to get the type of physical units specified for the database?
>
> I ask because there are problems if I load a database whose units are in
> meters but I do all my motion calculations in feet. Unfortunately I need to
> know the database unit type at runtime because sometimes I won't know what
> the physical database units are beforehand.
>
> Is there a way to do what I am trying to do?

#include <math.h>
#include <stdlib.h>
#include <X11/keysym.h>
#include <Performer/pf.h>
#include <Performer/pfutil.h>
#include <Performer/pfdu.h>

/* CLU: include pfflt.h */
#include <Performer/pfdb/pfflt.h>

typedef struct {
    int fcnt;
    char **fnames;
    pfScene *scene;
    pfGroup *group;
    int done;
    pfPipeWindow *pwin;
    int XInputInited;
    int exitFlag;
} SharedData;
SharedData *shared;

static void InitXInput(pfWSConnection dsp);
static void GetXInput(pfWSConnection dsp);

/* CLU: the flt loader callback will be invoked in pfdLoadFile */
void fltLoaderCB( pfNode* node, int mgOp, int* cbs, COMMENTcb* cbcom, void *ud )
{
    switch ( mgOp )
    {
        case CB_HEADER:
            *(int *)ud = ((HEADERcb*)cbs)->units;
            printf("cbs->units = %d\n", ((HEADERcb*)cbs)->units);
            break;
        default:
            break;
    }
}

int
main (int argc, char *argv[])
{
    void *arena;
    pfPipe *pipe;
    pfChannel *chan;
    pfNode *node;
    pfGeoState *gstate;
    pfCoord view;
    int i, j, k, n, w, h;
    int fnum, firstarg = 1;
    pfWSConnection dsp=NULL;
     
    /* CLU: Note, the type id stored in the header is different from the ones
     * defined in pfflt.h
     */
    int unitsType;
    char *unitsTypeName[] = { "METERS", /* 0 */
                              "KILOMETERS", /* 1 */
                              "", /* 2 */
                              "", /* 3 */
                              "FEET", /* 4 */
                              "INCHES", /* 5 */
                              "", /* 6 */
                              "", /* 7 */
                              "NAUT_MILES" /* 8 */
                            };

    fprintf(stderr,"\n\n\nPerformer %d.%d\n\n\n", PF_MAJOR_VERSION, PF_MINOR_VERSION );

    if (argc < 2)
    {
        printf("usage: %s file ... \n", argv[0]);
        exit(1);
    }
     
    for (i=1; i < argc; i++) {
        if (argv[i][0] == '-') {
            switch (argv[i][1]) {
              case 's':
                firstarg += 1;
                break;
              case 't':
                firstarg += 2;
                break;
            }
        }
    }

    /* Initialize Performer */
    pfInit();
     
    /* Get shared memory arena */
    arena = pfGetSharedArena();
    shared = (SharedData *) pfCalloc(1, sizeof(SharedData), arena);
    shared->fcnt = argc - firstarg;
    shared->fnames = (char **) pfCalloc(shared->fcnt,sizeof(char*), arena);
    for (i=0; i < shared->fcnt; i++) {
        shared->fnames[i] = argv[i + firstarg];
    }

    pfMultiprocess(PFMP_APPCULLDRAW|PFMP_FORK_DBASE);

    for (k = 1; k < argc; k++)
         pfdInitConverter(argv[k]);

    /* CLU: install loader callback */
    {
        fltRegisterNodeT pFunc = fltLoaderCB;
        pfdConverterAttr("flt", PFFLT_REGISTER_NODE, &pFunc );
        pfdConverterAttr("flt", PFFLT_REGISTER_USERDATA, &unitsType );
    }

    pfConfig();

    if (!(getenv("PFPATH")))
        pfFilePath( "."
                    ":./data"
                    ":/usr/share/Performer/data");

    shared->scene = pfNewScene();

    /* load files named on command line to scene graph */
    for (k = 1; k < argc; k++)
    {
        if ((node = pfdLoadFile(argv[k])) != NULL)
            pfAddChild(shared->scene, node);
    }
     
    /* CLU: print units info */
    printf("\n\n\nFLT Units Type: %s\n\n\n", unitsTypeName[unitsType] );
     
    /* Create a scene pfGeoState with lighting enabled */
    gstate = pfNewGState(arena);
    pfGStateMode(gstate, PFSTATE_ENLIGHTING, PF_ON);
    pfSceneGState(shared->scene, gstate);
    pfAddChild(shared->scene, pfNewLSource());

    /* Create and configure a pfChannel. */
    pipe = pfGetPipe(0);
    shared->pwin = pfNewPWin(pipe);
    pfPWinOriginSize(shared->pwin, 0, 0, 400, 400);
    pfOpenPWin(shared->pwin);
    chan = pfNewChan(pipe);
    pfChanScene(chan, shared->scene);
    pfChanFOV(chan, 60.0f, 0.0f);

    pfSetVec3(view.hpr, 0, 0, 0);
    pfSetVec3(view.xyz, 0, -10, 0);
    pfChanView(chan, view.xyz, view.hpr);

    dsp = pfGetCurWSConnection();

    while ( !shared->exitFlag )
    {
        pfSync();

        pfFrame();
         
        if (!shared->XInputInited)
            InitXInput(dsp);
        if (shared->XInputInited)
            GetXInput(dsp);

    }
    pfExit();
}

static void
InitXInput(pfWSConnection dsp)
{
    Window w;
     
    /* wait for X Window to exist in Performer shared memory */
    /* only take x events from window 0 */
    if (w = pfGetPWinWSWindow(shared->pwin))
    {
        XSelectInput(dsp, w, PointerMotionMask |
                        ButtonPressMask | ButtonReleaseMask | KeyPressMask);
        XMapWindow(dsp, w);
        XFlush(dsp);
        shared->XInputInited = 1;
    }
}

static void
GetXInput(pfWSConnection dsp)
{
    static int x=0, y=0;
     
    if (XEventsQueued(dsp, QueuedAfterFlush))
    while (XEventsQueued(dsp, QueuedAlready))
    {
        XEvent event;
             
        XNextEvent(dsp, &event);

        switch (event.type)
        {
            case KeyPress:
            {
                char buf[100];
                int rv;
                KeySym ks;
                rv = XLookupString(&event.xkey, buf, sizeof(buf), &ks, 0);
                switch(ks) {
                case XK_Escape:
                    shared->exitFlag = 1;
                    break;
                case XK_space:
                    printf("space bar\n");
                    break;
                case XK_s:
                    printf("s key\n");
                    break;
                default:
                    break;
                }
            }
        break;
        default:
            break;
        }/* switch */
    }
}


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2.0b2 on Wed Dec 15 1999 - 10:23:07 PST

This message has been cleansed for anti-spam protection. Replace '++at++' in any mail addresses with the '@' symbol.