3

Is there a simple complete code example using any gui toolkit (that will work in both Linux and Windows) of opening multiple opengl windows simultaneously? And how to handle their events and such separately of course. I tried it naively and it crashes.

I received a full working source code example from someone outside of stackoverflow. I'm pasting it here for all to benefit.

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
2
  • This used to be in wxcore hackage.haskell.org/package/wxcore-0.11.1.2 I only ever used it with a single window, but it worked well. You probably need to follow up with the developers to see why it was dropped. Commented Feb 2, 2011 at 17:27
  • Thanks. I've changed the question so it's not wx specific. Commented Feb 2, 2011 at 18:28

2 Answers 2

3
+50

GLUT, of course.

The GLUT homepage states

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

Hence you can use GLUT for managing multiple windows (I had used once). Here is a tutorial for what you need.

I've also found this article which you may look a little, since it's Haskell specific.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I was asking for a Haskell code example, but that powerpoint appears it might be possible to translate accross. I'll give that a try and post the results here.
1

OpenGL support in wxWidgets uses the WxGLCanvas class, which is in wxcore as GLCanvas. Unfortunately, it doesn't seem to exist in the wx package. You can probably implement your own control for GLCanvas without too much difficulty, using the other controls in the wx package and C++ usage examples as a reference.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.