Skip to content

Instantly share code, notes, and snippets.

@deadfoxygrandpa
Created November 12, 2014 18:35
Show Gist options
  • Save deadfoxygrandpa/3fac9a98052b903f138e to your computer and use it in GitHub Desktop.
Save deadfoxygrandpa/3fac9a98052b903f138e to your computer and use it in GitHub Desktop.
Vec4
import Math.Vector3 (vec3)
import Math.Vector4 (..)
import Math.Matrix4 (..)
import Graphics.WebGL (..)
-- Create a cube in which each vertex has a position and color
type Vertex =
{ color:Vec4
, position:Vec4
}
face : Color -> Vec4 -> Vec4 -> Vec4 -> Vec4 -> [Triangle Vertex]
face rawColor a b c d =
let color =
let c = toRgb rawColor in
vec4
(toFloat c.red / 255)
(toFloat c.green / 255)
(toFloat c.blue / 255)
1.0
vertex position =
Vertex color position
in
[ (vertex a, vertex b, vertex c)
, (vertex c, vertex d, vertex a)
]
cube : [Triangle Vertex]
cube =
let rft = vec4 1 1 1 1 -- right, front, top
lft = vec4 -1 1 1 1 -- left, front, top
lbt = vec4 -1 -1 1 1
rbt = vec4 1 -1 1 1
rbb = vec4 1 -1 -1 1
rfb = vec4 1 1 -1 1
lfb = vec4 -1 1 -1 1
lbb = vec4 -1 -1 -1 1
in
concat
[ face green rft rfb rbb rbt -- right
, face blue rft rfb lfb lft -- front
, face yellow rft lft lbt rbt -- top
, face red rfb lfb lbb rbb -- bottom
, face purple lft lfb lbb lbt -- left
, face orange rbt rbb lbb lbt -- back
]
-- Create the scene
main : Signal Element
main =
webgl (400,400) <~ lift scene angle
angle : Signal Float
angle =
foldp (\dt theta -> theta + dt / 5000) 0 (fps 25)
scene : Float -> [Entity]
scene angle =
[ entity vertexShader fragmentShader cube (uniforms angle) ]
uniforms : Float -> { rotation:Mat4, perspective:Mat4, camera:Mat4, shade:Float }
uniforms t =
{ rotation = mul (makeRotate (3*t) (vec3 0 1 0)) (makeRotate (2*t) (vec3 1 0 0))
, perspective = makePerspective 45 1 0.01 100
, camera = makeLookAt (vec3 0 0 5) (vec3 0 0 0) (vec3 0 1 0)
, shade = 0.8
}
-- Shaders
vertexShader : Shader { attr | position:Vec4, color:Vec4 }
{ unif | rotation:Mat4, perspective:Mat4, camera:Mat4 }
{ vcolor:Vec4 }
vertexShader = [glsl|
attribute vec4 position;
attribute vec4 color;
uniform mat4 perspective;
uniform mat4 camera;
uniform mat4 rotation;
varying vec4 vcolor;
void main () {
gl_Position = perspective * camera * rotation * position;
vcolor = color;
}
|]
fragmentShader : Shader {} { u | shade:Float } { vcolor:Vec4 }
fragmentShader = [glsl|
precision mediump float;
uniform float shade;
varying vec4 vcolor;
void main () {
gl_FragColor = shade * vcolor;
}
|]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment