Question about traversal functions

New Message Reply Date view Thread view Subject view Author view

From: Mark Gill (Mark.Gill++at++usm.edu)
Date: 11/21/2000 09:26:23


greetings,

Today's question is about traversal functions. I'm trying to use a single
traversal function, applying it to several pfDCS's (I want each pfDCS to
perform a similar task, but with different specific parameters). I'm trying
to control the performance of each function with setTravData -- passing it a
struct.

Here's my problem: Both of my objects act the same way. It's as if the
traversal function is over-writing the parameter structure for the second
object with that of the first.

Here's some code:
++++++++++++++++++
// structure for the planet data
/*
        The data in this structure controls how the planets behave,
        their rotation, revolution and distance from the sun.
        if pl_rot and pl_rev are set to the same value, then the planet
        will revolve once for every rotations, keeping the same face toward
        the sun */

struct planet_data {

        int pl_id;
        float pl_rot;
        float pl_rot_inc; // rotation inciment in degrees

        float pl_rev;
        float pl_rev_inc; // revolution incriment in degrees

        float pl_radius; // radius from the sun (origin)

        };

typedef struct planet_data planetdata;

*********************Then later on I do this:...

planet_data *earth_data, *moon_data;

earth_data = (planet_data *) pfMalloc (sizeof (planet_data),
pfGetSharedArena ());
moon_data = (planet_data *) pfMalloc (sizeof (planet_data), pfGetSharedArena
());

// set up the data for the planets

        earth_data->pl_id = 3;
        earth_data->pl_radius = 3000.0;
        earth_data->pl_rot = 0.0;
        earth_data->pl_rot_inc = 1.0;
        earth_data->pl_rev = 1.0;
        earth_data->pl_rev_inc = (1.0/365.0);

        moon_data->pl_id = 31;
        moon_data->pl_radius = 3000.0;
        moon_data->pl_rot = 0.0;
        moon_data->pl_rot_inc = (1.0/30);
        moon_data->pl_rev = 1.0;
        moon_data->pl_rev =(1.0/365);

pfVec3 earth_origin;

        // load the starfield
        pfNode *env = pfdLoadFile ("env2.iv");

        // load the sun object
        pfNode *sun = pfdLoadFile ("sun.iv");
                sundcs = new pfDCS;
                sundcs -> addChild (sun);

        // load the earth -- 3 levels of detail
        pfLOD *earth_lod = new pfLOD;

                pfNode *earth_l = pfdLoadFile ("earth_l.iv"); // low poly count
                pfNode *earth = pfdLoadFile ("earth.iv");
                pfNode *earth_h = pfdLoadFile ("earth_h.iv"); // high poly count

                earthdcs = new pfDCS;

                        //earth_origin.set(0.0,0.0,0.0);
                        //earth_lod -> setCenter (earth_origin);
                        earth_lod -> addChild (earth_h);
                        earth_lod -> setRange (1, 750.0);

                        earth_lod -> addChild (earth);
                        earth_lod -> setRange (2, 3000.0);

                        earth_lod -> addChild (earth_l);
                        earth_lod -> setRange (3, 12000);

                        //earth_lod -> setRange (-1, 100000.0);

                        earthdcs -> addChild (earth_lod);
                        earthdcs->setTravFuncs(PFTRAV_APP,pl_rotate,NULL);
                        earthdcs->setTravData (PFTRAV_APP, (void *) earth_data);

        // load the moon

        moondcs = new pfDCS;
                pfNode *moon = pfdLoadFile ("moon.iv");
                moondcs -> addChild (moon);
                moondcs -> setTravFuncs (PFTRAV_APP,pl_rotate,NULL);
                moondcs -> setTravData (PFTRAV_APP, (void *) moon_data);

And here's the rotation traversal function:

/*++++++++++++++++++++++++++++ planet_rotate +++++++++++++*/

static int pl_rotate(pfTraverser *trav,void * data)

{
        static planet_data * pl_data = (planet_data *) data;
        static float radians, x, y;

        //recylce the rotation and revolution data

        if (pl_data -> pl_rot > 359.9)
                pl_data -> pl_rot = 0.0;

        if (pl_data -> pl_rev > 359.9)
                pl_data -> pl_rev = 0.0;

                radians = 1 * 3.14/180; // leave in formula to let me tweak

                x = 0.0 +(pl_data->pl_radius * (cos (radians * pl_data->pl_rev)));
                y = 0.0 + (pl_data->pl_radius * (sin (radians * pl_data->pl_rev)));

        pfDCS *rot_dcs = (pfDCS *) trav->getNode();/*get a ptr to the DCS node
itself */

        rot_dcs -> setRot (pl_data -> pl_rot, 0.0, 0.0);
        rot_dcs -> setTrans (x, y, 0.0);

        pl_data -> pl_rot = pl_data -> pl_rot + pl_data -> pl_rot_inc;
        pl_data -> pl_rev = pl_data -> pl_rev + pl_data -> pl_rev_inc;

        // some debug code
        printf ("planet id: %i ", pl_data->pl_id);
        printf (" -- revolution = %f \n", pl_data->pl_rev);

        return PFTRAV_CONT;

}

When I run the program I can tell that both objects are being affected (they
both move), but they both act the same way, and printing out the debug data
shows that the same data is passing through the traversal function.

Am I just missing something -- overlooking something simple? I'm trying to
avoid writing a different traversal function for each object, but I know
that will work.

thanks in advance...

Mark Gill Visualization Researcher
CHL Stennis Space Center
Mark.Gill++at++usm.edu
----------------------------------------------


New Message Reply Date view Thread view Subject view Author view

This archive was generated by hypermail 2b29 : Tue Nov 21 2000 - 09:26:06 PST

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