[ Video ]
[ About ]
Random walking orb.
[ 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 |
#pragma once #include "ofMain.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) {}; vector<glm::vec3> base_color_list; int number_of_targets; vector<glm::vec2> target_list; vector<glm::vec3> target_color_list; int frame_span; float size; vector<glm::vec2> location_list; vector<int> current_index_list; vector<int> next_index_list; vector<glm::vec3> color_list; ofShader shader; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(0); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); ofSetCircleResolution(72); ofSetLineWidth(5); this->base_color_list.push_back(glm::vec3(0.4, 0.25, 0.25)); this->base_color_list.push_back(glm::vec3(0.25, 0.4, 0.25)); this->base_color_list.push_back(glm::vec3(0.25, 0.25, 0.4)); this->number_of_targets = 120; for (int i = 0; i < this->number_of_targets; i++) { this->target_list.push_back(glm::vec2()); this->target_color_list.push_back(glm::vec3(1, 1, 1)); } this->frame_span = 60; this->size = 15; while (this->location_list.size() < 110) { auto location = glm::vec2(ofRandom(80, ofGetWidth() - 80), ofRandom(80, ofGetHeight() - 80)); bool flag = true; for (int i = 0; i < this->location_list.size(); i++) { if (glm::distance(location, this->location_list[i]) < this->size * 3) { flag = false; break; } } if (flag) { this->location_list.push_back(location); } cout << this->location_list.size() << endl; } for (int i = 0; i < this->number_of_targets; i++) { this->current_index_list.push_back(0); this->next_index_list.push_back(ofRandom(this->location_list.size())); this->color_list.push_back(this->base_color_list[ofRandom(this->base_color_list.size())]); } this->shader.load("shader/shader.vert", "shader/shader.frag"); } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->current_index_list.size(); i++) { if (ofGetFrameNum() % (this->frame_span * 2) == 0) { int next_index = this->next_index_list[i]; int current_index = next_index; vector<int> near_indexes; for (int location_index = 0; location_index < this->location_list.size(); location_index++) { if (current_index == location_index) { continue; } if (glm::distance(this->location_list[current_index], this->location_list[location_index]) < this->size * 5) { near_indexes.push_back(location_index); } } if (near_indexes.size() > 0) { next_index = near_indexes[ofRandom(near_indexes.size())]; } this->current_index_list[i] = current_index; this->next_index_list[i] = next_index; } } for (int i = 0; i < this->number_of_targets; i++) { if (ofGetFrameNum() % (this->frame_span * 2) < this->frame_span) { this->target_list[i] = this->location_list[this->current_index_list[i]]; this->target_color_list[i] = this->color_list[i]; } else { this->target_list[i] = this->location_list[this->next_index_list[i]]; this->target_color_list[i] = this->color_list[i]; } } } //-------------------------------------------------------------- void ofApp::draw() { int frame_param = ofGetFrameNum() % this->frame_span; for (int i = 0; i < this->next_index_list.size(); i++) { ofSetColor(ofMap(this->target_color_list[i].x, 0.25, 0.4, 0, 255), ofMap(this->target_color_list[i].y, 0.25, 0.4, 0, 255), ofMap(this->target_color_list[i].z, 0.25, 0.4, 0, 255)); int current_index = this->current_index_list[i]; int next_index = this->next_index_list[i]; auto angle_current = std::atan2(this->location_list[next_index].y - this->location_list[current_index].y, this->location_list[next_index].x - this->location_list[current_index].x); auto satellite_point_current = this->location_list[current_index] + glm::vec2(this->size * cos(angle_current), this->size * sin(angle_current)); auto angle_next = std::atan2(this->location_list[current_index].y - this->location_list[next_index].y, this->location_list[current_index].x - this->location_list[next_index].x); auto satellite_point_next = this->location_list[next_index] + glm::vec2(this->size * cos(angle_next), this->size * sin(angle_next)); if (ofGetFrameNum() % (this->frame_span * 2) < this->frame_span) { auto distance = glm::distance(satellite_point_next, satellite_point_current); distance = ofMap(frame_param, 0, this->frame_span, 0, distance); auto direction = satellite_point_next - satellite_point_current; ofDrawLine(satellite_point_current, satellite_point_current + glm::normalize(direction) * distance); } else { auto distance = glm::distance(satellite_point_next, satellite_point_current); distance = ofMap(frame_param, 0, this->frame_span, distance, 0); auto direction = satellite_point_next - satellite_point_current; ofDrawLine(satellite_point_next, satellite_point_next - glm::normalize(direction) * distance); } } ofSetColor(255); ofNoFill(); for (int location_index = 0; location_index < this->location_list.size(); location_index++) { ofDrawCircle(this->location_list[location_index], this->size); } for (int i = 0; i < this->target_color_list.size(); i++) { if (ofGetFrameNum() % (this->frame_span * 2) < this->frame_span) { float p = ofGetFrameNum() % this->frame_span < this->frame_span * 0.5 ? 1 : ofMap(ofGetFrameNum() % this->frame_span, this->frame_span * 0.5, this->frame_span, 1, 0); this->target_color_list[i] = this->target_color_list[i] * p; } else { this->target_color_list[i] = this->target_color_list[i] * ofMap(ofGetFrameNum() % this->frame_span, 0, this->frame_span, 0, 1); } } ofFill(); this->shader.begin(); this->shader.setUniform1f("time", ofGetElapsedTimef()); this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); this->shader.setUniform2fv("targets", &this->target_list[0].x, this->number_of_targets); this->shader.setUniform3fv("colors", &this->target_color_list[0].x, this->number_of_targets); ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight()); this->shader.end(); } //-------------------------------------------------------------- int main() { ofGLWindowSettings settings; settings.setGLVersion(3, 2); settings.setSize(720, 720); ofCreateWindow(settings); ofRunApp(new ofApp()); } |
1 2 3 4 5 6 7 8 |
#version 150 uniform mat4 modelViewProjectionMatrix; in vec4 position; void main(){ gl_Position = modelViewProjectionMatrix * position; } |
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 |
#version 150 const int number_of_targets = 120; uniform float time; uniform vec2 resolution; uniform vec2 targets[number_of_targets]; uniform vec3 colors[number_of_targets]; out vec4 outputColor; void main() { vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y); vec3 color = vec3(0.0); for(int i = 0; i < number_of_targets; i++){ vec2 t = vec2(targets[i].x, -targets[i].y) / min(resolution.x, resolution.y) * 2.0; t.xy += vec2(-resolution.x, resolution.y) / min(resolution.x, resolution.y); float v = 0.035 / length(p - t); float r = v * colors[i].x; float g = v * colors[i].y; float b = v * colors[i].z; vec3 c = vec3(smoothstep(0.05, 1.0, r), smoothstep(0.05, 1.0, g), smoothstep(0.05, 1.0, b)); color += c; } outputColor = vec4(color, 1.0); } |