Skip to content

Instantly share code, notes, and snippets.

@rettuce
Last active November 13, 2024 10:39
Show Gist options
  • Save rettuce/bb8b17233137de776306 to your computer and use it in GitHub Desktop.
Save rettuce/bb8b17233137de776306 to your computer and use it in GitHub Desktop.

Revisions

  1. rettuce renamed this gist Aug 13, 2015. 1 changed file with 0 additions and 0 deletions.
  2. rettuce renamed this gist Aug 13, 2015. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. rettuce revised this gist Aug 13, 2015. No changes.
  4. rettuce revised this gist Aug 13, 2015. 1 changed file with 79 additions and 0 deletions.
    79 changes: 79 additions & 0 deletions ofApp.cpp
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,79 @@

    #include "ofMain.h"
    #include "ofxSequenceExport.h"

    const unsigned int WIDTH = 1920;
    const unsigned int HEIGHT = 1080;

    class ofApp : public ofBaseApp
    {
    ofxSequenceExport expo;
    ofFbo fbo;

    public:

    ofApp(){}

    void exit() {
    expo.close();
    }

    void setup()
    {
    ofSetFrameRate(60);
    ofBackground(0);

    fbo.allocate( WIDTH, HEIGHT, GL_RGBA );
    expo.setup( "sequence/test/", "png" );
    }

    void update()
    {
    fbo.begin();
    {
    ofClear(0);

    // drawing
    {
    for (int i=0; i<100; i++) {
    ofSetColor(ofRandom(255), ofRandom(255), ofRandom(255));
    ofCircle(ofRandom(WIDTH), ofRandom(HEIGHT), ofRandom(50));
    }
    }
    }
    fbo.end();

    if(expo.running()) expo.addQue( &fbo );
    }

    void draw()
    {
    ofSetColor(255);
    fbo.draw(0,0);
    ofDrawBitmapString(ofToString(ofGetFrameRate()), WIDTH-80, 30);
    ofDrawBitmapString("renderFrame/addFrame "+ofToString(expo.getRenderFrame())+" / "+ofToString(expo.getAddFrame()),
    30, 30 );
    }

    void keyPressed(int key)
    {
    switch (key)
    {
    case 's':
    if(expo.running()) expo.stop();
    else expo.start();
    break;
    }
    };
    };

    #include "ofAppGLFWWindow.h"
    int main(int argc, const char** argv)
    {
    ofAppGLFWWindow window;
    window.setMultiDisplayFullscreen(true);
    window.setNumSamples(4);
    ofSetupOpenGL(&window, WIDTH, HEIGHT, OF_WINDOW);
    ofRunApp(new ofApp);
    return 0;
    }
  5. rettuce created this gist Aug 13, 2015.
    120 changes: 120 additions & 0 deletions ofxSequenceExport.h
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,120 @@
    //
    // ofxSequenceExport.h
    // base : https://github.com/atduskgreg/ofxImageSequenceRecorder
    //
    #pragma once

    const unsigned int MULTI_THREAD_NUM = 6;

    typedef struct{
    string fName;
    ofPixels pixs;
    } QueImg;

    class SequenceExport : public ofThread
    {
    public:
    deque<QueImg> ques;
    int renderFrame = 0;

    void close(){ stop(); }
    bool running(){ return isThreadRunning(); }

    void start() {
    renderFrame = 0;
    this->startThread(false,true); // blocking, verbose
    }
    void stop() {
    this->stopThread();
    }

    void threadedFunction()
    {
    while(isThreadRunning()) {
    if(!ques.empty())
    {
    QueImg i = ques.front();
    ofSaveImage( i.pixs, i.fName);
    ques.pop_front();

    if( lock() ){
    renderFrame++;
    unlock();
    }
    }
    }
    }
    };

    class ofxSequenceExport
    {
    SequenceExport expos[ MULTI_THREAD_NUM ];

    string outpath,format;
    int addFrame, renderFrame;
    bool isThreadRunning = false;

    public:

    void setup( string outpath_, string format_ ) {
    addFrame = 0;
    renderFrame = 0;
    outpath = outpath_;
    format = format_;
    }

    void close(){
    stop();
    }

    void start()
    {
    addFrame = 0;
    renderFrame = 0;
    isThreadRunning = true;

    for (int i=0; i<MULTI_THREAD_NUM; i++) {
    expos[i].start();
    }
    }
    void stop(){
    isThreadRunning = false;
    for (int i=0; i<MULTI_THREAD_NUM; i++) {
    expos[i].stop();
    }
    }

    void addQue(ofFbo *fbo){
    ofPixels pixs;
    fbo->readToPixels( pixs );
    addQue( pixs );
    }

    void addQue(ofPixels pixs)
    {
    assert( outpath.length()>0 );
    assert( format.length()>0 );

    char fName[100];
    sprintf(fName, "%s%.6i.%s" , outpath.c_str(), addFrame, format.c_str() );

    QueImg que;
    que.fName = fName;
    que.pixs = pixs;
    expos[ addFrame%MULTI_THREAD_NUM ].ques.push_back( que );

    addFrame++;
    getRenderFrame();
    }

    int getRenderFrame() {
    renderFrame = 0;
    for (int i=0; i<MULTI_THREAD_NUM; i++) {
    renderFrame += expos[i].renderFrame;
    }
    return renderFrame;
    }
    int getAddFrame() { return addFrame; }

    bool running(){ return isThreadRunning; }
    };