What's the problem with my code?

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.

Hi, I took a different approach to your problem and it seems to work fine for me. I’m basically setting up the videoGrabber in ofApp, setting it to a ofTexture and passing the pointer to the ofTexture to my other classes. Here’s some very hurriedly written code…

Thing.h

class Thing{
public:
    Thing();
    void setup(ofTexture * _texture);
    ofTexture * texture;
    
    void draw();
};

Thing.cpp

#include "Thing.h"

Thing::Thing(){
    
}

void Thing::setup(ofTexture * _texture){
    texture = _texture;
}

void Thing::draw(){
    texture->draw(0, 0, texture->getWidth(), texture->getHeight());
}

ofApp.h (stuff I added)

ofVideoGrabber grabber;
ofTexture camTex;

Thing thing;

ofApp.cpp

//--------------------------------------------------------------
void ofApp::setup(){
    grabber.setDeviceID(0);
    grabber.setDesiredFrameRate(60);
    grabber.initGrabber(1024, 768);
    
    camTex.allocate(grabber.getWidth(), grabber.getHeight(), GL_RGBA);
    
    thing.setup(&camTex);
    
}

//--------------------------------------------------------------
void ofApp::update(){
    
    grabber.update();
    ofPixels grabberPixels;
    grabberPixels = grabber.getPixels();
    camTex.loadData(grabberPixels);

}

//--------------------------------------------------------------
void ofApp::draw(){
    thing.draw();

}

My webcam isn’t doing 60 fps though and I’m getting a bit of a bit of transparent strip in the bottom because my internal webcam is probably not properly set up to be 1024x768 but it does seem to work otherwise without any glitches. Maybe try this?

Quick edit: 1280x720 works fine!

Thanks a lot. I already thought about a similiar approach, using a texture pointer is a good idea.