[ Video ]
[ About ]
昨日のランダムウォークにZ軸を加えて3Dに
Random walk 3D.
[ Source ]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#pragma once #include "ofMain.h" #include "Particle.h" 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) {}; ofEasyCam cam; vector<Particle> particles; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(30); ofSetWindowTitle("openframeworks"); ofBackground(39); ofSetColor(239); ofEnableDepthTest(); for (int i = 0; i < 128; i++) { this->particles.push_back(Particle()); } } //-------------------------------------------------------------- void ofApp::update() { for (Particle& particle : particles) { particle.Upate(); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofRotateY(ofGetFrameNum() * 0.5); for (Particle& particle : particles) { particle.Draw(); } this->cam.end(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#pragma once #include "ofMain.h" class Particle{ public: Particle(); ~Particle(); void Upate(); void Draw(); private: int depth; ofPoint location; ofPoint direction; ofPoint directions[6]; std::deque<ofPoint> logs; }; |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
#include "Particle.h" Particle::Particle() { this->depth = ofGetWidth() > ofGetHeight() ? ofGetWidth() : ofGetHeight(); int x = (int)(ofRandom(ofGetWidth()) / 20) * 20; int y = (int)(ofRandom(ofGetHeight()) / 20) * 20; int z = (int)(ofRandom(this->depth) / 20) * 20; this->location = ofPoint(x, y, z); this->directions[0] = ofPoint(1, 0, 0); this->directions[1] = ofPoint(-1, 0, 0); this->directions[2] = ofPoint(0, 1, 0); this->directions[3] = ofPoint(0, -1, 0); this->directions[4] = ofPoint(0, 0, 1); this->directions[5] = ofPoint(0, 0, -1); } Particle::~Particle(){ } void Particle::Upate() { if (ofGetFrameNum() % 20 == 0) { int r = ofRandom(6); this->direction = this->directions[r]; if (this->location.x < 0) { this->direction = this->directions[0]; } else if (this->location.x > ofGetWidth()) { this->direction = this->directions[1]; } if (this->location.y < 0) { this->direction = this->directions[2]; } else if (this->location.y > ofGetHeight()) { this->direction = this->directions[3]; } if (this->location.z < 0) { this->direction = this->directions[4]; } else if (this->direction.z > this->depth) { this->direction = this->directions[5]; } } this->location += this->direction * 2; this->logs.push_front(location - ofPoint(ofGetWidth() * 0.5, ofGetHeight() * 0.5, this->depth * 0.5)); while (this->logs.size() > 90) { this->logs.pop_back(); } } void Particle::Draw() { ofFill(); ofDrawSphere(this->logs.front(), 3); ofNoFill(); ofBeginShape(); for (ofPoint& log : this->logs) { ofVertex(log); } ofEndShape(); } |