Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 21 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

Binding to WebGL for purescript.


This modules should be imported like:
~~~
import Control.Monad.Eff.WebGL as GL

```
import Effect.WebGL as GL
import Graphics.WebGL as GL
import Graphics.WebGLTexture as GL
import Graphics.WebGLFramebuffer as GL
~~~
```

I started with generating the raw interface by parsing the Kronos IDL and generating
a low level binding to Graphics.WebGLRaw. This can be found in the package
Expand Down Expand Up @@ -37,18 +37,18 @@ that you don't pick a value which is not allowed.

Start up by calling runWebGL with the canvas name to be used, an error handler
and a continuation which takes a context with the canvas name and runs with
the effect EffWebGL. This will fail if the browser does not support webgl.
the Effect. This will fail if the browser does not support webgl.

~~~
```
runWebGL "glcanvas" (\s -> alert s)
\ context -> do
~~~
```

Then you need to define the fshader and vshader in the shader language as two strings.
The Shaders constructor takes the two strings and has an additional phantom type,
which defines the bindings between purescript and the graphic processor. E.g.:

~~~
```
shaders :: Shaders {aVertexPosition :: Attribute Vec3, uPMatrix :: Uniform Mat4, uMVMatrix:: Uniform Mat4}
shaders = Shaders
"""precision mediump float;
Expand All @@ -67,30 +67,32 @@ shaders = Shaders
gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
}
"""
~~~
```

Although type safety is guaranteed in the purescript world, currently the correctness of your
types is not checked against the shading language definitions. Be careful as unused variables
in the sahder code will be optimized away and the binding will not be correct then.

By calling
~~~

```
withShaders shaders (\s -> alert s)
\ bindings -> do
~~~
```

compile the shaders, and the returned bindings are a record, with a type that matches the
shaders phantom type, plus an additional field, which holds the WebGlShaderProgram, which is
needed for some procedures. So here is the withShaders type:

~~~
withShaders :: forall bindings eff a. Shaders (Object bindings) -> (String -> EffWebGL eff a) ->
({webGLProgram :: WebGLProg | bindings} -> EffWebGL eff a) -> EffWebGL eff a
~~~
```
withShaders :: forall bindings a. Shaders (Object bindings) -> (String -> Effect a) ->
({webGLProgram :: WebGLProg | bindings} -> Effect a) -> Effect a
```

You can then bind values to uniforms and make buffers and draw. This is just on a slightly
higher leven then calling the original webgl procedures. Eg:

~~~
```
let pMatrix = M.makePerspective 45 (canvasWidth / canvasHeight) 0.1 100.0
setUniformFloats bindings.uPMatrix (M.toArray pMatrix)

Expand All @@ -102,12 +104,12 @@ higher leven then calling the original webgl procedures. Eg:
(-1.0), (-1.0), 0.0,
1.0, (-1.0), 0.0]
drawArr TRIANGLES buf1 bindings.aVertexPosition
~~~
```

Textures are supported via the module Graphics.WebGLTexture. A texture can be created by:

~~~
```
texture2DFor "crate.gif" MIPMAP \texture -> do
~~~
```

The interface is not stable, but the basic constructs (effect, shaders) will not be dropped.
39 changes: 15 additions & 24 deletions bower.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@
"Michael Karg <michael.karg@symbolian.net>"
],
"description": "WebGL binding for purescript",
"keywords": [
"purescript"
],
"keywords": ["purescript"],
"repository": {
"type": "git",
"url": "git://github.com/jutaro/purescript-webgl.git"
Expand All @@ -26,27 +24,20 @@
"package.json"
],
"dependencies": {
"purescript-prelude": "^3.1.0",
"purescript-monoid": "^3.1.0",
"purescript-control": "^3.3.1",
"purescript-invariant": "^3.0.0",
"purescript-maybe": "^3.0.0",
"purescript-functions": "^3.0.0",
"purescript-arraybuffer-types": "^2.0.0",
"purescript-typedarray": "^2.1.0",
"purescript-vector": "^2.0.0",
"purescript-matrix": "^2.1.0",
"purescript-extensions": "^2.2.0",
"purescript-integers": "^3.1.0",
"purescript-foldable-traversable": "^3.6.1",
"purescript-eff": "^3.1.0",
"purescript-either": "^3.1.0",
"purescript-tuples": "^4.1.0",
"purescript-unfoldable": "^3.0.0",
"purescript-canvas": "^3.1.0"
},
"resolutions": {
"purescript-prelude": "^4.0.0",
"purescript-control": "^4.1.0",
"purescript-invariant": "^4.0.0",
"purescript-maybe": "^4.0.0",
"purescript-functions": "^4.0.0",
"purescript-arraybuffer-types": "^2.0.0",
"purescript-extensions": "^2.2.0"
"purescript-typedarray": "mgmeier/purescript-typedarray#master",
"purescript-vector": "AlexMouton/purescript-vector#compiler-0.12",
"purescript-matrix": "AlexMouton/purescript-matrix#compiler-0.12",
"purescript-integers": "^4.0.0",
"purescript-foldable-traversable": "^4.0.0",
"purescript-either": "^4.0.0",
"purescript-tuples": "^5.0.0",
"purescript-unfoldable": "^4.0.0",
"purescript-canvas": "^4.0.0"
}
}
16 changes: 4 additions & 12 deletions docs/Control/Monad/Eff/WebGL.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,15 @@
## Module Control.Monad.Eff.WebGL
## Module Effect.WebGL

WebGL binding for purescript

#### `WebGl`

``` purescript
```purescript
data WebGl :: !
```

#### `EffWebGL`

``` purescript
type EffWebGL eff a = Eff (webgl :: WebGl | eff) a
```

#### `runWebGl_`

``` purescript
runWebGl_ :: forall a e. Eff (webgl :: WebGl | e) a -> Eff e a
```purescript
runWebGl_ :: forall e. Effect a -> Effect a
```


Loading