Last active
August 29, 2015 14:27
-
-
Save rettuce/bb8b17233137de776306 to your computer and use it in GitHub Desktop.
openFrameworks ofxSequenceExport.h / Multi Thread Sequence Exporter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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; } | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment