Hi Marcus,
thanks for your suggestion! Unfortunatly something else must be missing.
I've set the pointers of "trans" and "scene" to MTRectPrt. Now the torus
is not turning any more.. but this time even not if the
...->getChangeList()->applyAndClear(); are commented in. Attached is the
code. So I'm still stuck with this ;)
Cheers,
Christoph
13multithreading.cpp:
// all needed include files
#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
SimpleSceneManager *mgr;
//NodeRecPtr scene;
NodeMTRecPtr scene;
//we will store the transformation globally - this
//is not necessary, but comfortable
//TransformRecPtr trans;
TransformMTRecPtr trans;
Thread *animationThread;
Thread *applicationThread;
Barrier *syncBarrier;
int setupGLUT(int *argc, char *argv[]);
NodeTransitPtr createScenegraph(void)
{
// the scene must be created here
NodeRecPtr n = makeTorus(.5,2,16,16);
//add a simple Transformation
trans = Transform::create();
Matrix m;
m.setIdentity();
trans->setMatrix(m);
NodeRecPtr transNode = Node::create();
transNode->setCore(trans);
transNode->addChild(n);
return NodeTransitPtr(transNode);
}
//this function will run in a thread and simply will
//rotate the cube by setting a new transformation matrix
void rotate(void *args)
{
// sync this thread to the main thread, i.e. pull in all changes done
// during scene construction
syncBarrier->enter(2);
applicationThread->getChangeList()->applyAndClear();
syncBarrier->enter(2);
// we won't stop calculating new matrices....
while(true)
{
Real32 time = glutGet(GLUT_ELAPSED_TIME);
Matrix m;
m.setIdentity();
m.setRotate(Quaternion(Vec3f(0,1,0), time/1000));
trans->setMatrix(m);
// nothing unusual until here
//well that's new...
//wait until two threads are cought in the
//same barrier
syncBarrier->enter(2); // barrier (1)
//just the same again
syncBarrier->enter(2); // barrier (2)
}
}
int main(int argc, char **argv)
{
osgInit(argc,argv);
{
int winid = setupGLUT(&argc, argv);
GLUTWindowMTRecPtr gwin = GLUTWindow::create();
gwin->setGlutId(winid);
gwin->init();
scene = createScenegraph();
//create the barrier, that will be used to
//synchronize threads
//instead of NULL you could provide a name
syncBarrier = Barrier::get(NULL);
mgr = new SimpleSceneManager;
mgr->setWindow(gwin );
mgr->setRoot (scene);
mgr->showAll();
// store a pointer to the application thread
applicationThread = dynamic_cast<Thread
*>(ThreadManager::getAppThread());
//create the thread that will run generation of new matrices
animationThread = dynamic_cast<Thread
*>(ThreadManager::the()->getThread("anim"));
//do it...
animationThread->runFunction(rotate, 1, NULL);
// wait for animationThread to complete its sync
syncBarrier->enter(2);
syncBarrier->enter(2);
commitChanges();
}
glutMainLoop();
return 0;
}
void reshape(int w, int h)
{
mgr->resize(w, h);
glutPostRedisplay();
}
void display(void)
{
// we wait here until the animation thread enters
// barrier (1)
syncBarrier->enter(2);
//now we sync data
animationThread->getChangeList()->applyAndClear();
// now wait for animation thread to enter barrier (2)
syncBarrier->enter(2);
// !!!! Attention
// you will find a more detailed description
// of what's going on here in the documentation
// itself!
// now render...
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;
}
Marcus Lindblom schrieb:
> Christoph Schäfer wrote:
>
>> Hi List,
>>
>> I'm still struggling using two aspects in my mt application. So I deeply
>> analyzed the 13multithreading2.cpp again. I stumbled over some strange
>> behavior:
>>
>> If you comment out the lines calling
>> ...->getChangeList()->applyAndClear(); the application will still run
>> and the torus will still turn.It ssems like the sync between aspect 0
>> and 1 is done by entering the sync barrier? Is this intended behavior or
>> does the example not reflect the changes from OpenSG1.x to OpenSG2?
>>
>
> It looks like the example hasn't been updated w.r.t the change in
> pointer types in OSG2, i.e. the 'trans' pointer is not a MTRec-ptr, but
> a regular Ptr (the latter did select aspect in 1.x, but does not in
> 2.0), so it does not write to the second aspect, thus no need to sync.
>
> Try changing the ptr type to MTRecPtr, it ought to behave properly then.
>
> Cheers,
> /Marcus
>
>
> -------------------------------------------------------------------------
> This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
> Build the coolest Linux based applications with Moblin SDK & win great prizes
> Grand prize is a trip for two to an Open Source event anywhere in the world
> http://moblin-contest.org/redirect.php?banner_id=100&url=/
> _______________________________________________
> Opensg-users mailing list
> [email protected]
> https://lists.sourceforge.net/lists/listinfo/opensg-users
>
-------------------------------------------------------------------------
This SF.Net email is sponsored by the Moblin Your Move Developer's challenge
Build the coolest Linux based applications with Moblin SDK & win great prizes
Grand prize is a trip for two to an Open Source event anywhere in the world
http://moblin-contest.org/redirect.php?banner_id=100&url=/
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users