Skip to content

Instantly share code, notes, and snippets.

@roxlu
Created May 23, 2012 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save roxlu/2777136 to your computer and use it in GitHub Desktop.
Save roxlu/2777136 to your computer and use it in GitHub Desktop.
1D convection
#include "testApp.h"
void testApp::setup(){
ofSetFrameRate(60);
ofBackground(22,33,44);
gif.setup(ofGetWidth(), ofGetHeight());
record = false;
// setup some initial data.
dt = 0.1;
for(int i = 0; i < N; ++i) {
u[i] = ofNoise(dt * i);
}
}
void testApp::update(){
stepConvection(u);
}
void testApp::draw(){
drawConvection(u);
if(record && ofGetFrameNum() % 5 == 0) {
ofImage img;
img.grabScreen(0,0,ofGetWidth(), ofGetHeight());
gif.addFrame(img.getPixels());
}
}
void testApp::drawConvection(float* data) {
float s = ofGetWidth()/N;
float y = ofGetHeight() * 0.5;
y = 0;
float h = 100.0f;
// draw lines
glColor3f(1,1,1);
glBegin(GL_LINE_STRIP);
for(int i = 0; i < N; ++i) {
glVertex2f(i*s, y+data[i]*h);
}
glEnd();
// draw dots.
glColor3f(0,0.4,0.6);
glPointSize(5);
glBegin(GL_POINTS);
for(int i = 0; i < N; ++i) {
glVertex2f(i*s, y+data[i]*h);
}
glEnd();
}
void testApp::stepConvection(float* data) {
int dx = float(mouseX)/ofGetWidth() * N;
dx = std::min<int>(std::max<int>(0, dx), N-1);
data[0] = ofNoise(ofGetElapsedTimeMillis()*0.001)*2;
memcpy(u_copy, data, sizeof(float) * N);
for(int i = 1; i < N; ++i) {
data[i] = u_copy[i] - u_copy[i] * dt * (u_copy[i] - u_copy[i-1]);
}
}
void testApp::keyPressed(int key){
if(key == 'r') {
record = !record;
}
else if(key == 's') {
gif.save("wave_convection.gif");
record = false;
}
}
void testApp::keyReleased(int key){}
void testApp::mouseMoved(int x, int y){}
void testApp::mouseDragged(int x, int y, int button){ }
void testApp::mousePressed(int x, int y, int button){ }
void testApp::mouseReleased(int x, int y, int button){ }
void testApp::windowResized(int w, int h){ }
void testApp::gotMessage(ofMessage msg){ }
void testApp::dragEvent(ofDragInfo dragInfo){ }
#pragma once
#include "ofMain.h"
#include "Gif.h"
#define N 32
using namespace roxlu;
class testApp : public ofBaseApp{
public:
void setup();
void update();
void draw();
void keyPressed(int key);
void keyReleased(int key);
void mouseMoved(int x, int y);
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased(int x, int y, int button);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
float dt;
float u[N];
float u_copy[N];
void drawConvection(float* data);
void stepConvection(float* data);
Gif gif;
bool record;
};
@roxlu
Copy link
Author

roxlu commented May 23, 2012

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment