[ Video ]
[ About ]
ドーナツ状の座標を流れていくParticleを実装しました。数学の分野ではトーラスと呼ばれているらしく、方程式がwikiにも書いてあったので、拝借して実装してみました。
トーラス
https://ja.wikipedia.org/wiki/%E3%83%88%E3%83%BC%E3%83%A9%E3%82%B9
[ 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 |
#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; std::vector<std::unique_ptr<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 38 39 40 41 42 43 44 45 46 47 48 49 50 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(0); ofSetWindowTitle("Insta"); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); ofEnableDepthTest(); ofSetLineWidth(1.2); for (int i = 0; i < 255; i++) { std::unique_ptr<Particle> particle(new Particle(i)); this->particles.push_back(std::move(particle)); } } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->particles.size(); i++) { this->particles[i]->update(); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofRotateY(ofGetFrameNum() * 0.5); for (int i = 0; i < this->particles.size(); i++) { this->particles[i]->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 20 21 22 23 24 |
#pragma once #include "ofMain.h" class Particle { public: Particle(float value); ~Particle(); void update(); void draw(); ofPoint make_point(float radius, float small_radius, float deg, float small_deg); private: float radius; float small_radius; float deg; float small_deg; float deg_span; float small_deg_span; ofColor body_color; }; |
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 |
#include "Particle.h" //-------------------------------------------------------------- Particle::Particle(float value) { this->radius = 230; this->small_radius = 70; this->deg = value / 255.f * 360.f; this->small_deg = ofRandom(0, 360); this->deg_span = 1; this->small_deg_span = 10; this->body_color.setHsb(value, 255, 255); } //-------------------------------------------------------------- Particle::~Particle() { } //-------------------------------------------------------------- void Particle::update() { this->deg += this->deg_span; this->small_deg += this->small_deg_span; } //-------------------------------------------------------------- void Particle::draw() { ofSetColor(this->body_color, 255); ofVec3f point = make_point(this->radius, this->small_radius, this->deg, this->small_deg); ofDrawSphere(point, 1.5); for (int i = 0; i < 12; i++) { ofSetColor(this->body_color, 255 - i * 25); ofVec3f point_1 = make_point(this->radius, this->small_radius, this->deg - this->deg_span * i, this->small_deg - this->small_deg_span * i); ofVec3f point_2 = make_point(this->radius, this->small_radius, this->deg - this->deg_span * (i + 1), this->small_deg - this->small_deg_span * (i + 1)); ofDrawLine(point_1, point_2); } } //-------------------------------------------------------------- ofPoint Particle::make_point(float radius, float small_radius, float deg, float small_deg) { float x_1 = radius * cos(deg * DEG_TO_RAD); float y_1 = radius * sin(deg * DEG_TO_RAD); float x_2 = small_radius * cos(small_deg * DEG_TO_RAD) * cos(deg * DEG_TO_RAD); float y_2 = small_radius * cos(small_deg * DEG_TO_RAD) * sin(deg * DEG_TO_RAD); float x = x_1 + x_2; float y = y_1 + y_2; float z = small_radius * sin(small_deg * DEG_TO_RAD); return ofVec3f(x, y, z); } |