[ Video ]
[ About ]
Surfaceの傾き情報でオブジェクトの影の位置を変化させてみました。立体的に見えるかな?
[ 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 |
#pragma once #include "ofMain.h" #include "opencv2/opencv.hpp" #include <atlbase.h> #include <SensorsApi.h> #include <sensors.h> #pragma comment(lib, "Sensorsapi.lib") 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 windowResized(int w, int h) {}; void dragEvent(ofDragInfo dragInfo) {}; void gotMessage(ofMessage msg) {}; // Make Member ofFbo fbo; ofPixels pixels; cv::Mat pixels_mat; // Make Method ofPoint get_accelerometer_3d(); }; |
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 |
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup() { ofSetFrameRate(60); ofBackground(239); ofSetWindowTitle("Insta"); ofSetRectMode(ofRectMode::OF_RECTMODE_CENTER); this->fbo.allocate(ofGetWidth(), ofGetHeight()); this->fbo.readToPixels(this->pixels); this->pixels_mat = cv::Mat(this->pixels.getHeight(), this->pixels.getWidth(), CV_8UC4, this->pixels.getData()); } //-------------------------------------------------------------- void ofApp::update() { this->fbo.begin(); ofClear(0); ofTranslate(this->fbo.getWidth() * 0.5, this->fbo.getHeight() * 0.5); ofSetColor(59, 59, 239); int radius = 250; ofBeginShape(); for (int deg = ofGetFrameNum() * 2; deg <= ofGetFrameNum() * 2 + 360; deg += 120) { ofVertex(radius * cos(deg * DEG_TO_RAD), radius * sin(deg * DEG_TO_RAD)); } ofNextContour(); radius *= 0.7; for (int deg = ofGetFrameNum() * 2; deg <= ofGetFrameNum() * 2 + 360; deg += 120) { ofVertex(radius * cos(deg * DEG_TO_RAD), radius * sin(deg * DEG_TO_RAD)); } ofEndShape(true); this->fbo.end(); this->fbo.readToPixels(this->pixels); cv::Mat gray_mat; cv::cvtColor(this->pixels_mat, gray_mat, cv::COLOR_RGBA2GRAY); for (int y = 0; y < this->pixels_mat.cols; y++) { for (int x = 0; x < this->pixels_mat.rows; x++) { unsigned char value = gray_mat.at<unsigned char>(y, x); if (value > 0) { this->pixels_mat.at<cv::Vec4b>(y, x) = cv::Vec4b(value, value, value, this->pixels_mat.at<cv::Vec4b>(y, x)[3]); } } } cv::GaussianBlur(this->pixels_mat, this->pixels_mat, cv::Size(19, 19), 5, 5); } //-------------------------------------------------------------- void ofApp::draw() { ofTranslate(ofGetWidth() * 0.5, ofGetHeight() * 0.5); ofPoint point = this->get_accelerometer_3d(); ofSetColor(255, 192); ofImage draw_image; draw_image.setFromPixels(this->pixels); draw_image.draw(ofMap(point.x, -1, 1, -100, 100), ofMap(point.y, -1, 1, 100, -100)); ofSetColor(255); this->fbo.draw(0, 0); } //-------------------------------------------------------------- ofPoint ofApp::get_accelerometer_3d() { CComPtr<ISensorManager> sensor_manager; CComPtr<ISensorCollection> sensor_collection; CComPtr<ISensor> sensor; CComPtr<ISensorDataReport> data; if (FAILED(::CoInitializeEx(NULL, COINIT_MULTITHREADED))) { return ofPoint(); } if (FAILED(::CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&sensor_manager)))) { return ofPoint(); } if (FAILED(sensor_manager->GetSensorsByCategory(SENSOR_TYPE_ACCELEROMETER_3D, &sensor_collection))) { return ofPoint(); } if (FAILED(sensor_collection->GetAt(0, &sensor))) { return ofPoint(); } if (FAILED(sensor->GetData(&data))) { return ofPoint(); } PROPVARIANT x = {}; if (FAILED(data->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_X_G, &x))) { return ofPoint(); } PROPVARIANT y = {}; if (FAILED(data->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Y_G, &y))) { return ofPoint(); } PROPVARIANT z = {}; if (FAILED(data->GetSensorValue(SENSOR_DATA_TYPE_ACCELERATION_Z_G, &z))) { return ofPoint(); } return ofPoint(x.dblVal, y.dblVal, z.dblVal); } //-------------------------------------------------------------- int main() { ofSetupOpenGL(720, 720, OF_WINDOW); ofRunApp(new ofApp()); } |