Hi,
I have a setup where I need the texture of a ofVideoGrabber in several other cpp files. So my idea was to create a static variable which holds my ofVideoGrabber class and I have a accessor function which creates and returns the static variable if it is needed. I was able to reproduce the issue in a small file. Here is my header:
#pragma once
#include "ofMain.h"
#include <memory>
class Thing {
public:
Thing() {}
~Thing() {}
virtual void draw() {};
virtual void update() {};
};
class Video : public Thing {
public:
Video();
virtual void update();
virtual void draw();
ofVideoGrabber camera;
};
typedef std::shared_ptr<Thing> ThingPtr;
class ofApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
};
static ThingPtr cameraThing = nullptr;
And in my cpp file I have this:
#include "ofApp.h"
// create an instance of the video class if needed.
ThingPtr getCamera()
{
if (cameraThing == nullptr)
{
cameraThing = ThingPtr(new Video());
}
return cameraThing;
}
Video::Video()
{
camera.setup(1280,1024, true);
}
void Video::update()
{
camera.update();
}
void Video::draw()
{
camera.draw(0, 0);
}
//--------------------------------------------------------------
void ofApp::setup(){
}
//--------------------------------------------------------------
void ofApp::update(){
getCamera()->update();
}
//--------------------------------------------------------------
void ofApp::draw(){
getCamera()->draw();
}
If I am in debug mode I can see that all methods are executed correctly, the creator, the update and the draw method, unfortunately what I get is htis:
And it seems to depend on capture size. With 1280x1024 I the strange file, with 640x480 I get the correct camera image. In some cases, the image is first crap and later it switches to the correct image.
The camera is okay and everything works fine if I use the ofVideoGrabber directly. So I suppose this is an issue in my code and I have a general problem to understand how I sould build my code in this case. If anyone has an idea where the problem lies, please let me know. Thanks.