Safe Haskell | None |
---|---|
Language | Haskell2010 |
Basic low-level GL wrapper and reference.
- newVAO :: IO VAO
- bindVAO :: VAO -> IO ()
- deleteVAO :: VAO -> IO ()
- data VAO
- newBufferObject :: Storable a => Vector a -> UsageHint -> IO BufferObject
- bindVBO :: BufferObject -> IO ()
- bindElementArray :: BufferObject -> IO ()
- updateVBO :: Storable a => Vector a -> Int -> IO ()
- updateElementArray :: Storable a => Vector a -> Int -> IO ()
- deleteBufferObject :: BufferObject -> IO ()
- data BufferObject
- data UsageHint
- newProgram :: String -> String -> IO Program
- newProgramSafe :: String -> String -> IO (Either ProgramError Program)
- useProgram :: Program -> IO ()
- deleteProgram :: Program -> IO ()
- setUniform1f :: String -> [Float] -> IO ()
- setUniform2f :: String -> [V2 Float] -> IO ()
- setUniform3f :: String -> [V3 Float] -> IO ()
- setUniform4f :: String -> [V4 Float] -> IO ()
- setUniform1i :: String -> [Int] -> IO ()
- setUniform2i :: String -> [V2 Int] -> IO ()
- setUniform3i :: String -> [V3 Int] -> IO ()
- setUniform4i :: String -> [V4 Int] -> IO ()
- setUniform22 :: String -> [M22 Float] -> IO ()
- setUniform33 :: String -> [M33 Float] -> IO ()
- setUniform44 :: String -> [M44 Float] -> IO ()
- data Program
- data ProgramError
- = VertexShaderError String
- | FragmentShaderError String
- | LinkError String
- setVertexLayout :: [LayoutElement] -> IO ()
- data LayoutElement
- data DataType
- data Texture
- newTexture2D :: Storable a => Vector a -> (Int, Int) -> ImageFormat -> IO Texture
- newCubeMap :: Storable a => Cube (Vector a, (Int, Int)) -> ImageFormat -> IO Texture
- newEmptyTexture2D :: Int -> Int -> ImageFormat -> IO Texture
- newEmptyCubeMap :: Int -> Int -> ImageFormat -> IO Texture
- deleteTexture :: Texture -> IO ()
- setActiveTextureUnit :: Int -> IO ()
- bindTexture2D :: Texture -> IO ()
- bindTextureCubeMap :: Texture -> IO ()
- setTex2DFiltering :: Filtering -> IO ()
- setCubeMapFiltering :: Filtering -> IO ()
- setTex2DWrapping :: Wrapping -> IO ()
- setCubeMapWrapping :: Wrapping -> IO ()
- data Cube a = Cube {}
- data Filtering
- data Wrapping
- drawPoints :: Int -> IO ()
- drawLines :: Int -> IO ()
- drawLineStrip :: Int -> IO ()
- drawLineLoop :: Int -> IO ()
- drawTriangles :: Int -> IO ()
- drawTriangleStrip :: Int -> IO ()
- drawTriangleFan :: Int -> IO ()
- drawIndexedPoints :: Int -> IndexFormat -> IO ()
- drawIndexedLines :: Int -> IndexFormat -> IO ()
- drawIndexedLineStrip :: Int -> IndexFormat -> IO ()
- drawIndexedLineLoop :: Int -> IndexFormat -> IO ()
- drawIndexedTriangles :: Int -> IndexFormat -> IO ()
- drawIndexedTriangleStrip :: Int -> IndexFormat -> IO ()
- drawIndexedTriangleFan :: Int -> IndexFormat -> IO ()
- setViewport :: Viewport -> IO ()
- enableScissorTest :: Viewport -> IO ()
- disableScissorTest :: IO ()
- enableCulling :: Culling -> IO ()
- disableCulling :: IO ()
- data Viewport = Viewport {}
- data Culling
- data IndexFormat
- enableColorWriting :: IO ()
- disableColorWriting :: IO ()
- clearColorBuffer :: Real a => (a, a, a) -> IO ()
- enableDepthTest :: IO ()
- disableDepthTest :: IO ()
- clearDepthBuffer :: IO ()
- enableStencil :: Stencil -> IO ()
- disableStencil :: IO ()
- clearStencilBuffer :: IO ()
- basicStencil :: Stencil
- data Stencil = Stencil {
- func :: StencilFunc
- ref :: Int
- mask :: Word
- onStencilFail :: StencilOp
- onDepthFail :: StencilOp
- onBothPass :: StencilOp
- data StencilFunc
- = Never
- | Less
- | LessOrEqual
- | Greater
- | GreaterOrEqual
- | Equal
- | NotEqual
- | Always
- data StencilOp
- enableBlending :: Blending -> IO ()
- disableBlending :: IO ()
- basicBlending :: Blending
- data Blending = Blending {
- sFactor :: BlendFactor
- dFactor :: BlendFactor
- blendFunc :: BlendEquation
- blendColor :: (Float, Float, Float, Float)
- data BlendFactor
- data BlendEquation
- data FBO
- newFBO :: IO FBO
- bindFBO :: FBO -> IO ()
- bindDefaultFramebuffer :: IO ()
- deleteFBO :: FBO -> IO ()
- attachTex2D :: Texture -> IO ()
- attachCubeMap :: Texture -> (forall a. Cube a -> a) -> IO ()
- attachRBO :: RBO -> IO ()
- data RBO
- newRBO :: Int -> Int -> ImageFormat -> IO RBO
- deleteRBO :: RBO -> IO ()
- data GLError
- getGLError :: IO (Maybe GLError)
- assertNoGLError :: IO ()
- data ImageFormat
Overview
This library exposes a simplified subset of OpenGL that I hope is complete enough for following tutorials and making simple games or demos.
For a whirlwind tour of the machinery behind GL see the module: Graphics.GL.Low.EntirePictureUpFront
This library uses the gl
package for raw bindings to OpenGL and the
linear
package for matrices.
See submodules for specialized documentation of each subsystem.
Graphics.GL.Low.VAO
Graphics.GL.Low.BufferObject
Graphics.GL.Low.Shader
Graphics.GL.Low.VertexAttrib
Graphics.GL.Low.Texture
Graphics.GL.Low.Render
Graphics.GL.Low.Stencil
Graphics.GL.Low.Blending
Graphics.GL.Low.Framebuffer
VAO
See also Graphics.GL.Low.VAO.
Create a new VAO. The only thing you can do with a VAO is bind it to the vertex array binding target (bindVAO) or delete it.
bindVAO :: VAO -> IO () Source
Assign the VAO to the vertex array binding target. The VAO already bound will be replaced, if any.
Buffer Objects
See also Graphics.GL.Low.BufferObject.
newBufferObject :: Storable a => Vector a -> UsageHint -> IO BufferObject Source
Use this to create VBOs or element arrays from raw data. Choose the usage hint based on how often you expect to modify the VBO's contents.
bindVBO :: BufferObject -> IO () Source
Bind a VBO to the array buffer binding target. The buffer object bound there will be replaced, if any.
bindElementArray :: BufferObject -> IO () Source
Assign an element array to the element array binding target. It will replace the buffer object already bound there, if any. The binding will be remembered in the currently bound VAO.
updateVBO :: Storable a => Vector a -> Int -> IO () Source
Modify the data in the currently bound VBO starting from the specified index in bytes.
updateElementArray :: Storable a => Vector a -> Int -> IO () Source
Modify contents in the currently bound element array starting at the specified index in bytes.
deleteBufferObject :: BufferObject -> IO () Source
Delete a buffer object.
data BufferObject Source
Handle to a buffer object, such as a VBO or an element array.
Show BufferObject Source |
Usage hint for allocation of buffer object storage.
StaticDraw | Data will seldomly change. |
DynamicDraw | Data will change. |
StreamDraw | Data will change very often. |
Shader Program
See also Graphics.GL.Low.Shader.
:: String | vertex shader source code |
-> String | fragment shader source code |
-> IO Program |
Compile the code for a vertex shader and a fragment shader, then link them into a new program. If the compiler or linker fails it will throw a ProgramError.
newProgramSafe :: String -> String -> IO (Either ProgramError Program) Source
Same as newProgram
but does not throw exceptions.
useProgram :: Program -> IO () Source
Install a program into the rendering pipeline. Replaces the program already in use, if any.
deleteProgram :: Program -> IO () Source
Delete a program.
setUniform1f :: String -> [Float] -> IO () Source
setUniform2f :: String -> [V2 Float] -> IO () Source
setUniform3f :: String -> [V3 Float] -> IO () Source
setUniform4f :: String -> [V4 Float] -> IO () Source
setUniform1i :: String -> [Int] -> IO () Source
setUniform2i :: String -> [V2 Int] -> IO () Source
setUniform3i :: String -> [V3 Int] -> IO () Source
setUniform4i :: String -> [V4 Int] -> IO () Source
setUniform22 :: String -> [M22 Float] -> IO () Source
setUniform33 :: String -> [M33 Float] -> IO () Source
setUniform44 :: String -> [M44 Float] -> IO () Source
data ProgramError Source
The error message emitted by the driver when shader compilation or linkage fails.
VertexShaderError String | |
FragmentShaderError String | |
LinkError String |
Vertex Attributes
See also Graphics.GL.Low.VertexAttrib.
setVertexLayout :: [LayoutElement] -> IO () Source
This configures the currently bound VAO. It calls glVertexAttribPointer and glEnableVertexAttribArray.
data LayoutElement Source
The name of a vertex input to a program combined with the component format and number of components for that attribute in the vertex data. Alternatively the size of an unused section of the data in bytes.
Attrib String Int DataType | Name, component count and component format of a vertex attribute. |
Unused Int | Size in bytes of an unused section of the vertex data. |
Show LayoutElement Source |
The size and interpretation of a vertex attribute component.
GLFloat | 4-byte float |
GLByte | signed byte |
GLUnsignedByte | unsigned byte |
GLShort | 2-byte signed integer |
GLUnsignedShort | 2-byte unsigned integer |
GLInt | 4-byte signed integer |
GLUnsignedInt | 4-byte unsigned integer |
Textures
See also Graphics.GL.Low.Texture.
newTexture2D :: Storable a => Vector a -> (Int, Int) -> ImageFormat -> IO Texture Source
Create a new 2D texture from raw image data, its dimensions, and the assumed image format. The dimensions should be powers of 2.
newCubeMap :: Storable a => Cube (Vector a, (Int, Int)) -> ImageFormat -> IO Texture Source
Create a new cubemap texture from six raw data sources. Each side will have the same format.
newEmptyTexture2D :: Int -> Int -> ImageFormat -> IO Texture Source
Create an empty texture with the specified dimensions and format.
newEmptyCubeMap :: Int -> Int -> ImageFormat -> IO Texture Source
Create a cubemap texture where each of the six sides has the specified dimensions and format.
deleteTexture :: Texture -> IO () Source
Delete a texture.
setActiveTextureUnit :: Int -> IO () Source
Set the active texture unit. The default is zero.
bindTexture2D :: Texture -> IO () Source
Bind a 2D texture to the 2D texture binding target and the currently active texture unit.
bindTextureCubeMap :: Texture -> IO () Source
Bind a cubemap texture to the cubemap texture binding target and the currently active texture unit.
setTex2DFiltering :: Filtering -> IO () Source
Set the filtering for the 2D texture currently bound to the 2D texture binding target.
setCubeMapFiltering :: Filtering -> IO () Source
Set the filtering for the cubemap texture currently bound to the cubemap texture binding target.
setTex2DWrapping :: Wrapping -> IO () Source
Set the wrapping mode for the 2D texture currently bound to the 2D texture binding target.
setCubeMapWrapping :: Wrapping -> IO () Source
Set the wrapping mode for the cubemap texture currently bound to the cubemap texture binding target. Because no filtering occurs between cube faces you probably want ClampToEdge.
Six values, one on each side.
Texture filtering modes.
Texture wrapping modes.
Repeat | Tile the texture past the boundary. |
MirroredRepeat | Tile the texture but mirror every other tile. |
ClampToEdge | Use the edge color for anything past the boundary. |
Rendering
Primitives
See also Graphics.GL.Low.Render.
drawPoints :: Int -> IO () Source
drawLineStrip :: Int -> IO () Source
drawLineLoop :: Int -> IO () Source
drawTriangles :: Int -> IO () Source
drawTriangleStrip :: Int -> IO () Source
drawTriangleFan :: Int -> IO () Source
drawIndexedPoints :: Int -> IndexFormat -> IO () Source
drawIndexedLines :: Int -> IndexFormat -> IO () Source
drawIndexedLineStrip :: Int -> IndexFormat -> IO () Source
drawIndexedLineLoop :: Int -> IndexFormat -> IO () Source
drawIndexedTriangles :: Int -> IndexFormat -> IO () Source
drawIndexedTriangleStrip :: Int -> IndexFormat -> IO () Source
drawIndexedTriangleFan :: Int -> IndexFormat -> IO () Source
setViewport :: Viewport -> IO () Source
Set the viewport. The default viewport simply covers the entire window.
enableScissorTest :: Viewport -> IO () Source
Enable the scissor test. Graphics outside the scissor box will not be rendered.
disableScissorTest :: IO () Source
Disable the scissor test.
enableCulling :: Culling -> IO () Source
Enable face culling. The argument specifies whether front faces, back faces, or both will be omitted from rendering. If both front and back faces are culled you can still render points and lines.
disableCulling :: IO () Source
Disable face culling. Front and back faces will now be rendered.
A rectangular section of the window.
Face culling modes.
data IndexFormat Source
How indices are packed in an ElementArray buffer object.
UByteIndices | Each index is one unsigned byte. |
UShortIndices | Each index is a two byte unsigned int. |
UIntIndices | Each index is a four byte unsigned int. |
Color Buffer
enableColorWriting :: IO () Source
Allow rendering commands to modify the color buffer of the current framebuffer.
disableColorWriting :: IO () Source
Disable rendering to color buffer.
clearColorBuffer :: Real a => (a, a, a) -> IO () Source
Clear the color buffer of the current framebuffer with the specified color. Has no effect if writing to the color buffer is disabled.
Depth Test
enableDepthTest :: IO () Source
Enable the depth test. Attempting to render pixels with a depth value greater than the depth buffer at those pixels will have no effect. Otherwise the depth in the buffer will get updated to the new pixel's depth.
disableDepthTest :: IO () Source
Disable the depth test and depth buffer updates.
clearDepthBuffer :: IO () Source
Clear the depth buffer with the maximum depth value.
Stencil Test
See also Graphics.GL.Low.Stencil.
enableStencil :: Stencil -> IO () Source
Enable the stencil test with a set of operating parameters.
disableStencil :: IO () Source
Disable the stencil test and updates to the stencil buffer, if one exists.
clearStencilBuffer :: IO () Source
Clear the stencil buffer with all zeros.
basicStencil :: Stencil Source
In this basic configuration of the stencil, anything rendered will create a silhouette of 1s in the stencil buffer. Attempting to render a second time into the silhouette will have no effect because the stencil test will fail (ref=1 isn't greater than buffer=1).
def { func = Greater , ref = 1 , onBothPass = Replace }
Configuration of the stencil test and associated stencil buffer updating.
Stencil | |
|
data StencilFunc Source
The stencil test passes under what condition.
Modification action for the stencil buffer.
Keep | Do nothing. |
Zero | Set to zero. |
Replace | Write the ref value passed to enableStencil. |
Increment | |
Decrement | |
Invert | Bitwise complement. |
IncrementWrap | |
DecrementWrap |
Blending
See also Graphics.GL.Low.Blending.
enableBlending :: Blending -> IO () Source
Enable blending with the specified blending parameters.
disableBlending :: IO () Source
Disable alpha blending.
basicBlending :: Blending Source
This blending configuration is suitable for ordinary alpha blending transparency effects.
Blending { sFactor = BlendSourceAlpha , dFactor = BlendOneMinusSourceAlpha , blendFunc = FuncAdd }
Blending parameters.
Blending | |
|
data BlendFactor Source
Blending factors.
Eq BlendFactor Source | |
Ord BlendFactor Source | |
Read BlendFactor Source | |
Show BlendFactor Source | |
ToGL BlendFactor Source |
data BlendEquation Source
Blending functions.
FuncAdd | the default |
FuncSubtract | |
FuncReverseSubtract |
Eq BlendEquation Source | |
Ord BlendEquation Source | |
Read BlendEquation Source | |
Show BlendEquation Source | |
ToGL BlendEquation Source |
Framebuffers
See also Graphics.GL.Low.Framebuffer.
A framebuffer object is an alternative rendering destination. Once an FBO is bound to framebuffer binding target, it is possible to attach images (textures or RBOs) for color, depth, or stencil rendering.
Create a new framebuffer object. Before the framebuffer can be used for rendering it must have a color image attachment.
bindFBO :: FBO -> IO () Source
Binds an FBO to the framebuffer binding target. Replaces the framebuffer already bound there.
bindDefaultFramebuffer :: IO () Source
Binds the default framebuffer to the framebuffer binding target.
attachTex2D :: Texture -> IO () Source
Attach a 2D texture to the FBO currently bound to the framebuffer binding target.
attachCubeMap :: Texture -> (forall a. Cube a -> a) -> IO () Source
Attach one of the sides of a cube map texture to the FBO currently bound to the framebuffer binding target.
attachRBO :: RBO -> IO () Source
Attach an RBO to the FBO currently bound to the framebuffer binding target.
Renderbuffers
An RBO is a kind of image object used for rendering. The only thing you can do with an RBO is attach it to an FBO.
newRBO :: Int -> Int -> ImageFormat -> IO RBO Source
Create a new renderbuffer object with the specified dimensions and format.
Errors
Detectable errors.
InvalidEnum | Enum argument out of range. |
InvalidValue | Integer argument out of range. |
InvalidOperation | Operation illegal in current state. |
InvalidFramebufferOperation | Framebuffer is not complete. |
OutOfMemory |
getGLError :: IO (Maybe GLError) Source
Check for a GL Error. This call has the semantics of a dequeue. If an error is returned, then calling getGLError again may return more errors that have "stacked up." When it returns Nothing then there are no more errors to report. An error indicates that a bug in your code caused incorrect ussage of the API or that the implementation has run out of memory.
It has been suggested that using this after every single GL command may adversely affect performance (not to mention be very tedious). Since there is no reasonable way to recover from a GL error, a good idea might be to check this once per frame or even less often, and respond with a core dump.
assertNoGLError :: IO () Source
Throws an exception if getGLError
returns non-Nothing.