Mac, Haskell, OpenGLでサンプルを動かすまで(2017/02時点)
このサンプルを動かそうとしたのですが、かなりハマったので、忘れないうちにメモを取ります。サンプルそのものの問題ではないです。
基本的な考え方
利用環境のOpenGLのバージョンを確認する。
GLSLのバージョンを確認する。
確認したOpenGLのバージョンに合わせてGLFWでWindowを生成する時にHintを与える
例
106 main :: IO () 107 main = do 108 GLFW.init 109 -- GLFW.defaultWindowHints ### このようなHintを与えている部分を利用環境に合わせて下記のように明示的に指定する。 110 111 mapM_ GLFW.windowHint 112 [ GLFW.WindowHint'Samples 4 -- 4x antialiasing 113 , GLFW.WindowHint'ContextVersionMajor 4 -- OpenGL 4.1 114 , GLFW.WindowHint'ContextVersionMinor 1 115 -- we don't want the old OpenGL 116 , GLFW.WindowHint'OpenGLProfile GLFW.OpenGLProfile'Core 117 ,GLFW.WindowHint'OpenGLForwardCompat True 118 ] 119 120 Just win <- GLFW.createWindow 640 480 "GLFW Demo" Nothing Nothing 121 GLFW.makeContextCurrent (Just win) 122 GLFW.setWindowSizeCallback win (Just resizeWindow) 123 GLFW.setKeyCallback win (Just keyPressed) 124 GLFW.setWindowCloseCallback win (Just shutdown) 125 descriptor <- initResources 126 onDisplay win descriptor 127 GLFW.destroyWindow win 128 GLFW.terminate
確認したGLSLのバージョンに合わせてシェーダーのファイル(triangles.frac,triangles.vert等)を作成/修正する。
#version 410 core
必要に応じてコードの修正をする。
利用しているAPIのバージョンが変わっている場合は、それに応じて修正を行う。
実際の手順 その1 (記憶を辿って書いているので抜けがあると思います)
サンドボックスの作成
cabal sandbox
必要なライブラリのインストール(足りなかったら追加)
cabal install GLFW-b cabal install OpenGL cabal install GLUtil cabal install mtl
サンプルの取得
git clone https://github.com/madjestic/Haskell-OpenGL-Tutorial
各チュートリアルのディレクトリに移動してcabalファイルを作成する。
foo$ ls Main.hs NGL README.md Shaders tutorial05.png foo$ vim ./NGL/ Linear.hs LoadShaders.hs Rendering.hs Shape.hs Utils.hs foo$ vim ./NGL/Rendering.hs foo$ cabal init Warning: The package list for 'hackage.haskell.org' is 22 days old. Run 'cabal update' to get the latest list of available packages. Package name? [default: tutorial05] Package version? [default: 0.1.0.0] Please choose a license: 1) GPL-2 2) GPL-3 3) LGPL-2.1 4) LGPL-3 5) AGPL-3 6) BSD2 * 7) BSD3 8) MIT 9) ISC 10) MPL-2.0 11) Apache-2.0 12) PublicDomain 13) AllRightsReserved 14) Other (specify) Your choice? [default: BSD3] 8 Author name? [default: who] foo Maintainer email? [default: who@gmail.com] foo@bar.com Project homepage URL? Project synopsis? Project category: * 1) (none) 2) Codec 3) Concurrency 4) Control 5) Data 6) Database 7) Development 8) Distribution 9) Game 10) Graphics 11) Language 12) Math 13) Network 14) Sound 15) System 16) Testing 17) Text 18) Web 19) Other (specify) Your choice? [default: (none)] 10 What does the package build: 1) Library 2) Executable Your choice? 2 What is the main module of the executable: * 1) Main.hs 2) Other (specify) Your choice? [default: Main.hs] Source directory: * 1) (none) 2) src 3) Other (specify) Your choice? [default: (none)] What base language is the package written in: * 1) Haskell2010 2) Haskell98 3) Other (specify) Your choice? [default: Haskell2010] Add informative comments to each field in the cabal file (y/n)? [default: n] n Guessing dependencies... Warning: no package found providing Graphics.Rendering.OpenGL. Warning: no package found providing Graphics.UI.GLFW. Warning: no package found providing Graphics.GLUtil. Warning: no package found providing Linear. Warning: no package found providing Graphics.UI.GLUT. Generating LICENSE... Generating Setup.hs... Generating ChangeLog.md... Generating tutorial05.cabal... Warning: no synopsis given. You should edit the .cabal file and add one. You may want to edit the .cabal file and add a Description field.
生成したcabalファイルをsandbox配下に移動する。
foo$ mv tutorial05.cabal ../../
cabalファイルのライブラリ依存関係とソースディレクトリを必要に応じて追加修正を行う。
22 build-depends: base >=4.9 && <4.10, 23 filepath >=1.4 && <1.5, 24 bytestring >=0.10 && <0.11, 25 text >=1.2 && <1.3, 26 OpenGL >=3.0 && <3.1, 27 GLFW-b, 28 GLUtil 29 hs-source-dirs: Haskell-OpenGL-Tutorial/tutorial05
プログラムのWindowHintを修正する。(上述)
シェーダのファイルのバージョンを修正する。(上述)
ビルドする。
foo$ cabal clean cleaning... foo$ 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 22 days old. Run 'cabal update' to get the latest list of available packages. Resolving dependencies... Configuring tutorial05-0.1.0.0... Building tutorial05-0.1.0.0... Preprocessing executable 'tutorial05' for tutorial05-0.1.0.0... [1 of 5] Compiling NGL.Utils ( Haskell-OpenGL-Tutorial/tutorial05/NGL/Utils.hs, dist/build/tutorial05/tutorial05-tmp/NGL/Utils.o ) [2 of 5] Compiling NGL.LoadShaders ( Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs, dist/build/tutorial05/tutorial05-tmp/NGL/LoadShaders.o ) Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs:42:41: error: Ambiguous occurrence ‘packUtf8’ It could refer to either ‘Graphics.Rendering.OpenGL.packUtf8’, imported from ‘Graphics.Rendering.OpenGL’ at Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs:25:1-32 (and originally defined in ‘OpenGL-3.0.1.0:Graphics.Rendering.OpenGL.GL.ByteString’) or ‘NGL.LoadShaders.packUtf8’, defined at Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs:46:1 [3 of 5] Compiling NGL.Shape ( Haskell-OpenGL-Tutorial/tutorial05/NGL/Shape.hs, dist/build/tutorial05/tutorial05-tmp/NGL/Shape.o )
エラーが出たら適宜修正する。
今回の場合は、packUtf8
が重複したのでLoadShaders.hs
にあるpackUtf8
をpackUtf8'
に変更した。
foo$ vim Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs
ビルドする。
foo$ cabal build Building tutorial05-0.1.0.0... Preprocessing executable 'tutorial05' for tutorial05-0.1.0.0... [3 of 5] Compiling NGL.LoadShaders ( Haskell-OpenGL-Tutorial/tutorial05/NGL/LoadShaders.hs, dist/build/tutorial05/tutorial05-tmp/NGL/LoadShaders.o ) [4 of 5] Compiling NGL.Rendering ( Haskell-OpenGL-Tutorial/tutorial05/NGL/Rendering.hs, dist/build/tutorial05/tutorial05-tmp/NGL/Rendering.o ) Haskell-OpenGL-Tutorial/tutorial05/NGL/Rendering.hs:114:2: warning: [-Wtabs] Tab character found here. Please use spaces instead. [5 of 5] Compiling Main ( Haskell-OpenGL-Tutorial/tutorial05/Main.hs, dist/build/tutorial05/tutorial05-tmp/Main.o ) Linking dist/build/tutorial05/tutorial05 ...
生成されたファイルをチュートリアルの場所に移動する。
シェーダファイルを読み込む必要があるのでパスを合わせる必要があるため。
foo$ mv dist/build/tutorial05/tutorial05 Haskell-OpenGL-Tutorial/tutorial05/
実行する。
foo$ ./tutorial05
実際の手順 その2 (記憶を辿って書いているので抜けがあると思います)
下記のサンプルの場合
Linus's Blog - OpenGL from Haskell
修正ポイント
OpenGLRawのバージョンに関して、サンプルでは2系ですが、最新の3系に修正する必要があります。
OpenGLRaw: A raw binding for the OpenGL graphics system
WindowHint
の変更だけでなく、import
の変更と、プレフィックスのgl_
をGL_
に変更する必要がありました。
修正箇所
modify 2014-03-15-opengl-from-haskell.html · GitHub