2011-01-31 25 views
3

是否有一個簡單的完整的代碼示例使用任何gui工具包(這將在Linux和Windows中工作)同時打開多個opengl窗口?當然,如何分開處理他們的事件。我天真地嘗試了它,它崩潰了。如何在具有多個窗口的Haskell中使用OpenGL?

我收到了一個完整的工作源代碼示例從某人之外的計算器。我在這裏粘貼它爲所有人受益。在wxWidgets中

module Main where 

import Graphics.UI.GLUT 
import System.Exit (exitWith, ExitCode(ExitSuccess)) 

reshape :: ReshapeCallback 
reshape size = do 
    viewport $= (Position 0 0, size) 
    matrixMode $= Projection 
    loadIdentity 
    frustum (-1) 1 (-1) 1 1.5 20 
    matrixMode $= Modelview 0 

keyboard :: KeyboardMouseCallback 
keyboard (Char '\27') Down _ _ = exitWith ExitSuccess 
keyboard _   _ _ _ = return() 

renderCube :: Color3 GLfloat -> IO() 
renderCube c = do 
    clear [ ColorBuffer ] 

    let color3f = color :: Color3 GLfloat -> IO() 
     scalef = scale :: GLfloat -> GLfloat -> GLfloat -> IO() 

    color3f c 
    loadIdentity 
    lookAt (Vertex3 0 0 5) (Vertex3 0 0 0) (Vector3 0 1 0) 
    scalef 1 2 1 
    renderObject Wireframe (Cube 1) 
    flush 

displayR :: DisplayCallback 
displayR = renderCube (Color3 1 0 0) 

displayB :: DisplayCallback 
displayB = renderCube (Color3 0 0 1) 

createWindowWithDisplayFunc :: String -> Position -> DisplayCallback -> IO Window 
createWindowWithDisplayFunc name pos display = do 
    win <- createWindow name 
    windowPosition $= pos 
    clearColor $= Color4 0 0 0 0 
    shadeModel $= Flat 
    displayCallback $= display 
    reshapeCallback $= Just reshape 
    keyboardMouseCallback $= Just keyboard 
    return win 

main = do 
    getArgsAndInitialize 
    initialDisplayMode $= [ SingleBuffered, RGBMode ] 
    initialWindowSize $= Size 100 100 
    initialWindowPosition $= Position 100 100 

    createWindowWithDisplayFunc "R" (Position 10 10) displayR 
    createWindowWithDisplayFunc "B" (Position 110 10) displayB 

    mainLoop 
+0

這個曾經用在`wxcore` http://hackage.haskell.org/package/wxcore-0.11.1.2我只用過一個窗口,但它運行良好。您可能需要跟進開發人員,看看爲什麼它被丟棄。 – Anthony 2011-02-02 17:27:04

+0

謝謝。我已經改變了這個問題,所以它不是特定的。 – mentics 2011-02-02 18:28:10

回答

3

GLUT,當然。

GLUT homepage狀態

The toolkit supports: 
- Multiple windows for OpenGL rendering 
- Callback driven event processing 
- Sophisticated input devices 
- An 'idle' routine and timers 
- A simple, cascading pop-up menu facility 
- Utility routines to generate various solid and wire frame objects 
- Support for bitmap and stroke fonts 
- Miscellaneous window management functions 

因此,你可以使用GLUT來管理多個窗口(我用了一次)。 Here是一個你需要的教程。

我也發現this文章,你可能看起來有點,因爲它是Haskell特定的。

1

OpenGL支持使用WxGLCanvas class,這是in wxcore as GLCanvas。不幸的是,並不是似乎存在於wx包中。您可以使用wx軟件包中的其他控件和C++使用示例作爲參考,而沒有太多困難,可以實現您自己的GLCanvas控件。

相關問題