on dbfc352 help sign in
85 lines 2.8 kB history raw
1module Graphics.WebGL (mapTriangle, zipTriangle, unsafeCoerceGLSL, loadTex, model, webgl) where
3{-| The WebGL API is for high performance drawing.
5# Triangle Convenience Functions
6@docs Triangle, mapTriangle, zipTriangle
8# Shaders
9@docs Shader
11Shaders are created using inline GLSL blocks. The type of the Shader is determined by the top-level attribute, uniform, and varying declarations of the parsed GLSL source code. The type parameters to Shader are records with field names matching the names of the declarations.
13```haskell
14vertex : Shader { a | pos : V3 } { b | model : M4x4 } {}
15vertex = [glsl|
17attribute vec3 pos;
18uniform mat4 model;
20void main () {
21 gl_Position = model * vec4(pos,1.0);
24\]
25```
27GLSL attribute variables are per-vertex values. For example, it could hold the position or color of a specific vertex of a specific triangle in your model.
29GLSL uniform variables are global values which will be applied for all vertices of all triangles of the model.
31GLSL varying variables are shared between stages; they are output variables for vertex shaders, and input variables for fragment shaders.
33@docs unsafeCoerceGLSL
35# Loading Textures
36@docs loadTex
38# Packaging Models
39@docs model
41# WebGL Element
42@docs webgl
44-}
46import Graphics.Element (Element)
47import Http (Response)
48import Native.Graphics.WebGL
49import Signal (Signal)
51{-| Triangles are only homogenous 3-tuples. Each element describes something about each vertex, and must be a record to be used with GLSL shaders.
52-}
53type Triangle a = (a,a,a)
55{-| Apply a function to each vertex -}
56mapTriangle : (a -> b) -> Triangle a -> Triangle b
57mapTriangle f (x,y,z) = (f x, f y, f z)
59{-| Apply an arity-2 function element-wise on 2 triangles -}
60zipTriangle : (a -> b -> c) -> Triangle a -> Triangle b -> Triangle c
61zipTriangle f (x,y,z) (x',y',z') = (f x x', f y y', f z z')
63data Shader attr unif vary = Shader
65{-| Libary writers can use this function to create shader combinators. -}
66unsafeCoerceGLSL : String -> Shader attribute uniform varying
67unsafeCoerceGLSL = Native.Graphics.WebGL.unsafeCoerceGLSL
69data Texture = Texture
71{-| Loads a texture from the given url. PNG and JPEG are the preferred and tested-compatible source image format. -}
72loadTex : String -> Signal (Response Texture)
73loadTex = Native.Graphics.WebGL.loadTex
75data Model = Model
77{-| Packages a vertex shader, a fragment shader, triangle data, and uniform variables as a opaque Model.
78This is a simple container; however, externally there is no type information.
79-}
80model : Shader attribute uniform varying -> Shader {} uniform varying -> [Triangle attribute] -> uniform -> Model
81model = Native.Graphics.WebGL.model
83{-| Render a WebGL scene with the given dimensions and models -}
84webgl : (Int,Int) -> [Model] -> Element
85webgl = Native.Graphics.WebGL.webgl