Greetings,

I need to add geometry to the scenegraph on a second thread but it seems like 
it won't work correctly.
The source below, taken from the tutorials with little changes, demonstrates 
this.
If 'VERSION1' is defined, a new torus is created on a 2nd thread, 1 second 
after the program starts. It will continuously print the message "WARNING: 
recurse: core is Null,  don't know what to do!" after.
If 'VERSION2' is defined instead, it works correctly.

Regards,
Miguel

The complete source of the demonstration program follows:

#include <OpenSG/OSGGLUT.h>
#include <OpenSG/OSGConfig.h>
#include <OpenSG/OSGSimpleGeometry.h>
#include <OpenSG/OSGGLUTWindow.h>
#include <OpenSG/OSGSimpleSceneManager.h>
#include <OpenSG/OSGThreadManager.h>

OSG_USING_NAMESPACE
using namespace std;

#define VERSION1

Real32 gluttime;
SimpleSceneManager *mgr;
NodePtr scene;
TransformPtr trans;
Thread* animationThread;
Barrier *syncBarrier;

int setupGLUT( int *argc, char *argv[] );

NodePtr createScenegraph(){
        NodePtr n = makeTorus(.5,2,16,16);
        
        trans = Transform::create();
        beginEditCP(trans);
                Matrix m;
                m.setIdentity();
                trans->setMatrix(m);
        endEditCP(trans);
        
        NodePtr transNode = Node::create();
        beginEditCP(transNode);
                transNode->setCore(trans);
                transNode->addChild(n);
        endEditCP(transNode);
        
        return transNode;
}

//this function will run in a thread and simply will
//rotate the cube by setting a new transformation matrix
void rotate(void *args){
        // we won't stop calculating new matrices....
        while(true){
                Matrix m;
                m.setIdentity();
                m.setRotate(Quaternion(Vec3f(0,1,0), gluttime/1000));

#ifdef VERSION1
                static int done = 0;
                // this will create some geometry after 1 second, won't work
                gluttime = glutGet(GLUT_ELAPSED_TIME);
                if (done == 0 && gluttime>1000) {
                        cout << "hi there!" << endl;
                        NodePtr t = makeTorus(.2,.5,8,16);
                        beginEditCP(scene);
                                scene->addChild(t);
                        endEditCP(scene);
                        done = 1;
                }
#endif // VERSION1              
                
                beginEditCP(trans);
                        trans->setMatrix(m);
                endEditCP(trans);

                syncBarrier->enter(2);
                syncBarrier->enter(2);
        }
}

int main(int argc, char **argv)
{
        // set read write
        ChangeList::setReadWriteDefault();

    osgInit(argc,argv);
        
    int winid = setupGLUT(&argc, argv);
    GLUTWindowPtr gwin= GLUTWindow::create();
    gwin->setId(winid);
    gwin->init();

    scene =createScenegraph();
        
        syncBarrier = Barrier::get(NULL);

    mgr = new SimpleSceneManager;
    mgr->setWindow(gwin );
    mgr->setRoot  (scene);
    mgr->showAll();
        
        animationThread = dynamic_cast<Thread 
*>(ThreadManager::the()->getThread("anim"));

        animationThread->runFunction(rotate, 1, NULL);
        
    glutMainLoop();

    return 0;
}

void reshape(int w, int h)
{
    mgr->resize(w, h);
    glutPostRedisplay();
}

void display(void)
{
        syncBarrier->enter(2);
        animationThread->getChangeList()->applyAndClear();
        syncBarrier->enter(2);

#ifdef VERSION2
                static int done = 0;
                // this will create some geometry after 1 second, it works
                gluttime = glutGet(GLUT_ELAPSED_TIME);
                if (done == 0 && gluttime>1000) {
                        cout << "hi there!" << endl;
                        NodePtr t = makeTorus(.2,.5,8,16);
                        beginEditCP(scene);
                                scene->addChild(t);
                        endEditCP(scene);
                        done = 1;
                }
#endif // VERSION2              

        mgr->redraw();
}

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

void motion(int x, int y)
{
    mgr->mouseMove(x, y);
    glutPostRedisplay();
}

int setupGLUT(int *argc, char *argv[])
{
    glutInit(argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_DEPTH | GLUT_DOUBLE);
    
    int winid = glutCreateWindow("OpenSG First Application");
    
    glutDisplayFunc(display);
    glutMouseFunc(mouse);
    glutMotionFunc(motion);
    glutReshapeFunc(reshape);
    glutIdleFunc(display);
    return winid;
}


-------------------------------------------------------
SF.Net email is Sponsored by the Better Software Conference & EXPO
September 19-22, 2005 * San Francisco, CA * Development Lifecycle Practices
Agile & Plan-Driven Development * Managing Projects & Teams * Testing & QA
Security * Process Improvement & Measurement * http://www.sqe.com/bsce5sf
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to