[ Video ]
[ About ]
Polygon generater.
[ 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 27 28 29 30 31 32 33 34 35 36 |
#pragma once #include "ofMain.h" #include "ofxBox2d.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) {} void sort(vector<glm::vec2>& vertices); ofxBox2d box2d; vector<ofColor> color_list; vector<glm::vec2> walker_list; vector<glm::vec2> walker_noise_seed_list; vector<shared_ptr<ofxBox2dPolygon>> polygon_list; vector<float> polygon_lile_list; vector<ofColor> polygon_color_list; }; |
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 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetLineWidth(2); this->box2d.init(); this->box2d.setGravity(0, 50); this->box2d.createBounds(); this->box2d.setFPS(60); this->box2d.registerGrabbing(); ofColor color; vector<int> hex_list = { 0xef476f, 0xffd166, 0x06d6a0, 0x118ab2, 0x073b4c }; for (auto hex : hex_list) { color.setHex(hex); this->color_list.push_back(color); } for (int i = 0; i < 15; i++) { this->walker_list.push_back(glm::vec2()); this->walker_noise_seed_list.push_back(glm::vec2(ofRandom(1000), ofRandom(1000))); } } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->walker_list.size(); i++) { this->walker_list[i] = glm::vec2( ofMap(ofNoise(this->walker_noise_seed_list[i].x, ofGetFrameNum() * 0.005), 0, 1, 0, ofGetWidth()), ofMap(ofNoise(this->walker_noise_seed_list[i].y, ofGetFrameNum() * 0.005), 0, 1, 0, ofGetHeight() * 0.25) ); } if (ofGetFrameNum() % 3 == 0) { for (int i = 0; i < this->walker_list.size(); i++) { vector<glm::vec2> vertices; for (int k = i + 1; k < this->walker_list.size(); k++) { if (glm::distance(this->walker_list[i], this->walker_list[k]) < 50) { vertices.push_back(this->walker_list[k] - this->walker_list[i]); } } if (vertices.size() >= 2) { this->sort(vertices); for (int m = 0; m < vertices.size() - 1; m++) { auto polygon = make_shared<ofxBox2dPolygon>(); polygon->addVertex(this->walker_list[i].x, this->walker_list[i].y); polygon->addVertex(this->walker_list[i].x + vertices[m].x, this->walker_list[i].y + vertices[m].y); polygon->addVertex(this->walker_list[i].x + vertices[m + 1].x, this->walker_list[i].y + vertices[m + 1].y); polygon->setPhysics(1.0, 0.7, 0.3); polygon->create(box2d.getWorld()); this->polygon_list.push_back(polygon); this->polygon_lile_list.push_back(255); this->polygon_color_list.push_back(this->color_list[(int)ofRandom(this->color_list.size())]); } } } } for (int i = this->polygon_list.size() - 1; i > -1; i--) { this->polygon_lile_list[i] -= 1; if (this->polygon_lile_list[i] < 0) { this->polygon_list.erase(this->polygon_list.begin() + i); this->polygon_lile_list.erase(this->polygon_lile_list.begin() + i); this->polygon_color_list.erase(this->polygon_color_list.begin() + i); } } this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { ofSetColor(39); for (int i = 0; i < this->walker_list.size(); i++) { ofDrawCircle(this->walker_list[i], 4); for (int k = i + 1; k < this->walker_list.size(); k++) { if (glm::distance(this->walker_list[i], this->walker_list[k]) < 50) { ofDrawLine(this->walker_list[i], this->walker_list[k]); } } } for (int i = 0; i < this->polygon_list.size(); i++) { if (this->polygon_lile_list[i] > 128) { ofSetColor(this->polygon_color_list[i]); } else { ofSetColor(this->polygon_color_list[i], ofMap(this->polygon_lile_list[i], 128, 0, 255, 0)); } this->polygon_list[i]->draw(); } } //-------------------------------------------------------------- void ofApp::sort(vector<glm::vec2>& vertices) { glm::vec2 tmp; for (int i = 0; i < vertices.size(); i++) { for (int k = i + 1; k < vertices.size(); k++) { if (vertices[i].x < vertices[k].x) { tmp = vertices[i]; vertices[i] = vertices[k]; vertices[k] = tmp; } } } } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |