Hi Andreas,

On Mon, 2003-10-20 at 04:06, Andreas Kopecki wrote:
> > OK, if simple things like this can make you happy I'm all for it. ;)
> 
> It's always the simple things :-)
> 
> > What about something like this:
> > 
> > NodePtr n = makeNode<Transform>();
> 
> Looks perfect for me....

ok, I just checked in the changes needed for the FieldContainers and two
new functions:

template <class Core> 
inline NodePtr makeCoredNode(typename Core::Ptr *coreP = NULL);

NodePtr makeNodeFor(NodeCorePtr core);

Attached to this mail you can find an example on how to use them.

> 
> I would prefer the inherited version, although the docs would grow quite
> a bit, but maybe we need a poll here :-)

Well, I like it. So if nobody objects I'll change it... ;)

        Dirk


// Headers
#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGBaseFunctions.h>
#include <OpenSG/OSGTransform.h>
#include <OpenSG/OSGGroup.h>


// Activate the OpenSG namespace
OSG_USING_NAMESPACE


// number of copies to create
const UInt16 ncopies = 20;

// a seprate transformation for every copy
TransformPtr trans[ncopies];

// The SimpleSceneManager to manage simple applications
SimpleSceneManager *mgr;

// forward declaration so we can have the interesting stuff upfront
int setupGLUT( int *argc, char *argv[] );

// redraw the window
void display( void )
{
    // create the matrix
    Matrix m;
    Real32 t = glutGet(GLUT_ELAPSED_TIME );
    
    // set the transforms' matrices
    for(UInt16 i=0; i<ncopies; ++i)
    {
        m.setTransform(Vec3f(      osgsin(t / 1000.f + i * 4 * ncopies / Pi), 
                                   osgcos(t / 1000.f + i * 6 * ncopies / Pi), 
                                   osgsin(t / 1000.f + i * 7 * ncopies / Pi)),
                       Quaternion( Vec3f (1,1,0), 
                                          t / 1000.f + i * 4 * ncopies / Pi));
    
        beginEditCP(trans[i], Transform::MatrixFieldMask);
        {
            trans[i]->setMatrix(m);
        }
        endEditCP  (trans[i], Transform::MatrixFieldMask);
    }
    
    mgr->redraw();
}

// Initialize GLUT & OpenSG and set up the scene
int main(int argc, char **argv)
{
    // OSG init
    osgInit(argc,argv);

    // GLUT init
    int winid = setupGLUT(&argc, argv);

    // the connection between GLUT and OpenSG
    GLUTWindowPtr gwin= GLUTWindow::create();
    gwin->setId(winid);
    gwin->init();

    // create the scene
    
    // this time, create just the core of the geometry
    GeometryPtr torus = makeTorusGeo( .5, 2, 8, 12 );
    
    // create the root Group node
    NodePtr  scene = makeCoredNode<Group>();
    
    beginEditCP(scene, Node::ChildrenFieldMask);
    
    // create the copied geometry nodes and their transformations
    for(UInt16 i = 0; i<ncopies; ++i)
    {
        // add a transformation for every Geometry
        NodePtr transnode = makeCoredNode<Transform>(&trans[i]);
         
        beginEditCP(transnode, Node::CoreFieldMask | Node::ChildrenFieldMask);
        {
            transnode->addChild( makeNodeFor(torus) );
        }
        endEditCP  (transnode, Node::CoreFieldMask | Node::ChildrenFieldMask);
       
        scene->addChild(transnode);       
    }

    endEditCP  (scene, Node::ChildrenFieldMask);
 
    // create the SimpleSceneManager helper
    mgr = new SimpleSceneManager;

    // tell the manager what to manage
    mgr->setWindow(gwin );
    mgr->setRoot  (scene);

    // show the whole scene
    mgr->showAll();

    // GLUT main loop
    glutMainLoop();

    return 0;
}

//
// GLUT callback functions
//

// react to size changes
void reshape(int w, int h)
{
    mgr->resize(w, h);
    glutPostRedisplay();
}

// react to mouse button presses
void mouse(int button, int state, int x, int y)
{
    if (state)
        mgr->mouseButtonRelease(button, x, y);
    else
        mgr->mouseButtonPress(button, x, y);
        
    glutPostRedisplay();
}

// react to mouse motions with pressed buttons
void motion(int x, int y)
{
    mgr->mouseMove(x, y);
    glutPostRedisplay();
}

// react to keys
void keyboard(unsigned char k, int x, int y)
{
    switch(k)
    {
        case 27:    
        {
            OSG::osgExit();
            exit(0);
        }
        break;
    }
}

// setup the GLUT library which handles the windows for us
int setupGLUT(int *argc, char *argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    int winid = glutCreateWindow("OpenSG");
    
    glutReshapeFunc(reshape);
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutKeyboardFunc(keyboard);

    // call the redraw function whenever there's nothing else to do
    glutIdleFunc(display);

    return winid;
}

Reply via email to