This repository has been archived by the owner on Oct 10, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
58a9084
commit 762f907
Showing
7 changed files
with
297 additions
and
200 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,5 +13,6 @@ | |
#include "basicEffect.h" | ||
#include "shaderEffect.h" | ||
#include "videoEffect.h" | ||
#include "gifEffect.h" | ||
|
||
#endif |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
// | ||
// gifEffect.cpp | ||
// karmaMapper | ||
// | ||
// Created by Daan de Lange on 06/06/2014. | ||
// | ||
// - - - - | ||
// | ||
// Parent class for all effects. | ||
// Implements some standard methods for overall usage. | ||
// | ||
|
||
#include "gifEffect.h" | ||
|
||
gifEffect::gifEffect(){ | ||
basicEffect::basicEffect(); | ||
|
||
directory = ""; | ||
gifFiles.resize(0); | ||
shader.load("./gifEffect/videoShader"); | ||
timePerFrame = 0.05; | ||
loopCount = 0; | ||
loopStartTime = ofGetElapsedTimef(); | ||
cachedGif = -1; | ||
cachedFiles.resize(2); | ||
|
||
numLoopsPerGif = 4; | ||
for(int i=0;i<cachedFiles.size();i++) cachedFiles[i].setBackgroundColor( ofColor(0,0) ); | ||
} | ||
|
||
gifEffect::~gifEffect(){ | ||
basicEffect::~basicEffect(); | ||
} | ||
|
||
// spawns the effect @ the scene so it starts rendering (setup) | ||
// overrule this function with your own. | ||
void gifEffect::spawn(){ | ||
basicEffect::spawn(); | ||
} | ||
|
||
// update --> animation | ||
void gifEffect::render(){ | ||
// todo: what if gif contains only 1 frame ? | ||
if( pShape==NULL || cachedGif<0 || cachedGif >= cachedFiles.size() || cachedFiles[cachedGif].getNumFrames() < 1 ) return; | ||
|
||
//int currentFrame; | ||
|
||
// looped ? | ||
if(loopStartTime + timePerFrame*numLoopsPerGif*cachedFiles[cachedGif].getNumFrames() < ofGetElapsedTimef() ){ | ||
//cachedGif++; | ||
//cachedGif = cachedGif%cachedFiles.size(); | ||
|
||
if( loadGif( (currentGif+1)%gifFiles.size() ) ){ | ||
loopStartTime = ofGetElapsedTimef(); | ||
} | ||
// todo: skip gif if stuck/fail | ||
else { | ||
|
||
} | ||
} | ||
|
||
// calc current frame Nb | ||
//timePerFrame = cachedFiles[cachedGif].getDuration() / cachedFiles[cachedGif].getNumFrames(); | ||
int currentFrame = (int) floor( (ofGetElapsedTimef() - loopStartTime) /(timePerFrame) ) % (int) cachedFiles[cachedGif].getNumFrames(); | ||
|
||
|
||
// load shader | ||
shader.begin(); | ||
shader.setUniformTexture("tex0", *cachedFiles[cachedGif].getFrameAt(currentFrame)->getRawTexture() , 0); | ||
shader.setUniform2f("resolution", pShape->boundingBox.width, pShape->boundingBox.height); | ||
shader.setUniform2f("textureResolution", cachedFiles[cachedGif].getWidth(), cachedFiles[cachedGif].getHeight() ); | ||
shader.setUniform2f("shapeCenterOffset", pShape->getCenterOffsetFromBoundingBox().x, pShape->getCenterOffsetFromBoundingBox().y); | ||
shader.setUniform1f("textureScale", 1); | ||
|
||
// draw shape so GPU gets their vertex data | ||
pShape->render(); | ||
|
||
// flush the pipeline! :D | ||
shader.end(); | ||
|
||
} | ||
|
||
void gifEffect::update(){ | ||
basicEffect::update(); | ||
|
||
//stream.update(); | ||
|
||
// tmp | ||
if( ofGetKeyPressed('r') ){ | ||
shader.load("./gifEffect/videoShader"); | ||
cout << "Shader reloaded"<<endl; | ||
} | ||
} | ||
|
||
// resets all values | ||
// overrule this function with your own. | ||
void gifEffect::reset(){ | ||
basicEffect::reset(); | ||
} | ||
|
||
// called just before removal | ||
void gifEffect::destroy(){ | ||
basicEffect::destroy(); | ||
} | ||
|
||
bool gifEffect::setDirectory(string _dir){ | ||
// already set ? | ||
if(_dir==directory) return true; | ||
|
||
ofDirectory dir(_dir); | ||
|
||
|
||
if( !dir.exists() || !dir.canRead() ){ | ||
ofLogNotice("gifEffect") << "Unable to load " << _dir << endl; | ||
return false; | ||
} | ||
else{ | ||
// default video path | ||
if(directory=="") directory="./gifEffect/gifs/"; | ||
|
||
dir.allowExt("gif"); | ||
dir.sort(); | ||
dir.listDir(_dir); | ||
|
||
|
||
if(dir.size()==0){ | ||
if(!dir.exists()){ | ||
ofLogNotice("gifEffect") << "Folder «" << _dir << "» doesn't exist." << endl; | ||
} | ||
else if(!dir.canRead()){ | ||
ofLogNotice("gifEffect") << "Folder «" << _dir << "» is not readable." << endl; | ||
} | ||
else if(!dir.isDirectory()){ | ||
ofLogNotice("gifEffect") << "Folder «" << _dir << "» is not a directory." << endl; | ||
} | ||
else ofLogNotice("gifEffect") << "Folder «" << _dir << "» gif files." << endl; | ||
} | ||
else ofLogVerbose("gifEffect") << "Scanning «" << _dir << "» ... Found " << ofToString(dir.size()) << " file(s) (.gif) [readable=" << dir.canRead() << "] [isDirectory=" << ofToString(dir.isDirectory()) << "]" << endl; | ||
|
||
// get videos | ||
gifFiles.resize(0); | ||
for(int i=0; i<dir.size(); i++){ | ||
// test videos before adding... HEAVY (remove this check for performence?) | ||
//if(decoder.decode( dir.getPath(i) )){ | ||
gifFiles.push_back( dir.getName(i) ); | ||
//decoder.reset(); | ||
//} | ||
//else { | ||
// ofLogNotice("gifEffect") << "Could not add " << dir.getPath(i); | ||
//} | ||
} | ||
|
||
directory = _dir; | ||
|
||
//loadRandomGif(); | ||
loadGif(0); | ||
} | ||
|
||
return true; | ||
} | ||
|
||
bool gifEffect::loadRandomGif(){ | ||
return loadGif( (int)ofRandom(-.49, gifFiles.size()-.51) ); | ||
} | ||
|
||
bool gifEffect::loadGif(int gifID){ | ||
if(gifID < 0 || gifID >= gifFiles.size() ) return false; | ||
|
||
string tmp = ofToString(directory+"/"+gifFiles[gifID]); | ||
if( decoder.decode( tmp ) ){ | ||
cachedGif = (cachedGif+1)%cachedFiles.size(); | ||
cachedFiles[ cachedGif ] = decoder.getFile(); | ||
pShape->hasError = false; | ||
ofLogVerbose("gifEffect") << "Successfully loaded movie: " << gifFiles[gifID] << endl; | ||
currentGif = gifID; | ||
|
||
return true; | ||
} | ||
else{ | ||
ofLogNotice("gifEffect") << "Could not load "+ directory+"/"+gifFiles[gifID] << endl;; | ||
pShape->hasError = true; | ||
cachedGif = -1; | ||
return false; | ||
} | ||
} | ||
|
||
// not: string must exist in gifFiles | ||
bool gifEffect::loadGif(string videoName){ | ||
int found = -1; | ||
|
||
for(int i=0; i<gifFiles.size(); i++){ | ||
if(gifFiles[i]==videoName){ | ||
found=i; | ||
break; | ||
} | ||
} | ||
|
||
return (found<0)?false:loadGif(found); | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// | ||
// gifEffect.h | ||
// karmaMapper | ||
// | ||
// Created by Daan de Lange on 06/06/2014. | ||
// | ||
// | ||
|
||
#pragma once | ||
|
||
#include "ofMain.h" | ||
#include "basicEffect.h" | ||
#include "ofxGifDecoder.h" | ||
|
||
class gifEffect : public basicEffect { | ||
|
||
public: | ||
gifEffect(); | ||
~gifEffect(); | ||
|
||
virtual void spawn(); | ||
virtual void render(); | ||
virtual void update(); | ||
virtual void reset(); | ||
virtual void destroy(); | ||
|
||
bool setDirectory(string _dir); | ||
bool loadRandomGif(); | ||
bool loadGif(int videoID); | ||
bool loadGif(string videoName); | ||
|
||
protected: | ||
vector<string> gifFiles; | ||
string directory; | ||
ofShader shader; | ||
|
||
unsigned int numLoopsPerGif; | ||
unsigned int loopCount; | ||
unsigned int currentGif; | ||
float loopStartTime; | ||
float timePerFrame; | ||
ofxGifDecoder decoder; | ||
vector<ofxGifFile> cachedFiles; | ||
int cachedGif; // cachedFiles is for precaching... this tells which one is being used | ||
}; |
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
Oops, something went wrong.