I think that this do not work because you can not draw inside a thread, make the _img public or create a get method to return the image. Create an instance of your threaded video grabber in the main of class and draw there…
You might also want to take a look at the ofThreadChannel class, which significantly simplifies accessing resources across threads. In your case you’ll send your ofPixels to the main thread, where you can update your ofImage
As @theo mentioned, you can’t make OpenGL calls outside of the main thread. I’m not sure it is necessary, but you also might want to tell the grabber not to use textures (which you are not interested in anyway, since you’re sending ofPixels to the main thread). You can do this by calling:
Not sure what you are trying to accomplish putting the video grabber in a thread but it doesn’t make sense as it is. The grabber already runs internally in it’s own thread so the only thing you would be doing is making extra copies of the pixels which would be slower than not using a thread at all.
The only thing that ofVideoGrabber::update does is mostly uploading the new pixels to the texture so moving that onto a thread won’t make any difference.
As others have said you can’t update the texture from a thread, at least not using the same context, so you won’t get any advantage there either
Thanks for the answers. I looked again at the thread example and ended up doing the solution Theo mentioned and it is now working.
@arturo, thank you for this explanation. Reason I put the video grabber in a thread is that I want to do some more processing later on (thresholding, AR detection, …) and thought that putting all the video-related parts in a thread will not be blocking my main thread and will be cleaner.
But if I understand what you mean correctly, I should rather put the video grabber in the main thread (not to have its thread redundant, right?), send the pixels to my thread where I do all the post processing I need and then return the information/image after it’s being processed.
Would this be a better approach?
yes, that’s it. you want to put in a thread the cpu intensive tasks, the video graber update is not, so it won’t only not give you any advantage but the way you are doing it, you are copying the pixels in the main thread which will slow things down. the videograbber is already doing that, in a much more efficient manner.