Saving individual frames

Hi,

I’m having trouble implementing ofSaveFrame(). I throw up in the ofApp.cpp in the update() but all the saved frames are coming out black. I’ve set the framerate to 29. Thoughts?

Thanks for you help - Kirby

Hi Kirby,

What are you drawing in screen?

also, can you provide a small example that shows this behavior? I’d be interested to see where are you calling save frame from…

Hi!
I’m calling ofSaveFrame() from within ofApp in the draw. I have a small object class in a header file called “Molecule.h.” I then call several instances of the ‘molecule’ from within the ofApp. It’s the strangest thing as I’ve opened other applications I’ve made that I saved out frames and those continue to function.

#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
    ofSetFrameRate(24);
    ofSetWindowShape(530, 800);
    ofBackground(0, 0, 28);
    
    for(int i=0; i<NUM_MOL; i++){
        molecules[i].setup( nucleus[i] );
    }
}

//--------------------------------------------------------------
void ofApp::update(){
    for(int i=0; i<NUM_MOL; i++){
        molecules[i].update();
    }
}

//--------------------------------------------------------------
void ofApp::draw(){
    for(int i=0; i<NUM_MOL; i++){
        molecules[i].draw();
    }
    ofSaveFrame();
}

[code]
// Molecule.h

#include “ofMain.h”
#ifndef Molecule_h
#define Molecule_h
#define NUM_ORBITS 4

class Molecule{

public:
float ampA[NUM_ORBITS], ampB[NUM_ORBITS];
float angle[NUM_ORBITS], angleAdder[NUM_ORBITS];
float ampAdder[NUM_ORBITS];
float orbitAngle[NUM_ORBITS];
float orbitAngleAdder;
int electrons[NUM_ORBITS];
float electronInterval[NUM_ORBITS];
ofColor color;
ofPoint nucleus;
int time;

//--------------------------------------------------------------
void setup(ofPoint _nucleus){
    
    nucleus.x = _nucleus.x;
    nucleus.y = _nucleus.y;
    
    for(int i=0; i<NUM_ORBITS; i++){
        angle[i] = 0;
        angleAdder[i] = ofRandom(.01, .05);
        
        orbitAngle[i] = ofRandom(-1 * TWO_PI, TWO_PI);
        
        electrons[i] = 2 + 2*i;
        electronInterval[i] = TWO_PI/electrons[i];

    }
    
    color.r = 42;
    color.g = 88;
    color.b = 255;
    

    ampA[0] = 10;
    ampB[0] = 10;
    ampA[1] = 10;
    ampB[1] = -10;
    ampA[2] = -10;
    ampB[2] = 10;
    ampA[3] = -10;
    ampB[3] = -10;
    
    ampAdder[0] = .05;
    ampAdder[1] = .07;
    ampAdder[2] = .09;
    ampAdder[3] = .11;
    
    orbitAngleAdder = .01;

    nucleusSpeed.x = ofRandom(0,0);
    nucleusSpeed.y = ofRandom(-1,1);

    reverse = false;
}

//--------------------------------------------------------------
void update(){
    
    for(int i=0; i<NUM_ORBITS; i++){
        if (orbitAngle[i] > TWO_PI){
            orbitAngle[i] = 0;
        }
         orbitAngle[i] += orbitAngleAdder;
        
        if (angle[i] > TWO_PI) {
            angle[i] = 0;
        }
        angle[i] += angleAdder[i];
        
        ampA[i] += ampAdder[i];
        
    }
}

//--------------------------------------------------------------
void draw(){
    
    ofSetColor(color.r, color.g, color.b);
    ofFill();
    
    for(int j=0; j<NUM_ORBITS; j++){
        
        for (int i=0; i < electrons[j]; i++) {
            ofDrawCircle(nucleus.x + ampA[j] * cos(angle[j] + (i * electronInterval[j])) * cos(orbitAngle[j]) - ampB[j] * sin (angle[j] + (i * electronInterval[j])) * sin(orbitAngle[j]), nucleus.y + ampA[j] * sin(angle[j] + (i * electronInterval[j])) * sin(orbitAngle[j]) + ampB[j] * sin(angle[j] + (i * electronInterval[j])) * cos(orbitAngle[j]), 1);
        }
    }
}

};

#endif /* Molecule_h */[/code]

I’m not sure why ofSaveFrame doesn’t work for you, but you can try using grabScreen():

ofImage screen;
screen.grabScreen(0, 0, ofGetScreenWidth(), ofGetScreenHeight());
screen.saveImage("capture.png");

Hi gnouc_

Thank you for the suggestion, I tried it, the ofGetScreenWidth() / ofGetScreenHeight() did return an image, but it was very distorted. I then tried changing it to ofGetWidth() / ofGetHeight() and it resulted in the the correctly proportioned size, but the image was still black.

Update, I went ahead and started a new sketch to try and suss out where the error was happening. I started by testing out ofSaveFrame() with a really simple sketch drawing a circle. Sometimes ofSaveFrame() worked and sometimes it didn’t, but what I think it came down to was the debug app. I noticed that there were two in the bin folder, one that said “emptyExampleDebug” and one that said “emptyExample”. The sketch that output to “emptyExample” saved frames just fine while sketch that output to “emptyExampleDebug” did not. Not sure how to control where the sketch is rendered to, but that’s my best guess for the issue.

can you upload a zip file of what you mean or put the full code up? also, are you sure that your debug app is up to date with release (maybe it’s an older sketch) ? there is a drop down in xcode where you can set the target to be debug or release:

Hi Zach,
Here are the source files for the sketch that is rendering out all black frames.
atom-src.zip (3.5 KB)

The only difference I can see between the two is in the bin folder. The one with the working frame capture has both an emptyExample and an emptyExampleDebug. I believe I’m working out of the latest release (of_v0.9.4) and it’s a new sketch. When I go up to the schemes, I see the 64/32 options, but there is only one build option and it doesn’t have a “Debug” or “Release” suffix. I tried out creating a ‘new scheme’ to see if the debug/release specificity would be an option, but nope. Scheme structure is the same for the sketch that is working.

Thank you thank you for your help :slight_smile: much appreciated.

Hey there,

Running into the same problem with another sketch. I’ve been trying different things with the build settings to no avail. I uploaded the whole deal to dropbox https://www.dropbox.com/s/dwg1smgc6qykzpo/rose_2.zip?dl=0
I’m working on a project where my workflow is to make a sketch in oF, save frame, create a stop motion video from the frames and then edit to music.

Thanks in advance for your time,
Kirby

I experienced this same issue yesterday–with OS X 10.9 and OF 9.7.

From mucking around, it appears to have something to do with the window size (or maybe it’s just voodoo). For example, with a window that is 850 x 800–ofImage::grabScreen() saves black frames. But if the window size is 800x800, it works.

The issue seems to be taking place inside ofRendereder::saveScreen() and happens with both the programmable renderer and the GL renderer. If I alter the function, removing buffer.bind(), and set glReadPixels to write directly to the ofPixels buffer, then it works overtime.