#include "ofApp.h"
void ofApp::setup(){
ofSetCircleResolution(40);
for (int i=1; i<TAIL_LENGTH; i++) {
waveHistory[i] = glm::vec3(0, 0, 0);
}
for (int i=1; i<WAVEFORM_HISTORY; i++) {
horWaveHistory[i] = 0;
vertWaveHistory[i] = 0;
}
center.x = (ofGetWidth()-LEFT_MARGIN)*0.5f +LEFT_MARGIN;
center.y = (ofGetHeight()-TOP_MARGIN)*0.5f + TOP_MARGIN;
bScaleMouse=false;
scale=1;
hWaveMult=(ofGetWidth()-LEFT_MARGIN)/float(WAVEFORM_HISTORY);
vWaveMult=(ofGetHeight()-TOP_MARGIN)/float(WAVEFORM_HISTORY);
selectedOscilator=-1;
bSelectedOscHor = false;
bSelectedOscVert = false;
ofEnableSmoothing();
ofEnableAlphaBlending();
ofSetVerticalSync(true);
}
void ofApp::update(){
for (unsigned int i=0; i<horizontalOscilators.size(); i++) {
horizontalOscilators[i].update();
}
for (unsigned int i=0; i<verticalOscilators.size(); i++) {
verticalOscilators[i].update();
}
}
void ofApp::draw(){
ofBackgroundGradient(ofColor(245), ofColor(200));
ofSetColor(80);
ofDrawBitmapString("Click here to add horizontal oscillators.", LEFT_MARGIN +100, TOP_MARGIN-5 );
ofDrawBitmapString("Click and drag\nover an\noscillator to\nmodify it's\nspeed and\namplitude", 15,25);
ofDrawBitmapString("Click in this area and drag upwards/downwards to scale up/down.\nPress spacebar to delete all the oscillators.", LEFT_MARGIN + 200, ofGetHeight()-40);
ofPushMatrix();
ofTranslate(LEFT_MARGIN -5, ofGetHeight() - 100, 0);
ofRotateDeg(-90, 0, 0, 1);
ofDrawBitmapString("Click here to add vertical oscillators.", 0, 0 );
ofPopMatrix();
ofEnableSmoothing();
ofSetColor(0, 0, 0, 150);
ofDrawLine(LEFT_MARGIN, 0, LEFT_MARGIN, ofGetHeight());
ofDrawLine(0, TOP_MARGIN, ofGetWidth(), TOP_MARGIN);
ofSetColor(0, 0, 0, 80);
ofDrawLine(LEFT_MARGIN, center.y, ofGetWidth(), center.y);
ofDrawLine(center.x, TOP_MARGIN, center.x, ofGetHeight());
float horWave = 0;
float vertWave = 0;
for (unsigned int i=0; i<horizontalOscilators.size(); i++) {
ofSetColor(255, 127+i, 0,150);
horizontalOscilators[i].draw();
horWave += horizontalOscilators[i].waveSin;
}
for (unsigned int i=0; i<verticalOscilators.size(); i++) {
ofSetColor(0, 127+i, 255, 150);
verticalOscilators[i].draw();
vertWave += verticalOscilators[i].waveSin;
}
for (int i=1; i<TAIL_LENGTH; i++) {
waveHistory[i-1] = waveHistory[i];
}
for (int i=1; i<WAVEFORM_HISTORY; i++) {
horWaveHistory[i-1] = horWaveHistory[i];
vertWaveHistory[i-1]= vertWaveHistory[i];
}
horWaveHistory[WAVEFORM_HISTORY-1] = horWave;
vertWaveHistory[WAVEFORM_HISTORY-1] = vertWave;
waveHistory[TAIL_LENGTH-1] = glm::vec3(horWave, vertWave,0);
ofMesh wave;
wave.setMode(OF_PRIMITIVE_LINE_STRIP);
for (int i=0; i<TAIL_LENGTH; i++) {
wave.addColor(ofFloatColor(0.1f,0.1f,0.1f, 0.5f + 0.5f * i/float(TAIL_LENGTH) ));
wave.addVertex(waveHistory[i]);
}
ofMesh hWave;
hWave.setMode(OF_PRIMITIVE_LINE_STRIP);
ofMesh vWave;
vWave.setMode(OF_PRIMITIVE_LINE_STRIP);
for (int i=0; i<WAVEFORM_HISTORY; i++) {
hWave.addColor(ofFloatColor(255, 240,10, 255));
hWave.addVertex(glm::vec3(i*hWaveMult, horWaveHistory[i]*0.1f*scale, 0));
vWave.addColor(ofFloatColor(255, 240,10, 255));
vWave.addVertex(glm::vec3(vertWaveHistory[i]*0.1f*scale, i*vWaveMult, 0));
}
ofPushMatrix();
ofTranslate(LEFT_MARGIN, TOP_MARGIN, 0);
hWave.draw();
vWave.draw();
ofPopMatrix();
ofPushMatrix();
ofTranslate(center.x, center.y, 0);
ofScale(scale, scale, 0);
wave.draw();
ofSetColor(0,10, 255),
ofDrawCircle(horWave, vertWave, 10);
ofPopMatrix();
}
void ofApp::keyPressed(int key){
}
void ofApp::keyReleased(int key){
if (key == ' ') {
horizontalOscilators.clear();
verticalOscilators.clear();
}
}
void ofApp::mouseMoved(int x, int y ){
}
void ofApp::mouseDragged(int x, int y, int button){
if (selectedOscilator>-1) {
if(bSelectedOscHor==true){
horizontalOscilators[selectedOscilator].freq += 0.1f * (ofGetPreviousMouseX() - ofGetMouseX())/ float(ofGetWidth());
horizontalOscilators[selectedOscilator].amplitude += ofGetMouseY() - ofGetPreviousMouseY();
}else if (bSelectedOscVert==true) {
verticalOscilators[selectedOscilator].freq += 0.1f * (ofGetPreviousMouseY() - ofGetMouseY())/ float(ofGetHeight());
verticalOscilators[selectedOscilator].amplitude += ofGetMouseX() - ofGetPreviousMouseX();
}
}else if (bScaleMouse) {
scale += float(ofGetMouseY()-ofGetPreviousMouseY())/ofGetHeight();
}
}
void ofApp::mousePressed(int x, int y, int button){
if (y< TOP_MARGIN && x>LEFT_MARGIN) {
for (unsigned int i = 0; i < horizontalOscilators.size(); i++) {
if(horizontalOscilators[i].checkOver(x, y)){
setPressedOscilator(i, true);
break;
}
}
if (!bSelectedOscHor) {
horizontalOscilators.push_back(oscillator());
horizontalOscilators.back().setup(x, y);
}
}else if(y>TOP_MARGIN && x<LEFT_MARGIN){
for (unsigned int i = 0; i < verticalOscilators.size(); i++) {
if(verticalOscilators[i].checkOver(x, y)){
setPressedOscilator(i, false);
break;
}
}
if (!bSelectedOscVert) {
verticalOscilators.push_back(oscillator());
verticalOscilators.back().setup(x, y);
}
}else {
bScaleMouse=true;
}
}
void ofApp::setPressedOscilator(int index, bool isHorizontal){
selectedOscilator=index;
bSelectedOscHor =isHorizontal;
bSelectedOscVert =!isHorizontal;
}
void ofApp::mouseReleased(int x, int y, int button){
bScaleMouse=false;
selectedOscilator=-1;
bSelectedOscHor=false;
bSelectedOscVert=false;
}
void ofApp::mouseEntered(int x, int y){
}
void ofApp::mouseExited(int x, int y){
}
void ofApp::windowResized(int w, int h){
}
void ofApp::gotMessage(ofMessage msg){
}
void ofApp::dragEvent(ofDragInfo dragInfo){
}
Comments