Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Param manager #8

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions bin/data/johnWhitneyShader01/exampleCode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ void main()
vec3 color = vec3(0.);

float redVal = [[time]] * (uv.x * HORIZ_SCALE + RED_SCALE);
float redY = sin(redVal) * VERT_SCALE;
float redY = sin(redVal) * [[vertScale]];
color.r = drawLine(uv, redY);

float greenVal = [[time]] * (uv.x * HORIZ_SCALE + GREEN_SCALE);
float greenY = sin(greenVal) * VERT_SCALE;
float greenY = sin(greenVal) * [[vertScale]];
color.g = drawLine(uv, greenY);

float blueVal = [[time]] * (uv.x * HORIZ_SCALE + BLUE_SCALE);
float blueY = sin(blueVal) * VERT_SCALE;
float blueY = sin(blueVal) * [[vertScale]];
color.b = drawLine(uv, blueY);

gl_FragColor = vec4(color, 1.0);
Expand Down
8 changes: 5 additions & 3 deletions bin/data/johnWhitneyShader01/shader.frag
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ precision mediump float;
uniform float time;
uniform vec2 resolution;

uniform float vertScale;

float drawLine(vec2 uv, float y) {
float dist = abs(uv.y - y);
return smoothstep(STEP_MAX, STEP_MIN, dist);
Expand All @@ -30,15 +32,15 @@ void main()
vec3 color = vec3(0.);

float redVal = time * (uv.x * HORIZ_SCALE + RED_SCALE);
float redY = sin(redVal) * VERT_SCALE;
float redY = sin(redVal) * vertScale;
color.r = drawLine(uv, redY);

float greenVal = time * (uv.x * HORIZ_SCALE + GREEN_SCALE);
float greenY = sin(greenVal) * VERT_SCALE;
float greenY = sin(greenVal) * vertScale;
color.g = drawLine(uv, greenY);

float blueVal = time * (uv.x * HORIZ_SCALE + BLUE_SCALE);
float blueY = sin(blueVal) * VERT_SCALE;
float blueY = sin(blueVal) * vertScale;
color.b = drawLine(uv, blueY);

gl_FragColor = vec4(color, 1.0);
Expand Down
22 changes: 21 additions & 1 deletion d4nSFPCRunner.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
5CBB2AB3A60F65431D7B555D /* ofxButton.cpp in Sources */ = {isa = PBXBuildFile; fileRef = C88333E71C9457E441C33474 /* ofxButton.cpp */; };
63B57AC5BF4EF088491E0317 /* ofxXmlSettings.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 50DF87D612C5AAE17AAFA6C0 /* ofxXmlSettings.cpp */; };
7118B75E1C0519830092084A /* johnWhitneyShader01.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 7118B75C1C0519830092084A /* johnWhitneyShader01.cpp */; };
716EF7821C063048005FA544 /* animatedParameter.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 716EF7801C063048005FA544 /* animatedParameter.cpp */; };
716EF78B1C063E7A005FA544 /* easing.c in Sources */ = {isa = PBXBuildFile; fileRef = 716EF7891C063E7A005FA544 /* easing.c */; };
837220E80EB56CD44AD27F2A /* ofxSlider.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 15F2C6477A769C03A56D1401 /* ofxSlider.cpp */; };
856AA354D08AB4B323081444 /* ofxBaseGui.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 9604B925D32EE39065747725 /* ofxBaseGui.cpp */; };
85BCD90A1BF7E398004188EE /* yeseulScene.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 85BCD9081BF7E398004188EE /* yeseulScene.cpp */; };
Expand Down Expand Up @@ -77,6 +79,10 @@
52AFA1F08C420992CAAAE648 /* ofxSlider.h */ = {isa = PBXFileReference; explicitFileType = sourcecode.c.h; fileEncoding = 30; name = ofxSlider.h; path = ../../../addons/ofxGui/src/ofxSlider.h; sourceTree = SOURCE_ROOT; };
7118B75C1C0519830092084A /* johnWhitneyShader01.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = johnWhitneyShader01.cpp; sourceTree = "<group>"; };
7118B75D1C0519830092084A /* johnWhitneyShader01.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = johnWhitneyShader01.h; sourceTree = "<group>"; };
716EF7801C063048005FA544 /* animatedParameter.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = animatedParameter.cpp; sourceTree = "<group>"; };
716EF7811C063048005FA544 /* animatedParameter.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = animatedParameter.h; sourceTree = "<group>"; };
716EF7891C063E7A005FA544 /* easing.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = easing.c; sourceTree = "<group>"; };
716EF78A1C063E7A005FA544 /* easing.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = easing.h; sourceTree = "<group>"; };
78D67A00EB899FAC09430597 /* ofxLabel.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = ofxLabel.cpp; path = ../../../addons/ofxGui/src/ofxLabel.cpp; sourceTree = SOURCE_ROOT; };
802251BAF1B35B1D67B32FD0 /* ofxSliderGroup.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = ofxSliderGroup.cpp; path = ../../../addons/ofxGui/src/ofxSliderGroup.cpp; sourceTree = SOURCE_ROOT; };
832BDC407620CDBA568B713D /* tinyxmlerror.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; fileEncoding = 30; name = tinyxmlerror.cpp; path = ../../../addons/ofxXmlSettings/libs/tinyxmlerror.cpp; sourceTree = SOURCE_ROOT; };
Expand Down Expand Up @@ -194,6 +200,15 @@
path = johnWhitneyShader01;
sourceTree = "<group>";
};
716EF7881C063E7A005FA544 /* vendor */ = {
isa = PBXGroup;
children = (
716EF7891C063E7A005FA544 /* easing.c */,
716EF78A1C063E7A005FA544 /* easing.h */,
);
path = vendor;
sourceTree = "<group>";
};
85BCD9071BF7E398004188EE /* yeseulScene */ = {
isa = PBXGroup;
children = (
Expand All @@ -218,6 +233,8 @@
9238719F1BF7993A00171456 /* baseScene.h */,
923871A21BF7993A00171456 /* sceneManager.cpp */,
923871A31BF7993A00171456 /* sceneManager.h */,
716EF7801C063048005FA544 /* animatedParameter.cpp */,
716EF7811C063048005FA544 /* animatedParameter.h */,
);
path = scenes;
sourceTree = "<group>";
Expand Down Expand Up @@ -326,8 +343,9 @@
E4B69E1C0A3A1BDC003C02F2 /* src */ = {
isa = PBXGroup;
children = (
923871B81BF7D2C400171456 /* appConstants.h */,
9238719D1BF7993A00171456 /* scenes */,
716EF7881C063E7A005FA544 /* vendor */,
923871B81BF7D2C400171456 /* appConstants.h */,
E4B69E1D0A3A1BDC003C02F2 /* main.cpp */,
E4B69E1E0A3A1BDC003C02F2 /* ofApp.cpp */,
E4B69E1F0A3A1BDC003C02F2 /* ofApp.h */,
Expand Down Expand Up @@ -433,6 +451,7 @@
files = (
E4B69E200A3A1BDC003C02F2 /* main.cpp in Sources */,
E4B69E210A3A1BDC003C02F2 /* ofApp.cpp in Sources */,
716EF78B1C063E7A005FA544 /* easing.c in Sources */,
856AA354D08AB4B323081444 /* ofxBaseGui.cpp in Sources */,
5CBB2AB3A60F65431D7B555D /* ofxButton.cpp in Sources */,
D5F490EB1BF7E69B00F92C20 /* robbyRileyScene.cpp in Sources */,
Expand All @@ -452,6 +471,7 @@
923871B51BF7CCC900171456 /* emptyScene.cpp in Sources */,
923871A41BF7993A00171456 /* baseScene.cpp in Sources */,
5A4349E9754D6FA14C0F2A3A /* tinyxmlparser.cpp in Sources */,
716EF7821C063048005FA544 /* animatedParameter.cpp in Sources */,
D5F490E61BF7DE2D00F92C20 /* robbyMolnarScene.cpp in Sources */,
923871A61BF7993A00171456 /* sceneManager.cpp in Sources */,
7118B75E1C0519830092084A /* johnWhitneyShader01.cpp in Sources */,
Expand Down
57 changes: 57 additions & 0 deletions src/scenes/animatedParameter.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// animationTiming.cpp
// d4nSFPCRunner
//
// Created by Christopher Anderson on 11/25/15.
//
//

#include "animatedParameter.h"

animatedParameter::animatedParameter() : maxTime(0), loop(true)
{
}

void animatedParameter::addAnimation(float startTime, float endTime, float startVal, float endVal, AHEasingFunction f) {
animationTiming timing;

timing.startTime = startTime;
timing.endTime = endTime;
timing.startValue = startVal;
timing.endValue = endVal;
timing.easingFunction = f;

if (endTime > maxTime) {
maxTime = endTime;
}

timings.push_back(timing);
}

void animatedParameter::updateTime(float time) {
if (timings.size() == 0) {
return;
}

if (loop && time > maxTime) {
time = fmod(time, maxTime);
}

// Find the animation we're in right now
animationTiming *t = &timings[0];
for (auto &animationTiming : timings) {
if (time > animationTiming.startTime)
t = &animationTiming;
}

if (time < t->startTime) {
return t->startValue;
} else if (time > t->endTime) {
return t->endValue;
}

float p = (time - t->startTime) / (t->endTime - t->startTime);
float val = t->easingFunction(p) * (t->endValue - t->startValue) + t->startValue;

set(val);
}
32 changes: 32 additions & 0 deletions src/scenes/animatedParameter.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// animationTiming.hpp
// d4nSFPCRunner
//
// Created by Christopher Anderson on 11/25/15.
//
//

#pragma once

#include "ofMain.h"
#include "easing.h"

struct animationTiming {
float startTime, endTime;
float startValue, endValue;

AHEasingFunction easingFunction;
};

class animatedParameter : public ofParameter<float> {

public:
animatedParameter();

vector< animationTiming > timings;
float maxTime;
bool loop;

void addAnimation(float startTime, float endTime, float startVal, float endVal, AHEasingFunction f);
void updateTime(float time);
};
7 changes: 7 additions & 0 deletions src/scenes/baseScene.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,10 @@ string baseScene::getCodeWithParamsReplaced(){
return codeWithParams;

}

void baseScene::updateAnimatedParameters() {
float time = ofGetElapsedTimef();
for (auto animParam : animatedParameters) {
animParam->updateTime(time);
}
}
4 changes: 4 additions & 0 deletions src/scenes/baseScene.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
#pragma once

#include "ofMain.h"
#include "ofxXmlSettings.h"
#include "animatedParameter.h"

class baseScene {

Expand All @@ -26,5 +28,7 @@ class baseScene {
ofRectangle dimensions; // this is the dimensions of
// the surface you are drawing into.

void updateAnimatedParameters();
vector< animatedParameter* > animatedParameters;

};
17 changes: 14 additions & 3 deletions src/scenes/johnWhitneyShader01/johnWhitneyShader01.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,25 @@
void johnWhitneyShader01::setup(){

// setup pramaters
time.set("time", 0, 0, 3);
time.set("time", 0, 0, PI);
time.addAnimation(0, 4, 0, PI, QuadraticEaseInOut);
time.addAnimation(4, 8, PI, 0, QuadraticEaseInOut);
parameters.add(time);
animatedParameters.push_back(&time);

vertScale.set("vertScale", 0.15, 0, 1);
vertScale.addAnimation(8, 12, 0.15, 0.5, LinearInterpolation);
vertScale.addAnimation(12, 16, 0.5, 0.15, LinearInterpolation);
parameters.add(vertScale);
animatedParameters.push_back(&vertScale);

loadCode("johnWhitneyShader01/exampleCode.cpp");

shader.load("johnWhitneyShader01/shader");
}

void johnWhitneyShader01::update() {
time.set(ofMap(sin(ofGetElapsedTimef()), -1, 1, 0, PI));

}

void johnWhitneyShader01::draw() {
Expand All @@ -23,6 +32,8 @@ void johnWhitneyShader01::draw() {
shader.begin();
shader.setUniform1f("time", time.get());
shader.setUniform2fv("resolution", resolution);
shader.setUniform1f("vertScale", vertScale.get());

ofRect(0, 0, dimensions.width, dimensions.height);
shader.end();
}
2 changes: 1 addition & 1 deletion src/scenes/johnWhitneyShader01/johnWhitneyShader01.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ class johnWhitneyShader01 : public baseScene {
ofShader shader;
ofFbo fbo;

ofParameter<float> time;
animatedParameter time, vertScale;
};
7 changes: 6 additions & 1 deletion src/scenes/sceneManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,17 @@

void sceneManager::setup(){

// Has animated parameters
scenes.push_back(new johnWhitneyShader01());

// Old-style parameters
scenes.push_back(new robbyMolnarScene() );
scenes.push_back(new veraAnimatedScene() );
scenes.push_back(new exampleScene() );
scenes.push_back(new triangleScene() );
scenes.push_back(new robbyRileyScene() );
scenes.push_back(new yeseulScene() );
scenes.push_back(new johnWhitneyShader01());


sceneFbo.allocate(VISUALS_WIDTH, VISUALS_HEIGHT, GL_RGBA, 4);
codeFbo.allocate(VISUALS_WIDTH, VISUALS_HEIGHT, GL_RGBA, 4);
Expand Down Expand Up @@ -59,6 +63,7 @@ void sceneManager::setup(){

void sceneManager::update(){
scenes[currentScene]->update();
scenes[currentScene]->updateAnimatedParameters();
}

void sceneManager::draw(){
Expand Down
Loading