Skip to content

Instantly share code, notes, and snippets.

@vanderlin
Created August 27, 2018 13:39
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 vanderlin/87253102766f80644c41749106830c3d to your computer and use it in GitHub Desktop.
Save vanderlin/87253102766f80644c41749106830c3d to your computer and use it in GitHub Desktop.
#include "ofApp.h"
//--------------------------------------------------------------
void ofApp::setup(){
ofBackground(246, 189, 208);
gui.setup();
gui.add(value.set("value", 2.5, 0.00001, 3));
gui.add(speed.set("speed", 0.045, 0.00001, 3));
gui.add(drawModes.set("draw modes", 0, 0, 10));
gui.add(pointSize.set("point size", 1, 0.5, 55));
gui.add(enableAntiAliasing.set("anti aliasing", true));
drawModeName = "drawmode";
noisePhase = 0;
}
//--------------------------------------------------------------
void ofApp::update(){
if(enableAntiAliasing) {
ofEnableAntiAliasing();
} else {
ofDisableAntiAliasing();
}
noisePhase += speed;
for(auto &particle : particles) {
float xx = ofMap(particle.p.x, 0, ofGetWidth(), 0, 1) * value;
float yy = ofMap(particle.p.y, 0, ofGetHeight(), 0, 1) * value;
float nx = ofSignedNoise(xx + particle.u, yy, particle.u);
float ny = ofNoise(yy, xx + particle.u, particle.u);
particle.v.x = nx * 2;
particle.v.y = ny * 10;
ofVec2f vec = ofVec2f(ofGetMouseX(), ofGetMouseY()) - particle.p;
float len = vec.length();
if (len < 100) {
vec.normalize();
particle.v -= vec * 8;
}
particle.p += particle.v;
if (particle.p.x > ofGetWidth()) {
particle.p.x = 0;
}
if (particle.p.x < 0) {
particle.p.x = ofGetWidth();
}
if (particle.p.y > ofGetHeight()) {
particle.p.y = 0;
}
if (particle.p.y < 0) {
particle.p.y = ofGetHeight();
}
}
}
//--------------------------------------------------------------
void ofApp::draw(){
if (drawModes == 0) {
drawModeName = "immediate mode";
glPointSize(pointSize);
ofSetColor(88);
glBegin(GL_POINTS);
for(auto &particle : particles) {
glVertex3f(particle.p.x, particle.p.y, 0);
}
glEnd();
glPointSize(1);
}
if (drawModes == 1) {
drawModeName = "vbo mesh";
if (mesh.getVertices().size() != particles.size()) {
mesh.getVertices().resize(particles.size());
mesh.setUsage(GL_DYNAMIC_DRAW);
mesh.setMode(OF_PRIMITIVE_POINTS);
cout << "allocate " << endl;
}
for(int i=0; i<particles.size(); i++) {
mesh.setVertex(i, ofVec3f(particles[i].p.x, particles[i].p.y, 0));
}
glPointSize(pointSize);
ofSetColor(88);
mesh.draw();
glPointSize(1);
}
/*int step = 3;
noisePhase += speed;
for(int x=0; x<ofGetWidth(); x+=step) {
for(int y=0; y<ofGetHeight(); y+=step) {
float xx = ofMap(x, 0, ofGetWidth(), 0, 1);
float yy = ofMap(y, 0, ofGetHeight(), 0, 1);
float nx = ofNoise(noisePhase - (xx * value), yy * value) * 10;
float ny = ofNoise(xx * value, yy * value) * 10;
ofDrawLine(x, y, x+nx, y+ny);
}
}*/
string info = "fps " + ofToString(ofGetFrameRate(), 1) + "\n";
info += ofToString((int)particles.size()) +" particles \n";
info += drawModeName+" "+ofToString(drawModes);
ofDrawBitmapStringHighlight(info, gui.getPosition().x, gui.getPosition().y + gui.getHeight() + 20);
gui.draw();
}
//--------------------------------------------------------------
void ofApp::keyPressed(int key){
if (key == ' ') {
for(int i=0; i<1000; i++) {
Particle particle(ofGetMouseX() + ofRandom(-30, 30), ofGetMouseY() + ofRandom(-30, 30));
particles.push_back(particle);
}
}
if (key == 'c') {
particles.clear();
}
}
//--------------------------------------------------------------
void ofApp::keyReleased(int key){
}
//--------------------------------------------------------------
void ofApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void ofApp::mouseDragged(int x, int y, int button){
Particle p(ofGetMouseX() + ofRandom(-30, 30), ofGetMouseY() + ofRandom(-30, 30));
particles.push_back(p);
}
//--------------------------------------------------------------
void ofApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void ofApp::mouseReleased(int x, int y, int button){
}
//--------------------------------------------------------------
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){
}
#pragma once
#include "ofMain.h"
#include "ofxGui.h"
class Particle {
public:
Particle(float x, float y) {
u = ofRandomf();
p.set(x, y);
}
float u;
ofVec2f p, v;
};
class ofApp : 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 mouseEntered(int x, int y);
void mouseExited(int x, int y);
void windowResized(int w, int h);
void dragEvent(ofDragInfo dragInfo);
void gotMessage(ofMessage msg);
ofxPanel gui;
ofParameter<int> drawModes;
ofParameter<bool> enableAntiAliasing;
ofParameter<float> pointSize;
ofParameter<float> value, speed;
ofVboMesh mesh;
float noisePhase;
string drawModeName;
vector <Particle> particles;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment