[ Video ]
[ About ]
Day 15 Fire.
2次元配列による炎。0列目はパーリンノイズで値を決定、1列目以降は座標(x – 1, y – 1), (x, y – 1), (x + 1, y – 1), (x, y -2)の値の平均
Fire by two dimensional array. 0 column value determinate by Perlin Noise. From the 1 column onwards, value is average from (x – 1, y – 1), (x, y – 1), (x + 1, y – 1), (x, y -2).
[ 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 |
#pragma once #include "ofMain.h" #include "opencv2/opencv.hpp" 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) {}; cv::Mat frame; cv::Mat draw_frame; ofImage draw_image; }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofSetWindowTitle("openframeworks"); this->frame = cv::Mat(200, 200, CV_8UC3); this->draw_image.allocate(ofGetWidth(), ofGetHeight(), OF_IMAGE_COLOR); this->draw_frame = cv::Mat(this->draw_image.getHeight(), this->draw_image.getWidth(), CV_MAKETYPE(CV_8UC3, this->draw_image.getPixels().getNumChannels()), this->draw_image.getPixels().getData(), 0); } //-------------------------------------------------------------- void ofApp::update() { ofSeedRandom(39); for (int x = 0; x < this->frame.cols; x++) { unsigned char value = ofNoise(x * 0.05, ofGetFrameNum() * 0.05) * 255; this->frame.at<cv::Vec3b>(0, x) = cv::Vec3b(value, 0, 0); } for (int x = 0; x < this->frame.cols; x++) { for (int y = 1; y < this->frame.rows; y++) { int sum = 0; int count = 0; sum += this->frame.at<cv::Vec3b>(y - 1, x)[0]; count++; if (x >= 0) { sum += this->frame.at<cv::Vec3b>(y - 1, x - 1)[0]; count++; } if (x < this->frame.rows - 1) { sum += this->frame.at<cv::Vec3b>(y - 1, x + 1)[0]; count++; } if (y > 1) { sum += this->frame.at<cv::Vec3b>(y - 2, x)[0]; count++; } int avg = sum / count; unsigned char r = 0; unsigned char g = 0; if (avg > 255) { r = 255; } else if (avg < 0) { r = 0; } else { r = avg; if (avg > 150) { g = (unsigned char)ofMap(avg, 150, 255, 0, 100); } } this->frame.at<cv::Vec3b>(y, x) = cv::Vec3b(r, g, g * 0.1); } } this->frame *= 1.02; cv::resize(this->frame, this->draw_frame, cv::Size(ofGetWidth(), ofGetHeight())); this->draw_image.update(); } //-------------------------------------------------------------- void ofApp::draw() { ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5); ofRotate(180); this->draw_image.draw(ofGetWidth() * -0.5, ofGetHeight() * -0.5); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |