[ Video ]
[ About ]
GLSLを使ってライフゲーム。ピクセル単位で計算してるけど、サクサク動く!
Life game on GLSL. It is speedy.
[ 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 | #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) {}; 	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 | #include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { 	ofSetFrameRate(30); 	ofSetWindowTitle("openframeworks"); 	this->src.allocate(ofGetWidth(), ofGetHeight()); 	this->dst.allocate(ofGetWidth(), ofGetHeight()); 	this->shader.load("shader/shader.vert", "shader/shader.frag"); 	ofSeedRandom(39); 	this->src.begin(); 	ofClear(0); 	for (int i = 0; i < 50000; i++) { 		ofRandom(2) > 1 ? ofSetColor(255) : ofSetColor(128); 		ofDrawRectangle(ofRandom(ofGetWidth()), ofRandom(ofGetHeight()), 1, 1); 	} 	this->src.end(); 	ofSetColor(255); } //-------------------------------------------------------------- void ofApp::update() { } //-------------------------------------------------------------- void ofApp::draw() { 	this->dst.begin(); 	ofClear(0); 	this->shader.begin(); 	this->shader.setUniform1f("time", ofGetElapsedTimef()); 	this->shader.setUniform2f("resolution", ofGetWidth(), ofGetHeight()); 	this->shader.setUniformTexture("tex", this->src.getTexture(), 1); 	this->src.draw(0, 0); 	this->shader.end(); 	this->dst.end(); 	this->dst.draw(0, 0); 	std::swap(this->dst, this->src); } //-------------------------------------------------------------- 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 9 10 11 12 | #version 150 uniform mat4 modelViewProjectionMatrix; in vec4 position; in vec2 texcoord; out vec2 texCoordVarying; void main(){ 	texCoordVarying = texcoord; 	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 31 32 33 34 35 36 37 38 39 40 41 | #version 150 uniform float time; uniform vec2 resolution; uniform sampler2DRect tex; in vec2 texCoordVarying; out vec4 outputColor; void main() {   vec2 p = (gl_FragCoord.xy * 2.0 - resolution) / min(resolution.x, resolution.y);   float count = 0.0;   for(float x = -1.0; x <= 1.0; x++){     for(float y = -1.0; y <= 1.0; y++){         float r = texture(tex, texCoordVarying + vec2(x, y)).r;         if(r == 1.0){           count++;         }     }   }   float my = texture(tex, texCoordVarying).r;   float next;   if(my == 0.0){     if(count == 2){       next = 1.0;     }else{       next = 0.0;     }   }else if(my == 1.0){     next = 0.5;   }else if(my == 0.5){     next = 0.0;   }   outputColor = vec4(vec3(next, next, 0.0), 1.0); } |