[ Video ]
[ About ]
ofxBox2dで2D物理演算をした座標情報をシェーダーへ送信。完全に雰囲気でGLSLを書いている状態である。
Send the result of physical calculation by ofxBox2d to the Shader.
[ 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  | 
						#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) {}; 	ofxBox2d box2d; 	ofShader shader; 	int number_of_targets; 	vector<shared_ptr<ofxBox2dCircle>> circles; 	vector<glm::vec4> colors; 	vector<ofPoint> force_fields; 	int force_field_radius; };  | 
					
| 
					 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  | 
						#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { 	ofSetFrameRate(60); 	ofSetWindowTitle("openframeworks"); 	this->box2d.init(); 	this->box2d.setGravity(0, 0); 	this->box2d.createBounds(); 	this->box2d.setFPS(60); 	this->box2d.registerGrabbing(); 	this->number_of_targets = 39; 	for (int i = 0; i < this->number_of_targets; i++) { 		this->circles.push_back(shared_ptr<ofxBox2dCircle>(new ofxBox2dCircle)); 		this->circles.back().get()->setPhysics(1.5, 0.5, 0.1); 		this->circles.back().get()->setup(this->box2d.getWorld(), ofRandom(ofGetWidth()), ofRandom(ofGetHeight()), 15); 		int color_type = ofRandom(3); 		glm::vec4 color; 		switch (color_type) { 		case 0: 			color = glm::vec4(1.0, 0.0, 0.0, 0.0); 			break; 		case 1: 			color = glm::vec4(0.0, 1.0, 0.0, 0.0); 			break; 		case 2: 			color = glm::vec4(0.0, 0.0, 1.0, 0.0); 			break; 		} 		this->colors.push_back(color); 	} 	this->shader.load("shader/shader.vert", "shader/shader.frag"); 	this->force_fields.push_back(ofPoint(ofGetWidth() / 4, ofGetHeight() / 4)); 	this->force_fields.push_back(ofPoint(ofGetWidth() / 4 * 3, ofGetHeight() / 4)); 	this->force_fields.push_back(ofPoint(ofGetWidth() / 4 * 3, ofGetHeight() / 4 * 3)); 	this->force_fields.push_back(ofPoint(ofGetWidth() / 4, ofGetHeight() / 4 * 3)); 	this->force_field_radius = 300; } //-------------------------------------------------------------- void ofApp::update() { 	for (int i = 0; i < this->circles.size(); i++) { 		shared_ptr<ofxBox2dCircle> circle = this->circles[i]; 		ofPoint point = circle->getPosition(); 		for (int field_index = 0; field_index < this->force_fields.size(); field_index++) { 			float distance = point.distance(this->force_fields[field_index]); 			if (distance < this->force_field_radius) { 				ofPoint p = ofPoint(this->force_field_radius * cos((field_index * 90 + 10) * DEG_TO_RAD), this->force_field_radius * sin((field_index * 90 + 10) * DEG_TO_RAD)); 				circle->addForce(p, ofMap(distance, 0, this->force_field_radius, 0.1, 0.01)); 			} 		} 	} 	this->box2d.update(); } //-------------------------------------------------------------- void ofApp::draw() { 	vector<glm::vec4> points; 	for (int i = 0; i < this->number_of_targets; i++) { 		glm::vec4 p = glm::vec4(this->circles[i]->getPosition().x, this->circles[i]->getPosition().y, 0, 0); 		points.push_back(p); 	} 	this->shader.begin(); 	this->shader.setUniform1f("time", ofGetElapsedTimef()); 	this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); 	this->shader.setUniform4fv("targets", &points[0].x, this->number_of_targets); 	this->shader.setUniform4fv("colors", &this->colors[0].x, this->number_of_targets); 	ofRect(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  | 
						#version 150 const int number_of_targets = 39; out vec4 outputColor; uniform float time; uniform vec2 resolution; uniform vec4 targets[number_of_targets]; uniform vec4 colors[number_of_targets]; 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 r = 0.03 / length(p - t) * colors[i].x;     float g = 0.03 / length(p - t) * colors[i].y;     float b = 0.03 / length(p - t) * colors[i].z;     color += vec3(r, g, b);   }   outputColor = vec4(color, 1.0); }  |