HaskellでOpenGLのサンプルを動かす
どうもGLUT
は描画がメインで、GUIの操作はうまくできないらしく、GUI操作までやりたい場合はGLFW
を使うようです(この点は理解があやふや)。ともかく動いたのでメモ。
サンプル
ライブラリ
OpenGLのライブラリ(Macではデフォルトで入っている??)
GitHub - bsl/GLFW-b: Haskell bindings to GLFW
サンプル
GitHub - bsl/GLFW-b-demo: GLFW-b demo
インストール手順
サンドボックスを作成
cabal sandbox
OpenGL
のインストール
cabal install OpenGL
GLFW-b
のインストール
cabal install GLFW-b
mtl
のインストール
cabal install mtl
src
ディレクトリを作り、そこに、サンプルのソースをコピーする。
(root)$ ls src Gear.hs Main.hs
cabal
ファイルをinit``で作成する。あらかじめソースがあると依存関係をよしなに
cabal```ファイルに自動的に記述してくれるようです。
(root)$ cabal init
依存関係の該当部分は下記のようになっていました。(実際に生成された依存関係は1行でした。)
executable Main main-is: Main.hs -- other-modules: -- other-extensions: build-depends: base >=4.9 && <4.10, OpenGL >=3.0 && <3.1, stm >=2.4 && <2.5, mtl >=2.2 && <2.3, transformers >=0.5 && <0.6, pretty >=1.1 && <1.2, GLFW-b >=1.4 && <1.5
ビルドする。
(root)$ cabal build Package has never been configured. Configuring with default flags. If this fails, please run configure manually. Warning: The package list for 'hackage.haskell.org' is 19 days old. Run 'cabal update' to get the latest list of available packages. Resolving dependencies... Configuring Main-0.1.0.0... Building Main-0.1.0.0... Preprocessing executable 'Main' for Main-0.1.0.0... [1 of 2] Compiling Gear ( src/Gear.hs, dist/build/Main/Main-tmp/Gear.o ) [2 of 2] Compiling Main ( src/Main.hs, dist/build/Main/Main-tmp/Main.o ) Linking dist/build/Main/Main ...
実行する。
(root)$ dist/build/Main/Main ------------------------------------------------------------ '?': Print these instructions 'i': Print GLFW information * Mouse cursor, keyboard cursor keys, and/or joystick control rotation. * Mouse scroll wheel controls distance from scene. ------------------------------------------------------------ window focus: FocusState'Focused cursor pos: 26 695 cursor pos: 15 682 cursor pos: 11 665