Debian Codegraph @

src/Graphics/UI/GLUT/Colormap.hs

raw
1 --------------------------------------------------------------------------------
2 -- |
3 -- Module : Graphics.UI.GLUT.Colormap
4 -- Copyright : (c) Sven Panne 2002-2018
5 -- License : BSD3
6 --
7 -- Maintainer : Sven Panne <[email protected]>
8 -- Stability : stable
9 -- Portability : portable
10 --
11 -- OpenGL supports both RGBA and color index rendering. The RGBA mode is
12 -- generally preferable to color index because more OpenGL rendering
13 -- capabilities are available and color index mode requires the loading of
14 -- colormap entries.
15 --
16 -- The GLUT color index state variables are used to read and write entries in a
17 -- window\'s color index colormap. Every GLUT color index window has its own
18 -- logical color index colormap. The size of a window\'s colormap can be
19 -- determined by reading 'numColorMapEntries'.
20 --
21 -- GLUT color index windows within a program can attempt to share colormap
22 -- resources by copying a single color index colormap to multiple windows using
23 -- 'copyColormap'. If possible GLUT will attempt to share the actual colormap.
24 -- While copying colormaps using 'copyColormap' can potentially allow sharing of
25 -- physical colormap resources, logically each window has its own colormap. So
26 -- changing a copied colormap of a window will force the duplication of the
27 -- colormap. For this reason, color index programs should generally load a
28 -- single color index colormap, copy it to all color index windows within the
29 -- program, and then not modify any colormap cells.
30 --
31 -- Use of multiple colormaps is likely to result in colormap installation
32 -- problems where some windows are displayed with an incorrect colormap due to
33 -- limitations on colormap resources.
34 --
35 --------------------------------------------------------------------------------
36
37 module Graphics.UI.GLUT.Colormap (
38 colorMapEntry,
39 copyColormap,
40 numColorMapEntries,
41 transparentIndex
42 ) where
43
44 import Control.Monad.IO.Class ( MonadIO(..) )
45 import Data.StateVar ( GettableStateVar, makeGettableStateVar, StateVar, makeStateVar )
46 import Foreign.C.Types ( CInt )
47 import Graphics.Rendering.OpenGL.GL.VertexSpec ( Index1(..), Color3(..) )
48 import Graphics.Rendering.OpenGL ( GLint, GLfloat )
49
50 import Graphics.UI.GLUT.QueryUtils
51 import Graphics.UI.GLUT.Raw
52 import Graphics.UI.GLUT.Types
53
54 --------------------------------------------------------------------------------
55
56 -- | Controls the color index colormap entry of the /current window/\'s logical
57 -- colormap for the /layer in use/. The /layer in use/ of the /current window/
58 -- should be a color index window. The color index should be zero or greater and
59 -- less than the total number of colormap entries for the window (see
60 -- 'numColorMapEntries') and different from an overlay\'s transparent index (see
61 -- 'transparentIndex').
62 --
63 -- If the /layer in use/\'s colormap was copied by reference, setting a colormap
64 -- entry will force the duplication of the colormap.
65
66 colorMapEntry :: Index1 GLint -> StateVar (Color3 GLfloat)
67 colorMapEntry (Index1 cell) =
68 makeStateVar (getColorMapEntry (fromIntegral cell))
69 (setColorMapEntry (fromIntegral cell))
70
71 setColorMapEntry :: CInt -> Color3 GLfloat -> IO ()
72 setColorMapEntry cell (Color3 r g b) = glutSetColor cell r g b
73
74 getColorMapEntry :: CInt -> IO (Color3 GLfloat)
75 getColorMapEntry cell = do
76 r <- glutGetColor cell glut_RED
77 g <- glutGetColor cell glut_GREEN
78 b <- glutGetColor cell glut_BLUE
79 return $ Color3 r g b
80
81 --------------------------------------------------------------------------------
82
83 -- | Copy (lazily if possible to promote sharing) the logical colormap from a
84 -- specified window to the /current window/\'s /layer in use/. The copy will be
85 -- from the normal plane to the normal plane; or from the overlay to the overlay
86 -- (never across different layers). Once a colormap has been copied, avoid
87 -- setting cells in the colormap via 'colorMapEntry' since that will force an
88 -- actual copy of the colormap if it was previously copied by reference.
89 -- 'copyColormap' should only be called when both the /current window/ and the
90 -- specified window are color index windows.
91
92 copyColormap :: MonadIO m => Window -> m ()
93 copyColormap (Window win) = glutCopyColormap win
94
95 --------------------------------------------------------------------------------
96
97 -- | Contains the number of entries in the colormap of the /current window/\'s
98 -- current layer (0 in RGBA mode).
99
100 numColorMapEntries :: GettableStateVar GLint
101 numColorMapEntries =
102 makeGettableStateVar $ simpleGet fromIntegral glut_WINDOW_COLORMAP_SIZE
103
104 --------------------------------------------------------------------------------
105
106 -- | Contains the transparent color index of the overlay of the /current window/
107 -- or -1 if no overlay is in use.
108
109 transparentIndex :: GettableStateVar (Index1 GLint)
110 transparentIndex =
111 makeGettableStateVar $
112 layerGet (Index1 . fromIntegral) glut_TRANSPARENT_INDEX