[ Video ]
[ About ]
Day 13 Black Hole
openFrameworksで座標計算、GLSLで光球描写のパターン
openFframeworks is computing coordinate, GLSL is drawing light sphere.
[ 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 |
#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_location; vector<ofPoint> locations; vector<float> velocity; ofFbo src; ofFbo dst; 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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); ofBackground(0); this->number_of_location = 200; for (int i = 0; i < this->number_of_location; i++) { ofPoint point = ofPoint(ofRandom(-ofGetWidth(), ofGetWidth()), ofRandom(-ofGetHeight(), ofGetHeight())); this->locations.push_back(point); this->velocity.push_back(1.f); } this->src.allocate(ofGetWidth(), ofGetHeight()); this->dst.allocate(ofGetWidth(), ofGetHeight()); this->shader.load("shader/shader.vert", "shader/shader.frag"); } //-------------------------------------------------------------- void ofApp::update() { for (int i = 0; i < this->locations.size(); i++) { float len = this->locations[i].length(); if (len < 100) { ofPoint point; while (point.length() < 100) { point = ofPoint(ofRandom(-ofGetWidth(), ofGetWidth()), ofRandom(-ofGetHeight(), ofGetHeight())); } this->locations[i] = point; this->velocity[i] = 1.f; } else { this->locations[i] = this->locations[i].normalized() * (len * this->velocity[i]); this->velocity[i] *= 0.999f; } } } //-------------------------------------------------------------- void ofApp::draw() { vector<glm::vec2> targets; for (int i = 0; i < this->number_of_location; i++) { glm::vec2 target = glm::vec2(this->locations[i].x + ofGetWidth() * 0.5, this->locations[i].y + ofGetHeight() * 0.5); targets.push_back(target); } this->dst.begin(); this->shader.begin(); this->shader.setUniform1f("time", ofGetElapsedTimef()); this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); this->shader.setUniform2fv("targets", &targets[0].x, this->number_of_location); this->shader.setUniformTexture("fbo", this->src.getTexture(), 1); ofDrawRectangle(0, 0, ofGetWidth(), ofGetHeight()); this->shader.end(); this->dst.end(); this->dst.draw(0, 0); std::swap(this->src, this->dst); } //-------------------------------------------------------------- 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 |
#version 150 const int number_of_targets = 200; uniform float time; uniform vec2 resolution; uniform vec2 targets[number_of_targets]; uniform sampler2DRect fbo; 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.005 / length(p - t); vec3 c = vec3(smoothstep(0.01, 1.0, v)); color += c; } vec4 fbo_value = texture(fbo, vec2(gl_FragCoord.x, gl_FragCoord.y)); outputColor = vec4(color, 1.0) + fbo_value * 0.85; } |