[ Video ]
[ About ]
Day 18 Paper.
紙吹雪。
Confetti.
[ 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 "Confetti.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<unique_ptr<Confetti>> confettis; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofEnableDepthTest(); for (int i = 0; i < 512; i++) { unique_ptr<Confetti> confetti(new Confetti()); this->confettis.push_back(move(confetti)); } } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->confettis.size(); i++) { this->confettis[i]->update(); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofTranslate(0, 0, -ofGetWidth() / 2); ofRotateZ(180); ofTranslate(-ofGetWidth() / 2, -ofGetHeight() / 2); for (int i = 0; i < this->confettis.size(); i++) { this->confettis[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 |
#pragma once #include "ofMain.h" class Confetti { public: Confetti(); ~Confetti(); void update(); void draw(); private: int type; ofPoint location; ofPoint velocity; ofPoint rotate; 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 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 |
#include "Confetti.h" Confetti::Confetti() { this->type = ofRandom(3); this->location = ofPoint(ofRandom(ofGetWidth()), ofRandom(ofGetHeight(), ofRandom(ofGetWidth()))); this->velocity = ofPoint(ofRandom(-3, 3), ofRandom(0.5, 3), ofRandom(-3, 3)); this->rotate = ofPoint(0, 0, 0); this->body_color.setHsb(ofRandom(255), 255, 255); } Confetti::~Confetti() { } void Confetti::update() { this->location += this->velocity; if (this->location.x < 0) { this->location.x = ofGetWidth(); } else if (this->location.x > ofGetWidth()) { this->location.x = 0; } if (this->location.z < 0) { this->location.z = ofGetWidth();; } else if (this->location.z > ofGetWidth()) { this->location.z = 0; } if (this->location.y > ofGetHeight()) { this->location.y = 0; } this->rotate += this->velocity; } void Confetti::draw() { ofPushMatrix(); ofTranslate(this->location); ofRotateX(this->rotate.x); ofRotateY(this->rotate.y); ofRotateZ(this->rotate.z); ofSetColor(this->body_color); switch(this->type) { case 0: ofDrawRectangle(ofPoint(), 15, 15); break; case 1: ofDrawCircle(ofPoint(), 7.5); break; case 2: ofBeginShape(); for (int deg = 0; deg < 360; deg += 36) { deg % 72 == 0 ? ofVertex(15 * cos(deg * DEG_TO_RAD), 15 * sin(deg * DEG_TO_RAD)) : ofVertex(7.5 * cos(deg * DEG_TO_RAD), 7.5 * sin(deg * DEG_TO_RAD)); } ofEndShape(true); break; } ofPopMatrix(); } |