1. はじめに
インストールから1年弱経ちました。インストール満足野郎にならないよう気をつけます。今回は openFrameworks で画像を読み込んだり、画像の位置を動かしたります。
環境
OS : Windows 10
openFrameworks : 0.11.2
IDE : Visual Studio 2019
projectGenerator
まずは projectGenerator を起動し、プロジェクト名とパスを決めたら Generate ボタンを押します。これで ofMain.cpp / ofApp.cpp / ofApp.h という3つのプログラムが作成されます。
使用する画像
画像処理で著名なLennaの画像を使います。プロジェクト配下には bin/data というフォルダがありますので、ここに画像を置いておきます。
2. プログラム
今回のプログラムがやることは下記のとおりです。
- 普通に画像を読み込んで描画する
- 画像位置を counter 変数に基づいて動かす
- キーボードで画像表示を切り替える
- キー「s」を押下すると上下反転
- キー「d」を押下すると左右反転
ofMain.cpp
#include "ofMain.h" #include "ofApp.h" int main( ){ ofSetupOpenGL(640,480,OF_WINDOW); ofRunApp(new ofApp()); }
ofApp.cpp
#include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ ofSetWindowTitle("Load image file"); ofBackground(ofColor::white); myImg.load("Lenna.jpg"); iw = myImg.getWidth(); ih = myImg.getHeight(); counter = 0; } //-------------------------------------------------------------- void ofApp::update(){ counter = (counter % ofGetHeight()) + 1; } //-------------------------------------------------------------- void ofApp::draw(){ ofSetColor(ofColor::white); // image 1 myImg.draw(0, 0); // image 2 myImg.draw(iw, counter); int sh = ofGetHeight(); if (ih+counter > sh) { myImg.drawSubsection(iw,0,iw,ih+counter-sh, 0,sh-counter,iw,ih+counter-sh); } ofSetColor(ofColor::black); ofDrawBitmapString("press s to reflects the pixels across the vertial axis",20,ih+50); ofDrawBitmapString("press d to reflacts the pixels across the horizontal axis",20,ih+70); } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ if (key == 's') { myImg.mirror(true,false); }else if(key == 'd') { myImg.mirror(false,true); } } //-------------------------------------------------------------- void ofApp::keyReleased(int key){ } //-------------------------------------------------------------- void ofApp::mouseMoved(int x, int y ){ } //-------------------------------------------------------------- void ofApp::mouseDragged(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mousePressed(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseReleased(int x, int y, int button){ } //-------------------------------------------------------------- void ofApp::mouseEntered(int x, int y){ } //-------------------------------------------------------------- void ofApp::mouseExited(int x, int y){ } //-------------------------------------------------------------- void ofApp::windowResized(int w, int h){ } //-------------------------------------------------------------- void ofApp::gotMessage(ofMessage msg){ } //-------------------------------------------------------------- void ofApp::dragEvent(ofDragInfo dragInfo){ }
myImg.load("Lenna.jpg")で目的の画像をロードできます。bin/data に置いているのでパスを指定する必要はありません。draw関数の一行目は
ofSetColor(ofColor::white);
としました。こうしないと文字色に引っ張られて画像自体が真っ黒になってしまいます。いい方法あったら教えてください。
ofApp.h
#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 counter; int iw, ih; ofImage myImg; };