[ Video ]
[ About ]
ランダムウォークの行き先を座標情報で制限する。
Limit the destination of the random walk by coordinate information.
[ 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 37 38 39 40 41 42 43 44 45 46 47 48 49 |
#pragma once #include "ofMain.h" class Actor { public: Actor(vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list); Actor(int select_index, vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list); void update(const int& frame_span, vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list); glm::vec3 getLocation(); deque<glm::vec3> getLog(); private: int select_index; int next_index; glm::vec3 location; deque<glm::vec3> log; }; 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 windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; ofTrueTypeFont font; vector<string> word_list; int word_index; vector<glm::vec3> location_list; vector<vector<int>> next_index_list; vector<int> destination_list; vector<unique_ptr<Actor>> actor_list; ofEasyCam cam; }; |
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 |
#include "ofApp.h" //-------------------------------------------------------------- Actor::Actor(vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list) { this->select_index = ofRandom(location_list.size()); while (true) { auto itr = find(destination_list.begin(), destination_list.end(), this->select_index); if (itr == destination_list.end()) { destination_list.push_back(this->select_index); break; } this->select_index = (this->select_index + 1) % location_list.size(); } this->next_index = this->select_index; } //-------------------------------------------------------------- Actor::Actor(int select_index, vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list) { this->select_index = select_index; while (true) { auto itr = find(destination_list.begin(), destination_list.end(), this->select_index); if (itr == destination_list.end()) { destination_list.push_back(this->select_index); break; } this->select_index = (this->select_index + 1) % location_list.size(); } this->next_index = this->select_index; this->location = location_list[this->select_index]; } //-------------------------------------------------------------- void Actor::update(const int& frame_span, vector<glm::vec3>& location_list, vector<vector<int>>& next_index_list, vector<int>& destination_list) { if (ofGetFrameNum() % frame_span == 0) { auto tmp_index = this->select_index; this->select_index = this->next_index; int retry = next_index_list[this->select_index].size(); this->next_index = next_index_list[this->select_index][(int)ofRandom(next_index_list[this->select_index].size())]; while (--retry > 0) { auto destination_itr = find(destination_list.begin(), destination_list.end(), this->next_index); if (destination_itr == destination_list.end()) { if (tmp_index != this->next_index) { destination_list.push_back(this->next_index); break; } } this->next_index = next_index_list[this->select_index][(this->next_index + 1) % next_index_list[this->select_index].size()]; } if (retry <= 0) { destination_list.push_back(this->select_index); this->next_index = this->select_index; } } auto param = ofGetFrameNum() % frame_span; auto distance = location_list[this->next_index] - location_list[this->select_index]; this->location = location_list[this->select_index] + distance / frame_span * param; this->log.push_front(this->location); while (this->log.size() > 50) { this->log.pop_back(); } } //-------------------------------------------------------------- glm::vec3 Actor::getLocation() { return this->location; } //-------------------------------------------------------------- deque<glm::vec3> Actor::getLog() { return this->log; } //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(239); ofSetLineWidth(2); ofEnableDepthTest(); this->font.loadFont("fonts/Kazesawa-Bold.ttf", 350, true, true, true); this->word_list = vector<string>{ "E", "S", "C", "A", "P", "E" }; this->word_index = 0; } //-------------------------------------------------------------- void ofApp::update() { if (ofGetFrameNum() % 120 == 0) { this->location_list.clear(); this->next_index_list.clear(); this->destination_list.clear(); this->actor_list.clear(); ofFbo fbo; fbo.allocate(ofGetWidth(), ofGetHeight()); fbo.begin(); ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5); ofClear(0); ofSetColor(0); string word = this->word_list[this->word_index]; this->word_index = (this->word_index + 1) % this->word_list.size(); this->font.drawString(word, this->font.stringWidth(word) * -0.5, this->font.stringHeight(word) - 150); fbo.end(); auto span = 8; for (int x = span * 10; x < ofGetWidth() - span * 10; x += span) { for (int y = span * 10; y < ofGetHeight() - span * 10; y += span) { this->location_list.push_back(glm::vec3(x, y, 0)); } } for (auto& location : this->location_list) { vector<int> next_index = vector<int>(); int index = -1; for (auto& other : this->location_list) { index++; if (location == other) { continue; } float distance = glm::distance(location, other); if (distance <= span) { if (location.x > ofGetWidth() * 0.5 && location.x <= other.x) { if (location.y > ofGetHeight() * 0.5 && location.y <= other.y) { next_index.push_back(index); } if (location.y <= ofGetHeight() * 0.5 && location.y >= other.y) { next_index.push_back(index); } } if (location.x <= ofGetWidth() * 0.5 && location.x >= other.x) { if (location.y > ofGetHeight() * 0.5 && location.y <= other.y) { next_index.push_back(index); } if (location.y <= ofGetHeight() * 0.5 && location.y >= other.y) { next_index.push_back(index); } } } } this->next_index_list.push_back(next_index); } ofPixels pixels; fbo.readToPixels(pixels); for (int x = 0; x < fbo.getWidth(); x += span) { for (int y = 0; y < fbo.getHeight(); y += span) { ofColor color = pixels.getColor(x, y); if (color != ofColor(0, 0)) { for (int i = 0; i < this->location_list.size(); i++) { if (this->location_list[i] == glm::vec3(x, y, 0)) { this->actor_list.push_back(make_unique<Actor>(i, this->location_list, this->next_index_list, this->destination_list)); break; } } } } } } if (ofGetFrameNum() % 120 < 15) { return; } int frame_span = 1; int prev_index_size = 0; if (ofGetFrameNum() % frame_span == 0) { prev_index_size = this->destination_list.size(); } for (auto& actor : this->actor_list) { actor->update(frame_span, this->location_list, this->next_index_list, this->destination_list); } if (prev_index_size != 0) { this->destination_list.erase(this->destination_list.begin(), this->destination_list.begin() + prev_index_size); } } //-------------------------------------------------------------- void ofApp::draw() { this->cam.begin(); ofRotateX(180); ofTranslate(ofGetWidth() * -0.5, ofGetHeight() * -0.75); for (auto& actor : this->actor_list) { ofSetColor(39); ofFill(); ofDrawBox(actor->getLocation(), 8); ofSetColor(239); ofNoFill(); ofDrawBox(actor->getLocation(), 8); } this->cam.end(); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |
[ Link ]
https://github.com/junkiyoshi/Insta20210816
[ Kinako ]