Skip to content

Instantly share code, notes, and snippets.

@kashimAstro kashimAstro/main.cpp
Last active Feb 22, 2019

Embed
What would you like to do?
Simple Motion blur by (Keijiro Takahashi) rewritten in c++
/*
Rewritten Code by Keijiro Takahashi
source:
https://github.com/keijiro/sketches2016/blob/master/Moblur/Moblur.pde
*/
#include "ofMain.h"
#define N 13
class ofApp : public ofBaseApp{
public:
float gaussian[N];
float position(float t)
{
return (1.0 - cos(PI * 2 * t)) * 0.5;
}
void setup()
{
float sum = 0.0;
for (int i = 0; i < N; i++)
{
float d = (i + 0.5) * 2 / N - 1;
gaussian[i] = exp(-1.5 * d * d);
sum += gaussian[i];
}
for (int i = 0; i < N; i++)
gaussian[i] /= sum;
}
void update()
{
ofSetWindowTitle(ofToString(ofGetFrameRate()));
}
void draw()
{
float totalFrames = 13;
float t = (float)ofGetFrameNum() / totalFrames;
float delta = 1.0 / totalFrames;
if (ofGetFrameNum() > totalFrames * 2)
t = round((ofGetFrameNum() - totalFrames * 2) / 5) / totalFrames;
ofBackground(255);
drawBall(position(t), 100, 100, ofColor(0,0,0,255));
for (int i = 0; i < N; i++)
{
float dt = ((i + 0.5) / N - 0.5) * delta;
float w1 = 1.0 / N;
float w2 = (i + 0.5) * 2.0 / N;
if (w2 > 1) w2 = 2 - w2;
w2 *= 2.0 / N;
float w3 = gaussian[i];
drawBall(position(t + dt), 200, 100, ofColor(255,0,0,ofMap(w1,0.,1.,0,255)));
drawBall(position(t + dt), 300, 100, ofColor(0,255,0,ofMap(w2,0.,1.,0,255)));
drawBall(position(t + dt), 400, 100, ofColor(0,0,255,ofMap(w3,0.,1.,0,255)));
}
}
void drawBall(float x, float y, float w, ofColor color)
{
x = ofLerp(0.1, 0.95, x) * ofGetWidth();
ofSetColor(color);
ofDrawEllipse(x, y, w, w);
}
};
int main(){
ofSetupOpenGL(1024,768, OF_WINDOW);
ofRunApp( new ofApp());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.