[ Video ]
[ About ]
Rotating multi color.
[ 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" 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) {}; int number_of_targets; vector<glm::vec2> target_list; vector<glm::vec3> color_list; ofShader shader; float deg_noise_seed; float radius_noise_seed; vector<glm::vec2> location_list; vector<glm::vec2> velocity_list; vector<glm::vec3> location_color_list; vector<float> color_value_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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openFrameworks"); ofBackground(0); ofEnableBlendMode(ofBlendMode::OF_BLENDMODE_ADD); ofSetColor(255); this->number_of_targets = 500; for (int i = 0; i < this->number_of_targets; i++) { this->target_list.push_back(glm::vec2()); this->color_list.push_back(glm::vec3(1, 1, 1)); } this->shader.load("shader/shader.vert", "shader/shader.frag"); this->deg_noise_seed = ofRandom(1000); this->radius_noise_seed = ofRandom(1000); } //-------------------------------------------------------------- void ofApp::update() { int radius = ofMap(ofNoise(this->radius_noise_seed, ofGetFrameNum() * 0.005), 0, 1, -300, 300); int next_radius = ofMap(ofNoise(this->radius_noise_seed, (ofGetFrameNum() + 1) * 0.005), 0, 1, -300, 300); int deg = ofMap(ofNoise(this->deg_noise_seed, ofGetFrameNum() * 0.005), 0, 1, 0, 360); int next_deg = ofMap(ofNoise(this->deg_noise_seed, (ofGetFrameNum() + 1) * 0.005), 0, 1, 0, 360); for (int deg_param = 0; deg_param < 360; deg_param += 90) { auto location = glm::vec2(ofGetWidth() * 0.5 + radius * cos((deg + deg_param) * DEG_TO_RAD), ofGetHeight() * 0.5 + radius * sin((deg + deg_param) * DEG_TO_RAD)); auto next = glm::vec2(ofGetWidth() * 0.5 + next_radius * cos((next_deg + deg_param) * DEG_TO_RAD), ofGetHeight() * 0.5 + next_radius * sin((next_deg + deg_param) * DEG_TO_RAD)); auto distance = location - next; distance *= 0.5; auto future = location + distance * 30; auto random_deg = ofRandom(360); future += glm::vec2(30 * cos(random_deg * DEG_TO_RAD), 30 * sin(random_deg * DEG_TO_RAD)); auto future_distance = future - location; this->location_list.push_back(location); this->velocity_list.push_back(glm::normalize(future_distance) * glm::length(distance)); glm::vec3 tmp_color; switch ((int)ofRandom(3)) { case 0: tmp_color += glm::vec3(0.6, 0.4, 0.4); break; case 1: tmp_color += glm::vec3(0.4, 0.6, 0.4); break; case 2: tmp_color += glm::vec3(0.4, 0.4, 0.6); break; } this->location_color_list.push_back(tmp_color); this->color_value_list.push_back(1); } for (int i = this->location_list.size() - 1; i > -1; i--) { this->location_list[i] += this->velocity_list[i]; this->velocity_list[i] *= 1.01; this->color_value_list[i] *= 0.98; if (this->color_value_list[i] < 0.1 || glm::distance(glm::vec2(ofGetWidth() * 0.5, ofGetHeight() * 0.5), this->location_list[i]) > 720) { this->location_list.erase(this->location_list.begin() + i); this->velocity_list.erase(this->velocity_list.begin() + i); this->location_color_list.erase(this->location_color_list.begin() + i); this->color_value_list.erase(this->color_value_list.begin() + i); } } for (int i = 0; i < this->target_list.size(); i++) { if (i < this->location_list.size()) { this->target_list[i] = this->location_list[i]; this->color_list[i] = this->location_color_list[i] * this->color_value_list[i]; } else { this->color_list[i] = glm::vec3(0); } } } //-------------------------------------------------------------- void ofApp::draw() { 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->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 = 500; 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); } |