IT練習ノート

IT関連で調べたこと(実際は嵌ったこと)を書いています。

HaskellでOpenGLのサンプルを動かす

どうもGLUTは描画がメインで、GUIの操作はうまくできないらしく、GUI操作までやりたい場合はGLFWを使うようです(この点は理解があやふや)。ともかく動いたのでメモ。

サンプル

f:id:naotoogawa:20170202212747p:plain

ライブラリ

OpenGLのライブラリ(Macではデフォルトで入っている??)

GLFW - An OpenGL library

Haskellバインディング

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