Hi All!

I want to load a highResTexture while zooming to the object with a lowResTexture in a seperate Thread, which gets started here:


void OverlayViewport::selectImage(ImageMetaInfo* imageMetaInfo)
{
// If we are flying towards another Object, it should not load his texture anymore
   if (!_targetReached)
   {
Thread* imageLoadingThread = dynamic_cast<Thread *>(ThreadManager::the()->getThread("imageLoadingThread"));
       imageLoadingThread->terminate();
   }

   _targetReached = false;
   ImageMetaInfo::getCurrent()->switchTexture( false) ;

Thread* imageLoadingThread = dynamic_cast<Thread *>(ThreadManager::the()->getThread("imageLoadingThread")); imageLoadingThread->runFunction( switchTextureHighRes, 1, imageMetaInfo);

   ImageMetaInfo::setCurrent(imageMetaInfo);
}


No here comes the part, that goes in the extra Thread:

void switchTextureHighRes(void *args)
{
   ImageMetaInfo* meta = (ImageMetaInfo*) args;
   meta->switchTexture(true);

   Barrier* syncBarrier;
   syncBarrier = Barrier::get("imageLoadingSyncBarrier");
   syncBarrier->enter(2);
      // here the main thread can get the ChangeList
   syncBarrier->enter(2);
}

which calls this :

void ImageMetaInfo::switchTexture( bool high)
{
   ImagePtr tmpImage;
   if (high)
   {
           tmpImage = Image::create();

           if (!tmpImage->read( path.c_str() ))
           {
printf("Image %s could NOT be READ. Consult OpenSG.log.txt !\n",path.c_str());
           }

       beginEditCP(_simpleTexturedMaterialPtr);
           _simpleTexturedMaterialPtr->setImage( tmpImage);
       endEditCP(_simpleTexturedMaterialPtr);
   }
   else
   {
       beginEditCP(_simpleTexturedMaterialPtr);
           _simpleTexturedMaterialPtr->setImage( _lowResTexture);
       endEditCP(_simpleTexturedMaterialPtr);
   }
}


When the camera is before the object I want to get the image:

void OverlayViewport::targetReached()
{
   getImageFromLoadingThread();
   _targetReached = true;
}

void OverlayViewport::getImageFromLoadingThread()
{
   Thread* imageLoadingThread;
   Barrier* syncBarrier;
imageLoadingThread = dynamic_cast<Thread *>(ThreadManager::the()->getThread("imageLoadingThread"));
   syncBarrier = Barrier::get("imageLoadingSyncBarrier");

   syncBarrier->enter(2);
       imageLoadingThread->getChangeList()->applyAndClear();
   syncBarrier->enter(2);
}


This is similiar to what Oliver Abert did it in the tutorials. It does work, though I am not sure if it is water-proof.
So I would be glad if one or another could have a look at my code.

And there seems to be memory leaks, I thought the highResImages would be killed out of memory if i set it back to
       beginEditCP(_simpleTexturedMaterialPtr);
           _simpleTexturedMaterialPtr->setImage( _lowResTexture);
       endEditCP(_simpleTexturedMaterialPtr);
Is that so?
And when i terminate the thread before it is done, it could just be loading the image and i dont know if termination is safe then. Perhaps the leak is somewhere else, i'll try to get a non-crashing shutdown to test that :-)

Greetings,
Tobias Kilian




-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Opensg-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/opensg-users

Reply via email to