首先須要說明的一點是: openFrameworks
設計的初衷不是爲計算機專業人士準備的, 而是爲藝術專業人士準備的, 就像 Processing
同樣.javascript
openFrameworks
是一個功能強大, 上手超級簡單的 C/C++
開源框架. 它集成封裝了不少經常使用的庫, 諸如:css
openFrameworks
爲這些庫提供了一個統一的接口, 使得使用它們變得異常容易.html
另外值得一提的就是 openFrameworks
的跨平臺特性, 從桌面系統到移動終端, openFrameworks
目前支持:java
最新的 0.9.0
版本還支持運行 Linux
系統的 arm
開發板, 例如樹莓派.linux
openFrameworks
集成了 Emscripten
做爲插件, 咱們的 openFrameworks
程序能夠經過它很容易地轉換成 javascript
的格式, 這樣很是方便就把本地項目轉換成了 WEB
項目, 能夠直接發佈到網絡上.git
openFrameworks
除了支持命令行編譯外, 還支持比較經常使用的幾種集成開發環境. 以下:github
其實 openFrameworks
最值得稱道的是它的代碼結構極其簡單, 假設你的項目名爲 myOF
, 那麼代碼結構以下:web
#include "myOF.h" //-------------------------------------------------------------- void myOF::setup(){ } //-------------------------------------------------------------- void myOF::update(){ } //-------------------------------------------------------------- void myOF::draw(){ } //-------------------------------------------------------------- void myOF::keyPressed(int key){ } ...
若是你有過使用 Processing
的經驗, 就會感受很是熟悉, 沒錯, 這些函數跟 Processing
中的含義徹底同樣.編程
setup
在程序啓動時執行一次;draw
在程序啓動後每秒執行60次(該頻率可設), 用來繪製圖形;update
跟 draw
同樣, 每秒執行60次, 把須要更新的代碼放在這裏;keyPressed
處理按鍵事件;爲何會使用這種代碼結構? 由於 openFrameworks
本來的設計目的就是要讓非計算機專業的人士經過編程來創造各類計算機視覺/圖像/視頻/聲音藝術, 就像 Processing
的設計目標同樣, 因此它纔會搞成這種簡單易用可是功能強大的形式.ubuntu
openFrameworks
支持多個平臺的安裝, 從官網下載地址取得對應的版本, 以樹莓派爲例, 以下:
解壓後進入目錄:
pi@rpi /opt/software/openFrameworks $ cd scripts/linux/debian/ pi@rpi /opt/software/openFrameworks/scripts/linux/debian $ ls install_codecs.sh install_dependencies.sh pi@rpi /opt/software/openFrameworks/scripts/linux/debian $
安裝依賴:
pi@rpi /opt/software/openFrameworks/scripts/linux/debian $ sudo ./install_dependencies.sh
安裝編解碼:
pi@rpi /opt/software/openFrameworks/scripts/linux/debian $ sudo ./install_codecs.sh
編譯oF庫:
pi@rpi /opt/software/openFrameworks/scripts/linux $ ls archlinux buildAllExamples.sh cleanAllExamples.sh compileOF.sh debian removeFMOD.sh ubuntu archlinux_armv6 buildAllRPIExamples.sh codeblocks_wizard compilePG.sh fedora testAllExamples.sh ubuntu-omap4 pi@rpi /opt/software/openFrameworks/scripts/linux $ sudo ./compileOF.sh
編譯項目生成器:
pi@rpi /opt/software/openFrameworks/scripts/linux $ sudo ./compilePG.sh
編譯所有例程:
pi@rpi /opt/software/openFrameworks/scripts/linux $ sudo ./buildAllRPIExamples.sh
執行全部編譯好的例程:
pi@rpi /opt/software/openFrameworks/scripts/linux $ sudo ./testAllExamples.sh
也能夠單獨編譯某個例程, 只要進入其目錄操做便可.
安裝好以後, 在目錄 ./examples/empty/
下有一個空項目模板 emptyExample
, 咱們能夠基於這個空項目模板來寫本身的 oF
程序, 針對不一樣的平臺, 空項目模板的結構也不一樣, 例如:
Raspbian
平臺下, 空項目模板的結構以下:pi@rpi /opt/software/openFrameworks/examples/empty $ tree emptyExample/ emptyExample/ ├── bin │ ├── emptyExample │ ├── libs │ └── readMe.txt ├── config.make ├── Makefile ├── obj │ └── linuxarmv7l │ └── Release │ └── src │ ├── main.d │ ├── main.o │ ├── ofApp.d │ └── ofApp.o └── src ├── main.cpp ├── ofApp.cpp └── ofApp.h 7 directories, 11 files pi@rpi /opt/software/openFrameworks/examples/empty $
OSX
平臺下, 空項目模板的結構以下:admin@Air:~/Downloads/of_v0.9.0_osx_release/examples/empty$ tree emptyExample/ |more emptyExample/ ├── Makefile ├── Project.xcconfig ├── addons.make ├── bin │ ├── data │ └── emptyExample.app │ └── Contents │ ├── Frameworks │ │ └── GLUT.framework │ │ ├── GLUT -> Versions/Current/GLUT │ │ ├── Headers -> Versions/Current/Headers │ │ ├── Resources -> Versions/Current/Resources │ │ └── Versions │ │ ├── A │ │ │ ├── GLUT │ │ │ ├── Headers │ │ │ │ ├── copy.h │ │ │ │ ├── extrude.h │ │ │ │ ├── glsmap.h │ │ │ │ ├── glsmapint.h │ │ │ │ ├── glut.h │ │ │ │ ├── glutbitmap.h │ │ │ │ ├── glutf90.h │ │ │ │ ├── glutstroke.h │ │ │ │ ├── gutil.h │ │ │ │ ├── intersect.h │ │ │ │ ├── port.h │ │ │ │ ├── rot.h │ │ │ │ ├── segment.h │ │ │ │ ├── tube.h │ │ │ │ ├── tube_gc.h │ │ │ │ └── vvector.h │ │ │ └── Resources │ │ │ ├── Caution.tiff │ │ │ ├── English.lproj │ │ │ │ ├── GLUT.nib │ │ │ │ │ └── objects.nib │ │ │ │ ├── GLUTClipboard.nib │ │ │ │ │ └── objects.nib │ │ │ │ ├── GLUTPreferences.nib │ │ │ │ │ └── objects.nib │ │ │ │ ├── GLUTUI.strings │ │ │ │ └── InfoPlist.strings │ │ │ ├── Info.plist │ │ │ ├── blankCursor.tiff │ │ │ ├── bottomCursor.tiff │ │ │ ├── bottomleftCursor.tiff │ │ │ ├── bottomrightCursor.tiff │ │ │ ├── crossCursor.tiff │ │ │ ├── cycleCursor.tiff │ │ │ ├── destroyCursor.tiff │ │ │ ├── fingerCursor.tiff │ │ │ ├── helpCursor.tiff │ │ │ ├── leftCursor.tiff │ │ │ ├── leftRightCursor.tiff │ │ │ ├── rightArrowCursor.tiff │ │ │ ├── rightCursor.tiff │ │ │ ├── sprayCursor.tiff │ │ │ ├── topCursor.tiff │ │ │ ├── topleftCursor.tiff │ │ │ ├── toprightCursor.tiff │ │ │ ├── upDownCursor.tiff │ │ │ └── waitCursor.tiff │ │ └── Current -> A │ ├── Info.plist │ ├── MacOS │ │ ├── emptyExample │ │ └── libfmodex.dylib │ ├── PkgInfo │ └── Resources │ └── icon.icns ├── build │ └── emptyExample.build │ └── Release │ └── emptyExample.build │ ├── Objects-normal │ │ └── x86_64 │ │ ├── emptyExample.LinkFileList │ │ ├── emptyExample_dependency_info.dat │ │ ├── main.d │ │ ├── main.dia │ │ ├── main.o │ │ ├── ofApp.d │ │ ├── ofApp.dia │ │ └── ofApp.o │ ├── Script-E4B6FFFD0C3F9AB9008CF71C.sh │ ├── dgph │ ├── dgph~ │ └── emptyExample.hmap ├── config.make ├── emptyExample.xcodeproj │ ├── project.pbxproj │ └── xcshareddata │ └── xcschemes │ ├── emptyExample Debug.xcscheme │ └── emptyExample Release.xcscheme ├── openFrameworks-Info.plist └── src ├── main.cpp ├── ofApp.cpp └── ofApp.h 29 directories, 72 files admin@Air:~/Downloads/of_v0.9.0_osx_release/examples/empty$
這個空模板項目既能夠在諸如 XCode
之類的 IDE
中打開, 也能夠直接在命令行編輯, 而後在命令行編譯, 下面, 咱們分別在 Raspbian
和 OSX
的命令行下新建一個項目並編譯它.
首先把空模板拷貝一份, 假設咱們的項目叫 myOF
, 那麼執行下面這條命令:
pi@rpi /opt/software/openFrameworks/examples/empty $ cp -R emptyExample/ ./myOF pi@rpi /opt/software/openFrameworks/examples/empty $ ls emptyExample myOF pi@rpi /opt/software/openFrameworks/examples/empty $ cd myOF/ pi@rpi /opt/software/openFrameworks/examples/empty/myOF $ ls bin config.make Makefile obj src pi@rpi /opt/software/openFrameworks/examples/empty/myOF $
其中 src
目錄是源代碼目錄, 進入查看
pi@rpi /opt/software/openFrameworks/examples/empty/myOF $ cd src pi@rpi /opt/software/openFrameworks/examples/empty/myOF/src $ ls main.cpp ofApp.cpp ofApp.h pi@rpi /opt/software/openFrameworks/examples/empty/myOF/src $
總共3個文件, 分別看看內容:
pi@rpi /opt/software/openFrameworks/examples/empty/myOF/src $ cat main.cpp #include "ofMain.h" #include "ofApp.h" //======================================================================== int main( ){ ofSetupOpenGL(1024,768, OF_WINDOW); // <-------- setup the GL context // this kicks off the running of my app // can be OF_WINDOW or OF_FULLSCREEN // pass in width and height too: ofRunApp( new ofApp()); } pi@rpi /opt/software/openFrameworks/examples/empty/myOF/src $
pi@rpi /opt/software/openFrameworks/examples/empty/myOF/src $ cat ofApp.cpp #include "ofApp.h" //-------------------------------------------------------------- void ofApp::setup(){ } //-------------------------------------------------------------- void ofApp::update(){ } //-------------------------------------------------------------- void ofApp::draw(){ } //-------------------------------------------------------------- void ofApp::keyPressed(int key){ } //-------------------------------------------------------------- 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){ } pi@rpi /opt/software/openFrameworks/examples/empty/myOF/src $
pi@rpi /opt/software/openFrameworks/examples/empty/myOF/src $ cat 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); }; pi@rpi /opt/software/openFrameworks/examples/empty/myOF/src $
容易看出, 咱們要編輯的文件是 ofApp.cpp
, 咱們打算畫兩個不一樣顏色的圓, 代碼就是下面這些:
//-------------------------------------------------------------- void ofApp::draw(){ ofSetColor(255, 0, 255); ofCircle(200, 300, 60); ofSetColor(0, 255, 255); ofCircle(500, 500, 100); }
修改 ofApp.cpp
後保存, 回到上層目錄, 開始編譯:
pi@rpi /opt/software/openFrameworks/examples/empty/myOF $ sudo make HOST_OS=Linux HOST_ARCH=armv7l checking pkg-config libraries: cairo zlib gstreamer-app-1.0 gstreamer-1.0 gstreamer-video-1.0 gstreamer-base-1.0 libudev freetype2 fontconfig sndfile openal openssl libpulse-simple alsa gtk+-3.0 libmpg123 Compiling OF library for Release make[1]: Entering directory '/opt/software/openFrameworks/libs/openFrameworksCompiled/project' HOST_OS=Linux HOST_ARCH=armv7l checking pkg-config libraries: cairo zlib gstreamer-app-1.0 gstreamer-1.0 gstreamer-video-1.0 gstreamer-base-1.0 libudev freetype2 fontconfig sndfile openal openssl libpulse-simple alsa gtk+-3.0 libmpg123 HOST_OS=Linux HOST_ARCH=armv7l checking pkg-config libraries: cairo zlib gstreamer-app-1.0 gstreamer-1.0 gstreamer-video-1.0 gstreamer-base-1.0 libudev freetype2 fontconfig sndfile openal openssl libpulse-simple alsa gtk+-3.0 libmpg123 Compiling /opt/software/openFrameworks/libs/openFrameworks/utils/ofSystemUtils.cpp ... HOST_OS=Linux HOST_ARCH=armv7l checking pkg-config libraries: cairo zlib gstreamer-app-1.0 gstreamer-1.0 gstreamer-video-1.0 gstreamer-base-1.0 libudev freetype2 fontconfig sndfile openal openssl libpulse-simple alsa gtk+-3.0 libmpg123 compiling done to launch the application cd bin ./myOF - or - make run make[1]: Leaving directory '/opt/software/openFrameworks/examples/empty/myOF' pi@rpi /opt/software/openFrameworks/examples/empty/myOF $
成功編譯, 沒有錯誤!
咱們能夠直接進入目錄 bin/myOF.app/Contents/MacOS/
下運行 ./myOF
, 也能夠直接在當前目錄下運行 make run
來查看結果:
pi@rpi /opt/software/openFrameworks/examples/empty/myOF $ make run HOST_OS=Linux HOST_ARCH=armv7l checking pkg-config libraries: cairo zlib gstreamer-app-1.0 gstreamer-1.0 gstreamer-video-1.0 gstreamer-base-1.0 libudev freetype2 fontconfig sndfile openal openssl libpulse-simple alsa gtk+-3.0 libmpg123 [notice ] ofAppEGLWindow: setupRPiNativeWindow(): screenRect: 1280x1024 [notice ] ofAppEGLWindow: setupRPiNativeWindow(): windowRect: 1024x768 [notice ] ofAppEGLWindow: createSurface(): setting up EGL Display [notice ] ofAppEGLWindow: createSurface(): EGL Display correctly set 0x1 [notice ] ofAppEGLWindow: createSurface(): no current renderer selected [notice ] ofAppEGLWindow: createSurface(): default renderer detected [notice ] ofAppEGLWindow: createSurface(): surface created correctly [notice ] ofAppEGLWindow: createSurface(): API bound correctly [notice ] ofAppEGLWindow: createSurface(): -----EGL----- [notice ] ofAppEGLWindow: createSurface(): EGL_VERSION_MAJOR = 1 [notice ] ofAppEGLWindow: createSurface(): EGL_VERSION_MINOR = 4 [notice ] ofAppEGLWindow: createSurface(): EGL_CLIENT_APIS = OpenGL_ES OpenVG [notice ] ofAppEGLWindow: createSurface(): EGL_VENDOR = Broadcom [notice ] ofAppEGLWindow: createSurface(): EGL_VERSION = 1.4 [notice ] ofAppEGLWindow: createSurface(): EGL_EXTENSIONS = EGL_KHR_image EGL_KHR_image_base EGL_KHR_image_pixmap EGL_KHR_vg_parent_image EGL_KHR_gl_texture_2D_image EGL_KHR_gl_texture_cubemap_image EGL_KHR_lock_surface [notice ] ofAppEGLWindow: createSurface(): GL_RENDERER = VideoCore IV HW [notice ] ofAppEGLWindow: createSurface(): GL_VERSION = OpenGL ES-CM 1.1 [notice ] ofAppEGLWindow: createSurface(): GL_VENDOR = Broadcom [notice ] ofAppEGLWindow: createSurface(): ------------- [notice ] ofAppEGLWindow: setupPeripherals(): peripheral setup complete [notice ] ofAppEGLWindow: setupNativeUDev(): created udev object [notice ] ofAppEGLWindow: setupMouse(): mouse_fd= 5 devicePath=/dev/input/by-path/platform-3f980000.usb-usb-0:1.2:1.0-event-mouse [notice ] ofAppEGLWindow: setupMouse(): mouse device name = Logitech USB Optical Mouse [notice ] ofAppEGLWindow: setupKeyboard(): keyboard_fd= 6 devicePath=/dev/input/by-path/platform-3f980000.usb-usb-0:1.3:1.0-event-kbd [notice ] ofAppEGLWindow: setupKeyboard(): keyboard device name = USB Keyboard [notice ] ofAppEGLWindow: setupPeripherals(): native event setup complete
按 ESC
或者 Ctrl-c
退出.
由於 openFrameworks
經過 framebuffer
繪圖, 因此不須要 XWindows
.
說明: 由於樹莓派下截圖比較麻煩, 因此一概從 OSX
上截圖.
注意: 在樹莓派上運行須要有一個直接外接的屏幕, 只經過 VNC
鏈接是看不到繪製出的圖像的.
爲 OSX
準備的項目模板既能夠經過命令行編譯, 也能夠經過 XCode
編譯, 首次編譯花的時間比較長, 由於要編譯各類庫, 以後再編譯就快多了.
經過拷貝空項目模板來新建項目, 而後修改 ofApp.cpp
, 再進行編譯, 記錄以下:
... admin@Air:~/Downloads/of_v0.9.0_osx_release/examples/empty/myOF$ sudo make HOST_OS=Darwin HOST_ARCH=x86_64 Compiling OF library for Release HOST_OS=Darwin HOST_ARCH=x86_64 HOST_OS=Darwin HOST_ARCH=x86_64 HOST_OS=Darwin HOST_ARCH=x86_64 Done! Compiling myOF for Release HOST_OS=Darwin HOST_ARCH=x86_64 Compiling /Users/admin/Downloads/of_v0.9.0_osx_release/examples/empty/myOF/src/ofApp.cpp ... HOST_OS=Darwin HOST_ARCH=x86_64 TARGET=bin/myOF compiling done to launch the application cd bin/myOF.app/Contents/MacOS/ ./myOF - or - make run admin@Air:~/Downloads/of_v0.9.0_osx_release/examples/empty/myOF$
沒有任何錯誤, 完成編譯, 用跟 Raspbian
一樣的方式來運行編譯好的程序
admin@Air:~/Downloads/of_v0.9.0_osx_release/examples/empty/myOF$ make run HOST_OS=Darwin HOST_ARCH=x86_64
運行截圖以下:
若是打算增長點內容, 修改 ofApp.cpp
中對應的函數便可.
在 draw
中增長這一句:
ofDrawBitmapString(ofToString(ofGetFrameRate())+"fps", 10, 15);
能夠在程序窗口左上角顯示幀速率
在 setup
中增長這一句:
ofSetFrameRate(60);
在 setup
中增長這幾句:
// 修改繪製精度 ofSetCircleResolution(120); // 開啓抗鋸齒 ofEnableSmoothing(); // 開啓垂直同步 ofSetVerticalSync(true);
在 setup
中增長這一句:
ofBackground(0,0,0);
把背景色改成黑色.
截圖以下:
除了畫圓,oF 也能夠畫其餘的圖案:
openFrameworks
的API
函數能夠在它的官網文檔查詢, 該文檔不只包含對 openFrameworks
的 API
說明, 也包含了對插件 API
的說明, 至關詳細.
openFrameworks
支持第三方插件, 咱們先看看這個版本自帶了哪些插件:
admin@Air:~/Downloads/of_v0.9.0_osx_release/addons$ ls -al total 16 drwxr-xr-x@ 15 admin staff 510 12 17 17:25 . drwxr-xr-x@ 17 admin staff 578 12 16 21:19 .. -rw-r--r--@ 1 admin staff 6148 12 17 17:30 .DS_Store drwxr-xr-x@ 6 admin staff 204 11 8 17:30 ofxAssimpModelLoader drwxr-xr-x@ 4 admin staff 136 11 8 17:30 ofxEmscripten drwxr-xr-x@ 3 admin staff 102 11 8 17:30 ofxGui drwxr-xr-x@ 8 admin staff 272 11 8 17:30 ofxKinect drwxr-xr-x 17 admin staff 578 12 17 17:29 ofxLua drwxr-xr-x@ 4 admin staff 136 11 8 17:30 ofxNetwork drwxr-xr-x@ 6 admin staff 204 11 8 17:30 ofxOpenCv drwxr-xr-x@ 5 admin staff 170 11 8 17:30 ofxOsc drwxr-xr-x@ 4 admin staff 136 11 8 17:30 ofxSvg drwxr-xr-x@ 3 admin staff 102 11 8 17:30 ofxThreadedImageLoader drwxr-xr-x@ 4 admin staff 136 11 8 17:30 ofxVectorGraphics drwxr-xr-x@ 4 admin staff 136 11 8 17:30 ofxXmlSettings admin@Air:~/Downloads/of_v0.9.0_osx_release/addons$
想看到更多的插件能夠來這裏官網插件查找, 或者到 github
搜索 ofx
打頭的項目.
這裏列舉一些比較常見的插件
訪問這個插件的 Github
項目主頁;
把它克隆到你的 addons
目錄:
進入你的 openFrameworks
目錄下的 addons
目錄,git clone
該項目;
若是這個項目自帶 example
,能夠直接 make && make run
編譯和執行它看看結果。
說明: 本文大量參考A Brief Introduction to openFrameworks-介紹一個開源的 C++ 開發框架 openFrameworks文中的內容, 特此感謝.
A Brief Introduction to openFrameworks-介紹一個開源的 C++ 開發框架 openFrameworks